103 lines
3.4 KiB
SQL
103 lines
3.4 KiB
SQL
-- 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;
|