Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/check-migrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Check DB migrations
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:

check:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.13

- name: Install uv
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
echo "$HOME/.cargo/bin" >> $GITHUB_PATH

- name: Create virtual environment and install dependencies
run: |
uv venv
uv sync --extra dev

- name: Run migration checks
run: |
source .venv/bin/activate
python generate_migrations.py --check
38 changes: 29 additions & 9 deletions generate_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,29 @@ def run(cmd, env=None):
subprocess.run(cmd, shell=True, check=True, env=env or os.environ)


def print_db_schema():
print("📦 Printing database schema...")
query = """
SELECT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_schema = 'public'
ORDER BY table_name, ordinal_position;
"""
psql_cmd = (
f'psql -h localhost -p {DEFAULT_PORT} -U {POSTGRES_USER} '
f'-c "{query.strip()}"'
)
env = os.environ.copy()
env["PGPASSWORD"] = POSTGRES_PASSWORD
run(psql_cmd, env=env)


@click.command()
@click.option('--revision-message', '-m', required=False, help="Message for Alembic revision.")
@click.option('--check', is_flag=True, help="Only run 'alembic check' after DB container is up.")
def generate_migrations(revision_message, check):
"""Spin up a temp Postgres DB, apply migrations or run alembic check."""
@click.option('--print-schema', is_flag=True, help="Print the schema of the database after upgrade/check.")
def generate_migrations(revision_message, check, print_schema):
"""Spin up a temp Postgres DB, apply migrations or run alembic check, optionally print schema."""
if not check and not revision_message:
raise click.UsageError("Missing option '-m' / '--revision-message'. Required unless using --check.")

Expand All @@ -39,19 +57,21 @@ def generate_migrations(revision_message, check):
)

print("⏳ Waiting for database to be ready...")
time.sleep(5) # Could be enhanced with pg_isready
time.sleep(5)

print("🧱 Applying existing Alembic migrations...")
run("alembic upgrade head")

if check:
print("🧱 Applying existing Alembic migrations...")
run("alembic upgrade head")
print("🔍 Running 'alembic check'...")
run("alembic check")
else:
print("🧱 Applying existing Alembic migrations...")
run("alembic upgrade head")

elif revision_message:
print("📝 Generating new Alembic revision...")
run(f"alembic revision --autogenerate -m \"{revision_message}\"")
run(f'alembic revision --autogenerate -m "{revision_message}"')

if print_schema:
print_db_schema()

finally:
print("🧹 Cleaning up: stopping container...")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""add_group_membership
"""group_membership

Revision ID: 101f45395233
Revision ID: 07161e26f537
Revises:
Create Date: 2025-07-01 16:29:48.072722
Create Date: 2025-07-03 11:48:44.293471

"""
from typing import Sequence, Union
Expand All @@ -13,7 +13,7 @@


# revision identifiers, used by Alembic.
revision: str = '101f45395233'
revision: str = '07161e26f537'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
Expand All @@ -26,7 +26,7 @@ def upgrade() -> None:
sa.Column('group', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column('user_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column('user_email', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column('approval_status', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column('approval_status', sa.Enum('APPROVED', 'PENDING', 'REVOKED', name='ApprovalStatusEnum'), nullable=False),
sa.Column('updated_at', sa.DateTime(), nullable=False),
sa.Column('updated_by_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column('updated_by_email', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
Expand Down
37 changes: 0 additions & 37 deletions migrations/versions/e55434038b96_status_enum.py

This file was deleted.