def main(): connectionString = 'driver=' + DRIVER + ';server=' + SERVER_ADDRESS + ';database=' + DATABASE_NAME + ';uid=' + UID + ';pwd=' + PASSWORD cnxn = pyodbc.connect(connectionString) cursor = cnxn.cursor() mongoClient = MongoClient(MONGO_DATABASE_ADDRESS) db = mongoClient[DATABASE_NAME] columnData = getColumnData(cursor) tableNames = set(row.TABLE_NAME for row in columnData) pKeys = getPrimaryKeys(cursor) fKeys = getForeignKeys(cursor) graph = buildDependencyGraph(tableNames, columnData, pKeys, fKeys) if len(db.collection_names()) < 1: print "Exporting data..." tablesJSON = {} for table in graph: tableDataExtractor = TableDataExtractor() result = tableDataExtractor.extractData(cursor, table) tablesJSON[table.tableName] = result for tableName in tablesJSON: for entry in tablesJSON[tableName]: print "Inserting table " + tableName + " into Mongo..." db[tableName].insert(entry) print "Done." else: print "Checking for changes..." delta(graph, cursor, db) print "Operation Complete"
def delta(graph, cursor, mongoDB): extractor = TableDataExtractor() for table in graph: sqlKeys = extractor.getPrimaryKeyValues(cursor, table) #print "SQL Keys: " + str(sqlKeys) mongoKeys = [str(entry[table.primaryKey[0]]) for entry in mongoDB[table.tableName].find()] #print "Mongo Keys: " + str(mongoKeys) sqlSet = set(sqlKeys) mongoSet = set(mongoKeys) newKeys = sqlSet.difference(mongoSet) insertNewKeys(mongoDB, cursor, extractor, newKeys, table, table.primaryKey[0]) deletedKeys = mongoSet.difference(sqlSet) deleteRemovedKeys(mongoDB, deletedKeys, table.tableName, table.primaryKey[0])