initial alembic revision
followed article here for guidance: https://arunanshub.hashnode.dev/using-sqlmodel-with-alembic#heading-sql-constraint-naming-convention mirrored on my readeck: https://readeck.yuno.jack-case.pro/bookmarks/9ay79PHux348cgQBnA86UC
This commit is contained in:
0
slopserver/__init__.py
Normal file
0
slopserver/__init__.py
Normal file
1
slopserver/alembic/README
Normal file
1
slopserver/alembic/README
Normal file
@@ -0,0 +1 @@
|
||||
Generic single-database configuration.
|
||||
80
slopserver/alembic/env.py
Normal file
80
slopserver/alembic/env.py
Normal file
@@ -0,0 +1,80 @@
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
|
||||
from alembic import context
|
||||
|
||||
# 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
|
||||
from slopserver.models import metadata
|
||||
target_metadata = 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 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.
|
||||
|
||||
"""
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection, target_metadata=target_metadata,
|
||||
user_module_prefix="sqlmodel.sql.sqltypes."
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
29
slopserver/alembic/script.py.mako
Normal file
29
slopserver/alembic/script.py.mako
Normal file
@@ -0,0 +1,29 @@
|
||||
"""${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, Sequence[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:
|
||||
"""Upgrade schema."""
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
${downgrades if downgrades else "pass"}
|
||||
47
slopserver/alembic/versions/72dcd047d7bf_initial.py
Normal file
47
slopserver/alembic/versions/72dcd047d7bf_initial.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""initial
|
||||
|
||||
Revision ID: 72dcd047d7bf
|
||||
Revises:
|
||||
Create Date: 2025-10-17 20:10:04.230321
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
import sqlmodel.sql.sqltypes
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '72dcd047d7bf'
|
||||
down_revision: Union[str, Sequence[str], None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table('domain',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('domain_name', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('pk_domain'))
|
||||
)
|
||||
op.create_index(op.f('ix_domain_domain_name'), 'domain', ['domain_name'], unique=True)
|
||||
op.create_table('path',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('path', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
|
||||
sa.Column('domain_id', sa.Integer(), nullable=False),
|
||||
sa.ForeignKeyConstraint(['domain_id'], ['domain.id'], name=op.f('fk_path_domain_id_domain')),
|
||||
sa.PrimaryKeyConstraint('id', name=op.f('pk_path'))
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_table('path')
|
||||
op.drop_index(op.f('ix_domain_domain_name'), table_name='domain')
|
||||
op.drop_table('domain')
|
||||
# ### end Alembic commands ###
|
||||
25
slopserver/models.py
Normal file
25
slopserver/models.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from sqlmodel import Field, SQLModel, create_engine, Relationship
|
||||
|
||||
NAMING_CONVENTION = {
|
||||
"ix": "ix_%(column_0_label)s",
|
||||
"uq": "uq_%(table_name)s_%(column_0_name)s",
|
||||
"ck": "ck_%(table_name)s_%(constraint_name)s",
|
||||
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
|
||||
"pk": "pk_%(table_name)s",
|
||||
}
|
||||
|
||||
metadata = SQLModel.metadata
|
||||
metadata.naming_convention = NAMING_CONVENTION
|
||||
|
||||
class Domain(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
domain_name: str = Field(index=True, unique=True)
|
||||
|
||||
paths: list["Path"] = Relationship(back_populates="domain")
|
||||
|
||||
class Path(SQLModel, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
path: str
|
||||
|
||||
domain_id: int = Field(foreign_key="domain.id")
|
||||
domain: Domain = Relationship(back_populates="paths")
|
||||
Reference in New Issue
Block a user