-
Notifications
You must be signed in to change notification settings - Fork 0
[SPD-48239]: Adds the initial PostgreSQL layer for consent microservice #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ed13e69
6e8965a
d426970
3b27c79
735d92e
00e3fce
832a665
64e9c49
f269112
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Alembic configuration file. | ||
| # See https://alembic.sqlalchemy.org/en/latest/tutorial.html | ||
|
|
||
| [alembic] | ||
| # Path to the alembic scripts directory. | ||
| script_location = alembic | ||
|
|
||
| # Migration file template | ||
| file_template = %%(year)s%%(month)s%%(day)s_%%(rev)s_%%(slug)s | ||
|
|
||
| # Timezone for file timestamps | ||
| timezone = UTC | ||
|
|
||
| # Maximum length of revision identifiers | ||
| truncate_slug_length = 40 | ||
|
|
||
| # The SQLAlchemy URL is intentionally **not** set here. | ||
| # env.py reads DATABASE_URL from the Settings object so .env is the | ||
| # single source of truth. If you need to pass a URL on the CLI you can | ||
| # use: alembic -x sqlalchemy.url="postgresql+asyncpg://..." upgrade head | ||
|
|
||
| [loggers] | ||
| keys = root, sqlalchemy, alembic | ||
|
|
||
| [handlers] | ||
| keys = console | ||
|
|
||
| [formatters] | ||
| keys = generic | ||
|
|
||
| [logger_root] | ||
| level = WARN | ||
| handlers = console | ||
| qualname = | ||
|
|
||
| [logger_sqlalchemy] | ||
| level = WARN | ||
| handlers = | ||
| qualname = sqlalchemy.engine | ||
|
|
||
| [logger_alembic] | ||
| level = INFO | ||
| handlers = | ||
| qualname = alembic | ||
|
|
||
| [handler_console] | ||
| class = StreamHandler | ||
| args = (sys.stderr,) | ||
| level = NOTSET | ||
| formatter = generic | ||
|
|
||
| [formatter_generic] | ||
| format = %(levelname)-5.5s [%(name)s] %(message)s | ||
| datefmt = %H:%M:%S |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Generic single-database configuration with an async setup. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """ | ||
| Alembic migration environment for the tars service. | ||
|
|
||
| Async-aware (asyncpg + SQLAlchemy 2.x). | ||
| Reads DATABASE_URL from app.core.config.Settings so .env is the single | ||
| source of truth — no sqlalchemy.url in alembic.ini. | ||
|
|
||
| Import all ORM models below the Base import so that | ||
| Base.metadata knows about them when autogenerating migrations. | ||
| """ | ||
|
|
||
| import asyncio | ||
| import logging | ||
| import sys | ||
| from logging.config import fileConfig | ||
| from pathlib import Path | ||
|
|
||
| from alembic import context | ||
| from sqlalchemy.ext.asyncio import create_async_engine | ||
|
|
||
| # Add project root so `app` is importable when alembic is run from repo root. | ||
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # app imports — must be resolvable from the project root | ||
| # --------------------------------------------------------------------------- | ||
| from app.core.config import settings | ||
| from app.db.postgres import Base | ||
|
|
||
| # noqa: F401 — side-effect imports that populate Base.metadata | ||
| import app.db.models # noqa: F401 | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Alembic Config | ||
| # --------------------------------------------------------------------------- | ||
| alembic_config = context.config | ||
|
|
||
| if alembic_config.config_file_name: | ||
| fileConfig(alembic_config.config_file_name) | ||
|
|
||
| target_metadata = Base.metadata | ||
|
|
||
| logger = logging.getLogger("alembic.env") | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Offline mode — generates SQL without a live DB connection | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def run_migrations_offline() -> None: | ||
| context.configure( | ||
| url=settings.DATABASE_URL, | ||
| target_metadata=target_metadata, | ||
|
Comment on lines
+51
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CLI URL override is ignored
Want Baz to fix this for you? Activate Fixer Prompt for AI Agents |
||
| literal_binds=True, | ||
| dialect_opts={"paramstyle": "named"}, | ||
| compare_type=True, | ||
| compare_server_default=True, | ||
| ) | ||
| with context.begin_transaction(): | ||
| context.run_migrations() | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Online mode — runs migrations against a live DB | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def do_run_migrations(connection) -> None: # type: ignore[no-untyped-def] | ||
| context.configure( | ||
| connection=connection, | ||
| target_metadata=target_metadata, | ||
| compare_type=True, | ||
| compare_server_default=True, | ||
| ) | ||
| with context.begin_transaction(): | ||
| context.run_migrations() | ||
|
|
||
|
|
||
| async def run_migrations_online() -> None: | ||
| connectable = create_async_engine(settings.DATABASE_URL, echo=False) | ||
| async with connectable.connect() as connection: | ||
| await connection.run_sync(do_run_migrations) | ||
| await connectable.dispose() | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Entry point | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| if context.is_offline_mode(): | ||
| run_migrations_offline() | ||
| else: | ||
| asyncio.run(run_migrations_online()) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """${message} | ||
|
|
||
| Revision ID: ${up_revision} | ||
| Revises: ${down_revision | comma,n} | ||
| Create Date: ${create_date} | ||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
| ${imports if imports else ""} | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = ${repr(up_revision)} | ||
| down_revision: Union[str, None] = ${repr(down_revision)} | ||
| branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} | ||
| depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| ${upgrades if upgrades else "pass"} | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| ${downgrades if downgrades else "pass"} |
Uh oh!
There was an error while loading. Please reload this page.