mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-30 11:26:24 +02:00
generate new migration / fix migration files
This commit is contained in:
parent
90bfec6e7d
commit
b2a19af1f7
12 changed files with 366 additions and 157 deletions
|
|
@ -8,8 +8,8 @@ from typing import Sequence, Union
|
|||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects.postgresql import JSON
|
||||
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy import inspect
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "12"
|
||||
|
|
@ -20,52 +20,72 @@ depends_on: Union[str, Sequence[str], None] = None
|
|||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema - add LogLevel and LogStatus enums and logs table."""
|
||||
|
||||
# Create LogLevel enum
|
||||
|
||||
# Create LogLevel enum if it doesn't exist
|
||||
op.execute("""
|
||||
CREATE TYPE loglevel AS ENUM ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'loglevel') THEN
|
||||
CREATE TYPE loglevel AS ENUM ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL');
|
||||
END IF;
|
||||
END$$;
|
||||
""")
|
||||
|
||||
# Create LogStatus enum
|
||||
|
||||
# Create LogStatus enum if it doesn't exist
|
||||
op.execute("""
|
||||
CREATE TYPE logstatus AS ENUM ('IN_PROGRESS', 'SUCCESS', 'FAILED')
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'logstatus') THEN
|
||||
CREATE TYPE logstatus AS ENUM ('IN_PROGRESS', 'SUCCESS', 'FAILED');
|
||||
END IF;
|
||||
END$$;
|
||||
""")
|
||||
|
||||
# Create logs table
|
||||
|
||||
# Create logs table if it doesn't exist
|
||||
op.execute("""
|
||||
CREATE TABLE logs (
|
||||
CREATE TABLE IF NOT EXISTS logs (
|
||||
id SERIAL PRIMARY KEY,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
level loglevel NOT NULL,
|
||||
status logstatus NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
source VARCHAR(200),
|
||||
log_metadata JSONB DEFAULT '{}',
|
||||
search_space_id INTEGER NOT NULL REFERENCES searchspaces(id) ON DELETE CASCADE
|
||||
)
|
||||
);
|
||||
""")
|
||||
|
||||
# Create indexes
|
||||
op.create_index(op.f('ix_logs_id'), 'logs', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_logs_created_at'), 'logs', ['created_at'], unique=False)
|
||||
op.create_index(op.f('ix_logs_level'), 'logs', ['level'], unique=False)
|
||||
op.create_index(op.f('ix_logs_status'), 'logs', ['status'], unique=False)
|
||||
op.create_index(op.f('ix_logs_source'), 'logs', ['source'], unique=False)
|
||||
|
||||
# Get existing indexes
|
||||
conn = op.get_bind()
|
||||
inspector = inspect(conn)
|
||||
existing_indexes = [idx['name'] for idx in inspector.get_indexes('logs')]
|
||||
|
||||
# Create indexes only if they don't already exist
|
||||
if 'ix_logs_id' not in existing_indexes:
|
||||
op.create_index('ix_logs_id', 'logs', ['id'])
|
||||
if 'ix_logs_created_at' not in existing_indexes:
|
||||
op.create_index('ix_logs_created_at', 'logs', ['created_at'])
|
||||
if 'ix_logs_level' not in existing_indexes:
|
||||
op.create_index('ix_logs_level', 'logs', ['level'])
|
||||
if 'ix_logs_status' not in existing_indexes:
|
||||
op.create_index('ix_logs_status', 'logs', ['status'])
|
||||
if 'ix_logs_source' not in existing_indexes:
|
||||
op.create_index('ix_logs_source', 'logs', ['source'])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema - remove logs table and enums."""
|
||||
|
||||
|
||||
# Drop indexes
|
||||
op.drop_index(op.f('ix_logs_source'), table_name='logs')
|
||||
op.drop_index(op.f('ix_logs_status'), table_name='logs')
|
||||
op.drop_index(op.f('ix_logs_level'), table_name='logs')
|
||||
op.drop_index(op.f('ix_logs_created_at'), table_name='logs')
|
||||
op.drop_index(op.f('ix_logs_id'), table_name='logs')
|
||||
|
||||
op.drop_index('ix_logs_source', table_name='logs')
|
||||
op.drop_index('ix_logs_status', table_name='logs')
|
||||
op.drop_index('ix_logs_level', table_name='logs')
|
||||
op.drop_index('ix_logs_created_at', table_name='logs')
|
||||
op.drop_index('ix_logs_id', table_name='logs')
|
||||
|
||||
# Drop logs table
|
||||
op.drop_table('logs')
|
||||
|
||||
|
||||
# Drop enums
|
||||
op.execute("DROP TYPE IF EXISTS logstatus")
|
||||
op.execute("DROP TYPE IF EXISTS loglevel")
|
||||
op.execute("DROP TYPE IF EXISTS loglevel")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue