package store import ( "context" "encoding/json" "errors" "github.com/jackc/pgx/v5" ) func scanRoleDataset(row pgx.Row) (*RoleDataset, error) { var d RoleDataset var data []byte if err := row.Scan(&d.TenantID, &data, &d.CatalogueVersion, &d.Revision, &d.UpdatedBy, &d.UpdatedAt); err != nil { if errors.Is(err, pgx.ErrNoRows) { return nil, ErrNotFound } return nil, err } d.Data = json.RawMessage(data) return &d, nil } const roleDatasetSelect = ` SELECT tenant_id::text, data, catalogue_version, revision, updated_by, updated_at FROM role_datasets ` func (p *Postgres) GetRoleDataset(ctx context.Context, tenantID string) (*RoleDataset, error) { d, err := scanRoleDataset(p.pool.QueryRow(ctx, roleDatasetSelect+`WHERE tenant_id = $1::uuid`, tenantID)) if isInvalidUUID(err) { return nil, ErrNotFound } return d, err } func (p *Postgres) GetRoleDatasetRevision(ctx context.Context, tenantID string, revision int) (*RoleDataset, error) { d, err := scanRoleDataset(p.pool.QueryRow(ctx, ` SELECT tenant_id::text, data, catalogue_version, revision, changed_by, changed_at FROM role_dataset_revisions WHERE tenant_id = $1::uuid AND revision = $2`, tenantID, revision)) if isInvalidUUID(err) { return nil, ErrNotFound } return d, err } // SaveRoleDataset writes the dataset and its revision row in one // transaction, with the row locked against a concurrent save. func (p *Postgres) SaveRoleDataset(ctx context.Context, w RoleDatasetWrite) (*RoleDataset, bool, error) { if !json.Valid(w.Data) || w.CatalogueVersion == "" || w.By == "" { return nil, false, ErrInvalidInput } tx, err := p.pool.Begin(ctx) if err != nil { return nil, false, err } defer func() { _ = tx.Rollback(ctx) }() cur, err := scanRoleDataset(tx.QueryRow(ctx, roleDatasetSelect+`WHERE tenant_id = $1::uuid FOR UPDATE`, w.TenantID)) switch { case errors.Is(err, ErrNotFound): cur = nil case isInvalidUUID(err): return nil, false, ErrNotFound case err != nil: return nil, false, err } current := 0 if cur != nil { current = cur.Revision } if w.ExpectedRevision != current { return nil, false, &RevisionConflict{Current: current} } if cur != nil && cur.CatalogueVersion == w.CatalogueVersion && sameJSON(cur.Data, w.Data) { return cur, false, nil } next := current + 1 if cur == nil { _, err = tx.Exec(ctx, ` INSERT INTO role_datasets (tenant_id, data, catalogue_version, revision, updated_by, updated_at) VALUES ($1::uuid, $2::jsonb, $3, $4, $5, $6)`, w.TenantID, string(w.Data), w.CatalogueVersion, next, w.By, w.At.UTC()) } else { _, err = tx.Exec(ctx, ` UPDATE role_datasets SET data = $2::jsonb, catalogue_version = $3, revision = $4, updated_by = $5, updated_at = $6 WHERE tenant_id = $1::uuid`, w.TenantID, string(w.Data), w.CatalogueVersion, next, w.By, w.At.UTC()) } switch { case err == nil: case isFKViolation(err): return nil, false, ErrNotFound case isUniqueViolation(err): // A concurrent first save won: report its revision. return nil, false, &RevisionConflict{Current: 1} case isCheckViolation(err): return nil, false, ErrInvalidInput default: return nil, false, err } if _, err := tx.Exec(ctx, ` INSERT INTO role_dataset_revisions (tenant_id, revision, data, catalogue_version, changed_by, changed_at) SELECT tenant_id, revision, data, catalogue_version, updated_by, updated_at FROM role_datasets WHERE tenant_id = $1::uuid AND revision = $2`, w.TenantID, next); err != nil { return nil, false, err } if err := tx.Commit(ctx); err != nil { return nil, false, err } saved, err := p.GetRoleDataset(ctx, w.TenantID) return saved, true, err }