feat: persist versioned organization needs
This commit is contained in:
1 parent
f81cd0ae09
commit
ee3738ee73
14 files changed
+2388
No files matched your search
@@ -0,0 +1,98 @@
|
||||
-- +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;
|
||||
@@ -0,0 +1,102 @@
|
||||
-- The parent and first immutable snapshot are inserted in one caller-owned
|
||||
-- transaction. Deferred version references are checked when it commits.
|
||||
-- name: InsertOrganizationNeed :exec
|
||||
INSERT INTO organization_needs (
|
||||
id,
|
||||
organization_id,
|
||||
current_version,
|
||||
requirements_version,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(organization_id),
|
||||
sqlc.arg(current_version),
|
||||
sqlc.arg(requirements_version),
|
||||
sqlc.arg(created_at),
|
||||
sqlc.arg(updated_at)
|
||||
);
|
||||
|
||||
-- name: InsertOrganizationNeedVersion :exec
|
||||
INSERT INTO organization_need_versions (
|
||||
organization_id,
|
||||
need_id,
|
||||
version,
|
||||
snapshot
|
||||
) VALUES (
|
||||
sqlc.arg(organization_id),
|
||||
sqlc.arg(need_id),
|
||||
sqlc.arg(version),
|
||||
sqlc.arg(snapshot)::jsonb
|
||||
);
|
||||
|
||||
-- Read one current version with its organization-scoped parent metadata.
|
||||
-- name: ReadCurrentOrganizationNeed :one
|
||||
SELECT
|
||||
need.organization_id,
|
||||
need.id AS need_id,
|
||||
need.current_version,
|
||||
need.requirements_version,
|
||||
need.created_at,
|
||||
need.updated_at,
|
||||
version.snapshot::text AS snapshot
|
||||
FROM organization_needs AS need
|
||||
INNER JOIN organization_need_versions AS version
|
||||
ON version.organization_id = need.organization_id
|
||||
AND version.need_id = need.id
|
||||
AND version.version = need.current_version
|
||||
WHERE
|
||||
need.organization_id = sqlc.arg(organization_id)
|
||||
AND need.id = sqlc.arg(need_id);
|
||||
|
||||
-- Read one historical version without exposing another organization's record.
|
||||
-- name: ReadOrganizationNeedVersion :one
|
||||
SELECT snapshot::text AS snapshot
|
||||
FROM organization_need_versions
|
||||
WHERE
|
||||
organization_id = sqlc.arg(organization_id)
|
||||
AND need_id = sqlc.arg(need_id)
|
||||
AND version = sqlc.arg(version);
|
||||
|
||||
-- The conditional parent update is the compare-and-swap. Appending the
|
||||
-- snapshot is part of the same SQL statement, so a failed insert leaves the
|
||||
-- parent pointer unchanged even before the surrounding transaction rolls
|
||||
-- back. No row is returned when the expected version lost a race.
|
||||
-- name: AdvanceOrganizationNeed :one
|
||||
WITH advanced AS (
|
||||
UPDATE organization_needs
|
||||
SET
|
||||
current_version = organization_needs.current_version + 1,
|
||||
requirements_version = sqlc.arg(requirements_version),
|
||||
updated_at = sqlc.arg(updated_at)
|
||||
WHERE
|
||||
organization_needs.organization_id = sqlc.arg(organization_id)
|
||||
AND organization_needs.id = sqlc.arg(need_id)
|
||||
AND organization_needs.current_version = sqlc.arg(expected_version)
|
||||
AND sqlc.arg(next_version)::bigint = organization_needs.current_version + 1
|
||||
AND (
|
||||
sqlc.arg(requirements_version)::bigint = organization_needs.requirements_version
|
||||
OR sqlc.arg(requirements_version)::bigint = organization_needs.current_version + 1
|
||||
)
|
||||
RETURNING organization_needs.organization_id, organization_needs.id, organization_needs.current_version
|
||||
), appended AS (
|
||||
INSERT INTO organization_need_versions (
|
||||
organization_id,
|
||||
need_id,
|
||||
version,
|
||||
snapshot
|
||||
)
|
||||
SELECT
|
||||
advanced.organization_id,
|
||||
advanced.id,
|
||||
advanced.current_version,
|
||||
sqlc.arg(snapshot)::jsonb
|
||||
FROM advanced
|
||||
RETURNING
|
||||
organization_need_versions.organization_id,
|
||||
organization_need_versions.need_id,
|
||||
organization_need_versions.version,
|
||||
organization_need_versions.snapshot::text AS snapshot
|
||||
)
|
||||
SELECT appended.snapshot
|
||||
FROM appended;
|
||||
Reference in new issue
Block a user