import psycopg from app.platform import db _TABLES = {"account", "auth_code", "storefront", "storefront_membership"} def _table_names(conn) -> set[str]: rows = conn.execute( "SELECT tablename FROM pg_tables WHERE schemaname = 'public'" ).fetchall() return {r[0] for r in rows} def test_migrate_from_empty_applies_0001(fresh_db_url): with psycopg.connect(fresh_db_url) as conn: applied = db.migrate(conn) assert applied == ["0001_init.sql"] with psycopg.connect(fresh_db_url) as conn: assert _TABLES.issubset(_table_names(conn)) def test_migrate_is_idempotent(fresh_db_url): with psycopg.connect(fresh_db_url) as conn: db.migrate(conn) with psycopg.connect(fresh_db_url) as conn: applied_again = db.migrate(conn) assert applied_again == [] def test_membership_has_no_unique_account_constraint(fresh_db_url): # INV-4: the schema must keep many-storefronts-per-account open — no UNIQUE on # storefront_membership.account_id anywhere. with psycopg.connect(fresh_db_url) as conn: db.migrate(conn) rows = conn.execute( """ SELECT indexdef FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'storefront_membership' """ ).fetchall() defs = " ".join(r[0] for r in rows).lower() assert "unique" not in defs.replace("primary key", "") or "(account_id)" not in defs