56 lines
3.0 KiB
Python
56 lines
3.0 KiB
Python
"""journal search and rotation
|
|||
|
|
|
||
|
|
Revision ID: 0003
|
||
|
|
Revises: 0002
|
||
|
|
"""
|
||
|
|
from alembic import op
|
||
|
|
import sqlalchemy as sa
|
||
|
|
from sqlalchemy.dialects import postgresql
|
||
|
|
|
||
|
|
revision = '0003'
|
||
|
|
down_revision = '0002'
|
||
|
|
branch_labels = None
|
||
|
|
depends_on = None
|
||
|
|
|
||
|
|
|
||
|
|
def upgrade() -> None:
|
||
|
|
op.create_table('app_settings',
|
||
|
|
sa.Column('key', sa.String(length=50), nullable=False),
|
||
|
|
sa.Column('value', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
|
||
|
|
sa.PrimaryKeyConstraint('key')
|
||
|
|
)
|
||
|
|
op.create_table('clear_attempts',
|
||
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('user_id', sa.Integer(), nullable=False),
|
||
|
|
sa.Column('ts', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
|
||
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
||
|
|
sa.PrimaryKeyConstraint('id')
|
||
|
|
)
|
||
|
|
op.create_index(op.f('ix_clear_attempts_user_id'), 'clear_attempts', ['user_id'], unique=False)
|
||
|
|
op.add_column('audit_log', sa.Column('uid', sa.UUID(), server_default=sa.text('gen_random_uuid()'), nullable=False))
|
||
|
|
op.add_column('audit_log', sa.Column('message', sa.Text(), server_default='', nullable=False))
|
||
|
|
op.create_index('ix_audit_log_entity_type_action', 'audit_log', ['entity_type', 'action'], unique=False)
|
||
|
|
op.create_unique_constraint("audit_log_uid_key", "audit_log", ["uid"])
|
||
|
|
|
||
|
|
# бэкфилл человекочитаемого сообщения для существующих записей (с согласованием рода глагола)
|
||
|
|
nouns = {"organization": ("Организация", "f"), "vrf": ("VRF", "m"), "prefix": ("Префикс", "m"), "address": ("Адрес", "m"),
|
||
|
|
"device": ("Устройство", "n"), "device_type": ("Тип устройства", "m"), "isp": ("Оператор", "m")}
|
||
|
|
verbs = {"created": ("создан", "создана", "создано"), "updated": ("изменён", "изменена", "изменено"),
|
||
|
|
"deleted": ("удалён", "удалена", "удалено"), "assigned": ("назначен", "назначена", "назначено")}
|
||
|
|
for entity, (noun, gender) in nouns.items():
|
||
|
|
for action, forms in verbs.items():
|
||
|
|
verb = forms[{"m": 0, "f": 1, "n": 2}[gender]]
|
||
|
|
op.get_bind().execute(sa.text(
|
||
|
|
"UPDATE audit_log SET message = :noun || ' ' || entity_label || ' ' || :verb WHERE entity_type = :e AND action = :a"),
|
||
|
|
{"noun": noun, "verb": verb, "e": entity, "a": action})
|
||
|
|
op.execute("""INSERT INTO app_settings (key, value) VALUES ('journal', '{"retention_days": 90, "max_entries": 100000}')""")
|
||
|
|
|
||
|
|
|
||
|
|
def downgrade() -> None:
|
||
|
|
op.drop_constraint("audit_log_uid_key", "audit_log", type_="unique")
|
||
|
|
op.drop_index('ix_audit_log_entity_type_action', table_name='audit_log')
|
||
|
|
op.drop_column('audit_log', 'message')
|
||
|
|
op.drop_column('audit_log', 'uid')
|
||
|
|
op.drop_index(op.f('ix_clear_attempts_user_id'), table_name='clear_attempts')
|
||
|
|
op.drop_table('clear_attempts')
|
||
|
|
op.drop_table('app_settings')
|