Add alembic migrations

This commit is contained in:
Jacob Windsor
2025-02-19 15:55:16 +01:00
parent ac3727065f
commit 0d1e53acdb
10 changed files with 300 additions and 3 deletions
+1
View File
@@ -0,0 +1 @@
Generic single-database configuration.
+77
View File
@@ -0,0 +1,77 @@
from logging.config import fileConfig
from sqlalchemy.engine import Connection
from sqlmodel import SQLModel
from app.providers.db_provider import get_connection
from alembic import context
import asyncio
# 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 = SQLModel.metadata
# 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 do_run_migrations(connection: Connection) -> None:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_async_migrations() -> None:
async with get_connection() as connection:
await connection.run_sync(do_run_migrations)
def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
asyncio.run(run_async_migrations())
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+27
View File
@@ -0,0 +1,27 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
import sqlmodel.sql.sqltypes
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}
@@ -0,0 +1,49 @@
"""
Revision ID: bbee168ba406
Revises:
Create Date: 2025-02-19 15:48:47.075283
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
import sqlmodel.sql.sqltypes
# revision identifiers, used by Alembic.
revision: str = 'bbee168ba406'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('house')
op.drop_table('owner')
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('owner',
sa.Column('id', sa.UUID(), autoincrement=False, nullable=False),
sa.Column('user_id', sa.UUID(), autoincrement=False, nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['user.id'], name='owner_user_id_fkey'),
sa.PrimaryKeyConstraint('id', name='owner_pkey'),
postgresql_ignore_search_path=False
)
op.create_table('house',
sa.Column('id', sa.UUID(), autoincrement=False, nullable=False),
sa.Column('address', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.Column('city', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.Column('country', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.Column('price', sa.DOUBLE_PRECISION(precision=53), autoincrement=False, nullable=False),
sa.Column('description', sa.VARCHAR(), autoincrement=False, nullable=False),
sa.Column('owner_id', sa.UUID(), autoincrement=False, nullable=False),
sa.ForeignKeyConstraint(['owner_id'], ['owner.id'], name='house_owner_id_fkey'),
sa.PrimaryKeyConstraint('id', name='house_pkey')
)
# ### end Alembic commands ###