-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEveDBMigration.py
195 lines (161 loc) · 5.31 KB
/
EveDBMigration.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
import mysql.connector
import pyodbc
#Change the details of the databases here:
#ADDR is the server address
#PORT is the port (not required for MSSQL)
#UN is the username
#PW is the password
#DB is the database - make sure this is created in the new location before execution
MYSQLADDR = "evoice.cermmtd1vgvf.eu-west-2.rds.amazonaws.com"
MYSQLPORT = 3306
MYSQLUN = "eVoice"
MYSQLPW = "latymerevoice"
MYSQLDB = "evoice"
MSSQLADDR = "evoicemicrosoft.cermmtd1vgvf.eu-west-2.rds.amazonaws.com"
MSSQLPORT = None
MSSQLUN = "eVoice"
MSSQLPW = "latymerevoice"
MSSQLDB = "evoice"
keys = []
columnTypes = {}
class Key:
def __init__(self, ktype, tableName, colName, refTable, refColumn):
self.type = self.getType(ktype)
self.columnName = colName
self.ref = str(refTable) + "(" + str(refColumn) + ")"
self.table = tableName
def getType(self, name):
if name == "PRIMARY":
return "p"
else:
return "f"
def isDate(string):
string = str(string)
if string is None:
return False
if len(string) != 10:
return False
if string[4] != "-" or string[7] != "-":
return False
try:
int(string[:3])
int(string[5:7])
int(string[8:10])
except:
return False
return True
def connectMicrosoftSQLDatabase():
db = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + MSSQLADDR + ';DATABASE=' + MSSQLDB + ';UID=' + MSSQLUN + ';PWD=' + MSSQLPW)
cursor = db.cursor()
sql = "USE " + MSSQLDB
cursor.execute(sql)
return db, cursor
def connectMySQLDatabase():
lines = ["" for i in range(6)]
lines[0]=MYSQLADDR
lines[1]=MYSQLPORT
lines[2]=MYSQLUN
lines[3]=MYSQLPW
lines[4]=MYSQLDB
db = mysql.connector.connect(
host=lines[0],
port = int(lines[1]),
username=lines[2],
password=lines[3]
)
cursor = db.cursor()
sql = "USE " + lines[4]
cursor.execute(sql)
return db, cursor
def getTables():
sql = "SHOW tables"
mysqlcsr.execute(sql)
return [i[0] for i in mysqlcsr.fetchall()]
def createTable(tableName):
sql = ("SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH "
"FROM INFORMATION_SCHEMA.COLUMNS "
"WHERE TABLE_NAME = '" + tableName + "'")
mysqlcsr.execute(sql)
tableData = mysqlcsr.fetchall()
ct = {i:tableData[i][1] for i in range(len(tableData))}
columnTypes[tableName] = ct
sql = ("SELECT COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME, TABLE_NAME "
"FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE "
"WHERE TABLE_NAME = '" + tableName + "'")
mysqlcsr.execute(sql)
keyData = mysqlcsr.fetchall()
pKeys = []
for key in keyData:
k = Key(key[1], key[4], key[0], key[2], key[3])
if k.type == "p":
pKeys.append(k.columnName)
keys.append(k)
typesString = ""
for data in tableData:
addStr = ""
if data[0] in pKeys:
addStr = "NOT NULL"
if data[2] is not None:
typesString += data[0] + " " + data[1] + "(" + str(data[2]) + ") " + addStr + ", "
else:
typesString += data[0] + " " + data[1] + " " + addStr + ", "
typesString = typesString[:-2]
sql = "CREATE TABLE " + tableName + "(" + typesString + ")"
mssqlcsr.execute(sql)
mssqldb.commit()
def addKeys():
pKeys = {}
for k in keys:
if k.type == "p":
if k.table in pKeys.keys():
pKeys[k.table].append(k.columnName)
else:
pKeys[k.table] = [k.columnName]
for table in pKeys.keys():
cols = ", ".join(pKeys[table])
sql = "ALTER TABLE " + table + " ADD PRIMARY KEY (" + cols + ")"
mssqlcsr.execute(sql)
mssqldb.commit()
for k in keys:
if k.type == "f":
sql = "ALTER TABLE " + k.table + " ADD FOREIGN KEY (" + k.columnName + ") REFERENCES " + k.ref
mssqlcsr.execute(sql)
mssqldb.commit()
def migrateTableData(tableName):
sql = "SELECT * FROM " + tableName
mysqlcsr.execute(sql)
fullData = mysqlcsr.fetchall()
insString = "INSERT INTO " + tableName + " VALUES "
for data in fullData:
insString += "("
for i in range(len(data)):
val = data[i]
if val is None:
val = "NULL"
val = str(val)
if columnTypes[tableName][i].lower() == "varchar" or columnTypes[tableName][i].lower() == "date":
val = val.replace("'", "''")
insString += "'" + val + "',"
elif columnTypes[tableName][i].lower() == "int":
insString += val + ","
else:
val = val.replace("'", "''")
insString += "'" + val + "',"
insString = insString[:-1] + "), "
insString = insString[:-2]
mssqlcsr.execute(insString)
mssqldb.commit()
def main():
count = 0
for table in tables:
print("Creating table " + table)
createTable(table)
for table in tables:
print("Populating table " + table)
migrateTableData(table)
print("Setting keys")
addKeys()
mysqldb, mysqlcsr = connectMySQLDatabase()
mssqldb, mssqlcsr = connectMicrosoftSQLDatabase()
tables = getTables()
main()