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:
@@ -5,7 +5,7 @@
|
||||
# this is typically a path given in POSIX (e.g. forward slashes)
|
||||
# format, relative to the token %(here)s which refers to the location of this
|
||||
# ini file
|
||||
script_location = %(here)s/alembic
|
||||
script_location = %(here)s/slopserver/alembic
|
||||
|
||||
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
|
||||
# Uncomment the line below if you want the files to be prepended with date and time
|
||||
@@ -84,7 +84,7 @@ path_separator = os
|
||||
# database URL. This is consumed by the user-maintained env.py script only.
|
||||
# other means of configuring database URLs may be customized within the env.py
|
||||
# file.
|
||||
sqlalchemy.url = driver://user:pass@localhost/dbname
|
||||
sqlalchemy.url = postgresql+psycopg://slop-farmer@192.168.1.163/slop-farmer
|
||||
|
||||
|
||||
[post_write_hooks]
|
||||
|
||||
0
slopserver/__init__.py
Normal file
0
slopserver/__init__.py
Normal file
@@ -18,7 +18,8 @@ if config.config_file_name is not None:
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
# target_metadata = mymodel.Base.metadata
|
||||
target_metadata = None
|
||||
from slopserver.models import metadata
|
||||
target_metadata = metadata
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
@@ -65,7 +66,8 @@ def run_migrations_online() -> None:
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection, target_metadata=target_metadata
|
||||
connection=connection, target_metadata=target_metadata,
|
||||
user_module_prefix="sqlmodel.sql.sqltypes."
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
@@ -9,6 +9,7 @@ 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.
|
||||
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