Jack Case
2025-10-17 20:13:02 +00:00
parent d6ca3e35ed
commit c238551467
7 changed files with 79 additions and 4 deletions

View File

@@ -5,7 +5,7 @@
# this is typically a path given in POSIX (e.g. forward slashes) # 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 # format, relative to the token %(here)s which refers to the location of this
# ini file # 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 # 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 # 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. # 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 # other means of configuring database URLs may be customized within the env.py
# file. # file.
sqlalchemy.url = driver://user:pass@localhost/dbname sqlalchemy.url = postgresql+psycopg://slop-farmer@192.168.1.163/slop-farmer
[post_write_hooks] [post_write_hooks]

0
slopserver/__init__.py Normal file
View File

View File

@@ -18,7 +18,8 @@ if config.config_file_name is not None:
# for 'autogenerate' support # for 'autogenerate' support
# from myapp import mymodel # from myapp import mymodel
# target_metadata = mymodel.Base.metadata # 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, # other values from the config, defined by the needs of env.py,
# can be acquired: # can be acquired:
@@ -65,7 +66,8 @@ def run_migrations_online() -> None:
with connectable.connect() as connection: with connectable.connect() as connection:
context.configure( context.configure(
connection=connection, target_metadata=target_metadata connection=connection, target_metadata=target_metadata,
user_module_prefix="sqlmodel.sql.sqltypes."
) )
with context.begin_transaction(): with context.begin_transaction():

View File

@@ -9,6 +9,7 @@ from typing import Sequence, Union
from alembic import op from alembic import op
import sqlalchemy as sa import sqlalchemy as sa
import sqlmodel.sql.sqltypes
${imports if imports else ""} ${imports if imports else ""}
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.

View 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
View 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")