merge_db.py 978 B

12345678910111213141516171819202122232425262728293031323334
  1. import os
  2. from . import common
  3. from . import tsdb
  4. MIGRATE_QUERY = '''
  5. INSERT INTO {tablename}
  6. SELECT othertable.* FROM other.{tablename} othertable
  7. LEFT JOIN {tablename} mytable ON mytable.idint == othertable.idint
  8. WHERE mytable.idint IS NULL;
  9. '''
  10. def _migrate_helper(db, tablename):
  11. query = MIGRATE_QUERY.format(tablename=tablename)
  12. print(query)
  13. oldcount = db.cur.execute('SELECT count(*) FROM %s' % tablename).fetchone()[0]
  14. db.cur.execute(query)
  15. db.sql.commit()
  16. newcount = db.cur.execute('SELECT count(*) FROM %s' % tablename).fetchone()[0]
  17. print('Gained %d items.' % (newcount - oldcount))
  18. def merge_db(from_db_path, to_db_path):
  19. to_db = tsdb.TSDB(to_db_path)
  20. from_db = tsdb.TSDB(from_db_path)
  21. to_db.cur.execute('ATTACH DATABASE "%s" AS other' % from_db_path)
  22. _migrate_helper(to_db, 'submissions')
  23. _migrate_helper(to_db, 'comments')
  24. def merge_db_argparse(args):
  25. return merge_db(args.from_db_path, args.to_db_path)