-- +goose Up -- Organization needs keep a current version pointer and one bounded, -- immutable JSON snapshot for every committed version. Readiness and -- recommendations are derived from requirements and are not frozen here. CREATE TABLE organization_needs ( id uuid NOT NULL, organization_id uuid NOT NULL, current_version bigint NOT NULL, requirements_version bigint NOT NULL, created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL, CONSTRAINT organization_needs_pkey PRIMARY KEY (id), CONSTRAINT organization_needs_organization_id_id_key UNIQUE (organization_id, id), CONSTRAINT organization_needs_organization_fkey FOREIGN KEY (organization_id) REFERENCES organizations (id) ON DELETE RESTRICT, CONSTRAINT organization_needs_current_version_positive CHECK (current_version > 0), CONSTRAINT organization_needs_requirements_version CHECK ( requirements_version > 0 AND requirements_version <= current_version ), CONSTRAINT organization_needs_timestamps CHECK (updated_at >= created_at) ); -- Every version is tied to its organization-scoped need identity. The full -- public requirement data and provenance fit under the 256 KiB snapshot cap; -- the source text itself is bounded by the contract at 10,000 characters. CREATE TABLE organization_need_versions ( organization_id uuid NOT NULL, need_id uuid NOT NULL, version bigint NOT NULL, snapshot jsonb NOT NULL, CONSTRAINT organization_need_versions_pkey PRIMARY KEY (organization_id, need_id, version), CONSTRAINT organization_need_versions_need_fkey FOREIGN KEY (organization_id, need_id) REFERENCES organization_needs (organization_id, id) ON DELETE RESTRICT, CONSTRAINT organization_need_versions_version_positive CHECK (version > 0), CONSTRAINT organization_need_versions_snapshot_shape CHECK ( jsonb_typeof(snapshot) = 'object' AND snapshot ?& ARRAY['id', 'organization_id', 'version', 'requirements_version'] AND jsonb_typeof(snapshot -> 'id') = 'string' AND snapshot ->> 'id' = need_id::text AND jsonb_typeof(snapshot -> 'organization_id') = 'string' AND snapshot ->> 'organization_id' = organization_id::text AND jsonb_typeof(snapshot -> 'version') = 'number' AND snapshot ->> 'version' = version::text AND jsonb_typeof(snapshot -> 'requirements_version') = 'number' AND CASE WHEN snapshot ->> 'requirements_version' ~ '^[0-9]+$' THEN (snapshot ->> 'requirements_version')::bigint BETWEEN 1 AND version ELSE FALSE END AND octet_length(snapshot::text) <= 262144 ) ); -- The current and requirements versions must both name snapshots belonging -- to this same organization and need. Deferral lets creation insert the -- parent and version 1 in one transaction. ALTER TABLE organization_needs ADD CONSTRAINT organization_needs_current_snapshot_fkey FOREIGN KEY (organization_id, id, current_version) REFERENCES organization_need_versions (organization_id, need_id, version) DEFERRABLE INITIALLY DEFERRED, ADD CONSTRAINT organization_needs_requirements_snapshot_fkey FOREIGN KEY (organization_id, id, requirements_version) REFERENCES organization_need_versions (organization_id, need_id, version) DEFERRABLE INITIALLY DEFERRED; -- Application writes append versions only. Old snapshots remain available -- for future history reads and cannot be edited or deleted by ordinary SQL. -- TRUNCATE remains available to reset isolated integration databases. -- +goose StatementBegin CREATE FUNCTION reject_organization_need_version_change() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION USING ERRCODE = 'integrity_constraint_violation', CONSTRAINT = 'organization_need_versions_immutable', TABLE = 'organization_need_versions', MESSAGE = 'need versions are immutable: append a new version instead'; END; $$; -- +goose StatementEnd CREATE TRIGGER organization_need_versions_immutable BEFORE UPDATE OR DELETE ON organization_need_versions FOR EACH ROW EXECUTE FUNCTION reject_organization_need_version_change(); CREATE INDEX organization_needs_org_created_idx ON organization_needs (organization_id, created_at DESC, id DESC); -- +goose Down DROP INDEX organization_needs_org_created_idx; DROP TRIGGER organization_need_versions_immutable ON organization_need_versions; DROP FUNCTION reject_organization_need_version_change(); ALTER TABLE organization_needs DROP CONSTRAINT organization_needs_requirements_snapshot_fkey, DROP CONSTRAINT organization_needs_current_snapshot_fkey; DROP TABLE organization_need_versions; DROP TABLE organization_needs;