20260730_370_governance_domain_templates.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """Add generic governance object types and versioned domain templates."""
  2. from alembic import op
  3. revision = "20260730_370"
  4. down_revision = "20260730_360"
  5. branch_labels = None
  6. depends_on = None
  7. def upgrade() -> None:
  8. op.execute(
  9. """
  10. CREATE TABLE public.governance_domain_templates (
  11. uid UUID PRIMARY KEY,
  12. template_code VARCHAR(64) NOT NULL UNIQUE,
  13. name VARCHAR(200) NOT NULL,
  14. lifecycle_status VARCHAR(20) NOT NULL
  15. CHECK (lifecycle_status IN ('draft','active','retired')),
  16. current_version INTEGER NOT NULL CHECK (current_version >= 1),
  17. content_hash CHAR(64) NOT NULL,
  18. definition JSONB NOT NULL,
  19. created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
  20. updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
  21. CHECK (jsonb_typeof(definition) = 'object')
  22. );
  23. CREATE TABLE public.governance_domain_template_versions (
  24. uid UUID PRIMARY KEY,
  25. template_uid UUID NOT NULL
  26. REFERENCES public.governance_domain_templates(uid),
  27. version INTEGER NOT NULL CHECK (version >= 1),
  28. content_hash CHAR(64) NOT NULL,
  29. definition JSONB NOT NULL,
  30. actor_uid UUID NOT NULL REFERENCES public.users(id),
  31. created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
  32. UNIQUE (template_uid, version),
  33. CHECK (jsonb_typeof(definition) = 'object')
  34. );
  35. CREATE TABLE public.governance_object_types (
  36. uid UUID PRIMARY KEY,
  37. template_uid UUID NOT NULL
  38. REFERENCES public.governance_domain_templates(uid),
  39. type_code VARCHAR(64) NOT NULL,
  40. name VARCHAR(200) NOT NULL,
  41. lifecycle_status VARCHAR(20) NOT NULL
  42. CHECK (lifecycle_status IN ('draft','active','retired')),
  43. current_version INTEGER NOT NULL CHECK (current_version >= 1),
  44. stable_uid_prefix VARCHAR(16) NOT NULL,
  45. source_identity_fields JSONB NOT NULL,
  46. definition JSONB NOT NULL,
  47. created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
  48. updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
  49. UNIQUE (template_uid, type_code),
  50. CHECK (jsonb_typeof(source_identity_fields) = 'array'),
  51. CHECK (jsonb_array_length(source_identity_fields) > 0),
  52. CHECK (jsonb_typeof(definition) = 'object')
  53. );
  54. CREATE TABLE public.governance_domain_template_imports (
  55. uid UUID PRIMARY KEY,
  56. template_uid UUID NOT NULL
  57. REFERENCES public.governance_domain_templates(uid),
  58. operation VARCHAR(20) NOT NULL
  59. CHECK (operation IN ('import','rollback')),
  60. status VARCHAR(20) NOT NULL
  61. CHECK (status IN ('applied','failed')),
  62. version INTEGER NOT NULL CHECK (version >= 1),
  63. target_version INTEGER,
  64. before_state JSONB,
  65. after_state JSONB NOT NULL,
  66. diff JSONB NOT NULL,
  67. actor_uid UUID NOT NULL REFERENCES public.users(id),
  68. created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
  69. CHECK (before_state IS NULL OR jsonb_typeof(before_state) = 'object'),
  70. CHECK (jsonb_typeof(after_state) = 'object'),
  71. CHECK (jsonb_typeof(diff) = 'object')
  72. );
  73. CREATE INDEX idx_governance_domain_template_versions_created
  74. ON public.governance_domain_template_versions(
  75. template_uid, version DESC
  76. );
  77. CREATE INDEX idx_governance_object_types_status
  78. ON public.governance_object_types(
  79. template_uid, lifecycle_status, type_code
  80. );
  81. CREATE INDEX idx_governance_domain_template_imports_created
  82. ON public.governance_domain_template_imports(
  83. template_uid, created_at DESC
  84. );
  85. """
  86. )
  87. def downgrade() -> None:
  88. raise RuntimeError(
  89. "governance domain template history is append-only; "
  90. "schema downgrade requires an approved archival migration"
  91. )