Skip to content

Commit 6e368ae

Browse files
committed
fix: align migrations with models for bigint ids and required deck_id
1 parent 0a18f4b commit 6e368ae

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

‎app/models.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ class Card(BigIntAuditBase):
2727
front: orm.Mapped[str] = orm.mapped_column(sa.String, nullable=False)
2828
back: orm.Mapped[str | None] = orm.mapped_column(sa.String, nullable=True)
2929
hint: orm.Mapped[str | None] = orm.mapped_column(sa.String, nullable=True)
30-
deck_id: orm.Mapped[int] = orm.mapped_column(sa.Integer, sa.ForeignKey("decks.id"))
30+
deck_id: orm.Mapped[int] = orm.mapped_column(sa.BigInteger, sa.ForeignKey("decks.id"))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""bigint ids and required deck_id.
2+
3+
Revision ID: 8e4a2c7b1f6d
4+
Revises: 5d1c0f3a9b2e
5+
Create Date: 2026-10-16 22:00:00.000000
6+
7+
"""
8+
9+
import sqlalchemy as sa
10+
from alembic import op
11+
12+
13+
# revision identifiers, used by Alembic.
14+
revision = "8e4a2c7b1f6d"
15+
down_revision = "5d1c0f3a9b2e"
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade() -> None:
21+
op.alter_column("decks", "id", type_=sa.BigInteger(), existing_type=sa.Integer(), existing_nullable=False)
22+
op.execute("ALTER SEQUENCE decks_id_seq AS bigint")
23+
op.alter_column("cards", "id", type_=sa.BigInteger(), existing_type=sa.Integer(), existing_nullable=False)
24+
op.execute("ALTER SEQUENCE cards_id_seq AS bigint")
25+
op.alter_column("cards", "deck_id", type_=sa.BigInteger(), existing_type=sa.Integer(), nullable=False)
26+
27+
28+
def downgrade() -> None:
29+
op.alter_column("cards", "deck_id", type_=sa.Integer(), existing_type=sa.BigInteger(), nullable=True)
30+
op.execute("ALTER SEQUENCE cards_id_seq AS integer")
31+
op.alter_column("cards", "id", type_=sa.Integer(), existing_type=sa.BigInteger(), existing_nullable=False)
32+
op.execute("ALTER SEQUENCE decks_id_seq AS integer")
33+
op.alter_column("decks", "id", type_=sa.Integer(), existing_type=sa.BigInteger(), existing_nullable=False)

‎tests/test_migrations.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import typing
2+
3+
import sqlalchemy as sa
4+
from alembic.autogenerate import compare_metadata
5+
from alembic.runtime.migration import MigrationContext
6+
7+
from app.models import METADATA
8+
from app.resources.db import create_primary_sa_engine
9+
10+
11+
def _compare_with_models(connection: sa.Connection) -> list[typing.Any]:
12+
return compare_metadata(MigrationContext.configure(connection), METADATA)
13+
14+
15+
async def test_migrations_match_models() -> None:
16+
engine: typing.Final = create_primary_sa_engine()
17+
try:
18+
async with engine.connect() as connection:
19+
diff = await connection.run_sync(_compare_with_models)
20+
finally:
21+
await engine.dispose()
22+
23+
assert diff == []
24+
25+
26+
async def test_id_sequences_are_bigint() -> None:
27+
engine: typing.Final = create_primary_sa_engine()
28+
try:
29+
async with engine.connect() as connection:
30+
sequence_types = dict(
31+
(await connection.execute(sa.text("SELECT sequence_name, data_type FROM information_schema.sequences")))
32+
.tuples()
33+
.all()
34+
)
35+
finally:
36+
await engine.dispose()
37+
38+
assert sequence_types == {"decks_id_seq": "bigint", "cards_id_seq": "bigint"}

0 commit comments

Comments
 (0)