event database migration

This commit is contained in:
2025-06-08 12:47:39 +00:00
parent e3c9d1b19b
commit ea20905466
8 changed files with 444 additions and 10 deletions

1
server/alembic/README Normal file
View File

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

104
server/alembic/env.py Normal file
View File

@@ -0,0 +1,104 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
import sys
sys.path.append('/workspace')
import os
from dotenv import load_dotenv
from server.models import Base
# .env laden (optional)
env_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../.env'))
print(f"Loading environment variables from: {env_path}")
load_dotenv(env_path)
# Datenbank-Zugangsdaten aus .env
DB_USER = os.getenv("DB_USER")
DB_PASSWORD = os.getenv("DB_PASSWORD")
DB_HOST = os.getenv("DB_HOST", "localhost")
DB_PORT = os.getenv("DB_PORT", "3306")
DB_NAME = os.getenv("DB_NAME")
# 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)
DATABASE_URL = (
f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
)
print(f"Using DATABASE_URL: {DATABASE_URL}")
config.set_main_option("sqlalchemy.url", DATABASE_URL)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
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()

View File

@@ -0,0 +1,28 @@
"""${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:
"""Upgrade schema."""
${upgrades if upgrades else "pass"}
def downgrade() -> None:
"""Downgrade schema."""
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,88 @@
"""event_db
Revision ID: c1178d5fa549
Revises: f7dd3165f238
Create Date: 2025-06-08 12:29:28.366231
"""
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 = 'c1178d5fa549'
down_revision: Union[str, None] = 'f7dd3165f238'
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('events',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('client_uuid', sa.String(length=36), nullable=False),
sa.Column('title', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('start', sa.TIMESTAMP(timezone=True), nullable=False),
sa.Column('end', sa.TIMESTAMP(timezone=True), nullable=False),
sa.Column('event_type', sa.Enum('presentation', 'website', 'video', 'message', 'other', 'webuntis', name='eventtype'), nullable=False),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=True),
sa.Column('updated_at', sa.TIMESTAMP(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=True),
sa.Column('created_by', sa.Integer(), nullable=False),
sa.Column('updated_by', sa.Integer(), nullable=True),
sa.Column('is_active', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['client_uuid'], ['clients.uuid'], ),
sa.ForeignKeyConstraint(['created_by'], ['users.id'], ),
sa.ForeignKeyConstraint(['updated_by'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_events_client_uuid'), 'events', ['client_uuid'], unique=False)
op.create_index(op.f('ix_events_end'), 'events', ['end'], unique=False)
op.create_index(op.f('ix_events_start'), 'events', ['start'], unique=False)
op.create_table('event_media',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('event_id', sa.Integer(), nullable=False),
sa.Column('media_type', sa.Enum('presentation', 'website', 'video', 'message', 'other', 'webuntis', name='eventtype'), nullable=False),
sa.Column('url', sa.String(length=255), nullable=False),
sa.Column('order', sa.Integer(), nullable=True),
sa.Column('autoplay', sa.Boolean(), nullable=True),
sa.Column('loop', sa.Boolean(), nullable=True),
sa.Column('volume', sa.Float(), nullable=True),
sa.ForeignKeyConstraint(['event_id'], ['events.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.add_column('clients', sa.Column('is_active', sa.Boolean(), nullable=False))
op.create_index(op.f('ix_clients_hardware_hash'), 'clients', ['hardware_hash'], unique=False)
op.create_index(op.f('ix_clients_ip_address'), 'clients', ['ip_address'], unique=False)
op.add_column('users', sa.Column('is_active', sa.Boolean(), nullable=False))
op.alter_column('users', 'password_hash',
existing_type=mysql.VARCHAR(length=60),
type_=sa.String(length=128),
existing_nullable=False)
op.drop_index(op.f('username'), table_name='users')
op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_users_username'), table_name='users')
op.create_index(op.f('username'), 'users', ['username'], unique=True)
op.alter_column('users', 'password_hash',
existing_type=sa.String(length=128),
type_=mysql.VARCHAR(length=60),
existing_nullable=False)
op.drop_column('users', 'is_active')
op.drop_index(op.f('ix_clients_ip_address'), table_name='clients')
op.drop_index(op.f('ix_clients_hardware_hash'), table_name='clients')
op.drop_column('clients', 'is_active')
op.drop_table('event_media')
op.drop_index(op.f('ix_events_start'), table_name='events')
op.drop_index(op.f('ix_events_end'), table_name='events')
op.drop_index(op.f('ix_events_client_uuid'), table_name='events')
op.drop_table('events')
# ### end Alembic commands ###

View File

@@ -0,0 +1,32 @@
"""initial
Revision ID: f7dd3165f238
Revises:
Create Date: 2025-06-08 12:25:27.174339
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'f7dd3165f238'
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:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###