Skip to content

Commit 4a8da65

Browse files
authored
Fixing our env.py to work with sqlalchemy 2.0 (#6030)
1 parent a9f1cf3 commit 4a8da65

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

src/dispatch/database/revisions/core/env.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from alembic import context
2-
from sqlalchemy import engine_from_config, pool, text
2+
from sqlalchemy import create_engine, text
33

44
from dispatch.logging import logging
55
from dispatch.config import SQLALCHEMY_DATABASE_URI
@@ -23,10 +23,8 @@
2323

2424
def include_object(object, name, type_, reflected, compare_to):
2525
if type_ == "table":
26-
if object.schema == CORE_SCHEMA_NAME:
27-
return True
28-
else:
29-
return True
26+
return object.schema == CORE_SCHEMA_NAME
27+
return True
3028

3129

3230
def run_migrations_online():
@@ -36,28 +34,23 @@ def run_migrations_online():
3634
and associate a connection with the context.
3735
3836
"""
39-
40-
# don't create empty revisions
4137
def process_revision_directives(context, revision, directives):
4238
script = directives[0]
4339
if script.upgrade_ops.is_empty():
4440
directives[:] = []
4541
log.info("No changes found skipping revision creation.")
4642

47-
connectable = engine_from_config(
48-
config.get_section(config.config_ini_section), prefix="sqlalchemy.", poolclass=pool.NullPool
49-
)
43+
connectable = create_engine(SQLALCHEMY_DATABASE_URI)
5044

5145
log.info("Migrating dispatch core schema...")
5246
# migrate common tables
5347
with connectable.connect() as connection:
5448
set_search_path = text(f'set search_path to "{CORE_SCHEMA_NAME}"')
5549
connection.execute(set_search_path)
56-
connection.dialect.default_schema_name = CORE_SCHEMA_NAME
50+
connection.commit()
5751
context.configure(
5852
connection=connection,
5953
target_metadata=target_metadata,
60-
include_schemas=True,
6154
include_object=include_object,
6255
process_revision_directives=process_revision_directives,
6356
)

src/dispatch/database/revisions/tenant/env.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from alembic import context
2-
from sqlalchemy import engine_from_config, pool, inspect, text
2+
from sqlalchemy import create_engine, inspect, text
33

44

55
from dispatch.logging import logging
@@ -43,29 +43,26 @@ def run_migrations_online():
4343
and associate a connection with the context.
4444
4545
"""
46-
4746
def process_revision_directives(context, revision, directives):
4847
script = directives[0]
4948
if script.upgrade_ops.is_empty():
5049
directives[:] = []
5150
log.info("No changes found skipping revision creation.")
5251

53-
connectable = engine_from_config(
54-
config.get_section(config.config_ini_section), prefix="sqlalchemy.", poolclass=pool.NullPool
55-
)
52+
connectable = create_engine(SQLALCHEMY_DATABASE_URI)
5653

5754
with connectable.connect() as connection:
5855
# get the schema names
5956
for schema in get_tenant_schemas(connection):
6057
log.info(f"Migrating {schema}...")
6158
set_search_path = text(f'set search_path to "{schema}"')
6259
connection.execute(set_search_path)
63-
connection.dialect.default_schema_name = schema
60+
connection.commit()
6461

62+
print(target_metadata)
6563
context.configure(
6664
connection=connection,
6765
target_metadata=target_metadata,
68-
include_schemas=True,
6966
include_object=include_object,
7067
process_revision_directives=process_revision_directives,
7168
)

0 commit comments

Comments
 (0)