From 2ac7f0d5bb7487a35db837a4b2352ba302edeb0f Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sat, 23 Apr 2022 21:04:27 +0200 Subject: [PATCH 01/10] init alembic --- alembic/env.py | 54 +++++++++++++++++++++++++++++++++++++++ alembic/script.py.mako | 25 ++++++++++++++++++ alembic/versions/.gitkeep | 0 3 files changed, 79 insertions(+) create mode 100644 alembic/env.py create mode 100644 alembic/script.py.mako create mode 100644 alembic/versions/.gitkeep diff --git a/alembic/env.py b/alembic/env.py new file mode 100644 index 0000000..4d208a7 --- /dev/null +++ b/alembic/env.py @@ -0,0 +1,54 @@ +import asyncio + +from alembic import context +from sqlalchemy.ext.asyncio import create_async_engine + +from the_app.models.base import Base as app_base + +target_metadata = app_base.metadata + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = f"postgresql+asyncpg://user:secret@db:5432/devdb" + context.configure(url=url, target_metadata=target_metadata, literal_binds=True) + + with context.begin_transaction(): + context.run_migrations() + + +def do_run_migrations(connection): + context.configure(connection=connection, target_metadata=target_metadata) + + with context.begin_transaction(): + context.run_migrations() + + +async def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + url = f"postgresql+asyncpg://user:secret@db:5432/devdb" + connectable = create_async_engine(url) + + async with connectable.connect() as connection: + await connection.run_sync(do_run_migrations) + + +if context.is_offline_mode(): + run_migrations_offline() +else: + asyncio.run(run_migrations_online()) diff --git a/alembic/script.py.mako b/alembic/script.py.mako new file mode 100644 index 0000000..0fdcfba --- /dev/null +++ b/alembic/script.py.mako @@ -0,0 +1,25 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +import tm.db +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/alembic/versions/.gitkeep b/alembic/versions/.gitkeep new file mode 100644 index 0000000..e69de29 From 3439a0345ff9c55b802b6a6b0ccd367b5f5c0f90 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sat, 23 Apr 2022 21:04:32 +0200 Subject: [PATCH 02/10] init alembic --- alembic/README | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 alembic/README diff --git a/alembic/README b/alembic/README new file mode 100644 index 0000000..334cb7e --- /dev/null +++ b/alembic/README @@ -0,0 +1,16 @@ +Alembic commands +==================== + +### Check current revision +>alembic current + +### Make new revision +>alembic revision --autogenerate -m "first revision" + +After autogeneration it is highly recommended to check new revision in /versions/ folder, and adjust it if needed + +### Upgrade the DB to the newest revision +>alembic upgrade head + +### Downgrade DB to previous revision +>alembic downgrade -1 From bdfd97fc1255923724b962c26c44930f16bd0bb4 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sat, 23 Apr 2022 21:04:56 +0200 Subject: [PATCH 03/10] add new models to declarative base --- the_app/models/__init__.py | 1 + the_app/models/shakespeare.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/the_app/models/__init__.py b/the_app/models/__init__.py index 9a6ae8f..63287f6 100644 --- a/the_app/models/__init__.py +++ b/the_app/models/__init__.py @@ -1,2 +1,3 @@ from the_app.models.nonsense import Nonsense # noqa from the_app.models.stuff import Stuff # noqa +from the_app.models.shakespeare import Character # noqa diff --git a/the_app/models/shakespeare.py b/the_app/models/shakespeare.py index 3d76ff1..804c4bb 100644 --- a/the_app/models/shakespeare.py +++ b/the_app/models/shakespeare.py @@ -1,9 +1,9 @@ from sqlalchemy import (Column, ForeignKeyConstraint, Integer, PrimaryKeyConstraint, String, Table, Text, - UniqueConstraint) -from sqlalchemy.orm import declarative_base, relationship + UniqueConstraint, ForeignKey) +from sqlalchemy.orm import relationship -Base = declarative_base() +from the_app.models.base import Base metadata = Base.metadata From cbdae4da0608defd4641f1d6261b8c3a93840930 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sun, 24 Apr 2022 15:27:21 +0200 Subject: [PATCH 04/10] add schema create to sql script --- db/create.sql | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/db/create.sql b/db/create.sql index 2cd7a40..6d6618e 100644 --- a/db/create.sql +++ b/db/create.sql @@ -1,4 +1,9 @@ DROP DATABASE IF EXISTS devdb; CREATE DATABASE devdb; +\connect devdb; +CREATE SCHEMA shakespeare; + DROP DATABASE IF EXISTS testdb; CREATE DATABASE testdb; +\connect testdb; +CREATE SCHEMA shakespeare; From b2c388807f9e48a48cfce6a93a58e1668f8c0d36 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sun, 24 Apr 2022 18:48:00 +0200 Subject: [PATCH 05/10] add schema create to ci workflow --- .github/workflows/build-and-test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index adfe1b7..661cdc5 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -24,6 +24,8 @@ jobs: options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 steps: + - name: Create database schema + run: psql -h 127.0.0.1 -d testdb -U user -c '\connect testdb; CREATE SCHEMA shakespeare;' - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 From 3800c681ffa9f247342941ece6f33ea5dca67ba9 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sun, 24 Apr 2022 18:51:07 +0200 Subject: [PATCH 06/10] add schema create to ci workflow --- .github/workflows/build-and-test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 661cdc5..f257efd 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -42,3 +42,5 @@ jobs: SQL_HOST: 127.0.0.1 SQL_USER: user POSTGRES_PASSWORD: secret + PGPASSWORD: secret + From e76957c0e2047cce64e14937f645a999e11be6f8 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sun, 24 Apr 2022 18:55:38 +0200 Subject: [PATCH 07/10] add schema create to ci workflow --- .github/workflows/build-and-test.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index f257efd..14e7781 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -15,7 +15,7 @@ jobs: sqldb: image: postgres:13 env: - POSTGRES_USER: user + POSTGRES_USER: userdb POSTGRES_PASSWORD: secret POSTGRES_DB: testdb ports: @@ -25,7 +25,7 @@ jobs: steps: - name: Create database schema - run: psql -h 127.0.0.1 -d testdb -U user -c '\connect testdb; CREATE SCHEMA shakespeare;' + run: psql -h sqldb -d testdb -U userdb -c 'CREATE SCHEMA shakespeare;' - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 @@ -40,7 +40,8 @@ jobs: PYTHONUNBUFFERED: 1 SQL_DB: testdb SQL_HOST: 127.0.0.1 - SQL_USER: user - POSTGRES_PASSWORD: secret + SQL_USER: userdb +# POSTGRES_PASSWORD: secret PGPASSWORD: secret + From 0e3fc50600d1e8dd5d5b3f22cee23f996e245fc6 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sun, 24 Apr 2022 18:58:29 +0200 Subject: [PATCH 08/10] add schema create to ci workflow --- .github/workflows/build-and-test.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 14e7781..547843b 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -15,7 +15,7 @@ jobs: sqldb: image: postgres:13 env: - POSTGRES_USER: userdb + POSTGRES_USER: user POSTGRES_PASSWORD: secret POSTGRES_DB: testdb ports: @@ -25,7 +25,7 @@ jobs: steps: - name: Create database schema - run: psql -h sqldb -d testdb -U userdb -c 'CREATE SCHEMA shakespeare;' + run: PGPASSWORD=secret psql -h 127.0.0.1 -d testdb -U user -c '\connect testdb; CREATE SCHEMA shakespeare;' - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 @@ -40,8 +40,7 @@ jobs: PYTHONUNBUFFERED: 1 SQL_DB: testdb SQL_HOST: 127.0.0.1 - SQL_USER: userdb -# POSTGRES_PASSWORD: secret + SQL_USER: user + POSTGRES_PASSWORD: secret PGPASSWORD: secret - From 2485466db824a5d2727f9b1d9e0c0e31b416b32a Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sun, 24 Apr 2022 19:00:13 +0200 Subject: [PATCH 09/10] add schema create to ci workflow --- .github/workflows/build-and-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 547843b..89a9e4d 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -25,7 +25,7 @@ jobs: steps: - name: Create database schema - run: PGPASSWORD=secret psql -h 127.0.0.1 -d testdb -U user -c '\connect testdb; CREATE SCHEMA shakespeare;' + run: PGPASSWORD=secret psql -h 127.0.0.1:5432 -d testdb -U user -c "CREATE SCHEMA shakespeare;" - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2 From d70ce04294aef31cbc88a6ea44b30849bbff3f7c Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Sun, 24 Apr 2022 19:01:12 +0200 Subject: [PATCH 10/10] add schema create to ci workflow --- .github/workflows/build-and-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 89a9e4d..a446f09 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -25,7 +25,7 @@ jobs: steps: - name: Create database schema - run: PGPASSWORD=secret psql -h 127.0.0.1:5432 -d testdb -U user -c "CREATE SCHEMA shakespeare;" + run: PGPASSWORD=secret psql -h 127.0.0.1 -d testdb -U user -c "CREATE SCHEMA shakespeare;" - uses: actions/checkout@v2 - name: Set up Python uses: actions/setup-python@v2