Skip to content

Commit

Permalink
SQLAlchemy 2 kompatibilita (#258)
Browse files Browse the repository at this point in the history
resolves #257
  • Loading branch information
kokes committed Jan 27, 2023
1 parent d1005e2 commit 7bdaa39
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from collections import defaultdict
from importlib import import_module

from sqlalchemy import Boolean, MetaData, Table, create_engine
from sqlalchemy import Boolean, MetaData, Table, create_engine, text
from sqlalchemy.schema import AddConstraint, DropConstraint, ForeignKeyConstraint


Expand Down Expand Up @@ -169,7 +169,8 @@ def warninger(message, category, filename, lineno, line=None):

if engine.name == "postgresql":
table.schema = f"{args.schema_prefix}{module_name}"
engine.execute(f"CREATE SCHEMA IF NOT EXISTS {table.schema}")
with engine.begin() as conn:
conn.execute(text(f"CREATE SCHEMA IF NOT EXISTS {table.schema}"))
elif engine.name == "sqlite":
table.name = f"{args.schema_prefix}{module_name}_{table.name}"

Expand All @@ -181,11 +182,16 @@ def warninger(message, category, filename, lineno, line=None):
# z nejakeho duvodu jsou v sqlite nepojmenovany klice
if engine.name == "postgresql":
dbtable = Table(
table.name, MetaData(engine), schema=table.schema, autoload=True
table.name,
MetaData(engine),
schema=table.schema,
autoload_with=engine,
)
for fk in dbtable.constraints:
if isinstance(fk, ForeignKeyConstraint):
DropConstraint(fk).execute(engine)
sql = DropConstraint(fk).compile()
with engine.begin() as conn:
conn.execute(text(sql.string))

if engine.name == "postgresql":
full_table_name = f"{table.schema}.{table.name}"
Expand Down Expand Up @@ -231,6 +237,8 @@ def warninger(message, category, filename, lineno, line=None):
for fk in fkeys:
if not isinstance(fk, ForeignKeyConstraint):
continue
AddConstraint(fk).execute(bind=engine)
sql = AddConstraint(fk).compile()
with engine.begin() as conn:
conn.execute(text(sql.string))

print(f" ({time.time() - t:.2f}s)")
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ requests
tqdm
xlrd
cssselect
sqlalchemy<2.0.0
sqlalchemy

0 comments on commit 7bdaa39

Please sign in to comment.