|
| 1 | +""" |
| 2 | +Add communication preference columns to the user table. |
| 3 | +
|
| 4 | +Required when upgrading from <=0.1.27 to >0.1.27. The comm_opt_in, |
| 5 | +comm_updates, and comm_marketing columns were introduced in 0.1.28, and |
| 6 | +SQLModel create_all() does not alter existing tables. Run this against any |
| 7 | +local or deployed database that predates the columns. |
| 8 | +
|
| 9 | +Usage: |
| 10 | + uv run python -m migrations.add_communication_preferences .env |
| 11 | + uv run python -m migrations.add_communication_preferences .env --apply |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import argparse |
| 17 | +from dataclasses import dataclass |
| 18 | + |
| 19 | +from dotenv import load_dotenv |
| 20 | +from sqlalchemy import text |
| 21 | +from sqlmodel import Session, create_engine |
| 22 | + |
| 23 | +from utils.core.db import get_connection_url |
| 24 | + |
| 25 | +COLUMNS = ("comm_opt_in", "comm_updates", "comm_marketing") |
| 26 | + |
| 27 | + |
| 28 | +@dataclass |
| 29 | +class MigrationStats: |
| 30 | + missing_columns: tuple[str, ...] = () |
| 31 | + all_present: bool = False |
| 32 | + |
| 33 | + |
| 34 | +def _column_exists(session: Session, column_name: str) -> bool: |
| 35 | + result = session.connection().execute( |
| 36 | + text( |
| 37 | + """ |
| 38 | + SELECT 1 |
| 39 | + FROM information_schema.columns |
| 40 | + WHERE table_schema = 'public' |
| 41 | + AND table_name = 'user' |
| 42 | + AND column_name = :column_name |
| 43 | + """ |
| 44 | + ), |
| 45 | + {"column_name": column_name}, |
| 46 | + ) |
| 47 | + return result.first() is not None |
| 48 | + |
| 49 | + |
| 50 | +def add_communication_preference_columns(env_file: str, apply: bool) -> MigrationStats: |
| 51 | + load_dotenv(env_file, override=True) |
| 52 | + engine = create_engine(get_connection_url()) |
| 53 | + stats = MigrationStats() |
| 54 | + |
| 55 | + try: |
| 56 | + with Session(engine) as session: |
| 57 | + missing = tuple( |
| 58 | + column for column in COLUMNS if not _column_exists(session, column) |
| 59 | + ) |
| 60 | + stats.missing_columns = missing |
| 61 | + stats.all_present = not missing |
| 62 | + |
| 63 | + if apply and missing: |
| 64 | + session.connection().execute( |
| 65 | + text( |
| 66 | + """ |
| 67 | + ALTER TABLE "user" |
| 68 | + ADD COLUMN IF NOT EXISTS comm_opt_in BOOLEAN NOT NULL DEFAULT FALSE, |
| 69 | + ADD COLUMN IF NOT EXISTS comm_updates BOOLEAN NOT NULL DEFAULT FALSE, |
| 70 | + ADD COLUMN IF NOT EXISTS comm_marketing BOOLEAN NOT NULL DEFAULT FALSE |
| 71 | + """ |
| 72 | + ) |
| 73 | + ) |
| 74 | + session.commit() |
| 75 | + else: |
| 76 | + session.rollback() |
| 77 | + finally: |
| 78 | + engine.dispose() |
| 79 | + |
| 80 | + return stats |
| 81 | + |
| 82 | + |
| 83 | +def main() -> None: |
| 84 | + parser = argparse.ArgumentParser( |
| 85 | + description=( |
| 86 | + "Add comm_opt_in, comm_updates, and comm_marketing to the user table. " |
| 87 | + "Without --apply, runs in dry-run mode." |
| 88 | + ) |
| 89 | + ) |
| 90 | + parser.add_argument("env", help="Env file to use (e.g. .env)") |
| 91 | + parser.add_argument( |
| 92 | + "--apply", |
| 93 | + action="store_true", |
| 94 | + help="Apply the schema change (default is dry-run).", |
| 95 | + ) |
| 96 | + args = parser.parse_args() |
| 97 | + |
| 98 | + stats = add_communication_preference_columns(env_file=args.env, apply=args.apply) |
| 99 | + mode = "APPLY" if args.apply else "DRY-RUN" |
| 100 | + if stats.all_present: |
| 101 | + print(f"[{mode}] All communication preference columns already exist.") |
| 102 | + return |
| 103 | + |
| 104 | + print(f"[{mode}] missing_columns={list(stats.missing_columns)}") |
| 105 | + if args.apply: |
| 106 | + print(f"[{mode}] Columns added successfully.") |
| 107 | + else: |
| 108 | + print("Dry-run only. Re-run with --apply to add columns.") |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + main() |
0 commit comments