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
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

steps:
- name: Create database schema
run: PGPASSWORD=secret psql -h 127.0.0.1 -d testdb -U user -c "CREATE SCHEMA shakespeare_i_like; CREATE SCHEMA happy_hog;"
run: PGPASSWORD=secret psql -h 127.0.0.1 -d testdb -U user -c "CREATE SCHEMA shakespeare; CREATE SCHEMA happy_hog;"
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ Data set is coming form https://github.com/catherinedevlin/opensourceshakespeare
Next models were generated with https://github.com/agronholm/sqlacodegen
And after some tweaking I got desired result

Hope you enjoy it.
Hope you enjoy it.

### Change Log
- 4 JUN 2022 alembic migrations added to project
30 changes: 5 additions & 25 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from alembic import context
from sqlalchemy.ext.asyncio import create_async_engine


parent_dir = os.path.abspath(os.path.join(os.getcwd()))
sys.path.append(parent_dir)

Expand All @@ -14,27 +13,11 @@
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)
context.configure(connection=connection,
target_metadata=target_metadata,
include_schemas=True,
version_table_schema=target_metadata.schema)

with context.begin_transaction():
context.run_migrations()
Expand All @@ -54,7 +37,4 @@ async def run_migrations_online():
await connection.run_sync(do_run_migrations)


if context.is_offline_mode():
run_migrations_offline()
else:
asyncio.run(run_migrations_online())
asyncio.run(run_migrations_online())
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
"""init tables
"""initialize happy_cog and shakespeare tables

Revision ID: 421df2c2140f
Revision ID: 957232a5b7ee
Revises:
Create Date: 2022-05-22 13:02:56.908424
Create Date: 2022-06-04 13:02:45.839649

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '421df2c2140f'
revision = '957232a5b7ee'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('nonsense',
sa.Column('id', postgresql.UUID(as_uuid=True), autoincrement=True, nullable=True),
sa.Column('name', sa.String(), nullable=False),
sa.Column('description', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('name'),
sa.UniqueConstraint('id'),
sa.UniqueConstraint('name'),
schema='happy_hog'
)
op.create_table('stuff',
sa.Column('id', postgresql.UUID(as_uuid=True), autoincrement=True, nullable=True),
sa.Column('name', sa.String(), nullable=False),
sa.Column('description', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('name'),
sa.UniqueConstraint('id'),
sa.UniqueConstraint('name'),
schema='happy_hog'
)
op.create_table('character',
sa.Column('id', sa.String(length=32), nullable=False),
sa.Column('name', sa.String(length=64), nullable=False),
Expand Down Expand Up @@ -49,24 +67,6 @@ def upgrade():
sa.PrimaryKeyConstraint('id', name='work_pkey'),
schema='shakespeare'
)
op.create_table('nonsense',
sa.Column('id', postgresql.UUID(as_uuid=True), autoincrement=True, nullable=True),
sa.Column('name', sa.String(), nullable=False),
sa.Column('description', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('name'),
sa.UniqueConstraint('id'),
sa.UniqueConstraint('name'),
schema='the_others'
)
op.create_table('stuff',
sa.Column('id', postgresql.UUID(as_uuid=True), autoincrement=True, nullable=True),
sa.Column('name', sa.String(), nullable=False),
sa.Column('description', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('name'),
sa.UniqueConstraint('id'),
sa.UniqueConstraint('name'),
schema='the_others'
)
op.create_table('chapter',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('work_id', sa.String(length=32), nullable=False),
Expand Down Expand Up @@ -113,9 +113,9 @@ def downgrade():
op.drop_table('paragraph', schema='shakespeare')
op.drop_table('character_work', schema='shakespeare')
op.drop_table('chapter', schema='shakespeare')
op.drop_table('stuff', schema='the_others')
op.drop_table('nonsense', schema='the_others')
op.drop_table('work', schema='shakespeare')
op.drop_table('wordform', schema='shakespeare')
op.drop_table('character', schema='shakespeare')
op.drop_table('stuff', schema='happy_hog')
op.drop_table('nonsense', schema='happy_hog')
# ### end Alembic commands ###
4 changes: 2 additions & 2 deletions db/create.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
DROP DATABASE IF EXISTS devdb;
CREATE DATABASE devdb;
\connect devdb;
CREATE SCHEMA shakespeare_i_like;
CREATE SCHEMA shakespeare;
CREATE SCHEMA happy_hog;

DROP DATABASE IF EXISTS testdb;
CREATE DATABASE testdb;
\connect testdb;
CREATE SCHEMA shakespeare_i_like;
CREATE SCHEMA shakespeare;
CREATE SCHEMA happy_hog;
9 changes: 1 addition & 8 deletions the_app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,15 @@

logger = get_logger(__name__)

app = FastAPI(title="Stuff And Nonsense API", version="0.3")
app = FastAPI(title="Stuff And Nonsense API", version="0.4")

app.include_router(stuff_router)
app.include_router(nonsense_router)


async def start_db():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
await engine.dispose()


@app.on_event("startup")
async def startup_event():
logger.info("Starting up...")
await start_db()


@app.on_event("shutdown")
Expand Down
4 changes: 4 additions & 0 deletions the_app/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# for Alembic and unit tests
from the_app.models.stuff import * # noqa
from the_app.models.nonsense import * # noqa
from the_app.models.shakespeare import * # noqa
10 changes: 5 additions & 5 deletions the_app/models/shakespeare.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class Character(Base):
__tablename__ = "character"
__table_args__ = (PrimaryKeyConstraint("id", name="character_pkey"), {"schema": "shakespeare_i_like"})
__table_args__ = (PrimaryKeyConstraint("id", name="character_pkey"), {"schema": "shakespeare"})

id = Column(String(32))
name = Column(String(64), nullable=False)
Expand All @@ -32,7 +32,7 @@ class Character(Base):

class Wordform(Base):
__tablename__ = "wordform"
__table_args__ = (PrimaryKeyConstraint("id", name="wordform_pkey"), {"schema": "shakespeare_i_like"})
__table_args__ = (PrimaryKeyConstraint("id", name="wordform_pkey"), {"schema": "shakespeare"})

id = Column(Integer)
plain_text = Column(String(64), nullable=False)
Expand All @@ -43,7 +43,7 @@ class Wordform(Base):

class Work(Base):
__tablename__ = "work"
__table_args__ = (PrimaryKeyConstraint("id", name="work_pkey"), {"schema": "shakespeare_i_like"})
__table_args__ = (PrimaryKeyConstraint("id", name="work_pkey"), {"schema": "shakespeare"})

id = Column(String(32))
title = Column(String(32), nullable=False)
Expand All @@ -68,7 +68,7 @@ class Chapter(Base):
UniqueConstraint(
"work_id", "section_number", "chapter_number", name="chapter_work_id_section_number_chapter_number_key"
),
{"schema": "shakespeare_i_like"},
{"schema": "shakespeare"},
)

id = Column(Integer)
Expand Down Expand Up @@ -104,7 +104,7 @@ class Paragraph(Base):
),
ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="paragraph_work_id_fkey"),
PrimaryKeyConstraint("id", name="paragraph_pkey"),
{"schema": "shakespeare_i_like"},
{"schema": "shakespeare"},
)

id = Column(Integer)
Expand Down