-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
277 lines (225 loc) · 7.44 KB
/
main.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
from datetime import datetime
import os
from dataclasses import dataclass
from pydoc import describe
from unicodedata import category
import hashlib
import click
import rich
from rich.console import Console
from rich.table import Table, box
from rich import print as rprint
from rich.progress import track, Progress, BarColumn, TextColumn
import sqlparse
from dbmt.config import load_config_file
from dbmt.dataclasses import MigrationData
from dbmt.mysql import (
connect_to_mysql,
mysql_execute,
mysql_cursor_fetchone,
mysql_cursor_fetchall,
)
from dbmt.database_plugin import DatabasePlugin
from dbmt.error import print_error
from dbmt.config import VERBOSE, CHECK_VARIABLES, CONFIG, DRY_RUN, DIR
def connect_to_db():
cnx = connect_to_mysql(
user=CONFIG["database"]["username"],
password=CONFIG["database"]["password"],
host=CONFIG["database"]["host"],
database=CONFIG["database"]["database"],
)
return cnx
def add_schema_histort_table(cnx):
sql = (
"CREATE TABLE if not exists `dbmt_schema_history` ("
"`id` INT NOT NULL AUTO_INCREMENT , "
"`version` VARCHAR(10) NOT NULL , "
"`description` VARCHAR(255) NOT NULL , "
"`script` VARCHAR(255) NOT NULL , "
"`checksum` VARCHAR(64) NOT NULL , "
"`installed_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , "
"`success` INT NOT NULL, "
"PRIMARY KEY (`id`) "
") ENGINE = InnoDB;"
)
mysql_execute(cnx, sql)
cnx.commit()
def get_version(filename: str):
version = filename.split("__")[0][1:]
version = version.replace("_", ".")
return version
def get_description(filename):
description = str(filename.split("__")[1][:-4])
description = description.replace("_", " ")
description = description.capitalize()
return description
def progress_bar_settings():
text_column = TextColumn("{task.description}")
bar_column = BarColumn(bar_width=50)
progress = Progress(bar_column, text_column)
return progress
def generate_line(len: int, chr: str = "-") -> str:
string = ""
for _ in range(len):
string += chr
return string
def clean_database():
database = DatabasePlugin(CONFIG["database"]["database_plugin"])
database.connect()
database.clean_all_tables()
database.commit()
database.close()
def get_files():
files = os.listdir("sql")
files = sorted(files)
return_files = []
for file in files:
version = get_version(file)
description = get_description(file)
return_files.append(
{"filename": file, "description": description, "version": version}
)
return return_files
def get_version_info(version, schema_history_data):
for row in schema_history_data:
if row["version"] == version:
return row
return {}
def database_info():
database = DatabasePlugin(CONFIG["database"]["database_plugin"])
database.connect()
migration_info_data = get_migration_info_data(database)
table_data = []
for index in migration_info_data:
row = migration_info_data[index]
id = str(row.id)
description = str(row.description)
script = str(row.script)
total = str(row.total_queries)
done = str(row.done_queries)
installed_on = str(row.installed_on)
state = str(row.success)
table_data.append(
(
id,
description,
"SQL",
script,
total,
done,
installed_on,
state,
)
)
table = Table(box=box.ASCII2)
table.add_column("Id")
table.add_column("Description")
table.add_column("Type")
table.add_column("Script")
table.add_column("Total queries")
table.add_column("Done queries")
table.add_column("Installed on")
table.add_column("State")
for row in table_data:
table.add_row(*row)
console = Console()
console.print(table)
@click.group()
@click.option(
"--config-file",
"-c",
default="dbchangelog.yaml",
help="Config file for dbmt, default=dbmt.yml",
)
@click.option(
"--dry-run/--no-dry-run",
default=False,
help="Dry run on prints sql queries, wont execute any, default --no-dry-run",
)
@click.option(
"-d",
"--directory",
default="sql",
help="Directory with sql scripts, defalult=./sql",
)
def start(config_file, dry_run, directory):
"""db-migration-tool
Small utilitiy for sql migration from a source repo"""
global DRY_RUN
global DIR
global CONFIG
DIR = directory
DRY_RUN = dry_run
CONFIG = load_config_file(os.path.join(DIR, config_file))
@start.command(help="Migrate database")
def migrate():
migrate_database()
@start.command(help="Clean database")
def clean():
clean_database()
@start.command(help="Status of database migration")
def info():
database_info()
def load_sql_queries(filename):
sql_queries = None
with open(os.path.join("sql", filename), "r") as stream:
sql = stream.read()
sql_queries = sqlparse.split(sql)
return sql_queries
def get_scripts_data():
script_data = []
for change_set in CONFIG["databaseMigrations"]:
filename = change_set["sqlFile"]
with open(os.path.join(DIR, filename), "r") as stream:
sql_text = stream.read()
sql_queries = sqlparse.split(sql_text)
checksum = hashlib.sha256(sql_text.encode()).hexdigest()
script_data.append(
MigrationData(
**{
"id": change_set["changeSet"],
"description": "",
"script": filename,
"checksum": checksum,
"sql_queries": sql_queries,
"total_queries": len(sql_queries),
}
)
)
return script_data
def merge_migration_data(schema_history_table_data, scripts_data):
merge_data = {}
for row in scripts_data:
if row.id not in merge_data:
merge_data[row.id] = row
for row in schema_history_table_data:
if row.id not in merge_data:
merge_data[row.id] = row
else:
if row.checksum != merge_data[row.id].checksum:
print("error")
merge_data[row.id].done_queries = row.done_queries
merge_data[row.id].success = row.success
merge_data[row.id].installed_on = row.installed_on
return merge_data
def migrate_database():
database = DatabasePlugin(CONFIG["database"]["database_plugin"])
database.connect()
database.add_schema_history_table()
migration_info_data = get_migration_info_data(database)
for key in migration_info_data:
data = migration_info_data[key]
database.add_schema_history_table_entry(data)
if data.done_queries < data.total_queries:
for index in range(data.done_queries, data.total_queries):
sql = data.sql_queries[index]
database.execute(sql)
database.update_schema_history_table_entry(data, index)
database.commit()
database.close()
def get_migration_info_data(database):
schema_history_table_data = database.get_schema_history_table_data()
scripts_data = get_scripts_data()
migration_info_data = merge_migration_data(schema_history_table_data, scripts_data)
return migration_info_data