mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
Merge pull request #44 from grillazz/onboard-alembic😉
add alembic migrations
This commit is contained in:
commit
a487c762d0
2
.github/workflows/build-and-test.yml
vendored
2
.github/workflows/build-and-test.yml
vendored
@ -25,7 +25,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Create database schema
|
- 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
|
- uses: actions/checkout@v2
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v2
|
uses: actions/setup-python@v2
|
||||||
|
@ -43,3 +43,6 @@ Next models were generated with https://github.com/agronholm/sqlacodegen
|
|||||||
And after some tweaking I got desired result
|
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
|
@ -5,7 +5,6 @@ import sys
|
|||||||
from alembic import context
|
from alembic import context
|
||||||
from sqlalchemy.ext.asyncio import create_async_engine
|
from sqlalchemy.ext.asyncio import create_async_engine
|
||||||
|
|
||||||
|
|
||||||
parent_dir = os.path.abspath(os.path.join(os.getcwd()))
|
parent_dir = os.path.abspath(os.path.join(os.getcwd()))
|
||||||
sys.path.append(parent_dir)
|
sys.path.append(parent_dir)
|
||||||
|
|
||||||
@ -14,27 +13,11 @@ from the_app.models.base import Base as app_base
|
|||||||
target_metadata = app_base.metadata
|
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):
|
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():
|
with context.begin_transaction():
|
||||||
context.run_migrations()
|
context.run_migrations()
|
||||||
@ -54,7 +37,4 @@ async def run_migrations_online():
|
|||||||
await connection.run_sync(do_run_migrations)
|
await connection.run_sync(do_run_migrations)
|
||||||
|
|
||||||
|
|
||||||
if context.is_offline_mode():
|
asyncio.run(run_migrations_online())
|
||||||
run_migrations_offline()
|
|
||||||
else:
|
|
||||||
asyncio.run(run_migrations_online())
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
"""init tables
|
"""initialize happy_cog and shakespeare tables
|
||||||
|
|
||||||
Revision ID: 421df2c2140f
|
Revision ID: 957232a5b7ee
|
||||||
Revises:
|
Revises:
|
||||||
Create Date: 2022-05-22 13:02:56.908424
|
Create Date: 2022-06-04 13:02:45.839649
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from alembic import op
|
from alembic import op
|
||||||
@ -10,7 +10,7 @@ import sqlalchemy as sa
|
|||||||
from sqlalchemy.dialects import postgresql
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
# revision identifiers, used by Alembic.
|
||||||
revision = '421df2c2140f'
|
revision = '957232a5b7ee'
|
||||||
down_revision = None
|
down_revision = None
|
||||||
branch_labels = None
|
branch_labels = None
|
||||||
depends_on = None
|
depends_on = None
|
||||||
@ -18,6 +18,24 @@ depends_on = None
|
|||||||
|
|
||||||
def upgrade():
|
def upgrade():
|
||||||
# ### commands auto generated by Alembic - please adjust! ###
|
# ### 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',
|
op.create_table('character',
|
||||||
sa.Column('id', sa.String(length=32), nullable=False),
|
sa.Column('id', sa.String(length=32), nullable=False),
|
||||||
sa.Column('name', sa.String(length=64), nullable=False),
|
sa.Column('name', sa.String(length=64), nullable=False),
|
||||||
@ -49,24 +67,6 @@ def upgrade():
|
|||||||
sa.PrimaryKeyConstraint('id', name='work_pkey'),
|
sa.PrimaryKeyConstraint('id', name='work_pkey'),
|
||||||
schema='shakespeare'
|
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',
|
op.create_table('chapter',
|
||||||
sa.Column('id', sa.Integer(), nullable=False),
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
sa.Column('work_id', sa.String(length=32), 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('paragraph', schema='shakespeare')
|
||||||
op.drop_table('character_work', schema='shakespeare')
|
op.drop_table('character_work', schema='shakespeare')
|
||||||
op.drop_table('chapter', 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('work', schema='shakespeare')
|
||||||
op.drop_table('wordform', schema='shakespeare')
|
op.drop_table('wordform', schema='shakespeare')
|
||||||
op.drop_table('character', 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 ###
|
# ### end Alembic commands ###
|
@ -1,11 +1,11 @@
|
|||||||
DROP DATABASE IF EXISTS devdb;
|
DROP DATABASE IF EXISTS devdb;
|
||||||
CREATE DATABASE devdb;
|
CREATE DATABASE devdb;
|
||||||
\connect devdb;
|
\connect devdb;
|
||||||
CREATE SCHEMA shakespeare_i_like;
|
CREATE SCHEMA shakespeare;
|
||||||
CREATE SCHEMA happy_hog;
|
CREATE SCHEMA happy_hog;
|
||||||
|
|
||||||
DROP DATABASE IF EXISTS testdb;
|
DROP DATABASE IF EXISTS testdb;
|
||||||
CREATE DATABASE testdb;
|
CREATE DATABASE testdb;
|
||||||
\connect testdb;
|
\connect testdb;
|
||||||
CREATE SCHEMA shakespeare_i_like;
|
CREATE SCHEMA shakespeare;
|
||||||
CREATE SCHEMA happy_hog;
|
CREATE SCHEMA happy_hog;
|
||||||
|
@ -8,22 +8,15 @@ from the_app.utils import get_logger
|
|||||||
|
|
||||||
logger = get_logger(__name__)
|
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(stuff_router)
|
||||||
app.include_router(nonsense_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")
|
@app.on_event("startup")
|
||||||
async def startup_event():
|
async def startup_event():
|
||||||
logger.info("Starting up...")
|
logger.info("Starting up...")
|
||||||
await start_db()
|
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("shutdown")
|
@app.on_event("shutdown")
|
||||||
|
@ -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
|
@ -18,7 +18,7 @@ metadata = Base.metadata
|
|||||||
|
|
||||||
class Character(Base):
|
class Character(Base):
|
||||||
__tablename__ = "character"
|
__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))
|
id = Column(String(32))
|
||||||
name = Column(String(64), nullable=False)
|
name = Column(String(64), nullable=False)
|
||||||
@ -32,7 +32,7 @@ class Character(Base):
|
|||||||
|
|
||||||
class Wordform(Base):
|
class Wordform(Base):
|
||||||
__tablename__ = "wordform"
|
__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)
|
id = Column(Integer)
|
||||||
plain_text = Column(String(64), nullable=False)
|
plain_text = Column(String(64), nullable=False)
|
||||||
@ -43,7 +43,7 @@ class Wordform(Base):
|
|||||||
|
|
||||||
class Work(Base):
|
class Work(Base):
|
||||||
__tablename__ = "work"
|
__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))
|
id = Column(String(32))
|
||||||
title = Column(String(32), nullable=False)
|
title = Column(String(32), nullable=False)
|
||||||
@ -68,7 +68,7 @@ class Chapter(Base):
|
|||||||
UniqueConstraint(
|
UniqueConstraint(
|
||||||
"work_id", "section_number", "chapter_number", name="chapter_work_id_section_number_chapter_number_key"
|
"work_id", "section_number", "chapter_number", name="chapter_work_id_section_number_chapter_number_key"
|
||||||
),
|
),
|
||||||
{"schema": "shakespeare_i_like"},
|
{"schema": "shakespeare"},
|
||||||
)
|
)
|
||||||
|
|
||||||
id = Column(Integer)
|
id = Column(Integer)
|
||||||
@ -104,7 +104,7 @@ class Paragraph(Base):
|
|||||||
),
|
),
|
||||||
ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="paragraph_work_id_fkey"),
|
ForeignKeyConstraint(["work_id"], ["shakespeare.work.id"], name="paragraph_work_id_fkey"),
|
||||||
PrimaryKeyConstraint("id", name="paragraph_pkey"),
|
PrimaryKeyConstraint("id", name="paragraph_pkey"),
|
||||||
{"schema": "shakespeare_i_like"},
|
{"schema": "shakespeare"},
|
||||||
)
|
)
|
||||||
|
|
||||||
id = Column(Integer)
|
id = Column(Integer)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user