FIN: database schema compare

This commit is contained in:
JohnE 2015-06-25 09:56:51 -07:00
parent 80d9fbacd1
commit 42c8dfa493
2 changed files with 78 additions and 10 deletions

View File

@ -48,17 +48,17 @@ class DBSchema:
return 1
def importJson(self, filein):
self.tables = __importDBSchema(filein)
def importJson(self, filejson):
self.tablesJson = self.__importDBSchema(filejson)
def compareDBSchema(self, filein):
self.tablesJson = __importDBSchema(filein)
result = __DBSchemaCompare()
def compareDB(self, filejson):
self.tablesJson = self.__importDBSchema(filejson)
result = self.__DBSchemaCompare()
return result
def __importDBSchema(self, filein)
def __importDBSchema(self, filein):
tables = {}
try:
fh = open(filein, "r")
@ -67,16 +67,73 @@ class DBSchema:
tb = jsonData['tables']
keys = tb.keys()
for key in keys:
print "[[ Table <" + key + "> imported ]]"
newTable = TableDefinition()
newTable.importTable(key, dbmt[key], tb[key])
tables[key] = newTable
except Exception, e:
print "ERROR: problem loading json file: " + filein
print e
return tables
def __DBSchemaCompare(self):
# the json database schema definition is what our tools is expecting...
# ...so we use it as the baseline
# look for table, if exists, compare each
# if exists, compare each field
# else, add to unknown tables...or do a fuzzy compare (look at number of fields, field names)
for tableName in self.tablesJson.keys():
table = self.tables[tableName]
print "[[ Comparing Table: " + tableName + " ]]"
if (table):
self.__CompareTable(self.tablesJson[tableName], table)
else:
self.__FuzzyTable()
return
#
# Compare the Table Definitions.
# Compare Table 1 (Json table) to Table 2
#
def __CompareTable(self, tb1, tb2):
fieldsTotalCount = 0
fieldsErrorCount = 0
propTotalCount = 0
propErrorCount = 0
fields1 = tb1.fields
fields2 = tb2.fields
for field in fields1.keys():
field1 = fields1[field]
fieldsTotalCount += 1
if (fields2.has_key(field)):
field2 = fields1[field]
for properties in field1.keys():
propTotalCount += 1
if not field2.has_key(properties):
propErrorCount += 1
else:
fieldsErrorCount += 1
if (propErrorCount == 0 and fieldsErrorCount == 0):
print "100% compatible"
else:
totals = propTotalCount + fieldsTotalCount
errors = propErrorCount + fieldsErrorCount
print "Table difference found: " + str(errors)
#print str((errors/totals) * 100) + '% compatible total == ' + str(totals) + " errors == " + str(errors)
# look at un-identified tables and try to match fields by their properties
def __FuzzyTable():
return
def __openDB(self, filein):
conn = sqlite3.connect(filein)
cur = conn.cursor()
@ -93,10 +150,17 @@ class DBSchema:
def debugFingerprint(self):
keys = self.tables.keys()
if self.tables:
myDict = self.tables
elif self.tablesJson:
myDict = self.tablesJson
else:
return
keys = myDict.keys()
for key in keys:
print "[[ TABLE: <" + key + "> ]]"
tableDef = self.tables[key]
tableDef = myDict[key]
#print str(tableDef.SQLstr())
tableDef.toJSON()

View File

@ -11,7 +11,11 @@ def main():
(filein, fileout2, filejson, verbose) = parseArgs()
db = fingerprint.DBSchema()
if (filein):
if (filein and filejson):
db.scanDBFile(filein)
db.compareDB(filejson)
elif (filein):
retVal = db.scanDBFile(filein)
if (retVal > 0):
fh = open(fileout2, "w")
@ -21,7 +25,7 @@ def main():
else:
print db.getErrorString(retVal)
elif (filejson):
db.importDBSchema(filejson)
db.importJson(filejson)
db.debugFingerprint()