-
Notifications
You must be signed in to change notification settings - Fork 14.1k
/
benchmark_migration.py
244 lines (212 loc) · 8.86 KB
/
benchmark_migration.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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import importlib.util
import logging
import re
import time
from collections import defaultdict
from graphlib import TopologicalSorter
from inspect import getsource
from pathlib import Path
from types import ModuleType
from typing import Any
import click
from flask import current_app
from flask_appbuilder import Model
from flask_migrate import downgrade, upgrade
from progress.bar import ChargingBar
from sqlalchemy import create_engine, inspect
from sqlalchemy.ext.automap import automap_base
from superset import db
from superset.utils.mock_data import add_sample_rows
logger = logging.getLogger(__name__)
def import_migration_script(filepath: Path) -> ModuleType:
"""
Import migration script as if it were a module.
"""
spec = importlib.util.spec_from_file_location(filepath.stem, filepath)
if spec:
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module) # type: ignore
return module
raise Exception(f"No module spec found in location: `{str(filepath)}`")
def extract_modified_tables(module: ModuleType) -> set[str]:
"""
Extract the tables being modified by a migration script.
This function uses a simple approach of looking at the source code of
the migration script looking for patterns. It could be improved by
actually traversing the AST.
"""
tables: set[str] = set()
for function in {"upgrade", "downgrade"}:
source = getsource(getattr(module, function))
tables.update(re.findall(r'alter_table\(\s*"(\w+?)"\s*\)', source, re.DOTALL))
tables.update(re.findall(r'add_column\(\s*"(\w+?)"\s*,', source, re.DOTALL))
tables.update(re.findall(r'drop_column\(\s*"(\w+?)"\s*,', source, re.DOTALL))
return tables
def find_models(module: ModuleType) -> list[type[Model]]:
"""
Find all models in a migration script.
"""
models: list[type[Model]] = []
tables = extract_modified_tables(module)
# add models defined explicitly in the migration script
queue = list(module.__dict__.values())
while queue:
obj = queue.pop()
if hasattr(obj, "__tablename__"):
tables.add(obj.__tablename__)
elif isinstance(obj, list):
queue.extend(obj)
elif isinstance(obj, dict):
queue.extend(obj.values())
# build models by automapping the existing tables, instead of using current
# code; this is needed for migrations that modify schemas (eg, add a column),
# where the current model is out-of-sync with the existing table after a
# downgrade
sqlalchemy_uri = current_app.config["SQLALCHEMY_DATABASE_URI"]
engine = create_engine(sqlalchemy_uri)
Base = automap_base()
Base.prepare(engine, reflect=True)
seen = set()
while tables:
table = tables.pop()
seen.add(table)
try:
model = getattr(Base.classes, table)
except AttributeError:
continue
model.__tablename__ = table
models.append(model)
# add other models referenced in foreign keys
inspector = inspect(model)
for column in inspector.columns.values():
for foreign_key in column.foreign_keys:
table = foreign_key.column.table.name
if table not in seen:
tables.add(table)
# sort topologically so we can create entities in order and
# maintain relationships (eg, create a database before creating
# a slice)
sorter: TopologicalSorter[Any] = TopologicalSorter()
for model in models:
inspector = inspect(model)
dependent_tables: list[str] = []
for column in inspector.columns.values():
for foreign_key in column.foreign_keys:
if foreign_key.column.table.name != model.__tablename__:
dependent_tables.append(foreign_key.column.table.name)
sorter.add(model.__tablename__, *dependent_tables)
order = list(sorter.static_order())
models.sort(key=lambda model: order.index(model.__tablename__))
return models
@click.command()
@click.argument("filepath")
@click.option("--limit", default=1000, help="Maximum number of entities.")
@click.option("--force", is_flag=True, help="Do not prompt for confirmation.")
@click.option("--no-auto-cleanup", is_flag=True, help="Do not remove created models.")
def main(
filepath: str, limit: int = 1000, force: bool = False, no_auto_cleanup: bool = False
) -> None:
auto_cleanup = not no_auto_cleanup
print(f"Importing migration script: {filepath}")
module = import_migration_script(Path(filepath))
revision: str = getattr(module, "revision", "")
down_revision: str = getattr(module, "down_revision", "")
if not revision or not down_revision:
raise Exception(
"Not a valid migration script, couldn't find down_revision/revision"
)
print(f"Migration goes from {down_revision} to {revision}")
current_revision = db.engine.execute(
"SELECT version_num FROM alembic_version"
).scalar()
print(f"Current version of the DB is {current_revision}")
if current_revision != down_revision:
if not force:
click.confirm(
"\nRunning benchmark will downgrade the Superset DB to "
f"{down_revision} and upgrade to {revision} again. There may "
"be data loss in downgrades. Continue?",
abort=True,
)
downgrade(revision=down_revision)
print("\nIdentifying models used in the migration:")
models = find_models(module)
model_rows: dict[type[Model], int] = {}
for model in models:
rows = db.session.query(model).count()
print(f"- {model.__name__} ({rows} rows in table {model.__tablename__})")
model_rows[model] = rows
print("Benchmarking migration")
results: dict[str, float] = {}
start = time.time()
upgrade(revision=revision)
duration = time.time() - start
results["Current"] = duration
print(f"Migration on current DB took: {duration:.2f} seconds")
min_entities = 10
new_models: dict[type[Model], list[Model]] = defaultdict(list)
while min_entities <= limit:
downgrade(revision=down_revision)
print(f"Running with at least {min_entities} entities of each model")
for model in models:
missing = min_entities - model_rows[model]
if missing > 0:
entities: list[Model] = []
print(f"- Adding {missing} entities to the {model.__name__} model")
bar = ChargingBar("Processing", max=missing)
try:
for entity in add_sample_rows(model, missing):
entities.append(entity)
bar.next()
except Exception:
db.session.rollback()
raise
bar.finish()
model_rows[model] = min_entities
db.session.add_all(entities)
db.session.commit()
if auto_cleanup:
new_models[model].extend(entities)
start = time.time()
upgrade(revision=revision)
duration = time.time() - start
print(f"Migration for {min_entities}+ entities took: {duration:.2f} seconds")
results[f"{min_entities}+"] = duration
min_entities *= 10
print("\nResults:\n")
for label, duration in results.items():
print(f"{label}: {duration:.2f} s")
if auto_cleanup:
print("Cleaning up DB")
# delete in reverse order of creation to handle relationships
for model, entities in list(new_models.items())[::-1]:
db.session.query(model).filter(
model.id.in_(entity.id for entity in entities)
).delete(synchronize_session=False)
db.session.commit()
if current_revision != revision and not force:
click.confirm(f"\nRevert DB to {revision}?", abort=True)
upgrade(revision=revision)
print("Reverted")
if __name__ == "__main__":
from superset.app import create_app
app = create_app()
with app.app_context():
# pylint: disable=no-value-for-parameter
main()