Skip to content

Make chat_type a native enum and test migrations with pytest-alembic - #3

Closed
lesnik512 wants to merge 1 commit into
refactor-di-wiring-and-chat-listingfrom
enum-native-and-migration-tests
Closed

Make chat_type a native enum and test migrations with pytest-alembic#3
lesnik512 wants to merge 1 commit into
refactor-di-wiring-and-chat-listingfrom
enum-native-and-migration-tests

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Stacked on #2 — base is refactor-di-wiring-and-chat-listing so the diff stays
clean; GitHub retargets this to main when #2 merges.

Rationale in
planning/changes/2026-08-21.03-native-enum-and-migration-tests.md.

What

sa.Enum(native_enum=False, create_constraint=True) produced a permanent
autogenerate false positive — Postgres reflects the CHECK body as
chat_type::text = ANY (ARRAY[...]), which never matches what Alembic renders
from the model, so every run proposed dropping ck_chats_chattype. alembic check could therefore never be a drift gate.

  • Native enum. chat_type is now a native Postgres enum;
    migrations/env.py imports alembic-postgresql-enum for its autogenerate
    hooks. The library wrote the entire conversion migration, USING clause and
    working downgrade included.
  • pytest-alembic. tests/migrations/ runs test_single_head_revision,
    test_upgrade, test_up_down_consistency and
    test_model_definitions_match_ddl (the last is alembic check as a test).
    just test only ever ran downgrade base && upgrade head, which proves
    neither per-revision reversibility nor the absence of branched heads.
  • The suite cycles the schema out from under db_session's rollback fixture, so
    it is excluded from the default run (--ignore in addopts, omit in
    coverage) and gets just test-migrations plus its own CI step.

Incidental

  • Settings.sync_db_dsn_parsed owns the asyncpgpsycopg2 DSN rewrite that
    env.py and the alembic_engine fixture both need.
  • alembic.ini gains path_separator = os; without it Alembic's
    DeprecationWarning fails tests under filterwarnings = ["error"].

Verification

  • just test — 109 passed, 100% coverage.
  • just test-migrations — 4 passed.
  • just lint, just check-planning — clean.
  • alembic upgrade head && alembic check — clean (previously reported
    remove_constraint ck_chats_chattype); downgrade -1 && upgrade head
    round-trips.

sa.Enum(native_enum=False, create_constraint=True) produced a permanent
autogenerate false positive: Postgres reflects the CHECK body as
chat_type::text = ANY (ARRAY[...]), which never matches what Alembic renders
from the model, so every run proposed dropping ck_chats_chattype. That kept
alembic check from ever being usable as a drift gate.

chat_type is now a native Postgres enum and migrations/env.py imports
alembic-postgresql-enum for its autogenerate hooks - which wrote the whole
conversion, USING clause and working downgrade included.

tests/migrations/ runs the four pytest-alembic built-ins, including
test_model_definitions_match_ddl (alembic check as a test) and
test_up_down_consistency, which just test never covered. The suite cycles the
schema, so it is excluded from the default run and from coverage, and gets
its own just recipe and CI step.

Settings.sync_db_dsn_parsed now owns the asyncpg -> psycopg2 DSN rewrite that
env.py and the alembic_engine fixture both need. alembic.ini gains
path_separator = os so Alembic's deprecation warning stops failing tests under
filterwarnings = ["error"].
@lesnik512
lesnik512 deleted the branch refactor-di-wiring-and-chat-listing August 21, 2026 18:56
@lesnik512 lesnik512 closed this Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant