@require_admin_context
def archive_deleted_rows_for_table(context, tablename, max_rows):
 engine = get_engine()
 conn = engine.connect()
 metadata = MetaData()
 metadata.bind = engine
 table = Table(tablename, metadata, autoload=True)
 default_deleted_value = _get_default_deleted_value(table)
 shadow_tablename = ('shadow_' + tablename)
 rows_archived = 0
 try:
  shadow_table = Table(shadow_tablename, metadata, autoload=True)
 except NoSuchTableError:
  return rows_archived
 with conn.begin():
  try:
   column = table.c.id
   column_name = 'id'
  except AttributeError:
   column = table.c.domain
   column_name = 'domain'
  query = select([table], (table.c.deleted != default_deleted_value)).order_by(column).limit(max_rows)
  rows = conn.execute(query).fetchall()
  if rows:
   keys = [getattr(row, column_name) for row in rows]
   delete_statement = table.delete(column.in_(keys))
   try:
    result = conn.execute(delete_statement)
   except IntegrityError:
    return rows_archived
   insert_statement = shadow_table.insert()
   conn.execute(insert_statement, rows)
   rows_archived = result.rowcount
 return rows_archived

@ require_admin_context def archive_deleted_rows_for_table ( context , tablename , max_rows ) : engine = get_engine ( ) conn = engine . connect ( ) metadata = MetaData ( ) metadata . bind = engine table = Table ( tablename , metadata , autoload = True ) default_deleted_value = _get_default_deleted_value ( table ) shadow_tablename = ( 'shadow_' + tablename ) rows_archived = 0 try : shadow_table = Table ( shadow_tablename , metadata , autoload = True ) except NoSuchTableError : return rows_archived with conn . begin ( ) : try : column = table . c . id column_name = 'id' except AttributeError : column = table . c . domain column_name = 'domain' query = select ( [ table ] , ( table . c . deleted != default_deleted_value ) ) . order_by ( column ) . limit ( max_rows ) rows = conn . execute ( query ) . fetchall ( ) if rows : keys = [ getattr ( row , column_name ) for row in rows ] delete_statement = table . delete ( column . in_ ( keys ) ) try : result = conn . execute ( delete_statement ) except IntegrityError : return rows_archived insert_statement = shadow_table . insert ( ) conn . execute ( insert_statement , rows ) rows_archived = result . rowcount return rows_archived
move up to max_rows rows from tablename to corresponding shadow table .