feat(policy): add immutable versioned product policy store

This commit is contained in:
shira committed 2026-10-06 01:52:39 +03:00
1 parent 0e2a3b02ab
commit 7a5105acc7
30 files changed
+3893 -37

No files matched your search

@@ -33,6 +33,18 @@ type OutboxEvent struct {
PublishedAt pgtype.Timestamptz
}
type PolicyVersion struct {
ID pgtype.UUID
PolicyKey string
Version int32
Kind string
EffectiveAt pgtype.Timestamptz
Author string
Document []byte
DocumentSha256 []byte
CreatedAt pgtype.Timestamptz
}
type Session struct {
ID pgtype.UUID
UserID pgtype.UUID
@@ -0,0 +1,214 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: policy.sql
package sqlc
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const getEffectivePolicyVersion = `-- name: GetEffectivePolicyVersion :one
SELECT
id,
policy_key,
version,
kind,
effective_at,
author,
document,
document_sha256,
created_at
FROM policy_versions
WHERE
policy_key = $1
AND effective_at <= $2
ORDER BY version DESC
LIMIT 1
`
type GetEffectivePolicyVersionParams struct {
PolicyKey string
At pgtype.Timestamptz
}
// Among the versions of a family already effective at a time, the newest
// publication wins. A version scheduled for later therefore never undoes a
// version published after it. The unique (policy_key, version) index serves
// the backward scan.
func (q *Queries) GetEffectivePolicyVersion(ctx context.Context, arg GetEffectivePolicyVersionParams) (PolicyVersion, error) {
row := q.db.QueryRow(ctx, getEffectivePolicyVersion, arg.PolicyKey, arg.At)
var i PolicyVersion
err := row.Scan(
&i.ID,
&i.PolicyKey,
&i.Version,
&i.Kind,
&i.EffectiveAt,
&i.Author,
&i.Document,
&i.DocumentSha256,
&i.CreatedAt,
)
return i, err
}
const getLatestPolicyVersion = `-- name: GetLatestPolicyVersion :one
SELECT
id,
policy_key,
version,
kind,
effective_at,
author,
document,
document_sha256,
created_at
FROM policy_versions
WHERE policy_key = $1
ORDER BY version DESC
LIMIT 1
`
func (q *Queries) GetLatestPolicyVersion(ctx context.Context, policyKey string) (PolicyVersion, error) {
row := q.db.QueryRow(ctx, getLatestPolicyVersion, policyKey)
var i PolicyVersion
err := row.Scan(
&i.ID,
&i.PolicyKey,
&i.Version,
&i.Kind,
&i.EffectiveAt,
&i.Author,
&i.Document,
&i.DocumentSha256,
&i.CreatedAt,
)
return i, err
}
const getPolicyVersionByID = `-- name: GetPolicyVersionByID :one
SELECT
id,
policy_key,
version,
kind,
effective_at,
author,
document,
document_sha256,
created_at
FROM policy_versions
WHERE id = $1
`
func (q *Queries) GetPolicyVersionByID(ctx context.Context, id pgtype.UUID) (PolicyVersion, error) {
row := q.db.QueryRow(ctx, getPolicyVersionByID, id)
var i PolicyVersion
err := row.Scan(
&i.ID,
&i.PolicyKey,
&i.Version,
&i.Kind,
&i.EffectiveAt,
&i.Author,
&i.Document,
&i.DocumentSha256,
&i.CreatedAt,
)
return i, err
}
const insertPolicyVersion = `-- name: InsertPolicyVersion :one
INSERT INTO policy_versions (
id,
policy_key,
version,
kind,
effective_at,
author,
document,
document_sha256
) VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8
)
ON CONFLICT DO NOTHING
RETURNING
id,
policy_key,
version,
kind,
effective_at,
author,
document,
document_sha256,
created_at
`
type InsertPolicyVersionParams struct {
ID pgtype.UUID
PolicyKey string
Version int32
Kind string
EffectiveAt pgtype.Timestamptz
Author string
Document []byte
DocumentSha256 []byte
}
// Stores a version and returns the stored row with the publication time and
// effective time set by the stamp trigger. A taken ID or version number
// returns no row instead of an error, so a conflict does not abort the
// surrounding transaction. created_at is never taken from the caller, and a
// NULL effective_at means "at publication".
func (q *Queries) InsertPolicyVersion(ctx context.Context, arg InsertPolicyVersionParams) (PolicyVersion, error) {
row := q.db.QueryRow(ctx, insertPolicyVersion,
arg.ID,
arg.PolicyKey,
arg.Version,
arg.Kind,
arg.EffectiveAt,
arg.Author,
arg.Document,
arg.DocumentSha256,
)
var i PolicyVersion
err := row.Scan(
&i.ID,
&i.PolicyKey,
&i.Version,
&i.Kind,
&i.EffectiveAt,
&i.Author,
&i.Document,
&i.DocumentSha256,
&i.CreatedAt,
)
return i, err
}
const policyVersionExists = `-- name: PolicyVersionExists :one
SELECT exists(
SELECT 1 FROM policy_versions
WHERE policy_key = $1
) AS present
`
// Reports whether a family has any version without reading a document, so a
// caller that only needs presence never depends on decoding.
func (q *Queries) PolicyVersionExists(ctx context.Context, policyKey string) (bool, error) {
row := q.db.QueryRow(ctx, policyVersionExists, policyKey)
var present bool
err := row.Scan(&present)
return present, err
}
@@ -33,12 +33,25 @@ type Querier interface {
// statement was locked above.
DeleteUnverifiedUsersRegisteredBefore(ctx context.Context, arg DeleteUnverifiedUsersRegisteredBeforeParams) (int64, error)
GetActiveSessionByTokenHash(ctx context.Context, arg GetActiveSessionByTokenHashParams) (Session, error)
// Among the versions of a family already effective at a time, the newest
// publication wins. A version scheduled for later therefore never undoes a
// version published after it. The unique (policy_key, version) index serves
// the backward scan.
GetEffectivePolicyVersion(ctx context.Context, arg GetEffectivePolicyVersionParams) (PolicyVersion, error)
// Explains why ConsumeEmailToken matched no row.
GetEmailTokenState(ctx context.Context, arg GetEmailTokenStateParams) (GetEmailTokenStateRow, error)
GetLatestPolicyVersion(ctx context.Context, policyKey string) (PolicyVersion, error)
GetPolicyVersionByID(ctx context.Context, id pgtype.UUID) (PolicyVersion, error)
GetUserByEmailNormalized(ctx context.Context, emailNormalized string) (User, error)
GetUserByID(ctx context.Context, id pgtype.UUID) (User, error)
InsertEmailToken(ctx context.Context, arg InsertEmailTokenParams) error
InsertOutboxEvent(ctx context.Context, arg InsertOutboxEventParams) error
// Stores a version and returns the stored row with the publication time and
// effective time set by the stamp trigger. A taken ID or version number
// returns no row instead of an error, so a conflict does not abort the
// surrounding transaction. created_at is never taken from the caller, and a
// NULL effective_at means "at publication".
InsertPolicyVersion(ctx context.Context, arg InsertPolicyVersionParams) (PolicyVersion, error)
InsertSession(ctx context.Context, arg InsertSessionParams) error
InsertUser(ctx context.Context, arg InsertUserParams) error
InvalidateActiveEmailTokens(ctx context.Context, arg InvalidateActiveEmailTokensParams) (int64, error)
@@ -46,6 +59,9 @@ type Querier interface {
// was issued for.
MarkUserEmailVerified(ctx context.Context, arg MarkUserEmailVerifiedParams) (User, error)
Ping(ctx context.Context) (int32, error)
// Reports whether a family has any version without reading a document, so a
// caller that only needs presence never depends on decoding.
PolicyVersionExists(ctx context.Context, policyKey string) (bool, error)
RevokeSession(ctx context.Context, arg RevokeSessionParams) (int64, error)
RevokeUserSessions(ctx context.Context, arg RevokeUserSessionsParams) (int64, error)
// Records activity on an active session and reports whether it is active.