Skip to content

Commit

Permalink
added ability to insert arbitrary default statement at indexes of psq…
Browse files Browse the repository at this point in the history
…l push
  • Loading branch information
fourthetrees committed Jul 11, 2017
1 parent f789b82 commit e2d0470
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/export/psql.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def export(project,config,state,data):
ins = custom_insertion(fields,config['conversions'])
# default is just standard psycopg2 formatting...
else: ins = ','.join(['%s'] * len(fields))
cmd = 'INSERT INTO {} VALUES ({})'.format(tbl,ins)
cmd = 'INSERT INTO {} VALUES ({}) ON CONFLICT DO NOTHING'.format(tbl,ins)
print('pushing {} rows to psql...'.format(len(data)))
errors,errtxt,dups = handle_push(data,cmd,db)
duplicates += dups
Expand Down Expand Up @@ -90,8 +90,11 @@ def custom_insertion(fields,insmap):
# collection of all possible insertion strings.
inserts = {
'default' : '%s',
'to-timestamp' : 'to_timestamp(%s)'
'to-timestamp' : 'to_timestamp(%s)',
}
if 'psql-defaults' in insmap:
psql_defaults = insmap.pop('psql-defaults')
else: psql_defaults = []
for i in insmap:
if i not in fields:
raise Exception('unrecognized data field: {}'.format(i))
Expand All @@ -103,6 +106,9 @@ def custom_insertion(fields,insmap):
if f in insmap:
ins.append(inserts[insmap[f]])
else: ins.append(inserts['default'])
if psql_defaults:
for idx in psql_defaults:
ins.insert(int(idx),'DEFAULT')
return ','.join(ins)


Expand Down
31 changes: 31 additions & 0 deletions src/reshape/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,41 @@ def run_generators(project,config,state,data):
value = gen['value']
if value == 'current-time':
state,rows = generate_current_time(project,gen,state,rows)
if value == 'literal':
state,rows = generate_literal(project,gen,state,rows)
else:
error = mkerr('unrecognized `value` argument: ' + value)
raise Exception(error)
return state,rows

# add a field with some arbitrary value for all rows.
def generate_literal(project,config,state,rows):
if not rows: return
mkerr = field_error('generating field: `literal`')
title = config['title']
index = config.get('index','append')
value = config['ident']
if isinstance(index,str):
if index == "append":
fmt = lambda v,r: list(r) + [v]
else:
error = mkerr('unexpected value for `index`: ' + index)
raise Exception(error)
elif isinstance(index,int):
fmt = lambda v,r: list((*r[0:index],v,*r[index:]))
else:
error = mkerr('unexpected value for `index`: ' + str(index))
raise Exception(error)
fields = rows[0]._fields
newfields = fmt(title,fields)
mkrow = du.custom_row_generator(newfields)
modify = lambda r: mkrow(fmt(value,r))
newrows = []
for row in rows:
newrow = modify(row)
newrows.append(newrow)
return state,newrows

# add a generated field containing the current time.
def generate_current_time(project,config,state,rows):
if not rows: return
Expand Down Expand Up @@ -121,3 +151,4 @@ def generate_current_time(project,config,state,rows):
newrow = modify(row)
newrows.append(newrow)
return state,newrows

0 comments on commit e2d0470

Please sign in to comment.