-- 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); -- Read one bounded newest-first page of current needs. Both the organization -- scope and the tuple cursor are applied before the limit. -- name: ListOrganizationNeeds :many 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 ( sqlc.narg(after_created_at)::timestamptz IS NULL OR (need.created_at, need.id) < ( sqlc.narg(after_created_at)::timestamptz, sqlc.narg(after_need_id)::uuid ) ) ORDER BY need.created_at DESC, need.id DESC LIMIT sqlc.arg(row_limit); -- 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; -- The generic idempotency scope serializes retries. Replacing the child row -- is safe only in the transaction that reserves an expired scope for a new -- create; live retries read and return the stored response without writing. -- name: SaveOrganizationNeedCreationResult :exec INSERT INTO organization_need_creation_results (scope_hash, snapshot) VALUES (sqlc.arg(scope_hash), sqlc.arg(snapshot)::jsonb) ON CONFLICT (scope_hash) DO UPDATE SET snapshot = excluded.snapshot; -- Read only the immutable original response. The current need may have moved -- to a later version since this result was created. -- name: GetOrganizationNeedCreationResult :one SELECT snapshot::text AS snapshot FROM organization_need_creation_results WHERE scope_hash = sqlc.arg(scope_hash);