Login
Basic migrations with alembic
Get items
This commit is contained in:
2023-02-19 02:47:14 +00:00
parent c807fbcf03
commit 6700a4e2ef
25 changed files with 616 additions and 9 deletions

1
alembic/README Normal file
View File

@@ -0,0 +1 @@
Generic single-database configuration.

82
alembic/env.py Normal file
View File

@@ -0,0 +1,82 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
from api.config import settings
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = None
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""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 = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
alembic_config = config.get_section(config.config_ini_section)
alembic_config['sqlalchemy.url'] = settings.sqlalchemy_database_url
connectable = engine_from_config(
alembic_config,
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

24
alembic/script.py.mako Normal file
View File

@@ -0,0 +1,24 @@
"""${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() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,31 @@
"""create items table
Revision ID: 0c96f5d57afe
Revises: e5467b78d2a0
Create Date: 2023-02-19 02:29:29.348094
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '0c96f5d57afe'
down_revision = 'e5467b78d2a0'
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table(
'items',
sa.Column("id", sa.UUID(as_uuid=True), primary_key=True),
sa.Column("title", sa.String(255), nullable=False, index=True),
sa.Column("description", sa.Text, nullable=True),
sa.Column("owner_id", sa.UUID(as_uuid=True), nullable=False)
)
op.create_foreign_key("fk_items_user", "items", "users", ["owner_id"], ["id"])
def downgrade() -> None:
op.drop_table('items')

View File

@@ -0,0 +1,31 @@
"""create users table
Revision ID: e5467b78d2a0
Revises:
Create Date: 2023-02-19 01:57:02.748963
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'e5467b78d2a0'
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table(
"users",
sa.Column("id", sa.UUID(as_uuid=True), primary_key=True),
sa.Column("email", sa.String(255), unique=True, nullable=False),
sa.Column("display_name", sa.String(255), nullable=False),
sa.Column("hashed_password", sa.String(255), nullable=False),
sa.Column("is_active", sa.Boolean, nullable=False, default=True)
)
def downgrade() -> None:
op.drop_table("users")