63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
"""Add system_settings table for application-wide configuration flags.
|
|
|
|
Revision ID: 002_add_system_settings
|
|
Revises: 001_widen_ticket_history_values
|
|
Create Date: 2026-03-30
|
|
|
|
Rationale
|
|
---------
|
|
Introduces a generic key-value settings store so that runtime configuration
|
|
flags (such as registration_enabled) can be toggled by administrators through
|
|
the UI without requiring a code deployment or server restart.
|
|
|
|
Apply
|
|
-----
|
|
flask db upgrade
|
|
|
|
Rollback
|
|
--------
|
|
flask db downgrade
|
|
"""
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
revision = '002_add_system_settings'
|
|
down_revision = '001_widen_ticket_history_values'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade():
|
|
# Guard: db.create_all() on first startup already creates this table.
|
|
# Only create it if it doesn't exist so the migration is idempotent.
|
|
bind = op.get_bind()
|
|
inspector = sa.inspect(bind)
|
|
if 'system_settings' not in inspector.get_table_names():
|
|
op.create_table(
|
|
'system_settings',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('key', sa.String(100), nullable=False),
|
|
sa.Column('value', sa.String(500), nullable=False),
|
|
sa.Column('description', sa.String(256), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
)
|
|
op.create_index('ix_system_settings_key', 'system_settings', ['key'], unique=True)
|
|
op.execute(
|
|
"INSERT INTO system_settings (key, value, description, updated_at) "
|
|
"VALUES ('registration_enabled', 'true', "
|
|
"'Allow new users to self-register via /auth/register', NOW())"
|
|
)
|
|
# If the table already exists (created by db.create_all), ensure the
|
|
# index exists — create_all does not create named Alembic indexes.
|
|
else:
|
|
existing_indexes = [idx['name'] for idx in inspector.get_indexes('system_settings')]
|
|
if 'ix_system_settings_key' not in existing_indexes:
|
|
op.create_index('ix_system_settings_key', 'system_settings', ['key'], unique=True)
|
|
|
|
|
|
def downgrade():
|
|
op.drop_index('ix_system_settings_key', table_name='system_settings')
|
|
op.drop_table('system_settings')
|