Add Alembic to project for database migrations
This commit is contained in:
parent
7755bfb46d
commit
7ce43a2bfe
15 changed files with 256 additions and 49 deletions
63
migrations/env.py
Normal file
63
migrations/env.py
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
from logging.config import fileConfig
|
||||
|
||||
from alembic import context
|
||||
|
||||
from tofu_api.app import create_app
|
||||
from tofu_api.models import BaseModel
|
||||
|
||||
# This is the Alembic Config object, which provides access to the values within the .ini file in use.
|
||||
alembic_config = context.config
|
||||
|
||||
# Interpret the config file for Python logging. This line sets up loggers basically.
|
||||
if alembic_config.config_file_name is not None:
|
||||
fileConfig(alembic_config.config_file_name)
|
||||
|
||||
# Create Flask app, which loads the app config and initializes the database engine.
|
||||
app = create_app()
|
||||
db = app.dependencies.get_sqlalchemy()
|
||||
|
||||
|
||||
def process_revision_directives(_context, _revision, directives):
|
||||
"""
|
||||
Callback used to prevent generating empty migrations with autogenerate.
|
||||
Source: https://alembic.sqlalchemy.org/en/latest/cookbook.html#don-t-generate-empty-migrations-with-autogenerate
|
||||
"""
|
||||
if alembic_config.cmd_opts.autogenerate and directives[0].upgrade_ops.is_empty():
|
||||
directives[:] = []
|
||||
|
||||
|
||||
def run_migrations_offline():
|
||||
"""
|
||||
Run migrations in 'offline' mode, which does not require an actual database engine and can be used to generate SQL scripts.
|
||||
"""
|
||||
context.configure(
|
||||
url=app.config.sqlalchemy_database_uri,
|
||||
target_metadata=BaseModel.metadata,
|
||||
process_revision_directives=process_revision_directives,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online():
|
||||
"""
|
||||
Run migrations in 'online' mode, which requires a database engine.
|
||||
"""
|
||||
with db.engine.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=BaseModel.metadata,
|
||||
process_revision_directives=process_revision_directives,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
25
migrations/script.py.mako
Normal file
25
migrations/script.py.mako
Normal file
|
|
@ -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
|
||||
${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"}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
"""
|
||||
Initial revision
|
||||
|
||||
Revision ID: 716726b4a1fe
|
||||
Revises:
|
||||
Create Date: 2022-04-15 16:05:29.358429+02:00
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# Revision identifiers, used by Alembic.
|
||||
revision = '716726b4a1fe'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table(
|
||||
'tasks',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('title', sa.String(length=255), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('pk_tasks'))
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('tasks')
|
||||
# ### end Alembic commands ###
|
||||
Loading…
Add table
Add a link
Reference in a new issue