diff --git a/README.md b/README.md index f54845f..9f6b602 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file +Hope you enjoy it. + +### Change Log +- 4 JUN 2022 alembic migrations added to project \ No newline at end of file diff --git a/alembic/env.py b/alembic/env.py index 47c2b0f..afe8edb 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -5,7 +5,6 @@ import sys 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) @@ -14,27 +13,11 @@ 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) + context.configure(connection=connection, + target_metadata=target_metadata, + include_schemas=True, + version_table_schema=target_metadata.schema) with context.begin_transaction(): context.run_migrations() @@ -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()) diff --git a/alembic/versions/421df2c2140f_init_tables.py b/alembic/versions/957232a5b7ee_initialize_happy_cog_and_shakespeare_.py similarity index 95% rename from alembic/versions/421df2c2140f_init_tables.py rename to alembic/versions/957232a5b7ee_initialize_happy_cog_and_shakespeare_.py index 4ed79e6..89dbb79 100644 --- a/alembic/versions/421df2c2140f_init_tables.py +++ b/alembic/versions/957232a5b7ee_initialize_happy_cog_and_shakespeare_.py @@ -1,8 +1,8 @@ -"""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 @@ -10,7 +10,7 @@ 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 @@ -18,6 +18,24 @@ 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), @@ -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), @@ -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 ### diff --git a/db/create.sql b/db/create.sql index 6e4f650..95421a5 100644 --- a/db/create.sql +++ b/db/create.sql @@ -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; diff --git a/the_app/main.py b/the_app/main.py index 6d69815..f2e8346 100644 --- a/the_app/main.py +++ b/the_app/main.py @@ -8,22 +8,15 @@ from the_app.utils import get_logger 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") diff --git a/the_app/models/__init__.py b/the_app/models/__init__.py index e69de29..578857f 100644 --- a/the_app/models/__init__.py +++ b/the_app/models/__init__.py @@ -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 \ No newline at end of file diff --git a/the_app/models/shakespeare.py b/the_app/models/shakespeare.py index fb4e3cd..e554f36 100644 --- a/the_app/models/shakespeare.py +++ b/the_app/models/shakespeare.py @@ -18,7 +18,7 @@ metadata = Base.metadata 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) @@ -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) @@ -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) @@ -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) @@ -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)