17fc813823
M1, M2 and M3 complete * M1, M2 and M3 complete * review changes * docs * docs
395 lines
12 KiB
Go
395 lines
12 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.27.0
|
|
// source: customschemas.sql
|
|
|
|
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const createClientMetadataSchema = `-- name: CreateClientMetadataSchema :one
|
|
|
|
INSERT INTO client_metadata_schemas (
|
|
client_id,
|
|
name,
|
|
description,
|
|
schema_def,
|
|
version,
|
|
status,
|
|
created_by
|
|
) VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7
|
|
)
|
|
RETURNING id, client_id, name, description, schema_def, version, status, created_at, created_by
|
|
`
|
|
|
|
type CreateClientMetadataSchemaParams struct {
|
|
ClientID string `db:"client_id"`
|
|
Name string `db:"name"`
|
|
Description *string `db:"description"`
|
|
SchemaDef []byte `db:"schema_def"`
|
|
Version int32 `db:"version"`
|
|
Status SchemaStatusType `db:"status"`
|
|
CreatedBy string `db:"created_by"`
|
|
}
|
|
|
|
// Mutable metadata feature, milestone 1.4: schema CRUD queries for
|
|
// client_metadata_schemas. See plan Section 8.2 of
|
|
// plans/mutable.metadata.plan.combo.v4.md for the canonical list.
|
|
//
|
|
// Intentionally omitted in v4:
|
|
// - GetSchemaMetadataRecordCount (removed; canDelete derives from
|
|
// GetSchemaDocumentCount alone because document_custom_metadata has
|
|
// no schema_id column in v4).
|
|
// - Metadata, document-modification, and reset-metadata queries
|
|
// (Milestones 2 and 3, plan sections 8.2 continued).
|
|
//
|
|
// Insert a new schema row. Caller supplies an explicit version computed
|
|
// from GetMaxSchemaVersion + 1 and an explicit status (always 'active'
|
|
// in Milestone 1; supersede/retire go through SetSchemaStatus). The
|
|
// database defaults id and created_at.
|
|
//
|
|
// INSERT INTO client_metadata_schemas (
|
|
// client_id,
|
|
// name,
|
|
// description,
|
|
// schema_def,
|
|
// version,
|
|
// status,
|
|
// created_by
|
|
// ) VALUES (
|
|
// $1,
|
|
// $2,
|
|
// $3,
|
|
// $4,
|
|
// $5,
|
|
// $6,
|
|
// $7
|
|
// )
|
|
// RETURNING id, client_id, name, description, schema_def, version, status, created_at, created_by
|
|
func (q *Queries) CreateClientMetadataSchema(ctx context.Context, arg *CreateClientMetadataSchemaParams) (*ClientMetadataSchema, error) {
|
|
row := q.db.QueryRow(ctx, createClientMetadataSchema,
|
|
arg.ClientID,
|
|
arg.Name,
|
|
arg.Description,
|
|
arg.SchemaDef,
|
|
arg.Version,
|
|
arg.Status,
|
|
arg.CreatedBy,
|
|
)
|
|
var i ClientMetadataSchema
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ClientID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.SchemaDef,
|
|
&i.Version,
|
|
&i.Status,
|
|
&i.CreatedAt,
|
|
&i.CreatedBy,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const getClientMetadataSchema = `-- name: GetClientMetadataSchema :one
|
|
SELECT id, client_id, name, description, schema_def, version, status, created_at, created_by FROM client_metadata_schemas
|
|
WHERE id = $1
|
|
`
|
|
|
|
// Fetch a single schema by primary key.
|
|
//
|
|
// SELECT id, client_id, name, description, schema_def, version, status, created_at, created_by FROM client_metadata_schemas
|
|
// WHERE id = $1
|
|
func (q *Queries) GetClientMetadataSchema(ctx context.Context, id uuid.UUID) (*ClientMetadataSchema, error) {
|
|
row := q.db.QueryRow(ctx, getClientMetadataSchema, id)
|
|
var i ClientMetadataSchema
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ClientID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.SchemaDef,
|
|
&i.Version,
|
|
&i.Status,
|
|
&i.CreatedAt,
|
|
&i.CreatedBy,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const getLatestSchemaByName = `-- name: GetLatestSchemaByName :one
|
|
SELECT id, client_id, name, description, schema_def, version, status, created_at, created_by FROM client_metadata_schemas
|
|
WHERE client_id = $1 AND name = $2
|
|
ORDER BY version DESC
|
|
LIMIT 1
|
|
`
|
|
|
|
type GetLatestSchemaByNameParams struct {
|
|
ClientID string `db:"client_id"`
|
|
Name string `db:"name"`
|
|
}
|
|
|
|
// Return the highest-version row for a (client_id, name) pair regardless
|
|
// of status. Callers that need an active-only view should filter after.
|
|
//
|
|
// SELECT id, client_id, name, description, schema_def, version, status, created_at, created_by FROM client_metadata_schemas
|
|
// WHERE client_id = $1 AND name = $2
|
|
// ORDER BY version DESC
|
|
// LIMIT 1
|
|
func (q *Queries) GetLatestSchemaByName(ctx context.Context, arg *GetLatestSchemaByNameParams) (*ClientMetadataSchema, error) {
|
|
row := q.db.QueryRow(ctx, getLatestSchemaByName, arg.ClientID, arg.Name)
|
|
var i ClientMetadataSchema
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ClientID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.SchemaDef,
|
|
&i.Version,
|
|
&i.Status,
|
|
&i.CreatedAt,
|
|
&i.CreatedBy,
|
|
)
|
|
return &i, err
|
|
}
|
|
|
|
const getMaxSchemaVersion = `-- name: GetMaxSchemaVersion :one
|
|
SELECT COALESCE(MAX(version), 0) AS max_version
|
|
FROM client_metadata_schemas
|
|
WHERE client_id = $1 AND name = $2
|
|
`
|
|
|
|
type GetMaxSchemaVersionParams struct {
|
|
ClientID string `db:"client_id"`
|
|
Name string `db:"name"`
|
|
}
|
|
|
|
// Get the current max version for a (client_id, name) pair. Returns 0
|
|
// for a brand-new pair so the service layer can safely write version 1.
|
|
// Must be called inside the same transaction that holds the lock from
|
|
// LockSchemaVersionsForName.
|
|
//
|
|
// SELECT COALESCE(MAX(version), 0) AS max_version
|
|
// FROM client_metadata_schemas
|
|
// WHERE client_id = $1 AND name = $2
|
|
func (q *Queries) GetMaxSchemaVersion(ctx context.Context, arg *GetMaxSchemaVersionParams) (*int32, error) {
|
|
row := q.db.QueryRow(ctx, getMaxSchemaVersion, arg.ClientID, arg.Name)
|
|
var max_version *int32
|
|
err := row.Scan(&max_version)
|
|
return max_version, err
|
|
}
|
|
|
|
const getSchemaDocumentCount = `-- name: GetSchemaDocumentCount :one
|
|
SELECT COUNT(*) AS document_count
|
|
FROM documents
|
|
WHERE custom_schema_id = $1
|
|
`
|
|
|
|
// Count documents currently bound to a schema via documents.custom_schema_id.
|
|
// Used to derive canDelete and to drive retire-vs-delete decisions.
|
|
//
|
|
// SELECT COUNT(*) AS document_count
|
|
// FROM documents
|
|
// WHERE custom_schema_id = $1
|
|
func (q *Queries) GetSchemaDocumentCount(ctx context.Context, schemaID *uuid.UUID) (int64, error) {
|
|
row := q.db.QueryRow(ctx, getSchemaDocumentCount, schemaID)
|
|
var document_count int64
|
|
err := row.Scan(&document_count)
|
|
return document_count, err
|
|
}
|
|
|
|
const listClientMetadataSchemas = `-- name: ListClientMetadataSchemas :many
|
|
SELECT
|
|
cms.id, cms.client_id, cms.name, cms.description, cms.schema_def, cms.version, cms.status, cms.created_at, cms.created_by,
|
|
(SELECT COUNT(*) FROM documents WHERE documents.custom_schema_id = cms.id)::bigint AS document_count
|
|
FROM client_metadata_schemas cms
|
|
WHERE cms.client_id = $1
|
|
AND ($2::varchar IS NULL OR cms.name = $2::varchar)
|
|
AND ($3::bool OR cms.status = $4::schema_status_type)
|
|
AND (
|
|
$5::bool
|
|
OR (cms.client_id, cms.name, cms.version) IN (
|
|
SELECT client_id, name, MAX(version)
|
|
FROM client_metadata_schemas
|
|
WHERE client_id = $1
|
|
GROUP BY client_id, name
|
|
)
|
|
)
|
|
ORDER BY cms.name, cms.version DESC
|
|
LIMIT $7 OFFSET $6
|
|
`
|
|
|
|
type ListClientMetadataSchemasParams struct {
|
|
ClientID string `db:"client_id"`
|
|
NameFilter *string `db:"name_filter"`
|
|
IncludeAllStatuses bool `db:"include_all_statuses"`
|
|
StatusFilter SchemaStatusType `db:"status_filter"`
|
|
IncludeAllVersions bool `db:"include_all_versions"`
|
|
OffsetVal int64 `db:"offset_val"`
|
|
LimitVal int64 `db:"limit_val"`
|
|
}
|
|
|
|
type ListClientMetadataSchemasRow struct {
|
|
ID uuid.UUID `db:"id"`
|
|
ClientID string `db:"client_id"`
|
|
Name string `db:"name"`
|
|
Description *string `db:"description"`
|
|
SchemaDef []byte `db:"schema_def"`
|
|
Version int32 `db:"version"`
|
|
Status SchemaStatusType `db:"status"`
|
|
CreatedAt pgtype.Timestamptz `db:"created_at"`
|
|
CreatedBy string `db:"created_by"`
|
|
DocumentCount int64 `db:"document_count"`
|
|
}
|
|
|
|
// List schemas for a client with optional filters. Status filtering uses a
|
|
// two-parameter shape: when include_all_statuses is true, status is ignored;
|
|
// otherwise status_filter selects exactly one of active/superseded/retired.
|
|
// When include_all_versions is false, only the latest version per
|
|
// (client_id, name) is returned (via a max-version subquery). When true,
|
|
// every version is returned. Name filter uses sqlc.narg so passing NULL
|
|
// skips the filter; a non-NULL value is matched exactly. The document_count
|
|
// column is a correlated subquery so the handler can derive canDelete
|
|
// without a second round-trip.
|
|
//
|
|
// SELECT
|
|
// cms.id, cms.client_id, cms.name, cms.description, cms.schema_def, cms.version, cms.status, cms.created_at, cms.created_by,
|
|
// (SELECT COUNT(*) FROM documents WHERE documents.custom_schema_id = cms.id)::bigint AS document_count
|
|
// FROM client_metadata_schemas cms
|
|
// WHERE cms.client_id = $1
|
|
// AND ($2::varchar IS NULL OR cms.name = $2::varchar)
|
|
// AND ($3::bool OR cms.status = $4::schema_status_type)
|
|
// AND (
|
|
// $5::bool
|
|
// OR (cms.client_id, cms.name, cms.version) IN (
|
|
// SELECT client_id, name, MAX(version)
|
|
// FROM client_metadata_schemas
|
|
// WHERE client_id = $1
|
|
// GROUP BY client_id, name
|
|
// )
|
|
// )
|
|
// ORDER BY cms.name, cms.version DESC
|
|
// LIMIT $7 OFFSET $6
|
|
func (q *Queries) ListClientMetadataSchemas(ctx context.Context, arg *ListClientMetadataSchemasParams) ([]*ListClientMetadataSchemasRow, error) {
|
|
rows, err := q.db.Query(ctx, listClientMetadataSchemas,
|
|
arg.ClientID,
|
|
arg.NameFilter,
|
|
arg.IncludeAllStatuses,
|
|
arg.StatusFilter,
|
|
arg.IncludeAllVersions,
|
|
arg.OffsetVal,
|
|
arg.LimitVal,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []*ListClientMetadataSchemasRow{}
|
|
for rows.Next() {
|
|
var i ListClientMetadataSchemasRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ClientID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.SchemaDef,
|
|
&i.Version,
|
|
&i.Status,
|
|
&i.CreatedAt,
|
|
&i.CreatedBy,
|
|
&i.DocumentCount,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockSchemaVersionsForName = `-- name: LockSchemaVersionsForName :many
|
|
SELECT id, client_id, name, description, schema_def, version, status, created_at, created_by FROM client_metadata_schemas
|
|
WHERE client_id = $1 AND name = $2
|
|
FOR UPDATE
|
|
`
|
|
|
|
type LockSchemaVersionsForNameParams struct {
|
|
ClientID string `db:"client_id"`
|
|
Name string `db:"name"`
|
|
}
|
|
|
|
// Lock every version row of a (client_id, name) pair with FOR UPDATE to
|
|
// serialize concurrent version creation. Returns the locked rows so the
|
|
// service can read the current active row id and max version in one
|
|
// statement. Must be called inside a transaction.
|
|
//
|
|
// SELECT id, client_id, name, description, schema_def, version, status, created_at, created_by FROM client_metadata_schemas
|
|
// WHERE client_id = $1 AND name = $2
|
|
// FOR UPDATE
|
|
func (q *Queries) LockSchemaVersionsForName(ctx context.Context, arg *LockSchemaVersionsForNameParams) ([]*ClientMetadataSchema, error) {
|
|
rows, err := q.db.Query(ctx, lockSchemaVersionsForName, arg.ClientID, arg.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []*ClientMetadataSchema{}
|
|
for rows.Next() {
|
|
var i ClientMetadataSchema
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ClientID,
|
|
&i.Name,
|
|
&i.Description,
|
|
&i.SchemaDef,
|
|
&i.Version,
|
|
&i.Status,
|
|
&i.CreatedAt,
|
|
&i.CreatedBy,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, &i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const setSchemaStatus = `-- name: SetSchemaStatus :exec
|
|
UPDATE client_metadata_schemas
|
|
SET status = $1
|
|
WHERE id = $2
|
|
`
|
|
|
|
type SetSchemaStatusParams struct {
|
|
Status SchemaStatusType `db:"status"`
|
|
ID uuid.UUID `db:"id"`
|
|
}
|
|
|
|
// Update a schema's status. Used by the service layer to mark a row
|
|
// superseded when a new version is created, and to mark a row retired
|
|
// when the schema still has documents bound at delete time.
|
|
//
|
|
// UPDATE client_metadata_schemas
|
|
// SET status = $1
|
|
// WHERE id = $2
|
|
func (q *Queries) SetSchemaStatus(ctx context.Context, arg *SetSchemaStatusParams) error {
|
|
_, err := q.db.Exec(ctx, setSchemaStatus, arg.Status, arg.ID)
|
|
return err
|
|
}
|