def upsert(context, data_dict):
 engine = _get_engine(data_dict)
 context['connection'] = engine.connect()
 timeout = context.get('query_timeout', _TIMEOUT)
 trans = context['connection'].begin()
 try:
  context['connection'].execute(u'SET LOCAL statement_timeout TO {0}'.format(timeout))
  upsert_data(context, data_dict)
  trans.commit()
  return _unrename_json_field(data_dict)
 except IntegrityError as e:
  if (e.orig.pgcode == _PG_ERR_CODE['unique_violation']):
   raise ValidationError({'constraints': ['Cannot insert records or create index because of uniqueness constraint'], 'info': {'orig': str(e.orig), 'pgcode': e.orig.pgcode}})
  raise
 except DataError as e:
  raise ValidationError({'data': e.message, 'info': {'orig': [str(e.orig)]}})
 except DBAPIError as e:
  if (e.orig.pgcode == _PG_ERR_CODE['query_canceled']):
   raise ValidationError({'query': ['Query took too long']})
  raise
 except Exception as e:
  trans.rollback()
  raise
 finally:
  context['connection'].close()

def upsert ( context , data_dict ) : engine = _get_engine ( data_dict ) context [ 'connection' ] = engine . connect ( ) timeout = context . get ( 'query_timeout' , _TIMEOUT ) trans = context [ 'connection' ] . begin ( ) try : context [ 'connection' ] . execute ( u'SET LOCAL statement_timeout TO {0}' . format ( timeout ) ) upsert_data ( context , data_dict ) trans . commit ( ) return _unrename_json_field ( data_dict ) except IntegrityError as e : if ( e . orig . pgcode == _PG_ERR_CODE [ 'unique_violation' ] ) : raise ValidationError ( { 'constraints' : [ 'Cannot insert records or create index because of uniqueness constraint' ] , 'info' : { 'orig' : str ( e . orig ) , 'pgcode' : e . orig . pgcode } } ) raise except DataError as e : raise ValidationError ( { 'data' : e . message , 'info' : { 'orig' : [ str ( e . orig ) ] } } ) except DBAPIError as e : if ( e . orig . pgcode == _PG_ERR_CODE [ 'query_canceled' ] ) : raise ValidationError ( { 'query' : [ 'Query took too long' ] } ) raise except Exception as e : trans . rollback ( ) raise finally : context [ 'connection' ] . close ( )
this method combines upsert insert and update on the datastore .