feat(database): create all core tables with migrations
Add database migrations for mockupAWS v0.2.0: - DB-003: scenario_logs table * Stores received log entries with SHA256 hash for deduplication * PII detection flags * Metrics: size_bytes, token_count, sqs_blocks * Indexes on scenario_id, received_at, message_hash, has_pii - DB-004: scenario_metrics table * Time-series storage for metrics aggregation * Supports: sqs, lambda, bedrock, safety metric types * Flexible JSONB metadata field * BRIN index on timestamp for efficient queries - DB-005: aws_pricing table * Stores AWS service pricing by region * Supports price history with effective_from/to dates * Active pricing flag for current rates * Index on service, region, tier combination - DB-006: reports table * Generated report tracking * Supports PDF and CSV formats * File path and size tracking * Metadata JSONB for extensibility All tables include: - UUID primary keys with auto-generation - Foreign key constraints with CASCADE delete - Appropriate indexes for query performance - Check constraints for data validation Tasks: DB-003, DB-004, DB-005, DB-006 complete
This commit is contained in:
91
alembic/versions/e46de4b0264a_create_scenario_logs_table.py
Normal file
91
alembic/versions/e46de4b0264a_create_scenario_logs_table.py
Normal file
@@ -0,0 +1,91 @@
|
||||
"""create scenario_logs table
|
||||
|
||||
Revision ID: e46de4b0264a
|
||||
Revises: 8c29fdcbbf85
|
||||
Create Date: 2026-04-07 13:48:26.383709
|
||||
|
||||
"""
|
||||
|
||||
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 = "e46de4b0264a"
|
||||
down_revision: Union[str, Sequence[str], None] = "8c29fdcbbf85"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
op.create_table(
|
||||
"scenario_logs",
|
||||
sa.Column(
|
||||
"id",
|
||||
postgresql.UUID(as_uuid=True),
|
||||
primary_key=True,
|
||||
server_default=sa.text("uuid_generate_v4()"),
|
||||
),
|
||||
sa.Column(
|
||||
"scenario_id",
|
||||
postgresql.UUID(as_uuid=True),
|
||||
sa.ForeignKey("scenarios.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column(
|
||||
"received_at",
|
||||
sa.TIMESTAMP(timezone=True),
|
||||
server_default=sa.text("NOW()"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("message_hash", sa.String(64), nullable=False), # SHA256
|
||||
sa.Column("message_preview", sa.String(500), nullable=True),
|
||||
sa.Column("source", sa.String(100), server_default="unknown", nullable=False),
|
||||
sa.Column("size_bytes", sa.Integer(), server_default="0", nullable=False),
|
||||
sa.Column("has_pii", sa.Boolean(), server_default="false", nullable=False),
|
||||
sa.Column("token_count", sa.Integer(), server_default="0", nullable=False),
|
||||
sa.Column("sqs_blocks", sa.Integer(), server_default="1", nullable=False),
|
||||
)
|
||||
|
||||
# Add constraints
|
||||
op.create_check_constraint(
|
||||
"chk_size_positive", "scenario_logs", sa.column("size_bytes") >= 0
|
||||
)
|
||||
op.create_check_constraint(
|
||||
"chk_token_positive", "scenario_logs", sa.column("token_count") >= 0
|
||||
)
|
||||
op.create_check_constraint(
|
||||
"chk_blocks_positive", "scenario_logs", sa.column("sqs_blocks") >= 1
|
||||
)
|
||||
|
||||
# Add indexes
|
||||
op.create_index("idx_logs_scenario_id", "scenario_logs", ["scenario_id"])
|
||||
op.create_index(
|
||||
"idx_logs_received_at",
|
||||
"scenario_logs",
|
||||
["received_at"],
|
||||
postgresql_using="brin",
|
||||
)
|
||||
op.create_index("idx_logs_message_hash", "scenario_logs", ["message_hash"])
|
||||
op.create_index(
|
||||
"idx_logs_has_pii",
|
||||
"scenario_logs",
|
||||
["has_pii"],
|
||||
postgresql_where=sa.text("has_pii = true"),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# Drop indexes
|
||||
op.drop_index("idx_logs_has_pii", table_name="scenario_logs")
|
||||
op.drop_index("idx_logs_message_hash", table_name="scenario_logs")
|
||||
op.drop_index("idx_logs_received_at", table_name="scenario_logs")
|
||||
op.drop_index("idx_logs_scenario_id", table_name="scenario_logs")
|
||||
|
||||
# Drop table
|
||||
op.drop_table("scenario_logs")
|
||||
Reference in New Issue
Block a user