feat(store): key control-plane resources by (org, kind)
resources and resource_history are one document per org and kind, with per-org advisory locks, request-ID idempotency and audit keys. The control-plane Query/History/Command/Watch requests carry org_id; a request naming none acts in the default org until the caller's org is derived server-side.
This commit is contained in:
1 parent
662a5ee336
commit
bfc8d4b1b7
25 files changed
+459
-187
No files matched your search
@@ -0,0 +1,24 @@
|
||||
-- +goose Up
|
||||
|
||||
-- A control-plane resource is one document per (org, kind). Every existing row
|
||||
-- already carries org_id 'default' from 0013, so the wider key holds wherever
|
||||
-- the kind-only one did.
|
||||
ALTER TABLE resources DROP CONSTRAINT resources_pkey;
|
||||
ALTER TABLE resources ADD PRIMARY KEY (org_id, kind);
|
||||
|
||||
DROP INDEX idx_resource_history_kind_version;
|
||||
DROP INDEX idx_resource_history_kind_request;
|
||||
CREATE INDEX idx_resource_history_org_kind_version ON resource_history (org_id, kind, version);
|
||||
-- The request-ID idempotency key is per org and kind: a resource write replays
|
||||
-- only the same org's prior write of the same kind (resourceByRequest).
|
||||
CREATE UNIQUE INDEX idx_resource_history_org_kind_request ON resource_history (org_id, kind, request_id);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
DROP INDEX idx_resource_history_org_kind_request;
|
||||
DROP INDEX idx_resource_history_org_kind_version;
|
||||
CREATE UNIQUE INDEX idx_resource_history_kind_request ON resource_history (kind, request_id);
|
||||
CREATE INDEX idx_resource_history_kind_version ON resource_history (kind, version);
|
||||
|
||||
ALTER TABLE resources DROP CONSTRAINT resources_pkey;
|
||||
ALTER TABLE resources ADD PRIMARY KEY (kind);
|
||||
@@ -62,19 +62,21 @@ SELECT $1::timestamptz, 'resources', $2::text, d.field, d.old_value, d.new_value
|
||||
$3::bigint, $4::text, $5::text, $6::text
|
||||
FROM audit_json_diff(
|
||||
(SELECT convert_from(h.value, 'UTF8')::jsonb FROM resource_history h
|
||||
WHERE h.kind = $2::text AND h.version = $7::bigint),
|
||||
convert_from($8::bytea, 'UTF8')::jsonb
|
||||
WHERE h.org_id = $7::text AND h.kind = $8::text AND h.version = $9::bigint),
|
||||
convert_from($10::bytea, 'UTF8')::jsonb
|
||||
) d
|
||||
ORDER BY d.field
|
||||
`
|
||||
|
||||
type InsertResourceAuditParams struct {
|
||||
At time.Time
|
||||
Kind string
|
||||
RecordKey string
|
||||
Version int64
|
||||
Actor string
|
||||
Source string
|
||||
RequestID string
|
||||
OrgID string
|
||||
Kind string
|
||||
PreviousVersion int64
|
||||
Value []byte
|
||||
}
|
||||
@@ -84,11 +86,13 @@ type InsertResourceAuditParams struct {
|
||||
func (q *Queries) InsertResourceAudit(ctx context.Context, arg InsertResourceAuditParams) error {
|
||||
_, err := q.db.Exec(ctx, insertResourceAudit,
|
||||
arg.At,
|
||||
arg.Kind,
|
||||
arg.RecordKey,
|
||||
arg.Version,
|
||||
arg.Actor,
|
||||
arg.Source,
|
||||
arg.RequestID,
|
||||
arg.OrgID,
|
||||
arg.Kind,
|
||||
arg.PreviousVersion,
|
||||
arg.Value,
|
||||
)
|
||||
|
||||
@@ -11,19 +11,25 @@ import (
|
||||
)
|
||||
|
||||
const insertResource = `-- name: InsertResource :one
|
||||
INSERT INTO resources (kind, value, version, updated_at)
|
||||
VALUES ($1, $2, 1, $3)
|
||||
INSERT INTO resources (org_id, kind, value, version, updated_at)
|
||||
VALUES ($1, $2, $3, 1, $4)
|
||||
RETURNING kind, value, version, updated_at, org_id
|
||||
`
|
||||
|
||||
type InsertResourceParams struct {
|
||||
OrgID string
|
||||
Kind string
|
||||
Value []byte
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) InsertResource(ctx context.Context, arg InsertResourceParams) (Resource, error) {
|
||||
row := q.db.QueryRow(ctx, insertResource, arg.Kind, arg.Value, arg.UpdatedAt)
|
||||
row := q.db.QueryRow(ctx, insertResource,
|
||||
arg.OrgID,
|
||||
arg.Kind,
|
||||
arg.Value,
|
||||
arg.UpdatedAt,
|
||||
)
|
||||
var i Resource
|
||||
err := row.Scan(
|
||||
&i.Kind,
|
||||
@@ -37,15 +43,16 @@ func (q *Queries) InsertResource(ctx context.Context, arg InsertResourceParams)
|
||||
|
||||
const insertResourceHistory = `-- name: InsertResourceHistory :one
|
||||
INSERT INTO resource_history (
|
||||
kind, value, version, actor, source, request_id, expected_version,
|
||||
org_id, kind, value, version, actor, source, request_id, expected_version,
|
||||
current_version, at
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING kind, value, version, actor, source, request_id, expected_version,
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
RETURNING org_id, kind, value, version, actor, source, request_id, expected_version,
|
||||
current_version, at
|
||||
`
|
||||
|
||||
type InsertResourceHistoryParams struct {
|
||||
OrgID string
|
||||
Kind string
|
||||
Value []byte
|
||||
Version int64
|
||||
@@ -58,6 +65,7 @@ type InsertResourceHistoryParams struct {
|
||||
}
|
||||
|
||||
type InsertResourceHistoryRow struct {
|
||||
OrgID string
|
||||
Kind string
|
||||
Value []byte
|
||||
Version int64
|
||||
@@ -71,6 +79,7 @@ type InsertResourceHistoryRow struct {
|
||||
|
||||
func (q *Queries) InsertResourceHistory(ctx context.Context, arg InsertResourceHistoryParams) (InsertResourceHistoryRow, error) {
|
||||
row := q.db.QueryRow(ctx, insertResourceHistory,
|
||||
arg.OrgID,
|
||||
arg.Kind,
|
||||
arg.Value,
|
||||
arg.Version,
|
||||
@@ -83,6 +92,7 @@ func (q *Queries) InsertResourceHistory(ctx context.Context, arg InsertResourceH
|
||||
)
|
||||
var i InsertResourceHistoryRow
|
||||
err := row.Scan(
|
||||
&i.OrgID,
|
||||
&i.Kind,
|
||||
&i.Value,
|
||||
&i.Version,
|
||||
@@ -97,24 +107,31 @@ func (q *Queries) InsertResourceHistory(ctx context.Context, arg InsertResourceH
|
||||
}
|
||||
|
||||
const lockResourceWrite = `-- name: LockResourceWrite :exec
|
||||
SELECT pg_advisory_xact_lock(hashtextextended($1, 0))
|
||||
SELECT pg_advisory_xact_lock(hashtextextended($1::text || '/' || $2::text, 0))
|
||||
`
|
||||
|
||||
type LockResourceWriteParams struct {
|
||||
OrgID string
|
||||
Kind string
|
||||
}
|
||||
|
||||
// This lock is acquired in its own statement before a resource read. PostgreSQL
|
||||
// takes a fresh Read Committed snapshot for the subsequent read, so concurrent
|
||||
// optimistic writers see the committed revision rather than both writing v1.
|
||||
func (q *Queries) LockResourceWrite(ctx context.Context, hashtextextended string) error {
|
||||
_, err := q.db.Exec(ctx, lockResourceWrite, hashtextextended)
|
||||
// The lock is per (org, kind): two orgs writing the same kind do not contend.
|
||||
func (q *Queries) LockResourceWrite(ctx context.Context, arg LockResourceWriteParams) error {
|
||||
_, err := q.db.Exec(ctx, lockResourceWrite, arg.OrgID, arg.Kind)
|
||||
return err
|
||||
}
|
||||
|
||||
const reinsertResource = `-- name: ReinsertResource :one
|
||||
INSERT INTO resources (kind, value, version, updated_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
INSERT INTO resources (org_id, kind, value, version, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING kind, value, version, updated_at, org_id
|
||||
`
|
||||
|
||||
type ReinsertResourceParams struct {
|
||||
OrgID string
|
||||
Kind string
|
||||
Value []byte
|
||||
Version int64
|
||||
@@ -124,11 +141,12 @@ type ReinsertResourceParams struct {
|
||||
// Re-creates the current row for a revision resource_history already records.
|
||||
// The ledger outlives the row it describes, so a resource an operator removed
|
||||
// still has its revisions; putting one back needs the row re-created without a
|
||||
// second ledger entry, which idx_resource_history_kind_request would refuse.
|
||||
// second ledger entry, which idx_resource_history_org_kind_request would refuse.
|
||||
// The revision is the ledger's, not a fresh one: this is the row that write
|
||||
// produced, restored.
|
||||
func (q *Queries) ReinsertResource(ctx context.Context, arg ReinsertResourceParams) (Resource, error) {
|
||||
row := q.db.QueryRow(ctx, reinsertResource,
|
||||
arg.OrgID,
|
||||
arg.Kind,
|
||||
arg.Value,
|
||||
arg.Version,
|
||||
@@ -146,11 +164,17 @@ func (q *Queries) ReinsertResource(ctx context.Context, arg ReinsertResourcePara
|
||||
}
|
||||
|
||||
const resourceByKind = `-- name: ResourceByKind :one
|
||||
SELECT kind, value, version, updated_at, org_id FROM resources WHERE kind = $1
|
||||
SELECT kind, value, version, updated_at, org_id FROM resources
|
||||
WHERE org_id = $1 AND kind = $2
|
||||
`
|
||||
|
||||
func (q *Queries) ResourceByKind(ctx context.Context, kind string) (Resource, error) {
|
||||
row := q.db.QueryRow(ctx, resourceByKind, kind)
|
||||
type ResourceByKindParams struct {
|
||||
OrgID string
|
||||
Kind string
|
||||
}
|
||||
|
||||
func (q *Queries) ResourceByKind(ctx context.Context, arg ResourceByKindParams) (Resource, error) {
|
||||
row := q.db.QueryRow(ctx, resourceByKind, arg.OrgID, arg.Kind)
|
||||
var i Resource
|
||||
err := row.Scan(
|
||||
&i.Kind,
|
||||
@@ -163,18 +187,20 @@ func (q *Queries) ResourceByKind(ctx context.Context, kind string) (Resource, er
|
||||
}
|
||||
|
||||
const resourceByRequest = `-- name: ResourceByRequest :one
|
||||
SELECT kind, value, version, actor, source, request_id, expected_version,
|
||||
SELECT org_id, kind, value, version, actor, source, request_id, expected_version,
|
||||
current_version, at
|
||||
FROM resource_history
|
||||
WHERE kind = $1 AND request_id = $2
|
||||
WHERE org_id = $1 AND kind = $2 AND request_id = $3
|
||||
`
|
||||
|
||||
type ResourceByRequestParams struct {
|
||||
OrgID string
|
||||
Kind string
|
||||
RequestID string
|
||||
}
|
||||
|
||||
type ResourceByRequestRow struct {
|
||||
OrgID string
|
||||
Kind string
|
||||
Value []byte
|
||||
Version int64
|
||||
@@ -187,9 +213,10 @@ type ResourceByRequestRow struct {
|
||||
}
|
||||
|
||||
func (q *Queries) ResourceByRequest(ctx context.Context, arg ResourceByRequestParams) (ResourceByRequestRow, error) {
|
||||
row := q.db.QueryRow(ctx, resourceByRequest, arg.Kind, arg.RequestID)
|
||||
row := q.db.QueryRow(ctx, resourceByRequest, arg.OrgID, arg.Kind, arg.RequestID)
|
||||
var i ResourceByRequestRow
|
||||
err := row.Scan(
|
||||
&i.OrgID,
|
||||
&i.Kind,
|
||||
&i.Value,
|
||||
&i.Version,
|
||||
@@ -204,20 +231,22 @@ func (q *Queries) ResourceByRequest(ctx context.Context, arg ResourceByRequestPa
|
||||
}
|
||||
|
||||
const resourceHistory = `-- name: ResourceHistory :many
|
||||
SELECT kind, value, version, actor, source, request_id, expected_version,
|
||||
SELECT org_id, kind, value, version, actor, source, request_id, expected_version,
|
||||
current_version, at
|
||||
FROM resource_history
|
||||
WHERE kind = $1
|
||||
WHERE org_id = $1 AND kind = $2
|
||||
ORDER BY version DESC
|
||||
LIMIT NULLIF($2::bigint, 0)
|
||||
LIMIT NULLIF($3::bigint, 0)
|
||||
`
|
||||
|
||||
type ResourceHistoryParams struct {
|
||||
OrgID string
|
||||
Kind string
|
||||
EntryLimit int64
|
||||
}
|
||||
|
||||
type ResourceHistoryRow struct {
|
||||
OrgID string
|
||||
Kind string
|
||||
Value []byte
|
||||
Version int64
|
||||
@@ -230,7 +259,7 @@ type ResourceHistoryRow struct {
|
||||
}
|
||||
|
||||
func (q *Queries) ResourceHistory(ctx context.Context, arg ResourceHistoryParams) ([]ResourceHistoryRow, error) {
|
||||
rows, err := q.db.Query(ctx, resourceHistory, arg.Kind, arg.EntryLimit)
|
||||
rows, err := q.db.Query(ctx, resourceHistory, arg.OrgID, arg.Kind, arg.EntryLimit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -239,6 +268,7 @@ func (q *Queries) ResourceHistory(ctx context.Context, arg ResourceHistoryParams
|
||||
for rows.Next() {
|
||||
var i ResourceHistoryRow
|
||||
if err := rows.Scan(
|
||||
&i.OrgID,
|
||||
&i.Kind,
|
||||
&i.Value,
|
||||
&i.Version,
|
||||
@@ -260,11 +290,16 @@ func (q *Queries) ResourceHistory(ctx context.Context, arg ResourceHistoryParams
|
||||
}
|
||||
|
||||
const resourceVersion = `-- name: ResourceVersion :one
|
||||
SELECT version FROM resources WHERE kind = $1
|
||||
SELECT version FROM resources WHERE org_id = $1 AND kind = $2
|
||||
`
|
||||
|
||||
func (q *Queries) ResourceVersion(ctx context.Context, kind string) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, resourceVersion, kind)
|
||||
type ResourceVersionParams struct {
|
||||
OrgID string
|
||||
Kind string
|
||||
}
|
||||
|
||||
func (q *Queries) ResourceVersion(ctx context.Context, arg ResourceVersionParams) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, resourceVersion, arg.OrgID, arg.Kind)
|
||||
var version int64
|
||||
err := row.Scan(&version)
|
||||
return version, err
|
||||
@@ -272,12 +307,13 @@ func (q *Queries) ResourceVersion(ctx context.Context, kind string) (int64, erro
|
||||
|
||||
const updateResource = `-- name: UpdateResource :one
|
||||
UPDATE resources
|
||||
SET value = $2, version = version + 1, updated_at = $3
|
||||
WHERE kind = $1 AND version = $4
|
||||
SET value = $3, version = version + 1, updated_at = $4
|
||||
WHERE org_id = $1 AND kind = $2 AND version = $5
|
||||
RETURNING kind, value, version, updated_at, org_id
|
||||
`
|
||||
|
||||
type UpdateResourceParams struct {
|
||||
OrgID string
|
||||
Kind string
|
||||
Value []byte
|
||||
UpdatedAt time.Time
|
||||
@@ -286,6 +322,7 @@ type UpdateResourceParams struct {
|
||||
|
||||
func (q *Queries) UpdateResource(ctx context.Context, arg UpdateResourceParams) (Resource, error) {
|
||||
row := q.db.QueryRow(ctx, updateResource,
|
||||
arg.OrgID,
|
||||
arg.Kind,
|
||||
arg.Value,
|
||||
arg.UpdatedAt,
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
-- Records the fields one resource write changed, against the version it
|
||||
-- replaced. Runs in the write's own transaction.
|
||||
INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id)
|
||||
SELECT sqlc.arg(at)::timestamptz, 'resources', sqlc.arg(kind)::text, d.field, d.old_value, d.new_value,
|
||||
SELECT sqlc.arg(at)::timestamptz, 'resources', sqlc.arg(record_key)::text, d.field, d.old_value, d.new_value,
|
||||
sqlc.arg(version)::bigint, sqlc.arg(actor)::text, sqlc.arg(source)::text, sqlc.arg(request_id)::text
|
||||
FROM audit_json_diff(
|
||||
(SELECT convert_from(h.value, 'UTF8')::jsonb FROM resource_history h
|
||||
WHERE h.kind = sqlc.arg(kind)::text AND h.version = sqlc.arg(previous_version)::bigint),
|
||||
WHERE h.org_id = sqlc.arg(org_id)::text AND h.kind = sqlc.arg(kind)::text AND h.version = sqlc.arg(previous_version)::bigint),
|
||||
convert_from(sqlc.arg(value)::bytea, 'UTF8')::jsonb
|
||||
) d
|
||||
ORDER BY d.field;
|
||||
|
||||
@@ -2,55 +2,57 @@
|
||||
-- This lock is acquired in its own statement before a resource read. PostgreSQL
|
||||
-- takes a fresh Read Committed snapshot for the subsequent read, so concurrent
|
||||
-- optimistic writers see the committed revision rather than both writing v1.
|
||||
SELECT pg_advisory_xact_lock(hashtextextended($1, 0));
|
||||
-- The lock is per (org, kind): two orgs writing the same kind do not contend.
|
||||
SELECT pg_advisory_xact_lock(hashtextextended(sqlc.arg(org_id)::text || '/' || sqlc.arg(kind)::text, 0));
|
||||
|
||||
-- name: ResourceByKind :one
|
||||
SELECT kind, value, version, updated_at, org_id FROM resources WHERE kind = $1;
|
||||
SELECT kind, value, version, updated_at, org_id FROM resources
|
||||
WHERE org_id = $1 AND kind = $2;
|
||||
|
||||
-- name: ResourceByRequest :one
|
||||
SELECT kind, value, version, actor, source, request_id, expected_version,
|
||||
SELECT org_id, kind, value, version, actor, source, request_id, expected_version,
|
||||
current_version, at
|
||||
FROM resource_history
|
||||
WHERE kind = $1 AND request_id = $2;
|
||||
WHERE org_id = $1 AND kind = $2 AND request_id = $3;
|
||||
|
||||
-- name: ReinsertResource :one
|
||||
-- Re-creates the current row for a revision resource_history already records.
|
||||
-- The ledger outlives the row it describes, so a resource an operator removed
|
||||
-- still has its revisions; putting one back needs the row re-created without a
|
||||
-- second ledger entry, which idx_resource_history_kind_request would refuse.
|
||||
-- second ledger entry, which idx_resource_history_org_kind_request would refuse.
|
||||
-- The revision is the ledger's, not a fresh one: this is the row that write
|
||||
-- produced, restored.
|
||||
INSERT INTO resources (kind, value, version, updated_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
INSERT INTO resources (org_id, kind, value, version, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING kind, value, version, updated_at, org_id;
|
||||
|
||||
-- name: ResourceHistory :many
|
||||
SELECT kind, value, version, actor, source, request_id, expected_version,
|
||||
SELECT org_id, kind, value, version, actor, source, request_id, expected_version,
|
||||
current_version, at
|
||||
FROM resource_history
|
||||
WHERE kind = $1
|
||||
WHERE org_id = $1 AND kind = $2
|
||||
ORDER BY version DESC
|
||||
LIMIT NULLIF(sqlc.arg(entry_limit)::bigint, 0);
|
||||
|
||||
-- name: ResourceVersion :one
|
||||
SELECT version FROM resources WHERE kind = $1;
|
||||
SELECT version FROM resources WHERE org_id = $1 AND kind = $2;
|
||||
|
||||
-- name: InsertResource :one
|
||||
INSERT INTO resources (kind, value, version, updated_at)
|
||||
VALUES ($1, $2, 1, $3)
|
||||
INSERT INTO resources (org_id, kind, value, version, updated_at)
|
||||
VALUES ($1, $2, $3, 1, $4)
|
||||
RETURNING kind, value, version, updated_at, org_id;
|
||||
|
||||
-- name: UpdateResource :one
|
||||
UPDATE resources
|
||||
SET value = $2, version = version + 1, updated_at = $3
|
||||
WHERE kind = $1 AND version = $4
|
||||
SET value = $3, version = version + 1, updated_at = $4
|
||||
WHERE org_id = $1 AND kind = $2 AND version = $5
|
||||
RETURNING kind, value, version, updated_at, org_id;
|
||||
|
||||
-- name: InsertResourceHistory :one
|
||||
INSERT INTO resource_history (
|
||||
kind, value, version, actor, source, request_id, expected_version,
|
||||
org_id, kind, value, version, actor, source, request_id, expected_version,
|
||||
current_version, at
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING kind, value, version, actor, source, request_id, expected_version,
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
RETURNING org_id, kind, value, version, actor, source, request_id, expected_version,
|
||||
current_version, at;
|
||||
@@ -24,8 +24,8 @@ func NewResources(pool *pgxpool.Pool) *Resources {
|
||||
return &Resources{pool: pool}
|
||||
}
|
||||
|
||||
func (s *Resources) Resource(ctx context.Context, kind string) (storecontract.Resource, error) {
|
||||
resource, err := postgresdb.New(s.pool).ResourceByKind(ctx, kind)
|
||||
func (s *Resources) Resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error) {
|
||||
resource, err := postgresdb.New(s.pool).ResourceByKind(ctx, postgresdb.ResourceByKindParams{OrgID: orgID, Kind: kind})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return storecontract.Resource{}, storecontract.ErrResourceNotFound
|
||||
}
|
||||
@@ -35,12 +35,12 @@ func (s *Resources) Resource(ctx context.Context, kind string) (storecontract.Re
|
||||
return resourceFromCurrent(resource), nil
|
||||
}
|
||||
|
||||
func (s *Resources) ResourceHistory(ctx context.Context, kind string, limit int) ([]storecontract.Resource, error) {
|
||||
func (s *Resources) ResourceHistory(ctx context.Context, orgID, kind string, limit int) ([]storecontract.Resource, error) {
|
||||
if limit <= 0 {
|
||||
limit = 0
|
||||
}
|
||||
history, err := postgresdb.New(s.pool).ResourceHistory(ctx, postgresdb.ResourceHistoryParams{
|
||||
Kind: kind, EntryLimit: int64(limit),
|
||||
OrgID: orgID, Kind: kind, EntryLimit: int64(limit),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -48,7 +48,7 @@ func (s *Resources) ResourceHistory(ctx context.Context, kind string, limit int)
|
||||
resources := make([]storecontract.Resource, 0, len(history))
|
||||
for _, entry := range history {
|
||||
resources = append(resources, resourceFromAudit(
|
||||
entry.Kind, entry.Value, entry.Version, entry.Actor, entry.Source,
|
||||
entry.OrgID, entry.Kind, entry.Value, entry.Version, entry.Actor, entry.Source,
|
||||
entry.RequestID, entry.ExpectedVersion, entry.CurrentVersion, entry.At,
|
||||
))
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func (s *Resources) Audit(ctx context.Context, table string, keys []string, limi
|
||||
|
||||
// PutResource applies one control-plane resource write.
|
||||
//
|
||||
// It is idempotent per request ID: a write whose (kind, request_id) is already
|
||||
// It is idempotent per request ID: a write whose (org_id, kind, request_id) is already
|
||||
// in resource_history returns that revision instead of writing a second one, so a
|
||||
// retry that reached the store twice is still one revision. The ledger is what
|
||||
// carries the key -- resource_history outlives the row it describes -- so a
|
||||
@@ -90,17 +90,20 @@ func (s *Resources) Audit(ctx context.Context, table string, keys []string, limi
|
||||
// caller cannot tell the difference, and a kind can stay gone while every
|
||||
// process believes it holds a value.
|
||||
func (s *Resources) PutResource(ctx context.Context, write storecontract.ResourceWrite) (_ storecontract.Resource, retErr error) {
|
||||
if write.OrgID == "" {
|
||||
return storecontract.Resource{}, errors.New("resource write names no org")
|
||||
}
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
queries := postgresdb.New(tx)
|
||||
if err := queries.LockResourceWrite(ctx, write.Kind); err != nil {
|
||||
if err := queries.LockResourceWrite(ctx, postgresdb.LockResourceWriteParams{OrgID: write.OrgID, Kind: write.Kind}); err != nil {
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
if existing, err := queries.ResourceByRequest(ctx, postgresdb.ResourceByRequestParams{
|
||||
Kind: write.Kind, RequestID: write.RequestID,
|
||||
OrgID: write.OrgID, Kind: write.Kind, RequestID: write.RequestID,
|
||||
}); err == nil {
|
||||
resource, err := s.replayedResource(ctx, queries, tx, existing)
|
||||
if err != nil {
|
||||
@@ -111,7 +114,7 @@ func (s *Resources) PutResource(ctx context.Context, write storecontract.Resourc
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
|
||||
current, err := queries.ResourceVersion(ctx, write.Kind)
|
||||
current, err := queries.ResourceVersion(ctx, postgresdb.ResourceVersionParams{OrgID: write.OrgID, Kind: write.Kind})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
current = 0
|
||||
} else if err != nil {
|
||||
@@ -120,48 +123,62 @@ func (s *Resources) PutResource(ctx context.Context, write storecontract.Resourc
|
||||
if current != write.ExpectedVersion {
|
||||
return storecontract.Resource{}, fmt.Errorf("%w: expected %d, current %d", storecontract.ErrResourceVersionConflict, write.ExpectedVersion, current)
|
||||
}
|
||||
if write.At.IsZero() {
|
||||
write.At = time.Now().UTC()
|
||||
} else {
|
||||
write.At = write.At.UTC()
|
||||
}
|
||||
|
||||
var resource postgresdb.Resource
|
||||
if current == 0 {
|
||||
resource, err = queries.InsertResource(ctx, postgresdb.InsertResourceParams{
|
||||
Kind: write.Kind, Value: write.Value, UpdatedAt: write.At,
|
||||
})
|
||||
} else {
|
||||
resource, err = queries.UpdateResource(ctx, postgresdb.UpdateResourceParams{
|
||||
Kind: write.Kind, Value: write.Value, UpdatedAt: write.At, Version: current,
|
||||
})
|
||||
}
|
||||
history, err := appendResourceRevision(ctx, queries, write, current)
|
||||
if err != nil {
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
history, err := queries.InsertResourceHistory(ctx, postgresdb.InsertResourceHistoryParams{
|
||||
Kind: write.Kind, Value: write.Value, Version: resource.Version, Actor: write.Actor,
|
||||
Source: write.Source, RequestID: write.RequestID, ExpectedVersion: write.ExpectedVersion,
|
||||
CurrentVersion: current, At: write.At,
|
||||
})
|
||||
if err != nil {
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
if err := queries.InsertResourceAudit(ctx, postgresdb.InsertResourceAuditParams{
|
||||
At: write.At, Kind: write.Kind, Version: resource.Version, Actor: write.Actor,
|
||||
Source: write.Source, RequestID: write.RequestID, PreviousVersion: current, Value: write.Value,
|
||||
}); err != nil {
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
return resourceFromAudit(
|
||||
history.Kind, history.Value, history.Version, history.Actor, history.Source,
|
||||
history.OrgID, history.Kind, history.Value, history.Version, history.Actor, history.Source,
|
||||
history.RequestID, history.ExpectedVersion, history.CurrentVersion, history.At,
|
||||
), nil
|
||||
}
|
||||
|
||||
// appendResourceRevision writes the resource row, its ledger entry and its audit
|
||||
// trail for one accepted write, returning the ledger entry. current is the
|
||||
// version being replaced, zero for a first write.
|
||||
func appendResourceRevision(ctx context.Context, queries *postgresdb.Queries, write storecontract.ResourceWrite, current int64) (postgresdb.InsertResourceHistoryRow, error) {
|
||||
if write.At.IsZero() {
|
||||
write.At = time.Now().UTC()
|
||||
} else {
|
||||
write.At = write.At.UTC()
|
||||
}
|
||||
|
||||
var (
|
||||
resource postgresdb.Resource
|
||||
err error
|
||||
)
|
||||
if current == 0 {
|
||||
resource, err = queries.InsertResource(ctx, postgresdb.InsertResourceParams{
|
||||
OrgID: write.OrgID, Kind: write.Kind, Value: write.Value, UpdatedAt: write.At,
|
||||
})
|
||||
} else {
|
||||
resource, err = queries.UpdateResource(ctx, postgresdb.UpdateResourceParams{
|
||||
OrgID: write.OrgID, Kind: write.Kind, Value: write.Value, UpdatedAt: write.At, Version: current,
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
return postgresdb.InsertResourceHistoryRow{}, err
|
||||
}
|
||||
history, err := queries.InsertResourceHistory(ctx, postgresdb.InsertResourceHistoryParams{
|
||||
OrgID: write.OrgID, Kind: write.Kind, Value: write.Value, Version: resource.Version, Actor: write.Actor,
|
||||
Source: write.Source, RequestID: write.RequestID, ExpectedVersion: write.ExpectedVersion,
|
||||
CurrentVersion: current, At: write.At,
|
||||
})
|
||||
if err != nil {
|
||||
return postgresdb.InsertResourceHistoryRow{}, err
|
||||
}
|
||||
if err := queries.InsertResourceAudit(ctx, postgresdb.InsertResourceAuditParams{
|
||||
At: write.At, OrgID: write.OrgID, RecordKey: storecontract.ResourceAuditKey(write.OrgID, write.Kind), Kind: write.Kind, Version: resource.Version, Actor: write.Actor,
|
||||
Source: write.Source, RequestID: write.RequestID, PreviousVersion: current, Value: write.Value,
|
||||
}); err != nil {
|
||||
return postgresdb.InsertResourceHistoryRow{}, err
|
||||
}
|
||||
return history, nil
|
||||
}
|
||||
|
||||
// replayedResource answers a write whose request ID resource_history already
|
||||
// holds. The ledger's revision is the answer and a replay writes nothing, with
|
||||
// one repair: when the resource that revision describes is no longer the current
|
||||
@@ -170,9 +187,9 @@ func (s *Resources) PutResource(ctx context.Context, write storecontract.Resourc
|
||||
// revision it does not hold, so the caller reads a version for a kind that is
|
||||
// absent and the kind stays absent.
|
||||
func (s *Resources) replayedResource(ctx context.Context, queries *postgresdb.Queries, tx pgx.Tx, existing postgresdb.ResourceByRequestRow) (storecontract.Resource, error) {
|
||||
if _, err := queries.ResourceByKind(ctx, existing.Kind); errors.Is(err, pgx.ErrNoRows) {
|
||||
if _, err := queries.ResourceByKind(ctx, postgresdb.ResourceByKindParams{OrgID: existing.OrgID, Kind: existing.Kind}); errors.Is(err, pgx.ErrNoRows) {
|
||||
if _, err := queries.ReinsertResource(ctx, postgresdb.ReinsertResourceParams{
|
||||
Kind: existing.Kind, Value: existing.Value, Version: existing.Version, UpdatedAt: existing.At,
|
||||
OrgID: existing.OrgID, Kind: existing.Kind, Value: existing.Value, Version: existing.Version, UpdatedAt: existing.At,
|
||||
}); err != nil {
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
@@ -183,19 +200,19 @@ func (s *Resources) replayedResource(ctx context.Context, queries *postgresdb.Qu
|
||||
return storecontract.Resource{}, err
|
||||
}
|
||||
return resourceFromAudit(
|
||||
existing.Kind, existing.Value, existing.Version, existing.Actor, existing.Source,
|
||||
existing.OrgID, existing.Kind, existing.Value, existing.Version, existing.Actor, existing.Source,
|
||||
existing.RequestID, existing.ExpectedVersion, existing.CurrentVersion, existing.At,
|
||||
), nil
|
||||
}
|
||||
|
||||
func resourceFromCurrent(resource postgresdb.Resource) storecontract.Resource {
|
||||
return storecontract.Resource{
|
||||
Kind: resource.Kind, Value: resource.Value, Version: resource.Version, At: resource.UpdatedAt,
|
||||
OrgID: resource.OrgID, Kind: resource.Kind, Value: resource.Value, Version: resource.Version, At: resource.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func resourceFromAudit(
|
||||
kind string,
|
||||
orgID, kind string,
|
||||
value []byte,
|
||||
version int64,
|
||||
actor, source, requestID string,
|
||||
@@ -203,7 +220,7 @@ func resourceFromAudit(
|
||||
at time.Time,
|
||||
) storecontract.Resource {
|
||||
return storecontract.Resource{
|
||||
Kind: kind, Value: value, Version: version, Actor: actor, Source: source,
|
||||
OrgID: orgID, Kind: kind, Value: value, Version: version, Actor: actor, Source: source,
|
||||
RequestID: requestID, ExpectedVersion: expectedVersion, CurrentVersion: currentVersion, At: at,
|
||||
}
|
||||
}
|
||||
@@ -192,7 +192,8 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) {
|
||||
resources := resourcesFor(t)
|
||||
firstAt := time.Date(2026, 9, 24, 1, 0, 0, 0, time.UTC)
|
||||
first, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "archie-ui",
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: "settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "archie-ui",
|
||||
RequestID: "r1", ExpectedVersion: 0, At: firstAt,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -203,7 +204,8 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) {
|
||||
}
|
||||
|
||||
replay, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry",
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: "settings", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry",
|
||||
RequestID: "r1", ExpectedVersion: 0, At: firstAt.Add(time.Minute),
|
||||
})
|
||||
if err != nil {
|
||||
@@ -214,14 +216,16 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) {
|
||||
}
|
||||
|
||||
if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{"v":2}`), Actor: "operator", Source: "archie-ui",
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: "settings", Value: []byte(`{"v":2}`), Actor: "operator", Source: "archie-ui",
|
||||
RequestID: "r2", ExpectedVersion: 0, At: firstAt.Add(2 * time.Minute),
|
||||
}); !errors.Is(err, storecontract.ErrResourceVersionConflict) {
|
||||
t.Fatalf("stale PutResource = %v, want ErrResourceVersionConflict", err)
|
||||
}
|
||||
|
||||
second, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{"v":2}`), Actor: "operator", Source: "messaging",
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: "settings", Value: []byte(`{"v":2}`), Actor: "operator", Source: "messaging",
|
||||
RequestID: "r2", ExpectedVersion: 1, At: firstAt.Add(3 * time.Minute),
|
||||
})
|
||||
if err != nil {
|
||||
@@ -231,7 +235,8 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) {
|
||||
t.Fatalf("second write = %+v, want version 2 from version 1", second)
|
||||
}
|
||||
lateReplay, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry",
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: "settings", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry",
|
||||
RequestID: "r1", ExpectedVersion: 0, At: firstAt.Add(4 * time.Minute),
|
||||
})
|
||||
if err != nil {
|
||||
@@ -240,7 +245,7 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) {
|
||||
if lateReplay.Version != first.Version || string(lateReplay.Value) != string(first.Value) {
|
||||
t.Fatalf("late replay = %+v, want original %+v", lateReplay, first)
|
||||
}
|
||||
live, err := resources.Resource(t.Context(), "settings")
|
||||
live, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, "settings")
|
||||
if err != nil {
|
||||
t.Fatalf("Resource: %v", err)
|
||||
}
|
||||
@@ -248,21 +253,21 @@ func TestResourceWritesPreserveOptimisticAndIdempotentSemantics(t *testing.T) {
|
||||
t.Fatalf("live resource after replay = %+v, want second write %+v", live, second)
|
||||
}
|
||||
|
||||
history, err := resources.ResourceHistory(t.Context(), "settings", 1)
|
||||
history, err := resources.ResourceHistory(t.Context(), storecontract.DefaultOrgID, "settings", 1)
|
||||
if err != nil {
|
||||
t.Fatalf("ResourceHistory: %v", err)
|
||||
}
|
||||
if len(history) != 1 || history[0].Version != 2 || history[0].Source != "messaging" {
|
||||
t.Fatalf("limited history = %+v, want newest revision only", history)
|
||||
}
|
||||
history, err = resources.ResourceHistory(t.Context(), "settings", 0)
|
||||
history, err = resources.ResourceHistory(t.Context(), storecontract.DefaultOrgID, "settings", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("unlimited ResourceHistory: %v", err)
|
||||
}
|
||||
if len(history) != 2 {
|
||||
t.Fatalf("history after late replay = %+v, want two revisions", history)
|
||||
}
|
||||
if _, err := resources.Resource(t.Context(), "missing"); !errors.Is(err, storecontract.ErrResourceNotFound) {
|
||||
if _, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, "missing"); !errors.Is(err, storecontract.ErrResourceNotFound) {
|
||||
t.Fatalf("missing Resource = %v, want ErrResourceNotFound", err)
|
||||
}
|
||||
}
|
||||
@@ -278,7 +283,8 @@ func TestResourceReplayRecreatesARemovedResource(t *testing.T) {
|
||||
resources := NewResources(pool)
|
||||
at := time.Date(2026, 9, 27, 9, 0, 0, 0, time.UTC)
|
||||
write := storecontract.ResourceWrite{
|
||||
Kind: "workflow-definitions", Value: []byte(`{"definitions":[]}`), Actor: "system:migration",
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: "workflow-definitions", Value: []byte(`{"definitions":[]}`), Actor: "system:migration",
|
||||
Source: "legacy-config", RequestID: "import:workflow-definitions", ExpectedVersion: 0, At: at,
|
||||
}
|
||||
seeded, err := resources.PutResource(t.Context(), write)
|
||||
@@ -289,7 +295,7 @@ func TestResourceReplayRecreatesARemovedResource(t *testing.T) {
|
||||
if _, err := pool.Exec(t.Context(), `DELETE FROM resources WHERE kind = $1`, write.Kind); err != nil {
|
||||
t.Fatalf("remove the resource: %v", err)
|
||||
}
|
||||
if _, err := resources.Resource(t.Context(), write.Kind); !errors.Is(err, storecontract.ErrResourceNotFound) {
|
||||
if _, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, write.Kind); !errors.Is(err, storecontract.ErrResourceNotFound) {
|
||||
t.Fatalf("resource after the removal = %v, want ErrResourceNotFound", err)
|
||||
}
|
||||
|
||||
@@ -300,7 +306,7 @@ func TestResourceReplayRecreatesARemovedResource(t *testing.T) {
|
||||
if replayed.Version != seeded.Version || string(replayed.Value) != string(seeded.Value) {
|
||||
t.Fatalf("replay = %+v, want the revision the ledger records, %+v", replayed, seeded)
|
||||
}
|
||||
live, err := resources.Resource(t.Context(), write.Kind)
|
||||
live, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, write.Kind)
|
||||
if err != nil {
|
||||
t.Fatalf("the replay did not re-create the resource: %v", err)
|
||||
}
|
||||
@@ -309,7 +315,7 @@ func TestResourceReplayRecreatesARemovedResource(t *testing.T) {
|
||||
}
|
||||
// The repair re-creates the row the write produced; it does not append a
|
||||
// second ledger entry, which idx_resource_history_kind_request would refuse.
|
||||
history, err := resources.ResourceHistory(t.Context(), write.Kind, 0)
|
||||
history, err := resources.ResourceHistory(t.Context(), storecontract.DefaultOrgID, write.Kind, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ResourceHistory: %v", err)
|
||||
}
|
||||
@@ -320,7 +326,8 @@ func TestResourceReplayRecreatesARemovedResource(t *testing.T) {
|
||||
// A resource the store still holds is answered from the ledger, unchanged:
|
||||
// the idempotent replay the index exists for.
|
||||
again, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: write.Kind, Value: []byte(`{"definitions":[{"id":"different"}]}`), Actor: "other",
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: write.Kind, Value: []byte(`{"definitions":[{"id":"different"}]}`), Actor: "other",
|
||||
Source: "retry", RequestID: write.RequestID, ExpectedVersion: 0, At: at.Add(time.Minute),
|
||||
})
|
||||
if err != nil {
|
||||
@@ -340,14 +347,16 @@ func TestResourceRequestIDIsIdempotentPerKind(t *testing.T) {
|
||||
resources := resourcesFor(t)
|
||||
at := time.Date(2026, 9, 26, 12, 0, 0, 0, time.UTC)
|
||||
first, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "provider-settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "import",
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: "provider-settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "import",
|
||||
RequestID: "shared", ExpectedVersion: 0, At: at,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("first PutResource: %v", err)
|
||||
}
|
||||
second, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "scheduling-policy", Value: []byte(`{"v":2}`), Actor: "operator", Source: "import",
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: "scheduling-policy", Value: []byte(`{"v":2}`), Actor: "operator", Source: "import",
|
||||
RequestID: "shared", ExpectedVersion: 0, At: at,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -358,7 +367,8 @@ func TestResourceRequestIDIsIdempotentPerKind(t *testing.T) {
|
||||
}
|
||||
|
||||
replay, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "scheduling-policy", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry",
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: "scheduling-policy", Value: []byte(`{"v":999}`), Actor: "other", Source: "retry",
|
||||
RequestID: "shared", ExpectedVersion: 0, At: at.Add(time.Minute),
|
||||
})
|
||||
if err != nil {
|
||||
@@ -368,7 +378,7 @@ func TestResourceRequestIDIsIdempotentPerKind(t *testing.T) {
|
||||
t.Fatalf("replay = %+v, want scheduling-policy's original %+v", replay, second)
|
||||
}
|
||||
|
||||
live, err := resources.Resource(t.Context(), "scheduling-policy")
|
||||
live, err := resources.Resource(t.Context(), storecontract.DefaultOrgID, "scheduling-policy")
|
||||
if err != nil {
|
||||
t.Fatalf("Resource: %v", err)
|
||||
}
|
||||
@@ -389,7 +399,8 @@ func TestConcurrentResourceWritesAcceptOnlyOneExpectedVersion(t *testing.T) {
|
||||
go func() {
|
||||
<-start
|
||||
_, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "test",
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: "settings", Value: []byte(`{"v":1}`), Actor: "operator", Source: "test",
|
||||
RequestID: requestID, ExpectedVersion: 0,
|
||||
})
|
||||
results <- result{err: err}
|
||||
@@ -413,7 +424,7 @@ func TestConcurrentResourceWritesAcceptOnlyOneExpectedVersion(t *testing.T) {
|
||||
t.Fatalf("concurrent writes = %d succeeded, %d conflicted; want 1 each", succeeded, conflicted)
|
||||
}
|
||||
|
||||
history, err := resources.ResourceHistory(t.Context(), "settings", 0)
|
||||
history, err := resources.ResourceHistory(t.Context(), storecontract.DefaultOrgID, "settings", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ResourceHistory: %v", err)
|
||||
}
|
||||
@@ -429,7 +440,8 @@ func TestResourceWritesRecordFieldLevelAudit(t *testing.T) {
|
||||
put := func(value string, expected int64, request string) {
|
||||
t.Helper()
|
||||
if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(value), Actor: "sam", Source: "archie-ui",
|
||||
OrgID: storecontract.DefaultOrgID,
|
||||
Kind: "settings", Value: []byte(value), Actor: "sam", Source: "archie-ui",
|
||||
RequestID: request, ExpectedVersion: expected,
|
||||
}); err != nil {
|
||||
t.Fatalf("PutResource(%s): %v", value, err)
|
||||
@@ -455,3 +467,66 @@ func TestResourceWritesRecordFieldLevelAudit(t *testing.T) {
|
||||
t.Fatalf("audit =\n%s\nwant\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
|
||||
}
|
||||
}
|
||||
|
||||
// A kind is one document per org: two orgs write the same kind under the same
|
||||
// request ID and each keeps its own value, revision, history and audit trail.
|
||||
func TestResourcesAreIndependentPerOrg(t *testing.T) {
|
||||
resources := resourcesFor(t)
|
||||
at := time.Date(2026, 9, 30, 12, 0, 0, 0, time.UTC)
|
||||
put := func(orgID, value string, expected int64) storecontract.Resource {
|
||||
t.Helper()
|
||||
got, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
OrgID: orgID, Kind: "workflow-definitions", Value: []byte(value), Actor: "operator",
|
||||
Source: "test", RequestID: "shared-" + value, ExpectedVersion: expected, At: at,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("PutResource(%s): %v", orgID, err)
|
||||
}
|
||||
return got
|
||||
}
|
||||
put("acme", `{"v":"a1"}`, 0)
|
||||
acme := put("acme", `{"v":"a2"}`, 1)
|
||||
globex := put("globex", `{"v":"g1"}`, 0)
|
||||
if acme.Version != 2 || globex.Version != 1 {
|
||||
t.Fatalf("versions acme=%d globex=%d, want 2 and 1: orgs must not share a revision counter", acme.Version, globex.Version)
|
||||
}
|
||||
if _, err := resources.PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
OrgID: "globex", Kind: "workflow-definitions", Value: []byte(`{}`), Actor: "operator",
|
||||
Source: "test", RequestID: "stale", ExpectedVersion: 2, At: at,
|
||||
}); !errors.Is(err, storecontract.ErrResourceVersionConflict) {
|
||||
t.Fatalf("globex write expecting acme's version = %v, want ErrResourceVersionConflict", err)
|
||||
}
|
||||
|
||||
live, err := resources.Resource(t.Context(), "globex", "workflow-definitions")
|
||||
if err != nil || string(live.Value) != `{"v":"g1"}` || live.OrgID != "globex" {
|
||||
t.Fatalf("globex Resource = %+v, %v", live, err)
|
||||
}
|
||||
if _, err := resources.Resource(t.Context(), "initech", "workflow-definitions"); !errors.Is(err, storecontract.ErrResourceNotFound) {
|
||||
t.Fatalf("an org that never wrote the kind reads %v, want ErrResourceNotFound", err)
|
||||
}
|
||||
history, err := resources.ResourceHistory(t.Context(), "acme", "workflow-definitions", 0)
|
||||
if err != nil || len(history) != 2 {
|
||||
t.Fatalf("acme history = %d entries, %v; want its own 2", len(history), err)
|
||||
}
|
||||
audit, err := resources.Audit(t.Context(), storecontract.AuditTableResources,
|
||||
[]string{storecontract.ResourceAuditKey("globex", "workflow-definitions")}, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("Audit: %v", err)
|
||||
}
|
||||
for _, entry := range audit {
|
||||
if entry.Version != 1 {
|
||||
t.Fatalf("globex audit entry at version %d: acme's revisions leaked into its trail", entry.Version)
|
||||
}
|
||||
}
|
||||
if len(audit) == 0 {
|
||||
t.Fatal("globex audit is empty, want its first write")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPutResourceRefusesNoOrg(t *testing.T) {
|
||||
if _, err := resourcesFor(t).PutResource(t.Context(), storecontract.ResourceWrite{
|
||||
Kind: "settings", Value: []byte(`{}`), Actor: "operator", Source: "test", RequestID: "r",
|
||||
}); err == nil {
|
||||
t.Fatal("a write naming no org was accepted")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user