feat(assessment): add immutable family versions and the demo bank loader
This commit is contained in:
1 parent
94488e2b57
commit
204c6bae62
28 files changed
+6269
-9
No files matched your search
@@ -0,0 +1,308 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
// assessmentConstraintErrors maps the bank constraints and triggers whose
|
||||
// refusal is a caller-visible outcome to port sentinels by SQLSTATE and
|
||||
// constraint name, never by text. Duplicate versions are no errors: the
|
||||
// insert uses ON CONFLICT DO NOTHING.
|
||||
var assessmentConstraintErrors = map[constraintViolation]error{
|
||||
{integrityConstraintViolationCode, "assessment_families_immutable"}: outbound.ErrAssessmentFamilyImmutable,
|
||||
{integrityConstraintViolationCode, "assessment_families_lifecycle_transition"}: outbound.ErrAssessmentFamilyLifecycle,
|
||||
// A second published version of one family: another publication won.
|
||||
{uniqueViolationCode, "assessment_families_one_published_key"}: outbound.ErrAssessmentFamilyLifecycle,
|
||||
{integrityConstraintViolationCode, "assessment_category_openings_immutable"}: outbound.ErrAssessmentFamilyImmutable,
|
||||
{checkViolationCode, "assessment_families_published_requires_review"}: outbound.ErrAssessmentFamilyNotPublishable,
|
||||
{checkViolationCode, "assessment_category_openings_coverage"}: outbound.ErrAssessmentCoverageInsufficient,
|
||||
{checkViolationCode, "assessment_category_openings_policy_thresholds"}: outbound.ErrAssessmentGradingPolicyNotFound,
|
||||
{foreignKeyViolationCode, "assessment_category_openings_policy_version_id_fkey"}: outbound.ErrAssessmentGradingPolicyNotFound,
|
||||
}
|
||||
|
||||
// mapAssessmentError turns a known bank constraint or trigger error into its
|
||||
// port sentinel and keeps the constraint name. Any other PostgreSQL error is
|
||||
// replaced by its SQLSTATE and constraint name, so task content and operator
|
||||
// identities never reach error text. Deadlocks and serialization failures
|
||||
// also match ErrTransactionConflict. A connection error loses the pgx prefix
|
||||
// that names the database user and database.
|
||||
func mapAssessmentError(err error) error {
|
||||
var pgError *pgconn.PgError
|
||||
if !errors.As(err, &pgError) {
|
||||
return sanitizeConnectError(err)
|
||||
}
|
||||
if sentinel, ok := assessmentConstraintErrors[constraintViolation{code: pgError.Code, constraint: pgError.ConstraintName}]; ok {
|
||||
return fmt.Errorf("postgres.mapAssessmentError - violate constraint %s: %w", pgError.ConstraintName, sentinel)
|
||||
}
|
||||
return markTransactionConflict(&databaseError{code: pgError.Code, constraint: pgError.ConstraintName})
|
||||
}
|
||||
|
||||
// AssessmentBankStore provides the assessment bank outside a transaction.
|
||||
// Every call runs as its own statement on a pool connection. Called with a
|
||||
// context that belongs to a Transactor callback, it fails with
|
||||
// outbound.ErrNestedTransaction; use UnitOfWork.AssessmentBank there.
|
||||
type AssessmentBankStore struct {
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
func NewAssessmentBankStore(pool *pgxpool.Pool) *AssessmentBankStore {
|
||||
return &AssessmentBankStore{queries: sqlc.New(standaloneDB{pool: pool})}
|
||||
}
|
||||
|
||||
func (store *AssessmentBankStore) AssessmentBank() outbound.AssessmentBankRepository {
|
||||
return assessmentBankRepository{queries: store.queries, transactional: false}
|
||||
}
|
||||
|
||||
var _ outbound.AssessmentBankRepositories = (*AssessmentBankStore)(nil)
|
||||
var _ outbound.AssessmentBankRepositories = (*UnitOfWork)(nil)
|
||||
|
||||
func (unit *UnitOfWork) AssessmentBank() outbound.AssessmentBankRepository {
|
||||
return assessmentBankRepository{queries: unit.queries, transactional: true}
|
||||
}
|
||||
|
||||
type assessmentBankRepository struct {
|
||||
queries *sqlc.Queries
|
||||
// transactional is true for the repository of a UnitOfWork, the only one
|
||||
// where a transaction-scoped advisory lock lasts.
|
||||
transactional bool
|
||||
}
|
||||
|
||||
var _ outbound.AssessmentBankRepository = assessmentBankRepository{}
|
||||
|
||||
func (repository assessmentBankRepository) CreateFamily(ctx context.Context, draft domain.AssessmentFamilyDraft) (domain.AssessmentFamily, error) {
|
||||
const location = "assessmentBankRepository.CreateFamily"
|
||||
if draft.IsZero() {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("%s - draft was not built by domain.NewAssessmentFamilyDraft: %w", location, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
generator, err := draft.SeedGeneratorJSON()
|
||||
if err != nil {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("%s - encode seed generator: %w", location, err)
|
||||
}
|
||||
answerKey, err := draft.AnswerKeyJSON()
|
||||
if err != nil {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("%s - encode answer key: %w", location, err)
|
||||
}
|
||||
rubric, err := draft.RubricJSON()
|
||||
if err != nil {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("%s - encode rubric: %w", location, err)
|
||||
}
|
||||
core, hash := draft.Core(), draft.ContentHash()
|
||||
reviewer := pgtype.Text{}
|
||||
if core.Reviewer != "" {
|
||||
reviewer = pgtype.Text{String: string(core.Reviewer), Valid: true}
|
||||
}
|
||||
row, err := repository.queries.InsertAssessmentFamily(ctx, sqlc.InsertAssessmentFamilyParams{
|
||||
ID: pgUUID(draft.ID().UUID()),
|
||||
FamilyKey: string(core.Key),
|
||||
Version: core.Version,
|
||||
Category: string(core.Category),
|
||||
Specialization: string(core.Specialization),
|
||||
Title: core.Title,
|
||||
BundleName: string(core.BundleName),
|
||||
Author: string(core.Author),
|
||||
Reviewer: reviewer,
|
||||
ReviewStatus: string(core.ReviewStatus),
|
||||
SeedGenerator: generator,
|
||||
AnswerKey: answerKey,
|
||||
Rubric: rubric,
|
||||
ContentSha256: hash[:],
|
||||
HashScheme: draft.HashScheme(),
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("%s - insert %s version %d: %w", location, core.Key, core.Version, outbound.ErrAssessmentFamilyConflict)
|
||||
}
|
||||
if err != nil {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("%s - insert family: %w", location, mapAssessmentError(err))
|
||||
}
|
||||
return assessmentFamilyFromRow(row)
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) GetFamilyVersion(ctx context.Context, key domain.AssessmentFamilyKey, version int32) (domain.AssessmentFamily, error) {
|
||||
row, err := repository.queries.GetAssessmentFamilyVersion(ctx, sqlc.GetAssessmentFamilyVersionParams{FamilyKey: string(key), Version: version})
|
||||
if err != nil {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("assessmentBankRepository.GetFamilyVersion - select family: %w", mapAssessmentNoRows(err))
|
||||
}
|
||||
return assessmentFamilyFromRow(row)
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) GetLatestFamily(ctx context.Context, key domain.AssessmentFamilyKey) (domain.AssessmentFamily, error) {
|
||||
row, err := repository.queries.GetLatestAssessmentFamily(ctx, string(key))
|
||||
if err != nil {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("assessmentBankRepository.GetLatestFamily - select family: %w", mapAssessmentNoRows(err))
|
||||
}
|
||||
return assessmentFamilyFromRow(row)
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) ListFamilies(
|
||||
ctx context.Context,
|
||||
category domain.AssessmentCategory,
|
||||
after outbound.AssessmentFamilyCursor,
|
||||
limit int32,
|
||||
) ([]domain.AssessmentFamily, error) {
|
||||
if limit < 1 || limit > outbound.MaxAssessmentFamilyPageSize {
|
||||
return nil, fmt.Errorf("assessmentBankRepository.ListFamilies - limit must be between 1 and %d: %w", outbound.MaxAssessmentFamilyPageSize, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
rows, err := repository.queries.ListAssessmentFamiliesByCategory(ctx, sqlc.ListAssessmentFamiliesByCategoryParams{
|
||||
Category: string(category), AfterKey: string(after.Key), AfterVersion: after.Version, PageSize: limit,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("assessmentBankRepository.ListFamilies - select families: %w", mapAssessmentError(err))
|
||||
}
|
||||
families := make([]domain.AssessmentFamily, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
family, err := assessmentFamilyFromRow(row)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
families = append(families, family)
|
||||
}
|
||||
return families, nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) GetPublishedFamily(ctx context.Context, key domain.AssessmentFamilyKey) (domain.AssessmentFamily, error) {
|
||||
row, err := repository.queries.GetPublishedAssessmentFamily(ctx, string(key))
|
||||
if err != nil {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("assessmentBankRepository.GetPublishedFamily - select family: %w", mapAssessmentNoRows(err))
|
||||
}
|
||||
return assessmentFamilyFromRow(row)
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) LockCategory(ctx context.Context, category domain.AssessmentCategory) error {
|
||||
if !repository.transactional {
|
||||
return fmt.Errorf("assessmentBankRepository.LockCategory - lock %s: %w", category, outbound.ErrAssessmentLockOutsideTransaction)
|
||||
}
|
||||
if err := repository.queries.LockAssessmentCategory(ctx, string(category)); err != nil {
|
||||
return fmt.Errorf("assessmentBankRepository.LockCategory - take advisory lock: %w", mapAssessmentError(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) CountPublishedFamilies(ctx context.Context, category domain.AssessmentCategory) (int32, error) {
|
||||
count, err := repository.queries.CountPublishedAssessmentFamilies(ctx, string(category))
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("assessmentBankRepository.CountPublishedFamilies - count families: %w", mapAssessmentError(err))
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) MarkPublished(ctx context.Context, id domain.AssessmentFamilyID, by domain.AssessmentOperator) (domain.AssessmentFamily, error) {
|
||||
row, err := repository.queries.PublishAssessmentFamily(ctx, sqlc.PublishAssessmentFamilyParams{ID: pgUUID(id.UUID()), Actor: pgText(string(by))})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("assessmentBankRepository.MarkPublished - publish %s: %w", id, outbound.ErrAssessmentFamilyLifecycle)
|
||||
}
|
||||
if err != nil {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("assessmentBankRepository.MarkPublished - update family: %w", mapAssessmentError(err))
|
||||
}
|
||||
return assessmentFamilyFromRow(row)
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) MarkRetired(ctx context.Context, id domain.AssessmentFamilyID, by domain.AssessmentOperator) (domain.AssessmentFamily, error) {
|
||||
row, err := repository.queries.RetireAssessmentFamily(ctx, sqlc.RetireAssessmentFamilyParams{ID: pgUUID(id.UUID()), Actor: pgText(string(by))})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("assessmentBankRepository.MarkRetired - retire %s: %w", id, outbound.ErrAssessmentFamilyLifecycle)
|
||||
}
|
||||
if err != nil {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("assessmentBankRepository.MarkRetired - update family: %w", mapAssessmentError(err))
|
||||
}
|
||||
return assessmentFamilyFromRow(row)
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) CreateCategoryOpening(ctx context.Context, draft domain.AssessmentCategoryOpeningDraft) (domain.AssessmentCategoryOpening, error) {
|
||||
if draft.IsZero() {
|
||||
return domain.AssessmentCategoryOpening{}, fmt.Errorf("assessmentBankRepository.CreateCategoryOpening - draft was not built by domain.NewAssessmentCategoryOpeningDraft: %w", domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
row, err := repository.queries.InsertAssessmentCategoryOpening(ctx, sqlc.InsertAssessmentCategoryOpeningParams{
|
||||
ID: pgUUID(draft.ID()),
|
||||
Category: string(draft.Category()),
|
||||
PolicyVersionID: pgUUID(draft.PolicyVersionID().UUID()),
|
||||
OpenedBy: string(draft.OpenedBy()),
|
||||
})
|
||||
if err != nil {
|
||||
return domain.AssessmentCategoryOpening{}, fmt.Errorf("assessmentBankRepository.CreateCategoryOpening - insert opening: %w", mapAssessmentError(err))
|
||||
}
|
||||
return assessmentOpeningFromRow(row), nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) DatabaseTime(ctx context.Context) (time.Time, error) {
|
||||
now, err := repository.queries.GetDatabaseTime(ctx)
|
||||
if err != nil {
|
||||
return time.Time{}, fmt.Errorf("assessmentBankRepository.DatabaseTime - select clock: %w", mapAssessmentError(err))
|
||||
}
|
||||
return now.Time, nil
|
||||
}
|
||||
|
||||
func (repository assessmentBankRepository) GetCurrentCategoryOpening(ctx context.Context, category domain.AssessmentCategory) (domain.AssessmentCategoryOpening, error) {
|
||||
row, err := repository.queries.GetCurrentAssessmentCategoryOpening(ctx, string(category))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.AssessmentCategoryOpening{}, fmt.Errorf("assessmentBankRepository.GetCurrentCategoryOpening - select opening: %w", outbound.ErrAssessmentCategoryNotOpen)
|
||||
}
|
||||
if err != nil {
|
||||
return domain.AssessmentCategoryOpening{}, fmt.Errorf("assessmentBankRepository.GetCurrentCategoryOpening - select opening: %w", mapAssessmentError(err))
|
||||
}
|
||||
return assessmentOpeningFromRow(row), nil
|
||||
}
|
||||
|
||||
func pgText(value string) pgtype.Text {
|
||||
return pgtype.Text{String: value, Valid: true}
|
||||
}
|
||||
|
||||
func mapAssessmentNoRows(err error) error {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return outbound.ErrAssessmentFamilyNotFound
|
||||
}
|
||||
return mapAssessmentError(err)
|
||||
}
|
||||
|
||||
func assessmentFamilyFromRow(row sqlc.AssessmentFamily) (domain.AssessmentFamily, error) {
|
||||
family, err := domain.RestoreAssessmentFamily(domain.StoredAssessmentFamily{
|
||||
ID: domain.AssessmentFamilyID(row.ID.Bytes),
|
||||
FamilyKey: row.FamilyKey,
|
||||
Version: row.Version,
|
||||
Category: row.Category,
|
||||
Specialization: row.Specialization,
|
||||
Title: row.Title,
|
||||
BundleName: row.BundleName,
|
||||
Author: row.Author,
|
||||
Reviewer: row.Reviewer.String,
|
||||
ReviewStatus: row.ReviewStatus,
|
||||
SeedGenerator: row.SeedGenerator,
|
||||
AnswerKey: row.AnswerKey,
|
||||
Rubric: row.Rubric,
|
||||
ContentHash: row.ContentSha256,
|
||||
HashScheme: row.HashScheme,
|
||||
Lifecycle: row.Lifecycle,
|
||||
CreatedAt: row.CreatedAt.Time,
|
||||
PublishedAt: row.PublishedAt.Time,
|
||||
PublishedBy: row.PublishedBy.String,
|
||||
RetiredAt: row.RetiredAt.Time,
|
||||
RetiredBy: row.RetiredBy.String,
|
||||
})
|
||||
if err != nil {
|
||||
return domain.AssessmentFamily{}, fmt.Errorf("postgres.assessmentFamilyFromRow - restore family: %w", err)
|
||||
}
|
||||
return family, nil
|
||||
}
|
||||
|
||||
func assessmentOpeningFromRow(row sqlc.AssessmentCategoryOpening) domain.AssessmentCategoryOpening {
|
||||
return domain.AssessmentCategoryOpening{
|
||||
ID: row.ID.Bytes,
|
||||
Category: domain.AssessmentCategory(row.Category),
|
||||
PolicyVersionID: domain.PolicyVersionID(row.PolicyVersionID.Bytes),
|
||||
OpenedBy: domain.AssessmentOperator(row.OpenedBy),
|
||||
RequiredFamilies: row.RequiredFamilies,
|
||||
PublishedFamilies: row.PublishedFamilies,
|
||||
OpenedAt: row.OpenedAt.Time,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
func TestMapAssessmentErrorRecognizesBankConstraints(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
code string
|
||||
constraint string
|
||||
want error
|
||||
}{
|
||||
{code: integrityConstraintViolationCode, constraint: "assessment_families_immutable", want: outbound.ErrAssessmentFamilyImmutable},
|
||||
{code: integrityConstraintViolationCode, constraint: "assessment_families_lifecycle_transition", want: outbound.ErrAssessmentFamilyLifecycle},
|
||||
{code: integrityConstraintViolationCode, constraint: "assessment_category_openings_immutable", want: outbound.ErrAssessmentFamilyImmutable},
|
||||
{code: uniqueViolationCode, constraint: "assessment_families_one_published_key", want: outbound.ErrAssessmentFamilyLifecycle},
|
||||
{code: checkViolationCode, constraint: "assessment_category_openings_policy_thresholds", want: outbound.ErrAssessmentGradingPolicyNotFound},
|
||||
{code: checkViolationCode, constraint: "assessment_families_published_requires_review", want: outbound.ErrAssessmentFamilyNotPublishable},
|
||||
{code: checkViolationCode, constraint: "assessment_category_openings_coverage", want: outbound.ErrAssessmentCoverageInsufficient},
|
||||
{code: foreignKeyViolationCode, constraint: "assessment_category_openings_policy_version_id_fkey", want: outbound.ErrAssessmentGradingPolicyNotFound},
|
||||
} {
|
||||
err := mapAssessmentError(&pgconn.PgError{Code: test.code, ConstraintName: test.constraint, Detail: "Failing row contains (secret task content)."})
|
||||
if !errors.Is(err, test.want) {
|
||||
t.Fatalf("mapAssessmentError(%s %s) = %v, want %v", test.code, test.constraint, err, test.want)
|
||||
}
|
||||
if strings.Contains(err.Error(), "secret task content") {
|
||||
t.Fatalf("mapAssessmentError(%s) = %v, want no row values", test.constraint, err)
|
||||
}
|
||||
}
|
||||
other := mapAssessmentError(&pgconn.PgError{Code: checkViolationCode, ConstraintName: "assessment_families_title_format", Detail: "Failing row contains (secret task content)."})
|
||||
if strings.Contains(other.Error(), "secret task content") {
|
||||
t.Fatalf("mapAssessmentError(other) = %v, want only SQLSTATE and constraint", other)
|
||||
}
|
||||
if err := mapAssessmentError(&pgconn.PgError{Code: deadlockDetectedCode}); !errors.Is(err, outbound.ErrTransactionConflict) {
|
||||
t.Fatalf("mapAssessmentError(deadlock) = %v, want ErrTransactionConflict", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLockCategoryWorksOnlyInsideATransaction(t *testing.T) {
|
||||
// A repository without queries proves that no statement runs.
|
||||
standalone := assessmentBankRepository{transactional: false}
|
||||
if err := standalone.LockCategory(t.Context(), "backend"); !errors.Is(err, outbound.ErrAssessmentLockOutsideTransaction) {
|
||||
t.Fatalf("LockCategory(standalone) error = %v, want ErrAssessmentLockOutsideTransaction", err)
|
||||
}
|
||||
if store := NewAssessmentBankStore(nil).AssessmentBank(); !errors.Is(store.LockCategory(t.Context(), "backend"), outbound.ErrAssessmentLockOutsideTransaction) {
|
||||
t.Fatal("the standalone store must refuse LockCategory")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAssessmentBankRejectsUnbuiltDraftsBeforeQuerying(t *testing.T) {
|
||||
// A repository without queries proves that no statement runs.
|
||||
repository := assessmentBankRepository{}
|
||||
if _, err := repository.CreateFamily(t.Context(), domain.AssessmentFamilyDraft{}); !errors.Is(err, domain.ErrInvalidAssessmentFamily) {
|
||||
t.Fatalf("CreateFamily(zero draft) error = %v, want ErrInvalidAssessmentFamily", err)
|
||||
}
|
||||
for _, limit := range []int32{0, -1, outbound.MaxAssessmentFamilyPageSize + 1} {
|
||||
if _, err := repository.ListFamilies(t.Context(), "backend", outbound.AssessmentFamilyCursor{}, limit); !errors.Is(err, domain.ErrInvalidAssessmentFamily) {
|
||||
t.Fatalf("ListFamilies(limit %d) error = %v, want ErrInvalidAssessmentFamily", limit, err)
|
||||
}
|
||||
}
|
||||
if _, err := repository.CreateCategoryOpening(t.Context(), domain.AssessmentCategoryOpeningDraft{}); !errors.Is(err, domain.ErrInvalidAssessmentFamily) {
|
||||
t.Fatalf("CreateCategoryOpening(zero draft) error = %v, want ErrInvalidAssessmentFamily", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,733 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: assessment_bank.sql
|
||||
|
||||
package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const countPublishedAssessmentFamilies = `-- name: CountPublishedAssessmentFamilies :one
|
||||
SELECT count(DISTINCT family_key)::integer AS published
|
||||
FROM assessment_families
|
||||
WHERE
|
||||
category = $1
|
||||
AND lifecycle = 'published'
|
||||
`
|
||||
|
||||
// Counts the families of a category that have at least one published
|
||||
// version, however many versions each has.
|
||||
func (q *Queries) CountPublishedAssessmentFamilies(ctx context.Context, category string) (int32, error) {
|
||||
row := q.db.QueryRow(ctx, countPublishedAssessmentFamilies, category)
|
||||
var published int32
|
||||
err := row.Scan(&published)
|
||||
return published, err
|
||||
}
|
||||
|
||||
const getAssessmentFamilyByID = `-- name: GetAssessmentFamilyByID :one
|
||||
SELECT
|
||||
id,
|
||||
family_key,
|
||||
version,
|
||||
category,
|
||||
specialization,
|
||||
title,
|
||||
bundle_name,
|
||||
author,
|
||||
reviewer,
|
||||
review_status,
|
||||
seed_generator,
|
||||
answer_key,
|
||||
rubric,
|
||||
content_sha256,
|
||||
hash_scheme,
|
||||
lifecycle,
|
||||
created_at,
|
||||
published_at,
|
||||
published_by,
|
||||
retired_at,
|
||||
retired_by
|
||||
FROM assessment_families
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetAssessmentFamilyByID(ctx context.Context, id pgtype.UUID) (AssessmentFamily, error) {
|
||||
row := q.db.QueryRow(ctx, getAssessmentFamilyByID, id)
|
||||
var i AssessmentFamily
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.FamilyKey,
|
||||
&i.Version,
|
||||
&i.Category,
|
||||
&i.Specialization,
|
||||
&i.Title,
|
||||
&i.BundleName,
|
||||
&i.Author,
|
||||
&i.Reviewer,
|
||||
&i.ReviewStatus,
|
||||
&i.SeedGenerator,
|
||||
&i.AnswerKey,
|
||||
&i.Rubric,
|
||||
&i.ContentSha256,
|
||||
&i.HashScheme,
|
||||
&i.Lifecycle,
|
||||
&i.CreatedAt,
|
||||
&i.PublishedAt,
|
||||
&i.PublishedBy,
|
||||
&i.RetiredAt,
|
||||
&i.RetiredBy,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getAssessmentFamilyVersion = `-- name: GetAssessmentFamilyVersion :one
|
||||
SELECT
|
||||
id,
|
||||
family_key,
|
||||
version,
|
||||
category,
|
||||
specialization,
|
||||
title,
|
||||
bundle_name,
|
||||
author,
|
||||
reviewer,
|
||||
review_status,
|
||||
seed_generator,
|
||||
answer_key,
|
||||
rubric,
|
||||
content_sha256,
|
||||
hash_scheme,
|
||||
lifecycle,
|
||||
created_at,
|
||||
published_at,
|
||||
published_by,
|
||||
retired_at,
|
||||
retired_by
|
||||
FROM assessment_families
|
||||
WHERE
|
||||
family_key = $1
|
||||
AND version = $2
|
||||
`
|
||||
|
||||
type GetAssessmentFamilyVersionParams struct {
|
||||
FamilyKey string
|
||||
Version int32
|
||||
}
|
||||
|
||||
func (q *Queries) GetAssessmentFamilyVersion(ctx context.Context, arg GetAssessmentFamilyVersionParams) (AssessmentFamily, error) {
|
||||
row := q.db.QueryRow(ctx, getAssessmentFamilyVersion, arg.FamilyKey, arg.Version)
|
||||
var i AssessmentFamily
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.FamilyKey,
|
||||
&i.Version,
|
||||
&i.Category,
|
||||
&i.Specialization,
|
||||
&i.Title,
|
||||
&i.BundleName,
|
||||
&i.Author,
|
||||
&i.Reviewer,
|
||||
&i.ReviewStatus,
|
||||
&i.SeedGenerator,
|
||||
&i.AnswerKey,
|
||||
&i.Rubric,
|
||||
&i.ContentSha256,
|
||||
&i.HashScheme,
|
||||
&i.Lifecycle,
|
||||
&i.CreatedAt,
|
||||
&i.PublishedAt,
|
||||
&i.PublishedBy,
|
||||
&i.RetiredAt,
|
||||
&i.RetiredBy,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getCurrentAssessmentCategoryOpening = `-- name: GetCurrentAssessmentCategoryOpening :one
|
||||
SELECT
|
||||
id,
|
||||
category,
|
||||
policy_version_id,
|
||||
opened_by,
|
||||
required_families,
|
||||
published_families,
|
||||
opened_at
|
||||
FROM assessment_category_openings
|
||||
WHERE category = $1
|
||||
ORDER BY opened_at DESC, id DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetCurrentAssessmentCategoryOpening(ctx context.Context, category string) (AssessmentCategoryOpening, error) {
|
||||
row := q.db.QueryRow(ctx, getCurrentAssessmentCategoryOpening, category)
|
||||
var i AssessmentCategoryOpening
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Category,
|
||||
&i.PolicyVersionID,
|
||||
&i.OpenedBy,
|
||||
&i.RequiredFamilies,
|
||||
&i.PublishedFamilies,
|
||||
&i.OpenedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getDatabaseTime = `-- name: GetDatabaseTime :one
|
||||
SELECT clock_timestamp()::timestamptz AS now
|
||||
`
|
||||
|
||||
// The database clock, which stamps the effective time of a policy version, so
|
||||
// "the version in force now" is judged against the same clock.
|
||||
func (q *Queries) GetDatabaseTime(ctx context.Context) (pgtype.Timestamptz, error) {
|
||||
row := q.db.QueryRow(ctx, getDatabaseTime)
|
||||
var now pgtype.Timestamptz
|
||||
err := row.Scan(&now)
|
||||
return now, err
|
||||
}
|
||||
|
||||
const getLatestAssessmentFamily = `-- name: GetLatestAssessmentFamily :one
|
||||
SELECT
|
||||
id,
|
||||
family_key,
|
||||
version,
|
||||
category,
|
||||
specialization,
|
||||
title,
|
||||
bundle_name,
|
||||
author,
|
||||
reviewer,
|
||||
review_status,
|
||||
seed_generator,
|
||||
answer_key,
|
||||
rubric,
|
||||
content_sha256,
|
||||
hash_scheme,
|
||||
lifecycle,
|
||||
created_at,
|
||||
published_at,
|
||||
published_by,
|
||||
retired_at,
|
||||
retired_by
|
||||
FROM assessment_families
|
||||
WHERE family_key = $1
|
||||
ORDER BY version DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) GetLatestAssessmentFamily(ctx context.Context, familyKey string) (AssessmentFamily, error) {
|
||||
row := q.db.QueryRow(ctx, getLatestAssessmentFamily, familyKey)
|
||||
var i AssessmentFamily
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.FamilyKey,
|
||||
&i.Version,
|
||||
&i.Category,
|
||||
&i.Specialization,
|
||||
&i.Title,
|
||||
&i.BundleName,
|
||||
&i.Author,
|
||||
&i.Reviewer,
|
||||
&i.ReviewStatus,
|
||||
&i.SeedGenerator,
|
||||
&i.AnswerKey,
|
||||
&i.Rubric,
|
||||
&i.ContentSha256,
|
||||
&i.HashScheme,
|
||||
&i.Lifecycle,
|
||||
&i.CreatedAt,
|
||||
&i.PublishedAt,
|
||||
&i.PublishedBy,
|
||||
&i.RetiredAt,
|
||||
&i.RetiredBy,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getPublishedAssessmentFamily = `-- name: GetPublishedAssessmentFamily :one
|
||||
SELECT
|
||||
id,
|
||||
family_key,
|
||||
version,
|
||||
category,
|
||||
specialization,
|
||||
title,
|
||||
bundle_name,
|
||||
author,
|
||||
reviewer,
|
||||
review_status,
|
||||
seed_generator,
|
||||
answer_key,
|
||||
rubric,
|
||||
content_sha256,
|
||||
hash_scheme,
|
||||
lifecycle,
|
||||
created_at,
|
||||
published_at,
|
||||
published_by,
|
||||
retired_at,
|
||||
retired_by
|
||||
FROM assessment_families
|
||||
WHERE
|
||||
family_key = $1
|
||||
AND lifecycle = 'published'
|
||||
`
|
||||
|
||||
// The published version of a family, if any. The partial unique index
|
||||
// assessment_families_one_published_key allows at most one.
|
||||
func (q *Queries) GetPublishedAssessmentFamily(ctx context.Context, familyKey string) (AssessmentFamily, error) {
|
||||
row := q.db.QueryRow(ctx, getPublishedAssessmentFamily, familyKey)
|
||||
var i AssessmentFamily
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.FamilyKey,
|
||||
&i.Version,
|
||||
&i.Category,
|
||||
&i.Specialization,
|
||||
&i.Title,
|
||||
&i.BundleName,
|
||||
&i.Author,
|
||||
&i.Reviewer,
|
||||
&i.ReviewStatus,
|
||||
&i.SeedGenerator,
|
||||
&i.AnswerKey,
|
||||
&i.Rubric,
|
||||
&i.ContentSha256,
|
||||
&i.HashScheme,
|
||||
&i.Lifecycle,
|
||||
&i.CreatedAt,
|
||||
&i.PublishedAt,
|
||||
&i.PublishedBy,
|
||||
&i.RetiredAt,
|
||||
&i.RetiredBy,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertAssessmentCategoryOpening = `-- name: InsertAssessmentCategoryOpening :one
|
||||
INSERT INTO assessment_category_openings (
|
||||
id,
|
||||
category,
|
||||
policy_version_id,
|
||||
opened_by,
|
||||
required_families,
|
||||
published_families,
|
||||
opened_at
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
0,
|
||||
0,
|
||||
clock_timestamp()
|
||||
)
|
||||
RETURNING
|
||||
id,
|
||||
category,
|
||||
policy_version_id,
|
||||
opened_by,
|
||||
required_families,
|
||||
published_families,
|
||||
opened_at
|
||||
`
|
||||
|
||||
type InsertAssessmentCategoryOpeningParams struct {
|
||||
ID pgtype.UUID
|
||||
Category string
|
||||
PolicyVersionID pgtype.UUID
|
||||
OpenedBy string
|
||||
}
|
||||
|
||||
// Stores an opening. The insert trigger derives required_families from the
|
||||
// policy document and counts the published families, and the coverage CHECK
|
||||
// refuses the row when they are fewer; both numbers from the caller are
|
||||
// replaced by the trigger's.
|
||||
func (q *Queries) InsertAssessmentCategoryOpening(ctx context.Context, arg InsertAssessmentCategoryOpeningParams) (AssessmentCategoryOpening, error) {
|
||||
row := q.db.QueryRow(ctx, insertAssessmentCategoryOpening,
|
||||
arg.ID,
|
||||
arg.Category,
|
||||
arg.PolicyVersionID,
|
||||
arg.OpenedBy,
|
||||
)
|
||||
var i AssessmentCategoryOpening
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Category,
|
||||
&i.PolicyVersionID,
|
||||
&i.OpenedBy,
|
||||
&i.RequiredFamilies,
|
||||
&i.PublishedFamilies,
|
||||
&i.OpenedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const insertAssessmentFamily = `-- name: InsertAssessmentFamily :one
|
||||
INSERT INTO assessment_families (
|
||||
id,
|
||||
family_key,
|
||||
version,
|
||||
category,
|
||||
specialization,
|
||||
title,
|
||||
bundle_name,
|
||||
author,
|
||||
reviewer,
|
||||
review_status,
|
||||
seed_generator,
|
||||
answer_key,
|
||||
rubric,
|
||||
content_sha256,
|
||||
hash_scheme,
|
||||
lifecycle
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7,
|
||||
$8,
|
||||
$9,
|
||||
$10,
|
||||
$11,
|
||||
$12,
|
||||
$13,
|
||||
$14,
|
||||
$15,
|
||||
'draft'
|
||||
)
|
||||
ON CONFLICT DO NOTHING
|
||||
RETURNING
|
||||
id,
|
||||
family_key,
|
||||
version,
|
||||
category,
|
||||
specialization,
|
||||
title,
|
||||
bundle_name,
|
||||
author,
|
||||
reviewer,
|
||||
review_status,
|
||||
seed_generator,
|
||||
answer_key,
|
||||
rubric,
|
||||
content_sha256,
|
||||
hash_scheme,
|
||||
lifecycle,
|
||||
created_at,
|
||||
published_at,
|
||||
published_by,
|
||||
retired_at,
|
||||
retired_by
|
||||
`
|
||||
|
||||
type InsertAssessmentFamilyParams struct {
|
||||
ID pgtype.UUID
|
||||
FamilyKey string
|
||||
Version int32
|
||||
Category string
|
||||
Specialization string
|
||||
Title string
|
||||
BundleName string
|
||||
Author string
|
||||
Reviewer pgtype.Text
|
||||
ReviewStatus string
|
||||
SeedGenerator []byte
|
||||
AnswerKey []byte
|
||||
Rubric []byte
|
||||
ContentSha256 []byte
|
||||
HashScheme int16
|
||||
}
|
||||
|
||||
// Stores a new family version as a draft and returns the stored row with the
|
||||
// creation time set by the stamp trigger. A taken ID or (family_key, version)
|
||||
// returns no row instead of an error, so a conflict does not abort the
|
||||
// surrounding transaction.
|
||||
func (q *Queries) InsertAssessmentFamily(ctx context.Context, arg InsertAssessmentFamilyParams) (AssessmentFamily, error) {
|
||||
row := q.db.QueryRow(ctx, insertAssessmentFamily,
|
||||
arg.ID,
|
||||
arg.FamilyKey,
|
||||
arg.Version,
|
||||
arg.Category,
|
||||
arg.Specialization,
|
||||
arg.Title,
|
||||
arg.BundleName,
|
||||
arg.Author,
|
||||
arg.Reviewer,
|
||||
arg.ReviewStatus,
|
||||
arg.SeedGenerator,
|
||||
arg.AnswerKey,
|
||||
arg.Rubric,
|
||||
arg.ContentSha256,
|
||||
arg.HashScheme,
|
||||
)
|
||||
var i AssessmentFamily
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.FamilyKey,
|
||||
&i.Version,
|
||||
&i.Category,
|
||||
&i.Specialization,
|
||||
&i.Title,
|
||||
&i.BundleName,
|
||||
&i.Author,
|
||||
&i.Reviewer,
|
||||
&i.ReviewStatus,
|
||||
&i.SeedGenerator,
|
||||
&i.AnswerKey,
|
||||
&i.Rubric,
|
||||
&i.ContentSha256,
|
||||
&i.HashScheme,
|
||||
&i.Lifecycle,
|
||||
&i.CreatedAt,
|
||||
&i.PublishedAt,
|
||||
&i.PublishedBy,
|
||||
&i.RetiredAt,
|
||||
&i.RetiredBy,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listAssessmentFamiliesByCategory = `-- name: ListAssessmentFamiliesByCategory :many
|
||||
SELECT
|
||||
id,
|
||||
family_key,
|
||||
version,
|
||||
category,
|
||||
specialization,
|
||||
title,
|
||||
bundle_name,
|
||||
author,
|
||||
reviewer,
|
||||
review_status,
|
||||
seed_generator,
|
||||
answer_key,
|
||||
rubric,
|
||||
content_sha256,
|
||||
hash_scheme,
|
||||
lifecycle,
|
||||
created_at,
|
||||
published_at,
|
||||
published_by,
|
||||
retired_at,
|
||||
retired_by
|
||||
FROM assessment_families
|
||||
WHERE
|
||||
category = $1
|
||||
AND (family_key, version) > ($2::text, $3::integer)
|
||||
ORDER BY family_key ASC, version ASC
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
type ListAssessmentFamiliesByCategoryParams struct {
|
||||
Category string
|
||||
AfterKey string
|
||||
AfterVersion int32
|
||||
PageSize int32
|
||||
}
|
||||
|
||||
// One page of the versions of a category in key and version order, after the
|
||||
// given position; an empty key and version 0 start at the beginning. The
|
||||
// (family_key, version) unique index serves the scan.
|
||||
func (q *Queries) ListAssessmentFamiliesByCategory(ctx context.Context, arg ListAssessmentFamiliesByCategoryParams) ([]AssessmentFamily, error) {
|
||||
rows, err := q.db.Query(ctx, listAssessmentFamiliesByCategory,
|
||||
arg.Category,
|
||||
arg.AfterKey,
|
||||
arg.AfterVersion,
|
||||
arg.PageSize,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []AssessmentFamily
|
||||
for rows.Next() {
|
||||
var i AssessmentFamily
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.FamilyKey,
|
||||
&i.Version,
|
||||
&i.Category,
|
||||
&i.Specialization,
|
||||
&i.Title,
|
||||
&i.BundleName,
|
||||
&i.Author,
|
||||
&i.Reviewer,
|
||||
&i.ReviewStatus,
|
||||
&i.SeedGenerator,
|
||||
&i.AnswerKey,
|
||||
&i.Rubric,
|
||||
&i.ContentSha256,
|
||||
&i.HashScheme,
|
||||
&i.Lifecycle,
|
||||
&i.CreatedAt,
|
||||
&i.PublishedAt,
|
||||
&i.PublishedBy,
|
||||
&i.RetiredAt,
|
||||
&i.RetiredBy,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const lockAssessmentCategory = `-- name: LockAssessmentCategory :exec
|
||||
SELECT pg_advisory_xact_lock(
|
||||
hashtextextended('lincodex.assessment.category:' || $1::text, 0)
|
||||
)
|
||||
`
|
||||
|
||||
// Serializes the operations that change which versions of a category are
|
||||
// published or open the category, until the transaction ends. The key is
|
||||
// namespaced, so it cannot collide with another use of advisory locks.
|
||||
func (q *Queries) LockAssessmentCategory(ctx context.Context, category string) error {
|
||||
_, err := q.db.Exec(ctx, lockAssessmentCategory, category)
|
||||
return err
|
||||
}
|
||||
|
||||
const publishAssessmentFamily = `-- name: PublishAssessmentFamily :one
|
||||
UPDATE assessment_families
|
||||
SET
|
||||
lifecycle = 'published',
|
||||
published_by = $1
|
||||
WHERE
|
||||
id = $2
|
||||
AND lifecycle = 'draft'
|
||||
RETURNING
|
||||
id,
|
||||
family_key,
|
||||
version,
|
||||
category,
|
||||
specialization,
|
||||
title,
|
||||
bundle_name,
|
||||
author,
|
||||
reviewer,
|
||||
review_status,
|
||||
seed_generator,
|
||||
answer_key,
|
||||
rubric,
|
||||
content_sha256,
|
||||
hash_scheme,
|
||||
lifecycle,
|
||||
created_at,
|
||||
published_at,
|
||||
published_by,
|
||||
retired_at,
|
||||
retired_by
|
||||
`
|
||||
|
||||
type PublishAssessmentFamilyParams struct {
|
||||
Actor pgtype.Text
|
||||
ID pgtype.UUID
|
||||
}
|
||||
|
||||
// Moves a draft to published by the named operator and returns the row with
|
||||
// the publication time set by the guard trigger. No row means the version
|
||||
// was not a draft any more.
|
||||
func (q *Queries) PublishAssessmentFamily(ctx context.Context, arg PublishAssessmentFamilyParams) (AssessmentFamily, error) {
|
||||
row := q.db.QueryRow(ctx, publishAssessmentFamily, arg.Actor, arg.ID)
|
||||
var i AssessmentFamily
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.FamilyKey,
|
||||
&i.Version,
|
||||
&i.Category,
|
||||
&i.Specialization,
|
||||
&i.Title,
|
||||
&i.BundleName,
|
||||
&i.Author,
|
||||
&i.Reviewer,
|
||||
&i.ReviewStatus,
|
||||
&i.SeedGenerator,
|
||||
&i.AnswerKey,
|
||||
&i.Rubric,
|
||||
&i.ContentSha256,
|
||||
&i.HashScheme,
|
||||
&i.Lifecycle,
|
||||
&i.CreatedAt,
|
||||
&i.PublishedAt,
|
||||
&i.PublishedBy,
|
||||
&i.RetiredAt,
|
||||
&i.RetiredBy,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const retireAssessmentFamily = `-- name: RetireAssessmentFamily :one
|
||||
UPDATE assessment_families
|
||||
SET
|
||||
lifecycle = 'retired',
|
||||
retired_by = $1
|
||||
WHERE
|
||||
id = $2
|
||||
AND lifecycle = 'published'
|
||||
RETURNING
|
||||
id,
|
||||
family_key,
|
||||
version,
|
||||
category,
|
||||
specialization,
|
||||
title,
|
||||
bundle_name,
|
||||
author,
|
||||
reviewer,
|
||||
review_status,
|
||||
seed_generator,
|
||||
answer_key,
|
||||
rubric,
|
||||
content_sha256,
|
||||
hash_scheme,
|
||||
lifecycle,
|
||||
created_at,
|
||||
published_at,
|
||||
published_by,
|
||||
retired_at,
|
||||
retired_by
|
||||
`
|
||||
|
||||
type RetireAssessmentFamilyParams struct {
|
||||
Actor pgtype.Text
|
||||
ID pgtype.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) RetireAssessmentFamily(ctx context.Context, arg RetireAssessmentFamilyParams) (AssessmentFamily, error) {
|
||||
row := q.db.QueryRow(ctx, retireAssessmentFamily, arg.Actor, arg.ID)
|
||||
var i AssessmentFamily
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.FamilyKey,
|
||||
&i.Version,
|
||||
&i.Category,
|
||||
&i.Specialization,
|
||||
&i.Title,
|
||||
&i.BundleName,
|
||||
&i.Author,
|
||||
&i.Reviewer,
|
||||
&i.ReviewStatus,
|
||||
&i.SeedGenerator,
|
||||
&i.AnswerKey,
|
||||
&i.Rubric,
|
||||
&i.ContentSha256,
|
||||
&i.HashScheme,
|
||||
&i.Lifecycle,
|
||||
&i.CreatedAt,
|
||||
&i.PublishedAt,
|
||||
&i.PublishedBy,
|
||||
&i.RetiredAt,
|
||||
&i.RetiredBy,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -8,6 +8,40 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type AssessmentCategoryOpening struct {
|
||||
ID pgtype.UUID
|
||||
Category string
|
||||
PolicyVersionID pgtype.UUID
|
||||
OpenedBy string
|
||||
RequiredFamilies int32
|
||||
PublishedFamilies int32
|
||||
OpenedAt pgtype.Timestamptz
|
||||
}
|
||||
|
||||
type AssessmentFamily struct {
|
||||
ID pgtype.UUID
|
||||
FamilyKey string
|
||||
Version int32
|
||||
Category string
|
||||
Specialization string
|
||||
Title string
|
||||
BundleName string
|
||||
Author string
|
||||
Reviewer pgtype.Text
|
||||
ReviewStatus string
|
||||
SeedGenerator []byte
|
||||
AnswerKey []byte
|
||||
Rubric []byte
|
||||
ContentSha256 []byte
|
||||
HashScheme int16
|
||||
Lifecycle string
|
||||
CreatedAt pgtype.Timestamptz
|
||||
PublishedAt pgtype.Timestamptz
|
||||
PublishedBy pgtype.Text
|
||||
RetiredAt pgtype.Timestamptz
|
||||
RetiredBy pgtype.Text
|
||||
}
|
||||
|
||||
type ConsentRecord struct {
|
||||
ID pgtype.UUID
|
||||
SubjectID pgtype.UUID
|
||||
|
||||
@@ -48,6 +48,9 @@ type Querier interface {
|
||||
// returned registration version in the same transaction, rolled back when it
|
||||
// matches no row.
|
||||
ConsumeEmailToken(ctx context.Context, arg ConsumeEmailTokenParams) (EmailToken, error)
|
||||
// Counts the families of a category that have at least one published
|
||||
// version, however many versions each has.
|
||||
CountPublishedAssessmentFamilies(ctx context.Context, category string) (int32, error)
|
||||
DeleteMailRequest(ctx context.Context, id pgtype.UUID) (int64, error)
|
||||
DeleteOrganizationRepresentatives(ctx context.Context, organizationID pgtype.UUID) error
|
||||
// Deletes one batch of accounts that stayed unverified since before cutoff,
|
||||
@@ -91,13 +94,19 @@ type Querier interface {
|
||||
// provider's idempotency window reuses it instead of counting again.
|
||||
FindOpenMailQuotaReservation(ctx context.Context, arg FindOpenMailQuotaReservationParams) (FindOpenMailQuotaReservationRow, error)
|
||||
GetActiveSessionByTokenHash(ctx context.Context, arg GetActiveSessionByTokenHashParams) (Session, error)
|
||||
GetAssessmentFamilyByID(ctx context.Context, id pgtype.UUID) (AssessmentFamily, error)
|
||||
GetAssessmentFamilyVersion(ctx context.Context, arg GetAssessmentFamilyVersionParams) (AssessmentFamily, error)
|
||||
GetConsentText(ctx context.Context, arg GetConsentTextParams) (ConsentText, error)
|
||||
GetContactConsentRecordByInvitation(ctx context.Context, arg GetContactConsentRecordByInvitationParams) (GetContactConsentRecordByInvitationRow, error)
|
||||
GetCurrentAssessmentCategoryOpening(ctx context.Context, category string) (AssessmentCategoryOpening, error)
|
||||
// The newest unrevoked record of a purpose other than C4.
|
||||
GetCurrentConsentRecord(ctx context.Context, arg GetCurrentConsentRecordParams) (GetCurrentConsentRecordRow, error)
|
||||
// The text of a purpose that applies at a time: the latest publication not
|
||||
// after it.
|
||||
GetCurrentConsentText(ctx context.Context, arg GetCurrentConsentTextParams) (ConsentText, error)
|
||||
// The database clock, which stamps the effective time of a policy version, so
|
||||
// "the version in force now" is judged against the same clock.
|
||||
GetDatabaseTime(ctx context.Context) (pgtype.Timestamptz, error)
|
||||
GetDeliveryByID(ctx context.Context, id pgtype.UUID) (Delivery, error)
|
||||
// Aggregates the deliveries by kind and status for the metrics, at most once
|
||||
// per metrics interval. It reads only kind, status, and next_attempt_at,
|
||||
@@ -112,6 +121,7 @@ type Querier interface {
|
||||
GetEffectivePolicyVersion(ctx context.Context, arg GetEffectivePolicyVersionParams) (PolicyVersion, error)
|
||||
// Explains why ConsumeEmailToken matched no row.
|
||||
GetEmailTokenState(ctx context.Context, arg GetEmailTokenStateParams) (GetEmailTokenStateRow, error)
|
||||
GetLatestAssessmentFamily(ctx context.Context, familyKey string) (AssessmentFamily, error)
|
||||
// Reads the status of the newest delivery of a kind addressed to an account,
|
||||
// for the account's own view of its mail. It selects no address and no
|
||||
// content. The recipient index narrows the scan to the account's rows.
|
||||
@@ -133,6 +143,9 @@ type Querier interface {
|
||||
// clock that set occurred_at.
|
||||
GetOutboxBacklog(ctx context.Context) (GetOutboxBacklogRow, error)
|
||||
GetPolicyVersionByID(ctx context.Context, id pgtype.UUID) (PolicyVersion, error)
|
||||
// The published version of a family, if any. The partial unique index
|
||||
// assessment_families_one_published_key allows at most one.
|
||||
GetPublishedAssessmentFamily(ctx context.Context, familyKey string) (AssessmentFamily, error)
|
||||
// Reads the backlog for metrics without locking: jobs that a worker may
|
||||
// claim at now, how long the earliest of them has waited, and the dead
|
||||
// jobs. The
|
||||
@@ -146,6 +159,16 @@ type Querier interface {
|
||||
GetUnrevokedConsentRecordByVersion(ctx context.Context, arg GetUnrevokedConsentRecordByVersionParams) (GetUnrevokedConsentRecordByVersionRow, error)
|
||||
GetUserByEmailNormalized(ctx context.Context, emailNormalized string) (User, error)
|
||||
GetUserByID(ctx context.Context, id pgtype.UUID) (User, error)
|
||||
// Stores an opening. The insert trigger derives required_families from the
|
||||
// policy document and counts the published families, and the coverage CHECK
|
||||
// refuses the row when they are fewer; both numbers from the caller are
|
||||
// replaced by the trigger's.
|
||||
InsertAssessmentCategoryOpening(ctx context.Context, arg InsertAssessmentCategoryOpeningParams) (AssessmentCategoryOpening, error)
|
||||
// Stores a new family version as a draft and returns the stored row with the
|
||||
// creation time set by the stamp trigger. A taken ID or (family_key, version)
|
||||
// returns no row instead of an error, so a conflict does not abort the
|
||||
// surrounding transaction.
|
||||
InsertAssessmentFamily(ctx context.Context, arg InsertAssessmentFamilyParams) (AssessmentFamily, error)
|
||||
// Records a grant of C1, C2, C3, or C5. An unrevoked record of the same
|
||||
// subject, purpose, and text version already exists when no row returns;
|
||||
// the insert then changes nothing and does not abort the transaction.
|
||||
@@ -204,6 +227,10 @@ type Querier interface {
|
||||
InsertSession(ctx context.Context, arg InsertSessionParams) error
|
||||
InsertUser(ctx context.Context, arg InsertUserParams) error
|
||||
InvalidateActiveEmailTokens(ctx context.Context, arg InvalidateActiveEmailTokensParams) (int64, error)
|
||||
// One page of the versions of a category in key and version order, after the
|
||||
// given position; an empty key and version 0 start at the beginning. The
|
||||
// (family_key, version) unique index serves the scan.
|
||||
ListAssessmentFamiliesByCategory(ctx context.Context, arg ListAssessmentFamiliesByCategoryParams) ([]AssessmentFamily, error)
|
||||
// One page of the subject's ledger, newest grant first, optionally limited
|
||||
// to one state. Every record query is scoped by the subject, so a caller
|
||||
// never reads or revokes another subject's consent. The state is computed,
|
||||
@@ -222,6 +249,10 @@ type Querier interface {
|
||||
// Lists the other accounts of a provider that have usage in a month, so
|
||||
// startup can warn about a renamed EMAIL_ACCOUNT.
|
||||
ListOtherMailQuotaAccounts(ctx context.Context, arg ListOtherMailQuotaAccountsParams) ([]string, error)
|
||||
// Serializes the operations that change which versions of a category are
|
||||
// published or open the category, until the transaction ends. The key is
|
||||
// namespaced, so it cannot collide with another use of advisory locks.
|
||||
LockAssessmentCategory(ctx context.Context, category string) error
|
||||
// Locks a claimed job for its handler transaction and checks that the lease
|
||||
// still belongs to this attempt. It returns no row when the job was
|
||||
// canceled, or reclaimed after the lease expired. While the lock is held,
|
||||
@@ -264,6 +295,10 @@ type Querier interface {
|
||||
// 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)
|
||||
// Moves a draft to published by the named operator and returns the row with
|
||||
// the publication time set by the guard trigger. No row means the version
|
||||
// was not a draft any more.
|
||||
PublishAssessmentFamily(ctx context.Context, arg PublishAssessmentFamilyParams) (AssessmentFamily, error)
|
||||
// Advances a delivery past one attempt and appends the attempt in one
|
||||
// statement, so both happen or neither does, inside or outside a
|
||||
// transaction. The update applies only while the stored version still
|
||||
@@ -288,6 +323,7 @@ type Querier interface {
|
||||
// the job did not cause the failure. The same guard as the completion
|
||||
// applies, one (id, attempt) pair per job. It returns the IDs it released.
|
||||
ReleaseScheduledJobs(ctx context.Context, arg ReleaseScheduledJobsParams) ([]int64, error)
|
||||
RetireAssessmentFamily(ctx context.Context, arg RetireAssessmentFamilyParams) (AssessmentFamily, error)
|
||||
// Revokes every unrevoked record of a purpose other than C4, superseded ones
|
||||
// included, and returns them. A revocation time before the grant time, from
|
||||
// clock skew between instances, is stamped at the grant time instead.
|
||||
|
||||
Reference in new issue
Block a user