feat: add BLIF Flow v2 persistence scaffolding

This commit is contained in:
plx
2026-08-15 23:30:37 +00:00
parent 20bc91dec5
commit e99da64d8b
9 changed files with 533 additions and 4 deletions

View File

@@ -0,0 +1,52 @@
-- Additive, rebuildable BLIF Flow v2 projection scaffolding.
-- This migration does not alter opportunities.stage or activate Flow v2.
CREATE TABLE IF NOT EXISTS opportunity_flow_state_v2 (
opportunity_id UUID PRIMARY KEY REFERENCES opportunities(id) ON DELETE CASCADE,
material_process_key TEXT NOT NULL,
canonical_opportunity_id UUID NOT NULL REFERENCES opportunities(id) ON DELETE CASCADE,
is_duplicate_representation BOOLEAN NOT NULL DEFAULT FALSE,
business_state TEXT NOT NULL,
business_next_action TEXT,
diagnostic_status TEXT NOT NULL DEFAULT 'clear',
confidence TEXT NOT NULL DEFAULT 'low',
reason_code TEXT NOT NULL,
reason_text TEXT NOT NULL,
evidence_refs JSONB NOT NULL DEFAULT '[]'::jsonb,
flow_version TEXT NOT NULL,
source_fingerprint TEXT NOT NULL,
derived_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS ix_opportunity_flow_state_v2_material_process_key
ON opportunity_flow_state_v2(material_process_key);
CREATE INDEX IF NOT EXISTS ix_opportunity_flow_state_v2_canonical
ON opportunity_flow_state_v2(canonical_opportunity_id);
CREATE INDEX IF NOT EXISTS ix_opportunity_flow_state_v2_business_state
ON opportunity_flow_state_v2(business_state);
ALTER TABLE tasks ADD COLUMN IF NOT EXISTS resolution_code TEXT;
ALTER TABLE tasks ADD COLUMN IF NOT EXISTS resolved_at TIMESTAMPTZ;
ALTER TABLE tasks ADD COLUMN IF NOT EXISTS resolved_by_event_id UUID REFERENCES opportunity_events(id) ON DELETE SET NULL;
ALTER TABLE tasks ADD COLUMN IF NOT EXISTS superseded_by_task_id UUID REFERENCES tasks(id) ON DELETE SET NULL;
CREATE INDEX IF NOT EXISTS ix_tasks_resolved_by_event_id ON tasks(resolved_by_event_id);
CREATE INDEX IF NOT EXISTS ix_tasks_superseded_by_task_id ON tasks(superseded_by_task_id);
CREATE TABLE IF NOT EXISTS opportunity_flow_transitions (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
opportunity_id UUID NOT NULL REFERENCES opportunities(id) ON DELETE CASCADE,
from_state TEXT,
to_state TEXT NOT NULL,
reason_code TEXT NOT NULL,
reason_text TEXT NOT NULL,
evidence_refs JSONB NOT NULL DEFAULT '[]'::jsonb,
flow_version TEXT NOT NULL,
source_fingerprint TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (opportunity_id, from_state, to_state, flow_version, source_fingerprint)
);
CREATE INDEX IF NOT EXISTS ix_opportunity_flow_transitions_opportunity_created
ON opportunity_flow_transitions(opportunity_id, created_at DESC);

View File

@@ -0,0 +1,12 @@
-- Reversible rollback for BLIF Flow v2 persistence scaffolding.
DROP TABLE IF EXISTS opportunity_flow_transitions;
DROP INDEX IF EXISTS ix_tasks_superseded_by_task_id;
DROP INDEX IF EXISTS ix_tasks_resolved_by_event_id;
ALTER TABLE tasks DROP COLUMN IF EXISTS superseded_by_task_id;
ALTER TABLE tasks DROP COLUMN IF EXISTS resolved_by_event_id;
ALTER TABLE tasks DROP COLUMN IF EXISTS resolved_at;
ALTER TABLE tasks DROP COLUMN IF EXISTS resolution_code;
DROP TABLE IF EXISTS opportunity_flow_state_v2;