107 lines
5.6 KiB
Python
107 lines
5.6 KiB
Python
"""Init
|
|
|
|
Revision ID: 34ec41e7908b
|
|
Revises:
|
|
Create Date: 2026-04-21 18:48:24.829721
|
|
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = '34ec41e7908b'
|
|
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."""
|
|
op.create_table('users',
|
|
sa.Column('id', sa.Uuid(), nullable=False),
|
|
sa.Column('username', sa.String(), nullable=False),
|
|
sa.Column('full_name', sa.String(), nullable=False),
|
|
sa.Column('email', sa.String(), nullable=False),
|
|
sa.Column('type', sa.String(), nullable=False),
|
|
sa.Column('is_archived', sa.Boolean(), nullable=False),
|
|
sa.Column('hashed_password', sa.String(), nullable=False),
|
|
sa.PrimaryKeyConstraint('id', name=op.f('pk_users'))
|
|
)
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
op.execute("""CREATE FUNCTION "public_users_audit_insert"() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
INSERT INTO "users_audit" ("audit_operation", "audit_timestamp", "audit_current_user", "id", "username", "full_name", "email", "type", "is_archived", "hashed_password")
|
|
SELECT 'I', now(), current_user, NEW."id", NEW."username", NEW."full_name", NEW."email", NEW."type", NEW."is_archived", NEW."hashed_password";
|
|
RETURN NULL;
|
|
END
|
|
$$;""")
|
|
op.execute("""CREATE FUNCTION "public_users_audit_update"() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
INSERT INTO "users_audit" ("audit_operation", "audit_timestamp", "audit_current_user", "id", "username", "full_name", "email", "type", "is_archived", "hashed_password")
|
|
SELECT 'U', now(), current_user, NEW."id", NEW."username", NEW."full_name", NEW."email", NEW."type", NEW."is_archived", NEW."hashed_password";
|
|
RETURN NULL;
|
|
END
|
|
$$;""")
|
|
op.execute("""CREATE FUNCTION "public_users_audit_delete"() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
INSERT INTO "users_audit" ("audit_operation", "audit_timestamp", "audit_current_user", "id", "username", "full_name", "email", "type", "is_archived", "hashed_password")
|
|
SELECT 'D', now(), current_user, OLD."id", OLD."username", OLD."full_name", OLD."email", OLD."type", OLD."is_archived", OLD."hashed_password";
|
|
RETURN NULL;
|
|
END
|
|
$$;""")
|
|
op.execute("""CREATE TRIGGER "public_users_audit_insert" AFTER INSERT ON "users" FOR EACH ROW EXECUTE PROCEDURE "public_users_audit_insert"();""")
|
|
op.execute("""CREATE TRIGGER "public_users_audit_update" AFTER UPDATE ON "users" FOR EACH ROW EXECUTE PROCEDURE "public_users_audit_update"();""")
|
|
op.execute("""CREATE TRIGGER "public_users_audit_delete" AFTER DELETE ON "users" FOR EACH ROW EXECUTE PROCEDURE "public_users_audit_delete"();""")
|
|
op.create_table('api_log',
|
|
sa.Column('id', sa.Uuid(), nullable=False),
|
|
sa.Column('api_key', sa.Uuid(), nullable=True),
|
|
sa.Column('ip_address', postgresql.INET(), nullable=False),
|
|
sa.Column('path', sa.String(), nullable=False),
|
|
sa.Column('method', sa.String(), nullable=False),
|
|
sa.Column('status_code', sa.Integer(), nullable=False),
|
|
sa.Column('request_body', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
|
|
sa.Column('response_body', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
|
|
sa.Column('query_params', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
|
|
sa.Column('path_params', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
|
|
sa.Column('process_time', sa.Float(), nullable=False),
|
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
|
sa.PrimaryKeyConstraint('id', name=op.f('pk_api_log'))
|
|
)
|
|
op.create_index('ix_unique_active_email', 'users', ['email'], unique=True, postgresql_where='is_archived = false')
|
|
op.create_index('ix_unique_active_username', 'users', ['username'], unique=True, postgresql_where='is_archived = false')
|
|
op.create_table('users_audit',
|
|
sa.Column('audit_pk', sa.Integer(), nullable=False),
|
|
sa.Column('audit_operation', sa.Unicode(length=1), nullable=False),
|
|
sa.Column('audit_timestamp', sa.DateTime(timezone=True), nullable=False),
|
|
sa.Column('audit_current_user', sa.Unicode(length=64), nullable=False),
|
|
sa.Column('id', sa.Uuid(), nullable=True),
|
|
sa.Column('username', sa.String(), nullable=True),
|
|
sa.Column('full_name', sa.String(), nullable=True),
|
|
sa.Column('email', sa.String(), nullable=True),
|
|
sa.Column('type', sa.String(), nullable=True),
|
|
sa.Column('is_archived', sa.Boolean(), nullable=True),
|
|
sa.Column('hashed_password', sa.String(), nullable=True),
|
|
sa.PrimaryKeyConstraint('audit_pk', name=op.f('pk_users_audit'))
|
|
)
|
|
# ### end Alembic commands ###
|
|
|
|
|
|
def downgrade() -> None:
|
|
"""Downgrade schema."""
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
op.drop_table('users_audit')
|
|
op.drop_index('ix_unique_active_username', table_name='users', postgresql_where='is_archived = false')
|
|
op.drop_index('ix_unique_active_email', table_name='users', postgresql_where='is_archived = false')
|
|
op.drop_table('users')
|
|
op.drop_table('api_log')
|
|
op.execute("""DROP TRIGGER "public_users_audit_delete" ON "users";""")
|
|
op.execute("""DROP TRIGGER "public_users_audit_update" ON "users";""")
|
|
op.execute("""DROP TRIGGER "public_users_audit_insert" ON "users";""")
|
|
op.execute("""DROP FUNCTION public_users_audit_delete();""")
|
|
op.execute("""DROP FUNCTION public_users_audit_update();""")
|
|
op.execute("""DROP FUNCTION public_users_audit_insert();""")
|
|
# ### end Alembic commands ###
|