Skip to content

Commit bec7319

Browse files
Merge pull request #38 from AustralianBioCommons/db-migrations
AAI-119 don't automatically update the production DB - use migrations
2 parents 9e36422 + 65a3880 commit bec7319

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,25 @@ python generate_migrations.py -m migration_name
8585
```
8686

8787
and commit them to git. Once your updated code has been
88-
deployed on AWS, you can use `aws ecs execute-command`
89-
to run the migrations.
88+
deployed on AWS, you can connect to the container via
89+
the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html).
90+
Run `aws sso login` first, then `aws ecs execute-command`
91+
to access a shell in the container:
92+
93+
```shell
94+
aws ecs execute-command \
95+
--cluster <cluster-id> \
96+
--task <task-id> \
97+
--container FastAPIContainer \
98+
--command "/bin/sh" \
99+
--interactive
100+
```
101+
102+
and run the migrations:
103+
104+
```shell
105+
uv run alembic upgrade head
106+
```
90107

91108
# Deployment
92109

db/setup.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import logging
12
import os
23
from typing import Tuple
34

45
from dotenv import dotenv_values
56
from sqlmodel import Session, SQLModel, create_engine
67

8+
log = logging.getLogger('uvicorn.error')
9+
710

811
def get_db_config() -> Tuple[str, dict]:
912
"""
@@ -40,7 +43,12 @@ def get_db_config() -> Tuple[str, dict]:
4043

4144

4245
def create_db_and_tables():
43-
SQLModel.metadata.create_all(engine)
46+
# NOTE: we only do this in dev (with sqlite).
47+
# For production, we manage the DB schema with alembic
48+
db_url, connect_args = get_db_config()
49+
if db_url.startswith("sqlite://"):
50+
log.info("Automatically creating DB tables for sqlite")
51+
SQLModel.metadata.create_all(engine)
4452

4553

4654
def get_db_session():
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""status_enum
2+
3+
Revision ID: e55434038b96
4+
Revises: 101f45395233
5+
Create Date: 2025-07-03 09:41:04.657535
6+
7+
"""
8+
from typing import Sequence, Union
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
import sqlmodel
13+
14+
15+
# revision identifiers, used by Alembic.
16+
revision: str = 'e55434038b96'
17+
down_revision: Union[str, None] = '101f45395233'
18+
branch_labels: Union[str, Sequence[str], None] = None
19+
depends_on: Union[str, Sequence[str], None] = None
20+
21+
22+
def upgrade() -> None:
23+
# ### commands auto generated by Alembic - please adjust! ###
24+
op.alter_column('groupmembership', 'approval_status',
25+
existing_type=sa.VARCHAR(),
26+
type_=sa.Enum('APPROVED', 'PENDING', 'REVOKED', name='ApprovalStatusEnum'),
27+
existing_nullable=False)
28+
# ### end Alembic commands ###
29+
30+
31+
def downgrade() -> None:
32+
# ### commands auto generated by Alembic - please adjust! ###
33+
op.alter_column('groupmembership', 'approval_status',
34+
existing_type=sa.Enum('APPROVED', 'PENDING', 'REVOKED', name='ApprovalStatusEnum'),
35+
type_=sa.VARCHAR(),
36+
existing_nullable=False)
37+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)