Skip to content

Commit

Permalink
GH-4: Introduce Alembic for database migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
markhobson committed Oct 20, 2023
1 parent 025b13a commit 63584cc
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 6 deletions.
2 changes: 2 additions & 0 deletions alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alembic]
script_location = schemes:migrations
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "1.0.0"
description = "ATE Schemes App."
requires-python = ">=3.11"
dependencies = [
"alembic~=1.12.0",
"authlib~=1.2.0",
"flask~=2.3.0",
"govuk-frontend-jinja~=2.7.0",
Expand Down
17 changes: 11 additions & 6 deletions schemes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import os
from typing import Any, Mapping

import alembic.config
import inject
from alembic import command
from authlib.integrations.flask_client import OAuth
from authlib.oauth2.rfc7523 import PrivateKeyJWT
from flask import Config, Flask, Response, render_template, request, url_for
from inject import Binder
from jinja2 import ChoiceLoader, FileSystemLoader, PackageLoader, PrefixLoader
from sqlalchemy import Engine, MetaData, create_engine
from sqlalchemy import Engine, create_engine

from schemes import api, auth, home, start
from schemes.config import DevConfig
from schemes.users import DatabaseUserRepository, User, UserRepository, add_tables
from schemes.users import DatabaseUserRepository, User, UserRepository


def create_app(test_config: Mapping[str, Any] | None = None) -> Flask:
Expand Down Expand Up @@ -107,11 +109,14 @@ def _configure_oidc(app: Flask) -> None:


def _create_database() -> None:
metadata = MetaData()
add_tables(metadata)

engine = inject.instance(Engine)
metadata.create_all(engine)

alembic_config = alembic.config.Config()
alembic_config.set_main_option("script_location", "schemes:migrations")

with engine.connect() as connection:
alembic_config.attributes["connection"] = connection
command.upgrade(alembic_config, "head")


def _configure_users() -> None:
Expand Down
7 changes: 7 additions & 0 deletions schemes/migrations/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from alembic import context

connection = context.config.attributes["connection"]
context.configure(connection=connection, target_metadata=None)

with context.begin_transaction():
context.run_migrations()
24 changes: 24 additions & 0 deletions schemes/migrations/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = ${repr(up_revision).replace("'", '"')}
down_revision: Union[str, None] = ${repr(down_revision).replace("'", '"')}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels).replace("'", '"')}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on).replace("'", '"')}


def upgrade() -> None:
${upgrades if upgrades else "pass"}


def downgrade() -> None:
${downgrades if downgrades else "pass"}
28 changes: 28 additions & 0 deletions schemes/migrations/versions/bfc3cbc1bb13_create_users_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Create users table
Revision ID: bfc3cbc1bb13
Revises:
Create Date: 2023-10-20 10:15:51.912421
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "bfc3cbc1bb13"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
op.create_table(
"users",
sa.Column("id", sa.Integer, primary_key=True),
sa.Column("email", sa.String(256), nullable=False, unique=True),
)


def downgrade() -> None:
op.drop_table("users")

0 comments on commit 63584cc

Please sign in to comment.