-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
285 lines (246 loc) · 12.2 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/python
import psycopg2
from psycopg2 import sql
class Database:
def __init__(self, schema, username, database, pg_password):
self.conn = psycopg2.connect(database=database,
user=username,
password=pg_password)
self.cursor = self.conn.cursor()
self.schema = schema
def __del__(self):
# make sure to commit transactions or else wont save in DB
self.conn.commit()
self.cursor.close()
self.conn.close()
def checkRefInt(self):
for table in self.schema:
try:
print("\n*********************************\
\n DB REF INT CHECKING TABLE:", table,
"\n*********************************")
# if table has no fk nothing to check
if(len(self.schema[table]['Fks']) == 0):
print('\nDB Ref Int CHECK, Table: ',
table, ' -> No fK to check')
self.schema[table]['dbRefInt'] = True
continue
# skip DB check if table has no PK
if(len(self.schema[table]['Pk']) == 0):
raise Exception('Skipping table: ',
table, ' -> No PK')
# skip DB if FK references invalid column or table
if(len(self.schema[table]['invalidFks']) > 0):
raise Exception('Skipping table: ', table, ' -> invalid FK: ',
self.schema[table]['invalidFks'])
# check ref Int for remaining tables that have PK and Valid Fk's
for ttc in self.schema[table]['validFks']:
# get table and attr from foreignKey
fKTableName = ttc.split('.')[0]
fKTableAttr = ttc.split('.')[1]
refKey = self.schema[table]['validFks'][ttc]
# get table and attr to check in referenced table
refTableName = self.schema[table]['validFks'][ttc].split('.')[
0]
refTableAttr = self.schema[table]['validFks'][ttc].split('.')[
1]
print('-----> checking refInt for fk: ',
ttc, ' -> ', refKey, '\n')
# check if any foreign Key row is NULL
self.cursor.execute(
sql.SQL(
"""
SELECT * FROM {} WHERE {} IS NULL
"""
).format(sql.Identifier(fKTableName),
sql.Identifier(fKTableAttr))
)
totalNullForKeys = self.cursor.rowcount
self.schema[table]['dbCommands'].append(
self.cursor.query.strip())
# check if all rows in referencing table
# exist in referenced table
self.cursor.execute(
sql.SQL(
"""
SELECT * FROM {}
"""
).format(sql.Identifier(fKTableName))
)
totalRows = self.cursor.rowcount
self.schema[table]['dbCommands'].append(
self.cursor.query.strip())
self.cursor.execute(
sql.SQL(
"""
SELECT t.{}, b.{} FROM {} t JOIN {} b ON t.{} = b.{}
"""
).format(
sql.Identifier(fKTableAttr),
sql.Identifier(refTableAttr),
sql.Identifier(fKTableName),
sql.Identifier(refTableName),
sql.Identifier(fKTableAttr),
sql.Identifier(refTableAttr)
),
)
totalReferencedKeys = self.cursor.rowcount
self.schema[table]['dbCommands'].append(
self.cursor.query.strip())
print('Total Nulls FOR FK in table ',
fKTableName, ' : ', totalNullForKeys)
print('Total Rows in table ',
fKTableName, ' : ', totalRows)
print('Total JOIN Referenced Keys in table ',
refTableName, ' : ', totalReferencedKeys)
# failed if we have nulls for foreign keys
if (totalNullForKeys != 0):
raise Exception(' Failed!!! NULLS : ', totalNullForKeys,
' For:', fKTableName + '.' + fKTableAttr,
' -> ' + refTableName + "." + refTableAttr)
# if all rows for referencing table
# are not in referenced table, dbRefInt failed
if(totalRows != totalReferencedKeys):
raise Exception('Failed!!!', totalRows, ' total rows in table ',
fKTableName,
' Doesnt Match', totalReferencedKeys, 'totalReferencedKeys in',
refTableName,
' For:', fKTableName + '.' + fKTableAttr,
' -> ' + refTableName + "." + refTableAttr)
# if no nulls and row count is equal to referenced keys
if(totalRows == totalReferencedKeys):
print('\nPASSED DB REF INT: ', fKTableName + '.' + fKTableAttr,
' -> ' + refTableName + "." + refTableAttr)
self.schema[ttc.split('.')[0]]['dbRefInt'] = True
except Exception as e:
# if any DB errors for that table print error
# commit db commands and fail refInt for table
# and skip checking rest of table
self.conn.commit()
print('DB REFINT CHECK FAILED SKIPPING TABLE: ', table, '\n', e)
self.schema[table]['dbRefInt'] = False
continue
def checkNormal(self):
for table in self.schema:
try:
print("\n*****************************************\
\n DB NORMALIZATION CHECKING TABLE:", table,
"\n*****************************************")
# skip DB normalization check if table has no PK
if(len(self.schema[table]['Pk']) == 0):
raise Exception('Skipping table: ',
table, ' -> No PK')
# skip DB normalization if any FK references invalid column or table
if(len(self.schema[table]['invalidFks']) > 0):
raise Exception('Skipping table: ', table, ' -> invalid FK: ',
self.schema[table]['invalidFks'])
# if only one NonPrime attribute than table is in 3NF
if(len(self.schema[table]['nonPrimaryAttributes']) == 1):
print('Only one NonPrimeAttr by default in 3NF',)
self.schema[table]['normalized'] = True
continue
# check npa -> npa for 3NF
print('\nALL NPA: ', self.schema[table]['nonPrimaryAttributes'])
for npa in self.schema[table]['nonPrimaryAttributes']:
# if column has no duplicates than skip it, it passes by default
self.cursor.execute(
sql.SQL(
"""
SELECT {} FROM {} GROUP BY({}) HAVING count({}) > 1
"""
).format(
sql.Identifier(npa),
sql.Identifier(table),
sql.Identifier(npa),
sql.Identifier(npa)
),
)
self.schema[table]['dbCommands'].append(
self.cursor.query.strip())
if self.cursor.rowcount == 0:
print("\nNPA: ", npa, " -> has no duplicates passed 3NF",
"checking rest of table")
self.schema[table]['normalized'] = True
continue
# check all other npa attributes except this one
for checkNpa in self.schema[table]['nonPrimaryAttributes']:
if checkNpa == npa:
continue
print('\n--> checking npa: ', npa)
print('-----> with attr: ', checkNpa)
self.cursor.execute(
sql.SQL(
"""
DROP TABLE IF EXISTS temp
"""
)
)
self.schema[table]['dbCommands'].append(
self.cursor.query.strip())
self.cursor.execute(
sql.SQL(
"""
DROP TABLE IF EXISTS pairs
"""
)
)
self.schema[table]['dbCommands'].append(
self.cursor.query.strip())
self.cursor.execute(
sql.SQL(
"""
CREATE TABLE IF NOT EXISTS pairs AS (SELECT {} FROM {} GROUP BY {} HAVING COUNT({}) > 1)
"""
).format(
sql.Identifier(npa),
sql.Identifier(table),
sql.Identifier(npa),
sql.Identifier(npa)
),
)
self.schema[table]['dbCommands'].append(
self.cursor.query.strip())
self.cursor.execute(
sql.SQL(
"""
CREATE TABLE IF NOT EXISTS temp AS (SELECT {}, {} FROM {} WHERE {} IN (SELECT {} FROM pairs) GROUP BY {}, {})
"""
).format(
sql.Identifier(npa),
sql.Identifier(checkNpa),
sql.Identifier(table),
sql.Identifier(npa),
sql.Identifier(npa),
sql.Identifier(npa),
sql.Identifier(checkNpa)
),
)
self.schema[table]['dbCommands'].append(
self.cursor.query.strip())
self.cursor.execute(
sql.SQL(
"""
SELECT EXISTS (SELECT {} FROM temp GROUP BY {} HAVING COUNT({}) > 1)
"""
).format(
sql.Identifier(npa),
sql.Identifier(npa),
sql.Identifier(npa)
),
)
finalResult = self.cursor.fetchall()[0][0]
self.schema[table]['dbCommands'].append(
self.cursor.query.strip())
if(finalResult == False):
# if not normalized column break out of loop, whole table fails
raise Exception('Column: ', npa, ' -> ', checkNpa,
' failed 3NF')
if(finalResult == True):
print('Column: ', npa, ' -> ', checkNpa,
' passed 3NF checking rest of table')
self.schema[table]['normalized'] = True
except Exception as e:
self.conn.commit()
self.schema[table]['normalized'] = False
print('DB NORMAL COMMANDS FAILED SKIPPING TABLE: ', table, '\n', e)
continue