Skip to content

Commit 540c4f2

Browse files
committed
Inject table for fixtures processing in all binds
1 parent 58d64f8 commit 540c4f2

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

src/alembic/env.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import logging
22
from asyncio import get_event_loop
3+
from datetime import datetime
34

5+
from sqlalchemy import Table, Column, String, DateTime
46
from sqlalchemy.ext.asyncio import AsyncEngine
7+
from sqlalchemy.orm import registry
58

69
from alembic import context
710
from common.bootstrap import application_init
@@ -32,6 +35,19 @@
3235
db_names = target_metadata.keys()
3336
config.set_main_option("databases", ",".join(db_names))
3437

38+
def inject_fixture_tables(registry_mapper: registry):
39+
Table(
40+
"alembic_fixtures",
41+
registry_mapper.metadata,
42+
Column("filename", String(), primary_key=True),
43+
Column("signature", String(), nullable=False),
44+
Column("processed_at", DateTime(timezone=True), nullable=False, default=datetime.now),
45+
)
46+
47+
for name in db_names:
48+
inject_fixture_tables(sa_manager.get_bind(name).registry_mapper)
49+
50+
3551
# add your model's MetaData objects here
3652
# for 'autogenerate' support. These must be set
3753
# up to hold just those tables targeting a
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Initialize fixture tables
2+
3+
Revision ID: 52b1246eda46
4+
Revises:
5+
Create Date: 2025-01-26 21:23:26.321986
6+
7+
"""
8+
9+
import sqlalchemy as sa
10+
11+
from alembic import op
12+
13+
# revision identifiers, used by Alembic.
14+
revision = "52b1246eda46"
15+
down_revision = None
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade(engine_name: str) -> None:
21+
globals()[f"upgrade_{engine_name}"]()
22+
23+
24+
def downgrade(engine_name: str) -> None:
25+
globals()[f"downgrade_{engine_name}"]()
26+
27+
28+
def upgrade_default() -> None:
29+
op.create_table('alembic_fixtures',
30+
sa.Column('filename', sa.String(), nullable=False),
31+
sa.Column('signature', sa.String(), nullable=False),
32+
sa.Column('processed_at', sa.DateTime(timezone=True), nullable=False),
33+
sa.PrimaryKeyConstraint('filename')
34+
)
35+
36+
37+
def downgrade_default() -> None:
38+
op.drop_table('alembic_fixtures')

src/alembic/versions/2022-11-09-203313-52b1246eda46_create_tables.py renamed to src/alembic/versions/2025-01-26-212826-bd73bd8a2ac4_create_books_table.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
"""create books table
1+
"""Create books table
22
3-
Revision ID: 52b1246eda46
4-
Revises:
5-
Create Date: 2022-11-09 20:33:13.035514
3+
Revision ID: bd73bd8a2ac4
4+
Revises: 52b1246eda46
5+
Create Date: 2025-01-26 21:28:26.321986
66
77
"""
8-
8+
from alembic import op
99
import sqlalchemy as sa
1010

11-
from alembic import op
1211

1312
# revision identifiers, used by Alembic.
14-
revision = "52b1246eda46"
15-
down_revision = None
13+
revision = 'bd73bd8a2ac4'
14+
down_revision = '52b1246eda46'
1615
branch_labels = None
1716
depends_on = None
1817

0 commit comments

Comments
 (0)