Created
May 30, 2012 17:04
-
-
Save kevinmcconnell/2837655 to your computer and use it in GitHub Desktop.
hacky script to make a subset of a table's rows match those on another database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import create_engine | |
from sqlalchemy.orm import sessionmaker | |
FROM = 'postgresql://master/master' | |
TO = 'postgresql://core/core' | |
sfrom = sessionmaker(bind=create_engine(FROM, client_encoding='utf-8'))() | |
sto = sessionmaker(bind=create_engine(TO, client_encoding='utf-8'))() | |
def utfish(values): | |
result = {} | |
for k in values.keys(): | |
val = values[k] | |
if isinstance(val, str): | |
val = val.decode('utf-8') | |
result[k] = val | |
return result | |
def match(sfrom, sto, key, tbl, filter): | |
# backup first! | |
sto.execute("create table kevin_backup_%s as select * from %s" % ( | |
tbl, tbl)) | |
rows = sfrom.execute("select * from %s %s" % (tbl, filter)) | |
for row in rows: | |
values = utfish(row) | |
print values[key] | |
dest = sto.execute("select * from %s where %s=:key" % (tbl, key), | |
{'key': values[key]}).first() | |
if not dest: | |
qry = 'insert into %s (%s) values (%s)' % ( | |
tbl, | |
', '.join(k for k in row.keys()), | |
', '.join(':' + k for k in row.keys())) | |
print qry | |
sto.execute(qry, utfish(row)) | |
else: | |
qry = 'update %s set %s where %s=:%s' % ( | |
tbl, | |
', '.join('%s=:%s' % (k, k) for k in row.keys()), | |
key, | |
key) | |
print qry | |
sto.execute(qry, utfish(row)) | |
rows = sto.execute("select * from %s %s" % (tbl, filter)) | |
for row in rows: | |
values = utfish(row) | |
print values[key] | |
dest = sfrom.execute("select * from %s where %s=:key" % (tbl, key), | |
{'key': values[key]}).first() | |
if not dest: | |
try: | |
sto.execute("delete from %s where %s=:key" % (tbl, key), | |
{'key': row[key]}) | |
except: | |
sto.rollback() | |
match(sfrom, sto, 'template_id', 'template', | |
'where account_id = 0 and template_collection_id is not null') | |
sfrom.rollback() | |
sto.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment