Skip to content

Commit

Permalink
fix: create fk model in benchmark script (apache#15804)
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida authored Jul 20, 2021
1 parent c9dad05 commit 5cc95bb
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions scripts/benchmark_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,22 @@ def find_models(module: ModuleType) -> List[Type[Model]]:
engine = create_engine(sqlalchemy_uri)
Base = automap_base()
Base.prepare(engine, reflect=True)
for table in tables:
seen = set()
while tables:
table = tables.pop()
seen.add(table)
model = getattr(Base.classes, table)
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)
Expand All @@ -108,7 +119,8 @@ def find_models(module: ModuleType) -> List[Type[Model]]:
dependent_tables: List[str] = []
for column in inspector.columns.values():
for foreign_key in column.foreign_keys:
dependent_tables.append(foreign_key.target_fullname.split(".")[0])
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__))
Expand Down

0 comments on commit 5cc95bb

Please sign in to comment.