<s> user = "<STR_LIT:root>" <EOL> passwd = "<STR_LIT:password>" <EOL> new_db = "<STR_LIT>" <EOL> old_db = "<STR_LIT>" <EOL> import os <EOL> import sys <EOL> import subprocess <EOL> import MySQLdb <EOL> config = "<STR_LIT>" <EOL> if os . access ( config , os . R_OK ) : <EOL> f = open ( config , "<STR_LIT:r>" ) <EOL> lines = f . readlines ( ) <EOL> f . close ( ) <EOL> for line in lines : <EOL> findstring = "<STR_LIT>" <EOL> if findstring in line : <EOL> passwd = line . replace ( findstring , "<STR_LIT>" ) . strip ( ) <EOL> db1 = MySQLdb . connection ( host = "<STR_LIT:localhost>" , user = user , passwd = passwd , db = new_db ) <EOL> db2 = MySQLdb . connection ( host = "<STR_LIT:localhost>" , user = user , passwd = passwd , db = old_db ) <EOL> def tablelist ( db ) : <EOL> db . query ( "<STR_LIT>" ) <EOL> r = db . store_result ( ) <EOL> tables = [ ] <EOL> for row in r . fetch_row ( <NUM_LIT> ) : <EOL> tables . append ( row [ <NUM_LIT:0> ] ) <EOL> return tables <EOL> def tablestruct ( db ) : <EOL> tablestruct = { } <EOL> tables = tablelist ( db ) <EOL> for table in tables : <EOL> db . query ( "<STR_LIT>" % table ) <EOL> r = db . store_result ( ) <EOL> structure = [ ] <EOL> for row in r . fetch_row ( <NUM_LIT:100> ) : <EOL> structure . append ( row [ <NUM_LIT:0> ] ) <EOL> tablestruct [ table ] = structure <EOL> return tablestruct <EOL> struct1 = tablestruct ( db1 ) <EOL> struct2 = tablestruct ( db2 ) <EOL> tables_to_delete = [ ] <EOL> fields_to_delete = { } <EOL> for key in struct2 : <EOL> fields_to_delete [ key ] = [ ] <EOL> try : <EOL> fields1 = struct1 [ key ] <EOL> fields2 = struct2 [ key ] <EOL> for field in fields2 : <EOL> try : <EOL> fields1 . index ( field ) <EOL> except : <EOL> fields_to_delete [ key ] . append ( field ) <EOL> except : <EOL> tables_to_delete . append ( key ) <EOL> for table in fields_to_delete . keys ( ) : <EOL> if fields_to_delete [ table ] == [ ] : <EOL> del fields_to_delete [ table ] <EOL> print tables_to_delete <EOL> print fields_to_delete <EOL> for table in tables_to_delete : <EOL> db2 . query ( "<STR_LIT>" ) <EOL> db2 . query ( "<STR_LIT>" % table ) <EOL> db2 . query ( "<STR_LIT>" ) <EOL> problems = "<STR_LIT>" <EOL> filename = "<STR_LIT>" <EOL> for table in fields_to_delete : <EOL> for field in fields_to_delete [ table ] : <EOL> print "<STR_LIT>" + table + "<STR_LIT>" + field + "<STR_LIT>" <EOL> db2 . query ( "<STR_LIT>" ) <EOL> try : <EOL> db2 . query ( "<STR_LIT>" + table + "<STR_LIT>" + field + "<STR_LIT>" ) <EOL> except : <EOL> print "<STR_LIT>" % ( table , field ) <EOL> cmd = "<STR_LIT>" % filename <EOL> subprocess . call ( cmd , shell = True ) <EOL> if os . access ( filename , os . R_OK ) : <EOL> f = open ( filename , "<STR_LIT:r>" ) <EOL> lines = f . readlines ( ) <EOL> f . close ( ) <EOL> fk = lines [ <NUM_LIT:1> ] . split ( "<STR_LIT>" ) [ <NUM_LIT:1> ] . split ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . strip ( ) <EOL> print "<STR_LIT>" + table + "<STR_LIT>" + fk + "<STR_LIT>" <EOL> try : <EOL> db2 . query ( "<STR_LIT>" + table + "<STR_LIT>" + fk + "<STR_LIT>" ) <EOL> except ( MySQLdb . OperationalError , ) : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> print "<STR_LIT>" % ( fk , table ) + e . message <EOL> print "<STR_LIT>" + table + "<STR_LIT>" + field + "<STR_LIT>" <EOL> try : <EOL> db2 . query ( "<STR_LIT>" + table + "<STR_LIT>" + field + "<STR_LIT>" ) <EOL> except ( MySQLdb . OperationalError , ) : <EOL> e = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> message = "<STR_LIT>" % ( field , table ) + e . message <EOL> print message <EOL> problems += message <EOL> db2 . query ( "<STR_LIT>" ) <EOL> print problems </s>
