feat: 添加数据库迁移脚本并更新Dockerfile配置

- 在Dockerfile中添加alembic配置文件和目录的复制指令
- 更新alembic/env.py注册新的模块模型:events、workflows、writebacks
- 生成完整的初始数据库schema迁移脚本,包含以下表:
  - approval_requests, attendance_records, audit_logs, domain_events
  - expenses, feishu_event_receipts, fund_accounts, legacy_sync_runs
  - official_writeback_runs, performance_metrics, policies, procurements
  - projects, report_push_runs, risk_event_actions, risk_events
  - standards, suppliers, work_reports, work_tasks, workflow_actions
  - workflow_instances等21个数据表结构定义
- 在API路由器中添加新模块的路由:events、workflows、writebacks、observability
```
This commit is contained in:
2026-07-08 14:08:03 +08:00
parent 92f490b97e
commit 19e59e83cc
59 changed files with 3271 additions and 247 deletions

View File

@@ -6,29 +6,550 @@ Create Date: 2026-07-06
"""
from alembic import op
import sqlalchemy as sa
from app.core.db_base import Base
from app.modules.approvals import models as approval_models
from app.modules.audit import models as audit_models
from app.modules.business import models as business_models
from app.modules.feishu import models as feishu_models
revision = "202607060001"
down_revision = None
branch_labels = None
depends_on = None
# Keep imports referenced so SQLAlchemy model classes register with Base.metadata.
_REGISTERED_MODEL_MODULES = (
approval_models,
audit_models,
business_models,
feishu_models,
)
def upgrade() -> None:
Base.metadata.create_all(bind=op.get_bind())
op.create_table(
"approval_requests",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("ticket_id", sa.String(length=64), nullable=False),
sa.Column("domain", sa.String(length=128), nullable=False),
sa.Column("record_id", sa.String(length=128), nullable=True),
sa.Column("action", sa.String(length=128), nullable=False),
sa.Column("applicant", sa.String(length=128), nullable=False),
sa.Column("approver", sa.String(length=128), nullable=True),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("reason", sa.Text(), nullable=True),
sa.Column("payload", sa.Text(), nullable=True),
sa.Column("decision_comment", sa.Text(), nullable=True),
sa.Column("used_by", sa.String(length=128), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.Column("decided_at", sa.DateTime(), nullable=True),
sa.Column("used_at", sa.DateTime(), nullable=True),
)
op.create_index(op.f("ix_approval_requests_action"), "approval_requests", ['action'], unique=False)
op.create_index(op.f("ix_approval_requests_applicant"), "approval_requests", ['applicant'], unique=False)
op.create_index(op.f("ix_approval_requests_approver"), "approval_requests", ['approver'], unique=False)
op.create_index(op.f("ix_approval_requests_created_at"), "approval_requests", ['created_at'], unique=False)
op.create_index(op.f("ix_approval_requests_domain"), "approval_requests", ['domain'], unique=False)
op.create_index(op.f("ix_approval_requests_record_id"), "approval_requests", ['record_id'], unique=False)
op.create_index(op.f("ix_approval_requests_status"), "approval_requests", ['status'], unique=False)
op.create_index(op.f("ix_approval_requests_ticket_id"), "approval_requests", ['ticket_id'], unique=True)
op.create_index(op.f("ix_approval_requests_used_by"), "approval_requests", ['used_by'], unique=False)
op.create_table(
"attendance_records",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("employee_name", sa.String(length=128), nullable=False),
sa.Column("employee_id", sa.String(length=128), nullable=True),
sa.Column("department", sa.String(length=128), nullable=True),
sa.Column("project_code", sa.String(length=64), nullable=True),
sa.Column("work_date", sa.Date(), nullable=False),
sa.Column("check_in_at", sa.DateTime(), nullable=True),
sa.Column("check_out_at", sa.DateTime(), nullable=True),
sa.Column("status", sa.String(length=64), nullable=False),
sa.Column("location", sa.String(length=255), nullable=True),
sa.Column("source_system", sa.String(length=64), nullable=False),
sa.Column("external_id", sa.String(length=128), nullable=True),
sa.Column("note", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_attendance_records_code"), "attendance_records", ['code'], unique=True)
op.create_index(op.f("ix_attendance_records_department"), "attendance_records", ['department'], unique=False)
op.create_index(op.f("ix_attendance_records_employee_id"), "attendance_records", ['employee_id'], unique=False)
op.create_index(op.f("ix_attendance_records_employee_name"), "attendance_records", ['employee_name'], unique=False)
op.create_index(op.f("ix_attendance_records_external_id"), "attendance_records", ['external_id'], unique=False)
op.create_index(op.f("ix_attendance_records_project_code"), "attendance_records", ['project_code'], unique=False)
op.create_index(op.f("ix_attendance_records_status"), "attendance_records", ['status'], unique=False)
op.create_index(op.f("ix_attendance_records_work_date"), "attendance_records", ['work_date'], unique=False)
op.create_table(
"audit_logs",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("actor", sa.String(length=128), nullable=False),
sa.Column("source", sa.String(length=64), nullable=False),
sa.Column("action", sa.String(length=128), nullable=False),
sa.Column("target_type", sa.String(length=128), nullable=True),
sa.Column("target_id", sa.String(length=128), nullable=True),
sa.Column("risk_level", sa.String(length=32), nullable=False),
sa.Column("request_payload", sa.Text(), nullable=True),
sa.Column("response_payload", sa.Text(), nullable=True),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("request_id", sa.String(length=64), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_audit_logs_action"), "audit_logs", ['action'], unique=False)
op.create_index(op.f("ix_audit_logs_actor"), "audit_logs", ['actor'], unique=False)
op.create_index(op.f("ix_audit_logs_created_at"), "audit_logs", ['created_at'], unique=False)
op.create_index(op.f("ix_audit_logs_request_id"), "audit_logs", ['request_id'], unique=False)
op.create_index(op.f("ix_audit_logs_risk_level"), "audit_logs", ['risk_level'], unique=False)
op.create_index(op.f("ix_audit_logs_source"), "audit_logs", ['source'], unique=False)
op.create_index(op.f("ix_audit_logs_status"), "audit_logs", ['status'], unique=False)
op.create_table(
"domain_events",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("event_id", sa.String(length=64), nullable=False),
sa.Column("event_type", sa.String(length=128), nullable=False),
sa.Column("source", sa.String(length=64), nullable=False),
sa.Column("aggregate_type", sa.String(length=128), nullable=False),
sa.Column("aggregate_id", sa.String(length=128), nullable=True),
sa.Column("actor", sa.String(length=128), nullable=False),
sa.Column("payload", sa.JSON(), nullable=True),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("attempts", sa.Integer(), nullable=False),
sa.Column("idempotency_key", sa.String(length=255), nullable=True),
sa.Column("last_error", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("processed_at", sa.DateTime(), nullable=True),
)
op.create_index(op.f("ix_domain_events_actor"), "domain_events", ['actor'], unique=False)
op.create_index(op.f("ix_domain_events_aggregate_id"), "domain_events", ['aggregate_id'], unique=False)
op.create_index(op.f("ix_domain_events_aggregate_type"), "domain_events", ['aggregate_type'], unique=False)
op.create_index(op.f("ix_domain_events_created_at"), "domain_events", ['created_at'], unique=False)
op.create_index(op.f("ix_domain_events_event_id"), "domain_events", ['event_id'], unique=True)
op.create_index(op.f("ix_domain_events_event_type"), "domain_events", ['event_type'], unique=False)
op.create_index(op.f("ix_domain_events_idempotency_key"), "domain_events", ['idempotency_key'], unique=True)
op.create_index(op.f("ix_domain_events_processed_at"), "domain_events", ['processed_at'], unique=False)
op.create_index(op.f("ix_domain_events_source"), "domain_events", ['source'], unique=False)
op.create_index(op.f("ix_domain_events_status"), "domain_events", ['status'], unique=False)
op.create_table(
"expenses",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("expense_type", sa.String(length=128), nullable=False),
sa.Column("amount", sa.Numeric(precision=14, scale=2), nullable=False),
sa.Column("applicant", sa.String(length=128), nullable=True),
sa.Column("department", sa.String(length=128), nullable=True),
sa.Column("project_code", sa.String(length=64), nullable=True),
sa.Column("budget_subject", sa.String(length=128), nullable=True),
sa.Column("payment_account", sa.String(length=128), nullable=True),
sa.Column("invoice_status", sa.String(length=64), nullable=False),
sa.Column("approval_status", sa.String(length=64), nullable=False),
sa.Column("payment_status", sa.String(length=64), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_expenses_approval_status"), "expenses", ['approval_status'], unique=False)
op.create_index(op.f("ix_expenses_code"), "expenses", ['code'], unique=True)
op.create_index(op.f("ix_expenses_department"), "expenses", ['department'], unique=False)
op.create_index(op.f("ix_expenses_expense_type"), "expenses", ['expense_type'], unique=False)
op.create_index(op.f("ix_expenses_payment_status"), "expenses", ['payment_status'], unique=False)
op.create_index(op.f("ix_expenses_project_code"), "expenses", ['project_code'], unique=False)
op.create_table(
"feishu_event_receipts",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("event_key", sa.String(length=256), nullable=False),
sa.Column("source", sa.String(length=64), nullable=False),
sa.Column("event_id", sa.String(length=128), nullable=True),
sa.Column("message_id", sa.String(length=128), nullable=True),
sa.Column("received_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_feishu_event_receipts_event_id"), "feishu_event_receipts", ['event_id'], unique=False)
op.create_index(op.f("ix_feishu_event_receipts_event_key"), "feishu_event_receipts", ['event_key'], unique=True)
op.create_index(op.f("ix_feishu_event_receipts_message_id"), "feishu_event_receipts", ['message_id'], unique=False)
op.create_index(op.f("ix_feishu_event_receipts_received_at"), "feishu_event_receipts", ['received_at'], unique=False)
op.create_index(op.f("ix_feishu_event_receipts_source"), "feishu_event_receipts", ['source'], unique=False)
op.create_table(
"fund_accounts",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column("account_type", sa.String(length=64), nullable=False),
sa.Column("current_balance", sa.Numeric(precision=16, scale=2), nullable=False),
sa.Column("expected_receivable", sa.Numeric(precision=16, scale=2), nullable=False),
sa.Column("expected_payable", sa.Numeric(precision=16, scale=2), nullable=False),
sa.Column("safety_line", sa.Numeric(precision=16, scale=2), nullable=False),
sa.Column("risk_level", sa.String(length=32), nullable=False),
sa.Column("note", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_fund_accounts_code"), "fund_accounts", ['code'], unique=True)
op.create_index(op.f("ix_fund_accounts_name"), "fund_accounts", ['name'], unique=False)
op.create_index(op.f("ix_fund_accounts_risk_level"), "fund_accounts", ['risk_level'], unique=False)
op.create_table(
"legacy_sync_runs",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("domain", sa.String(length=128), nullable=False),
sa.Column("source_table", sa.String(length=128), nullable=True),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("started_at", sa.DateTime(), nullable=False),
sa.Column("finished_at", sa.DateTime(), nullable=True),
sa.Column("created_count", sa.Integer(), nullable=False),
sa.Column("updated_count", sa.Integer(), nullable=False),
sa.Column("skipped_count", sa.Integer(), nullable=False),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("note", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_legacy_sync_runs_code"), "legacy_sync_runs", ['code'], unique=True)
op.create_index(op.f("ix_legacy_sync_runs_domain"), "legacy_sync_runs", ['domain'], unique=False)
op.create_index(op.f("ix_legacy_sync_runs_source_table"), "legacy_sync_runs", ['source_table'], unique=False)
op.create_index(op.f("ix_legacy_sync_runs_started_at"), "legacy_sync_runs", ['started_at'], unique=False)
op.create_index(op.f("ix_legacy_sync_runs_status"), "legacy_sync_runs", ['status'], unique=False)
op.create_table(
"official_writeback_runs",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("domain", sa.String(length=128), nullable=False),
sa.Column("record_id", sa.String(length=128), nullable=True),
sa.Column("action", sa.String(length=128), nullable=False),
sa.Column("actor", sa.String(length=128), nullable=False),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("approval_ticket_id", sa.String(length=64), nullable=True),
sa.Column("idempotency_key", sa.String(length=255), nullable=True),
sa.Column("request_payload", sa.JSON(), nullable=True),
sa.Column("provider_response", sa.JSON(), nullable=True),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.Column("submitted_at", sa.DateTime(), nullable=True),
sa.Column("sent_at", sa.DateTime(), nullable=True),
)
op.create_index(op.f("ix_official_writeback_runs_action"), "official_writeback_runs", ['action'], unique=False)
op.create_index(op.f("ix_official_writeback_runs_actor"), "official_writeback_runs", ['actor'], unique=False)
op.create_index(op.f("ix_official_writeback_runs_approval_ticket_id"), "official_writeback_runs", ['approval_ticket_id'], unique=False)
op.create_index(op.f("ix_official_writeback_runs_code"), "official_writeback_runs", ['code'], unique=True)
op.create_index(op.f("ix_official_writeback_runs_created_at"), "official_writeback_runs", ['created_at'], unique=False)
op.create_index(op.f("ix_official_writeback_runs_domain"), "official_writeback_runs", ['domain'], unique=False)
op.create_index(op.f("ix_official_writeback_runs_idempotency_key"), "official_writeback_runs", ['idempotency_key'], unique=True)
op.create_index(op.f("ix_official_writeback_runs_record_id"), "official_writeback_runs", ['record_id'], unique=False)
op.create_index(op.f("ix_official_writeback_runs_sent_at"), "official_writeback_runs", ['sent_at'], unique=False)
op.create_index(op.f("ix_official_writeback_runs_status"), "official_writeback_runs", ['status'], unique=False)
op.create_index(op.f("ix_official_writeback_runs_submitted_at"), "official_writeback_runs", ['submitted_at'], unique=False)
op.create_table(
"performance_metrics",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column("applies_to_role", sa.String(length=128), nullable=True),
sa.Column("formula", sa.Text(), nullable=True),
sa.Column("weight", sa.Numeric(precision=5, scale=2), nullable=False),
sa.Column("data_source", sa.String(length=255), nullable=True),
sa.Column("auto_score", sa.Numeric(precision=8, scale=2), nullable=False),
sa.Column("confirmed_score", sa.Numeric(precision=8, scale=2), nullable=True),
sa.Column("status", sa.String(length=64), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_performance_metrics_code"), "performance_metrics", ['code'], unique=True)
op.create_index(op.f("ix_performance_metrics_name"), "performance_metrics", ['name'], unique=False)
op.create_index(op.f("ix_performance_metrics_status"), "performance_metrics", ['status'], unique=False)
op.create_table(
"policies",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("title", sa.String(length=255), nullable=False),
sa.Column("policy_type", sa.String(length=128), nullable=True),
sa.Column("owner_department", sa.String(length=128), nullable=True),
sa.Column("version", sa.String(length=32), nullable=False),
sa.Column("status", sa.String(length=64), nullable=False),
sa.Column("effective_date", sa.Date(), nullable=True),
sa.Column("feishu_doc_url", sa.String(length=1024), nullable=True),
sa.Column("summary", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_policies_code"), "policies", ['code'], unique=True)
op.create_index(op.f("ix_policies_status"), "policies", ['status'], unique=False)
op.create_index(op.f("ix_policies_title"), "policies", ['title'], unique=False)
op.create_table(
"procurements",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column("applicant", sa.String(length=128), nullable=True),
sa.Column("project_code", sa.String(length=64), nullable=True),
sa.Column("supplier_name", sa.String(length=255), nullable=True),
sa.Column("budget_subject", sa.String(length=128), nullable=True),
sa.Column("expected_amount", sa.Numeric(precision=14, scale=2), nullable=False),
sa.Column("actual_amount", sa.Numeric(precision=14, scale=2), nullable=False),
sa.Column("approval_status", sa.String(length=64), nullable=False),
sa.Column("delivery_status", sa.String(length=64), nullable=False),
sa.Column("payment_status", sa.String(length=64), nullable=False),
sa.Column("comparison_summary", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_procurements_approval_status"), "procurements", ['approval_status'], unique=False)
op.create_index(op.f("ix_procurements_code"), "procurements", ['code'], unique=True)
op.create_index(op.f("ix_procurements_delivery_status"), "procurements", ['delivery_status'], unique=False)
op.create_index(op.f("ix_procurements_name"), "procurements", ['name'], unique=False)
op.create_index(op.f("ix_procurements_payment_status"), "procurements", ['payment_status'], unique=False)
op.create_index(op.f("ix_procurements_project_code"), "procurements", ['project_code'], unique=False)
op.create_table(
"projects",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column("owner", sa.String(length=128), nullable=True),
sa.Column("status", sa.String(length=64), nullable=False),
sa.Column("priority", sa.String(length=32), nullable=False),
sa.Column("progress_percent", sa.Integer(), nullable=False),
sa.Column("risk_level", sa.String(length=32), nullable=False),
sa.Column("budget_amount", sa.Numeric(precision=14, scale=2), nullable=False),
sa.Column("actual_amount", sa.Numeric(precision=14, scale=2), nullable=False),
sa.Column("start_date", sa.Date(), nullable=True),
sa.Column("due_date", sa.Date(), nullable=True),
sa.Column("description", sa.Text(), nullable=True),
sa.Column("source_system", sa.String(length=64), nullable=False),
sa.Column("external_id", sa.String(length=128), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_projects_code"), "projects", ['code'], unique=True)
op.create_index(op.f("ix_projects_due_date"), "projects", ['due_date'], unique=False)
op.create_index(op.f("ix_projects_external_id"), "projects", ['external_id'], unique=False)
op.create_index(op.f("ix_projects_name"), "projects", ['name'], unique=False)
op.create_index(op.f("ix_projects_owner"), "projects", ['owner'], unique=False)
op.create_index(op.f("ix_projects_risk_level"), "projects", ['risk_level'], unique=False)
op.create_index(op.f("ix_projects_status"), "projects", ['status'], unique=False)
op.create_table(
"report_push_runs",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("report_type", sa.String(length=64), nullable=False),
sa.Column("title", sa.String(length=255), nullable=True),
sa.Column("receive_id", sa.String(length=128), nullable=True),
sa.Column("receive_id_type", sa.String(length=64), nullable=False),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("task_id", sa.String(length=128), nullable=True),
sa.Column("actor", sa.String(length=128), nullable=False),
sa.Column("provider_response", sa.JSON(), nullable=True),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("queued_at", sa.DateTime(), nullable=False),
sa.Column("sent_at", sa.DateTime(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_report_push_runs_actor"), "report_push_runs", ['actor'], unique=False)
op.create_index(op.f("ix_report_push_runs_code"), "report_push_runs", ['code'], unique=True)
op.create_index(op.f("ix_report_push_runs_queued_at"), "report_push_runs", ['queued_at'], unique=False)
op.create_index(op.f("ix_report_push_runs_receive_id"), "report_push_runs", ['receive_id'], unique=False)
op.create_index(op.f("ix_report_push_runs_receive_id_type"), "report_push_runs", ['receive_id_type'], unique=False)
op.create_index(op.f("ix_report_push_runs_report_type"), "report_push_runs", ['report_type'], unique=False)
op.create_index(op.f("ix_report_push_runs_status"), "report_push_runs", ['status'], unique=False)
op.create_index(op.f("ix_report_push_runs_task_id"), "report_push_runs", ['task_id'], unique=False)
op.create_table(
"risk_event_actions",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("risk_event_id", sa.Integer(), nullable=False),
sa.Column("action", sa.String(length=64), nullable=False),
sa.Column("actor", sa.String(length=128), nullable=False),
sa.Column("from_status", sa.String(length=32), nullable=True),
sa.Column("to_status", sa.String(length=32), nullable=True),
sa.Column("assigned_to", sa.String(length=128), nullable=True),
sa.Column("comment", sa.Text(), nullable=True),
sa.Column("payload", sa.JSON(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_risk_event_actions_action"), "risk_event_actions", ['action'], unique=False)
op.create_index(op.f("ix_risk_event_actions_actor"), "risk_event_actions", ['actor'], unique=False)
op.create_index(op.f("ix_risk_event_actions_assigned_to"), "risk_event_actions", ['assigned_to'], unique=False)
op.create_index(op.f("ix_risk_event_actions_code"), "risk_event_actions", ['code'], unique=True)
op.create_index(op.f("ix_risk_event_actions_created_at"), "risk_event_actions", ['created_at'], unique=False)
op.create_index(op.f("ix_risk_event_actions_risk_event_id"), "risk_event_actions", ['risk_event_id'], unique=False)
op.create_table(
"risk_events",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("title", sa.String(length=255), nullable=False),
sa.Column("risk_type", sa.String(length=64), nullable=False),
sa.Column("risk_level", sa.String(length=32), nullable=False),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("source_domain", sa.String(length=128), nullable=False),
sa.Column("source_record_id", sa.String(length=128), nullable=True),
sa.Column("project_code", sa.String(length=64), nullable=True),
sa.Column("owner", sa.String(length=128), nullable=True),
sa.Column("detected_at", sa.DateTime(), nullable=False),
sa.Column("due_date", sa.Date(), nullable=True),
sa.Column("assigned_to", sa.String(length=128), nullable=True),
sa.Column("resolved_at", sa.DateTime(), nullable=True),
sa.Column("closed_at", sa.DateTime(), nullable=True),
sa.Column("closed_reason", sa.Text(), nullable=True),
sa.Column("review_summary", sa.Text(), nullable=True),
sa.Column("description", sa.Text(), nullable=True),
sa.Column("mitigation", sa.Text(), nullable=True),
sa.Column("evidence", sa.JSON(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_risk_events_assigned_to"), "risk_events", ['assigned_to'], unique=False)
op.create_index(op.f("ix_risk_events_code"), "risk_events", ['code'], unique=True)
op.create_index(op.f("ix_risk_events_detected_at"), "risk_events", ['detected_at'], unique=False)
op.create_index(op.f("ix_risk_events_due_date"), "risk_events", ['due_date'], unique=False)
op.create_index(op.f("ix_risk_events_owner"), "risk_events", ['owner'], unique=False)
op.create_index(op.f("ix_risk_events_project_code"), "risk_events", ['project_code'], unique=False)
op.create_index(op.f("ix_risk_events_risk_level"), "risk_events", ['risk_level'], unique=False)
op.create_index(op.f("ix_risk_events_risk_type"), "risk_events", ['risk_type'], unique=False)
op.create_index(op.f("ix_risk_events_source_domain"), "risk_events", ['source_domain'], unique=False)
op.create_index(op.f("ix_risk_events_source_record_id"), "risk_events", ['source_record_id'], unique=False)
op.create_index(op.f("ix_risk_events_status"), "risk_events", ['status'], unique=False)
op.create_index(op.f("ix_risk_events_title"), "risk_events", ['title'], unique=False)
op.create_table(
"standards",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("title", sa.String(length=255), nullable=False),
sa.Column("standard_type", sa.String(length=128), nullable=True),
sa.Column("applies_to", sa.String(length=255), nullable=True),
sa.Column("status", sa.String(length=64), nullable=False),
sa.Column("check_items", sa.Text(), nullable=True),
sa.Column("remediation", sa.Text(), nullable=True),
sa.Column("policy_code", sa.String(length=64), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_standards_code"), "standards", ['code'], unique=True)
op.create_index(op.f("ix_standards_policy_code"), "standards", ['policy_code'], unique=False)
op.create_index(op.f("ix_standards_status"), "standards", ['status'], unique=False)
op.create_index(op.f("ix_standards_title"), "standards", ['title'], unique=False)
op.create_table(
"suppliers",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("name", sa.String(length=255), nullable=False),
sa.Column("category", sa.String(length=128), nullable=True),
sa.Column("contact", sa.String(length=128), nullable=True),
sa.Column("quality_score", sa.Numeric(precision=5, scale=2), nullable=False),
sa.Column("delivery_score", sa.Numeric(precision=5, scale=2), nullable=False),
sa.Column("price_score", sa.Numeric(precision=5, scale=2), nullable=False),
sa.Column("risk_level", sa.String(length=32), nullable=False),
sa.Column("blacklist_status", sa.String(length=32), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_suppliers_blacklist_status"), "suppliers", ['blacklist_status'], unique=False)
op.create_index(op.f("ix_suppliers_category"), "suppliers", ['category'], unique=False)
op.create_index(op.f("ix_suppliers_code"), "suppliers", ['code'], unique=True)
op.create_index(op.f("ix_suppliers_name"), "suppliers", ['name'], unique=False)
op.create_index(op.f("ix_suppliers_risk_level"), "suppliers", ['risk_level'], unique=False)
op.create_table(
"work_reports",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("report_type", sa.String(length=32), nullable=False),
sa.Column("title", sa.String(length=255), nullable=False),
sa.Column("reporter", sa.String(length=128), nullable=False),
sa.Column("department", sa.String(length=128), nullable=True),
sa.Column("project_code", sa.String(length=64), nullable=True),
sa.Column("period_start", sa.Date(), nullable=False),
sa.Column("period_end", sa.Date(), nullable=False),
sa.Column("content", sa.Text(), nullable=False),
sa.Column("metrics", sa.JSON(), nullable=True),
sa.Column("risk_summary", sa.JSON(), nullable=True),
sa.Column("status", sa.String(length=64), nullable=False),
sa.Column("source_system", sa.String(length=64), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_work_reports_code"), "work_reports", ['code'], unique=True)
op.create_index(op.f("ix_work_reports_department"), "work_reports", ['department'], unique=False)
op.create_index(op.f("ix_work_reports_period_end"), "work_reports", ['period_end'], unique=False)
op.create_index(op.f("ix_work_reports_period_start"), "work_reports", ['period_start'], unique=False)
op.create_index(op.f("ix_work_reports_project_code"), "work_reports", ['project_code'], unique=False)
op.create_index(op.f("ix_work_reports_report_type"), "work_reports", ['report_type'], unique=False)
op.create_index(op.f("ix_work_reports_reporter"), "work_reports", ['reporter'], unique=False)
op.create_index(op.f("ix_work_reports_status"), "work_reports", ['status'], unique=False)
op.create_index(op.f("ix_work_reports_title"), "work_reports", ['title'], unique=False)
op.create_table(
"work_tasks",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("title", sa.String(length=255), nullable=False),
sa.Column("project_code", sa.String(length=64), nullable=True),
sa.Column("owner", sa.String(length=128), nullable=True),
sa.Column("status", sa.String(length=64), nullable=False),
sa.Column("priority", sa.String(length=32), nullable=False),
sa.Column("due_date", sa.Date(), nullable=True),
sa.Column("completed_at", sa.DateTime(), nullable=True),
sa.Column("blocker", sa.Text(), nullable=True),
sa.Column("description", sa.Text(), nullable=True),
sa.Column("source_system", sa.String(length=64), nullable=False),
sa.Column("external_id", sa.String(length=128), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_work_tasks_code"), "work_tasks", ['code'], unique=True)
op.create_index(op.f("ix_work_tasks_due_date"), "work_tasks", ['due_date'], unique=False)
op.create_index(op.f("ix_work_tasks_external_id"), "work_tasks", ['external_id'], unique=False)
op.create_index(op.f("ix_work_tasks_owner"), "work_tasks", ['owner'], unique=False)
op.create_index(op.f("ix_work_tasks_project_code"), "work_tasks", ['project_code'], unique=False)
op.create_index(op.f("ix_work_tasks_status"), "work_tasks", ['status'], unique=False)
op.create_index(op.f("ix_work_tasks_title"), "work_tasks", ['title'], unique=False)
op.create_table(
"workflow_actions",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("workflow_code", sa.String(length=64), nullable=False),
sa.Column("action", sa.String(length=128), nullable=False),
sa.Column("actor", sa.String(length=128), nullable=False),
sa.Column("from_status", sa.String(length=32), nullable=True),
sa.Column("to_status", sa.String(length=32), nullable=True),
sa.Column("payload", sa.JSON(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
)
op.create_index(op.f("ix_workflow_actions_action"), "workflow_actions", ['action'], unique=False)
op.create_index(op.f("ix_workflow_actions_actor"), "workflow_actions", ['actor'], unique=False)
op.create_index(op.f("ix_workflow_actions_code"), "workflow_actions", ['code'], unique=True)
op.create_index(op.f("ix_workflow_actions_created_at"), "workflow_actions", ['created_at'], unique=False)
op.create_index(op.f("ix_workflow_actions_workflow_code"), "workflow_actions", ['workflow_code'], unique=False)
op.create_table(
"workflow_instances",
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("workflow_type", sa.String(length=128), nullable=False),
sa.Column("aggregate_type", sa.String(length=128), nullable=False),
sa.Column("aggregate_id", sa.String(length=128), nullable=True),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("actor", sa.String(length=128), nullable=False),
sa.Column("current_step", sa.String(length=128), nullable=True),
sa.Column("payload", sa.JSON(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.Column("completed_at", sa.DateTime(), nullable=True),
)
op.create_index(op.f("ix_workflow_instances_actor"), "workflow_instances", ['actor'], unique=False)
op.create_index(op.f("ix_workflow_instances_aggregate_id"), "workflow_instances", ['aggregate_id'], unique=False)
op.create_index(op.f("ix_workflow_instances_aggregate_type"), "workflow_instances", ['aggregate_type'], unique=False)
op.create_index(op.f("ix_workflow_instances_code"), "workflow_instances", ['code'], unique=True)
op.create_index(op.f("ix_workflow_instances_completed_at"), "workflow_instances", ['completed_at'], unique=False)
op.create_index(op.f("ix_workflow_instances_created_at"), "workflow_instances", ['created_at'], unique=False)
op.create_index(op.f("ix_workflow_instances_status"), "workflow_instances", ['status'], unique=False)
op.create_index(op.f("ix_workflow_instances_workflow_type"), "workflow_instances", ['workflow_type'], unique=False)
def downgrade() -> None:

View File

@@ -0,0 +1,208 @@
"""Add V3 enterprise automation foundation tables.
Revision ID: 202607080002
Revises: 202607080001
Create Date: 2026-07-08
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect
revision = "202607080002"
down_revision = "202607080001"
branch_labels = None
depends_on = None
AUDIT_LOGS_TABLE = "audit_logs"
DOMAIN_EVENTS_TABLE = "domain_events"
WORKFLOW_INSTANCES_TABLE = "workflow_instances"
WORKFLOW_ACTIONS_TABLE = "workflow_actions"
OFFICIAL_WRITEBACK_RUNS_TABLE = "official_writeback_runs"
def _table_exists(table_name: str) -> bool:
inspector = inspect(op.get_bind())
return table_name in inspector.get_table_names()
def _column_names(table_name: str) -> set[str]:
inspector = inspect(op.get_bind())
if table_name not in inspector.get_table_names():
return set()
return {column["name"] for column in inspector.get_columns(table_name)}
def _add_column_if_missing(table_name: str, column: sa.Column) -> None:
if column.name not in _column_names(table_name):
op.add_column(table_name, column)
def _create_indexes(table_name: str, indexes: list[tuple[str, bool]]) -> None:
for column_name, unique in indexes:
op.create_index(
op.f(f"ix_{table_name}_{column_name}"),
table_name,
[column_name],
unique=unique,
if_not_exists=True,
)
def upgrade() -> None:
_add_column_if_missing(
AUDIT_LOGS_TABLE,
sa.Column("request_id", sa.String(length=64), nullable=True),
)
op.create_index(
op.f("ix_audit_logs_request_id"),
AUDIT_LOGS_TABLE,
["request_id"],
unique=False,
if_not_exists=True,
)
if not _table_exists(DOMAIN_EVENTS_TABLE):
op.create_table(
DOMAIN_EVENTS_TABLE,
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("event_id", sa.String(length=64), nullable=False),
sa.Column("event_type", sa.String(length=128), nullable=False),
sa.Column("source", sa.String(length=64), nullable=False),
sa.Column("aggregate_type", sa.String(length=128), nullable=False),
sa.Column("aggregate_id", sa.String(length=128), nullable=True),
sa.Column("actor", sa.String(length=128), nullable=False),
sa.Column("payload", sa.JSON(), nullable=True),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("attempts", sa.Integer(), nullable=False),
sa.Column("idempotency_key", sa.String(length=255), nullable=True),
sa.Column("last_error", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("processed_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
_create_indexes(
DOMAIN_EVENTS_TABLE,
[
("event_id", True),
("event_type", False),
("source", False),
("aggregate_type", False),
("aggregate_id", False),
("actor", False),
("status", False),
("idempotency_key", True),
("created_at", False),
("processed_at", False),
],
)
if not _table_exists(WORKFLOW_INSTANCES_TABLE):
op.create_table(
WORKFLOW_INSTANCES_TABLE,
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("workflow_type", sa.String(length=128), nullable=False),
sa.Column("aggregate_type", sa.String(length=128), nullable=False),
sa.Column("aggregate_id", sa.String(length=128), nullable=True),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("actor", sa.String(length=128), nullable=False),
sa.Column("current_step", sa.String(length=128), nullable=True),
sa.Column("payload", sa.JSON(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.Column("completed_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
_create_indexes(
WORKFLOW_INSTANCES_TABLE,
[
("code", True),
("workflow_type", False),
("aggregate_type", False),
("aggregate_id", False),
("status", False),
("actor", False),
("created_at", False),
("completed_at", False),
],
)
if not _table_exists(WORKFLOW_ACTIONS_TABLE):
op.create_table(
WORKFLOW_ACTIONS_TABLE,
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("workflow_code", sa.String(length=64), nullable=False),
sa.Column("action", sa.String(length=128), nullable=False),
sa.Column("actor", sa.String(length=128), nullable=False),
sa.Column("from_status", sa.String(length=32), nullable=True),
sa.Column("to_status", sa.String(length=32), nullable=True),
sa.Column("payload", sa.JSON(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
_create_indexes(
WORKFLOW_ACTIONS_TABLE,
[
("code", True),
("workflow_code", False),
("action", False),
("actor", False),
("created_at", False),
],
)
if not _table_exists(OFFICIAL_WRITEBACK_RUNS_TABLE):
op.create_table(
OFFICIAL_WRITEBACK_RUNS_TABLE,
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("code", sa.String(length=64), nullable=False),
sa.Column("domain", sa.String(length=128), nullable=False),
sa.Column("record_id", sa.String(length=128), nullable=True),
sa.Column("action", sa.String(length=128), nullable=False),
sa.Column("actor", sa.String(length=128), nullable=False),
sa.Column("status", sa.String(length=32), nullable=False),
sa.Column("approval_ticket_id", sa.String(length=64), nullable=True),
sa.Column("idempotency_key", sa.String(length=255), nullable=True),
sa.Column("request_payload", sa.JSON(), nullable=True),
sa.Column("provider_response", sa.JSON(), nullable=True),
sa.Column("error_message", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
sa.Column("submitted_at", sa.DateTime(), nullable=True),
sa.Column("sent_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
_create_indexes(
OFFICIAL_WRITEBACK_RUNS_TABLE,
[
("code", True),
("domain", False),
("record_id", False),
("action", False),
("actor", False),
("status", False),
("approval_ticket_id", False),
("idempotency_key", True),
("created_at", False),
("submitted_at", False),
("sent_at", False),
],
)
def downgrade() -> None:
for table_name in [
OFFICIAL_WRITEBACK_RUNS_TABLE,
WORKFLOW_ACTIONS_TABLE,
WORKFLOW_INSTANCES_TABLE,
DOMAIN_EVENTS_TABLE,
]:
if _table_exists(table_name):
op.drop_table(table_name)
if "request_id" in _column_names(AUDIT_LOGS_TABLE):
op.drop_index(op.f("ix_audit_logs_request_id"), table_name=AUDIT_LOGS_TABLE)
op.drop_column(AUDIT_LOGS_TABLE, "request_id")