File tree 2 files changed +5
-7
lines changed
2 files changed +5
-7
lines changed Original file line number Diff line number Diff line change 1
1
from psycopg2 import connect
2
+ from psycopg2 .extras import execute_values
2
3
3
4
from addok .config import config
4
5
@@ -31,9 +32,6 @@ def fetch(self, *keys):
31
32
32
33
def upsert (self , * docs ):
33
34
"""
34
- Once psycopg2.7 is released, switch to `extras.execute_values`
35
- which avoids the template dance.
36
-
37
35
Potential performance boost, using copy_from:
38
36
* https://gist.github.com/jsheedy/efa9a69926a754bebf0e9078fd085df6
39
37
* https://gist.github.com/jsheedy/ed81cdf18190183b3b7d
@@ -42,11 +40,11 @@ def upsert(self, *docs):
42
40
* http://stackoverflow.com/a/8150329
43
41
"""
44
42
insert_into_query = '''
45
- INSERT INTO {PG_TABLE} (key, data) VALUES {template}
43
+ INSERT INTO {PG_TABLE} (key, data) VALUES %s
46
44
ON CONFLICT DO NOTHING
47
- ''' .format (template = ',' . join ([ '%s' ] * len ( docs )), ** config )
45
+ ''' .format (** config )
48
46
with self .conn .cursor () as curs :
49
- curs . execute ( insert_into_query , docs )
47
+ execute_values ( curs , insert_into_query , docs )
50
48
self .conn .commit ()
51
49
52
50
def remove (self , * keys ):
Original file line number Diff line number Diff line change 1
- psycopg2
1
+ psycopg2 == 2.7.1
You can’t perform that action at this time.
0 commit comments