schema changes, part #1

This commit is contained in:
Yusur 2025-10-10 17:42:10 +02:00
parent b5112c6565
commit 7a60f50b3a
8 changed files with 311 additions and 95 deletions

1
alembic/README Normal file
View file

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

79
alembic/env.py Normal file
View file

@ -0,0 +1,79 @@
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 salvi.models import Base
target_metadata = Base.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
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

26
alembic/script.py.mako Normal file
View file

@ -0,0 +1,26 @@
"""${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
${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"}

View file

@ -0,0 +1,86 @@
"""empty message
Sorry, due to unattended changes, upgrade from 1.0.0 is not possible. Sorry.
Revision ID: ae0587e14725
Revises:
Create Date: 2025-10-04 09:43:41.158057
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
# revision identifiers, used by Alembic.
revision: str = 'ae0587e14725'
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_index('pagepolicykey_passphrase_sec_code', table_name='pagepolicykey')
#op.drop_index('page_owner', table_name='page')
op.create_index(op.f('ix_page_calendar'), 'page', ['calendar'], unique=False)
op.create_index('page_calendar', 'page', ['calendar'], unique=False)
op.drop_index('user_id', table_name='usergroupmembership')
op.drop_index('user_id_2', table_name='usergroupmembership')
#op.drop_index('usergroupmembership_group_id', table_name='usergroupmembership')
#op.drop_index('usergroupmembership_user_id', table_name='usergroupmembership')
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_index('usergroupmembership_user_id', 'usergroupmembership', ['user_id'], unique=False)
op.create_index('usergroupmembership_group_id', 'usergroupmembership', ['group_id'], unique=False)
op.create_index('user_id_2', 'usergroupmembership', ['user_id', 'group_id'], unique=True)
op.create_index('user_id', 'usergroupmembership', ['user_id', 'group_id'], unique=True)
op.drop_index('page_calendar', table_name='page')
op.drop_index(op.f('ix_page_calendar'), table_name='page')
op.create_index('page_owner', 'page', ['owner_id'], unique=False)
op.create_table('upload',
sa.Column('id', mysql.INTEGER(display_width=11), autoincrement=True, nullable=False),
sa.Column('name', mysql.VARCHAR(length=256), nullable=False),
sa.Column('url_name', mysql.VARCHAR(length=256), nullable=True),
sa.Column('filetype', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False),
sa.Column('filesize', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False),
sa.Column('upload_date', mysql.DATETIME(), nullable=False),
sa.Column('md5', mysql.VARCHAR(length=32), nullable=False),
sa.PrimaryKeyConstraint('id'),
mariadb_collate='utf8mb4_general_ci',
mariadb_default_charset='utf8mb4',
mariadb_engine='InnoDB'
)
op.create_index('upload_upload_date', 'upload', ['upload_date'], unique=False)
op.create_index('upload_md5', 'upload', ['md5'], unique=False)
op.create_table('pagepolicy',
sa.Column('id', mysql.INTEGER(display_width=11), autoincrement=True, nullable=False),
sa.Column('page_id', mysql.INTEGER(display_width=11), autoincrement=False, nullable=True),
sa.Column('type', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False),
sa.Column('key_id', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False),
sa.Column('sitewide', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False),
sa.ForeignKeyConstraint(['key_id'], ['pagepolicykey.id'], name='pagepolicy_FK_0_0'),
sa.ForeignKeyConstraint(['page_id'], ['page.id'], name='pagepolicy_FK_1_0'),
sa.PrimaryKeyConstraint('id'),
mariadb_collate='utf8mb4_general_ci',
mariadb_default_charset='utf8mb4',
mariadb_engine='InnoDB'
)
op.create_index('pagepolicy_page_id_key_id', 'pagepolicy', ['page_id', 'key_id'], unique=True)
op.create_index('pagepolicy_page_id', 'pagepolicy', ['page_id'], unique=False)
op.create_index('pagepolicy_key_id', 'pagepolicy', ['key_id'], unique=False)
op.create_table('pagepolicykey',
sa.Column('id', mysql.INTEGER(display_width=11), autoincrement=True, nullable=False),
sa.Column('passphrase', mysql.VARCHAR(length=255), nullable=False),
sa.Column('sec_code', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False),
sa.PrimaryKeyConstraint('id'),
mariadb_collate='utf8mb4_general_ci',
mariadb_default_charset='utf8mb4',
mariadb_engine='InnoDB'
)
op.create_index('pagepolicykey_passphrase_sec_code', 'pagepolicykey', ['passphrase', 'sec_code'], unique=True)
# ### end Alembic commands ###