Files
query-orchestration/internal/database/repository/custommetadata.sql.go
T
Jay Brown 17fc813823 Merged in feature/mutable-metadata1 (pull request #221)
M1, M2 and M3 complete

* M1, M2 and M3 complete

* review changes

* docs

* docs
2026-04-16 23:11:26 +00:00

524 lines
17 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.27.0
// source: custommetadata.sql
package repository
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
const countDocumentCustomMetadataVersions = `-- name: CountDocumentCustomMetadataVersions :one
SELECT COUNT(*)::int AS version_count
FROM document_custom_metadata
WHERE document_id = $1
`
// Return the number of metadata version rows for a document. Used by
// ResetDocumentMetadata to populate metadataVersionsDeleted on its
// response. Run under the parent-row lock from LockDocumentForReset so
// the count is authoritative at commit time.
//
// SELECT COUNT(*)::int AS version_count
// FROM document_custom_metadata
// WHERE document_id = $1
func (q *Queries) CountDocumentCustomMetadataVersions(ctx context.Context, documentID uuid.UUID) (int32, error) {
row := q.db.QueryRow(ctx, countDocumentCustomMetadataVersions, documentID)
var version_count int32
err := row.Scan(&version_count)
return version_count, err
}
const createDocumentCustomMetadata = `-- name: CreateDocumentCustomMetadata :one
INSERT INTO document_custom_metadata (
document_id,
metadata,
version,
created_by
) VALUES (
$1,
$2,
$3,
$4
)
RETURNING id, document_id, metadata, version, created_at, created_by
`
type CreateDocumentCustomMetadataParams struct {
DocumentID uuid.UUID `db:"document_id"`
Metadata []byte `db:"metadata"`
Version int32 `db:"version"`
CreatedBy string `db:"created_by"`
}
// Mutable metadata feature, milestone 2.3: document_custom_metadata CRUD
// queries plus document custom_schema_id accessors.
//
// See plan Section 8.2 / tracking §2.3 of
// plans/mutable.metadata.plan.combo.v4.md for the canonical list.
//
// v4 notes:
// - No schema_id parameter on the metadata insert. The schema binding
// lives on the document row; metadata rows don't duplicate it.
// - GetCurrentDocumentCustomMetadata and GetDocumentCustomMetadataByVersion
// join documents + client_metadata_schemas so the handler can populate
// schemaId/schemaName/schemaVersion in one round-trip.
// - LockDocumentForMetadataWrite takes a parent-row lock on documents.
// Replaces v3's LockDocumentCustomMetadataForVersion.
// - DeleteDocumentCustomMetadata / NullifyDocumentCustomSchemaId are
// intentionally absent: FK cascades handle document deletion and the
// reset-metadata workstream writes a direct UPDATE instead.
//
// Insert a new metadata version for a document. Caller supplies an
// explicit version computed from GetMaxDocumentMetadataVersion + 1 under
// the parent-row lock from LockDocumentForMetadataWrite. No schema_id
// column exists on this table in v4.
//
// INSERT INTO document_custom_metadata (
// document_id,
// metadata,
// version,
// created_by
// ) VALUES (
// $1,
// $2,
// $3,
// $4
// )
// RETURNING id, document_id, metadata, version, created_at, created_by
func (q *Queries) CreateDocumentCustomMetadata(ctx context.Context, arg *CreateDocumentCustomMetadataParams) (*DocumentCustomMetadatum, error) {
row := q.db.QueryRow(ctx, createDocumentCustomMetadata,
arg.DocumentID,
arg.Metadata,
arg.Version,
arg.CreatedBy,
)
var i DocumentCustomMetadatum
err := row.Scan(
&i.ID,
&i.DocumentID,
&i.Metadata,
&i.Version,
&i.CreatedAt,
&i.CreatedBy,
)
return &i, err
}
const deleteDocumentCustomMetadataForReset = `-- name: DeleteDocumentCustomMetadataForReset :exec
DELETE FROM document_custom_metadata WHERE document_id = $1
`
// @sqlc-vet-disable
// Delete every metadata version row for a document. Owned by the
// reset-metadata path — the Delete cascade for document deletion is
// handled by ON DELETE CASCADE on document_custom_metadata.document_id,
// not by this query. Name disambiguates from any future cascade helper.
//
// DELETE FROM document_custom_metadata WHERE document_id = $1
func (q *Queries) DeleteDocumentCustomMetadataForReset(ctx context.Context, documentID uuid.UUID) error {
_, err := q.db.Exec(ctx, deleteDocumentCustomMetadataForReset, documentID)
return err
}
const getCurrentDocumentCustomMetadata = `-- name: GetCurrentDocumentCustomMetadata :one
SELECT
dcm.id,
dcm.document_id,
dcm.metadata,
dcm.version,
dcm.created_at,
dcm.created_by,
cms.id AS schema_id,
cms.name AS schema_name,
cms.version AS schema_version
FROM document_custom_metadata dcm
JOIN documents d ON d.id = dcm.document_id
LEFT JOIN client_metadata_schemas cms ON cms.id = d.custom_schema_id
WHERE dcm.document_id = $1
ORDER BY dcm.version DESC
LIMIT 1
`
type GetCurrentDocumentCustomMetadataRow struct {
ID uuid.UUID `db:"id"`
DocumentID uuid.UUID `db:"document_id"`
Metadata []byte `db:"metadata"`
Version int32 `db:"version"`
CreatedAt pgtype.Timestamptz `db:"created_at"`
CreatedBy string `db:"created_by"`
SchemaID *uuid.UUID `db:"schema_id"`
SchemaName *string `db:"schema_name"`
SchemaVersion *int32 `db:"schema_version"`
}
// Return the most-recent metadata row for a document, decorated with the
// current schema name and version pulled from the bound schema row.
// Joins documents (for the binding) and client_metadata_schemas (for
// name/version). idx_dcm_doc_version_desc backs the ORDER BY ... LIMIT 1.
//
// SELECT
// dcm.id,
// dcm.document_id,
// dcm.metadata,
// dcm.version,
// dcm.created_at,
// dcm.created_by,
// cms.id AS schema_id,
// cms.name AS schema_name,
// cms.version AS schema_version
// FROM document_custom_metadata dcm
// JOIN documents d ON d.id = dcm.document_id
// LEFT JOIN client_metadata_schemas cms ON cms.id = d.custom_schema_id
// WHERE dcm.document_id = $1
// ORDER BY dcm.version DESC
// LIMIT 1
func (q *Queries) GetCurrentDocumentCustomMetadata(ctx context.Context, documentID uuid.UUID) (*GetCurrentDocumentCustomMetadataRow, error) {
row := q.db.QueryRow(ctx, getCurrentDocumentCustomMetadata, documentID)
var i GetCurrentDocumentCustomMetadataRow
err := row.Scan(
&i.ID,
&i.DocumentID,
&i.Metadata,
&i.Version,
&i.CreatedAt,
&i.CreatedBy,
&i.SchemaID,
&i.SchemaName,
&i.SchemaVersion,
)
return &i, err
}
const getDocumentClientID = `-- name: GetDocumentClientID :one
SELECT clientId FROM documents WHERE id = $1
`
// Read a document's clientId for the AssignSchema cross-client check.
// documents.clientId is the unquoted (lowercased) varchar column.
//
// SELECT clientId FROM documents WHERE id = $1
func (q *Queries) GetDocumentClientID(ctx context.Context, id uuid.UUID) (string, error) {
row := q.db.QueryRow(ctx, getDocumentClientID, id)
var clientid string
err := row.Scan(&clientid)
return clientid, err
}
const getDocumentCustomMetadataByVersion = `-- name: GetDocumentCustomMetadataByVersion :one
SELECT
dcm.id,
dcm.document_id,
dcm.metadata,
dcm.version,
dcm.created_at,
dcm.created_by,
cms.id AS schema_id,
cms.name AS schema_name,
cms.version AS schema_version
FROM document_custom_metadata dcm
JOIN documents d ON d.id = dcm.document_id
LEFT JOIN client_metadata_schemas cms ON cms.id = d.custom_schema_id
WHERE dcm.document_id = $1
AND dcm.version = $2
`
type GetDocumentCustomMetadataByVersionParams struct {
DocumentID uuid.UUID `db:"document_id"`
Version int32 `db:"version"`
}
type GetDocumentCustomMetadataByVersionRow struct {
ID uuid.UUID `db:"id"`
DocumentID uuid.UUID `db:"document_id"`
Metadata []byte `db:"metadata"`
Version int32 `db:"version"`
CreatedAt pgtype.Timestamptz `db:"created_at"`
CreatedBy string `db:"created_by"`
SchemaID *uuid.UUID `db:"schema_id"`
SchemaName *string `db:"schema_name"`
SchemaVersion *int32 `db:"schema_version"`
}
// Return a specific historical version with the same schema decoration
// as the current-version query above.
//
// SELECT
// dcm.id,
// dcm.document_id,
// dcm.metadata,
// dcm.version,
// dcm.created_at,
// dcm.created_by,
// cms.id AS schema_id,
// cms.name AS schema_name,
// cms.version AS schema_version
// FROM document_custom_metadata dcm
// JOIN documents d ON d.id = dcm.document_id
// LEFT JOIN client_metadata_schemas cms ON cms.id = d.custom_schema_id
// WHERE dcm.document_id = $1
// AND dcm.version = $2
func (q *Queries) GetDocumentCustomMetadataByVersion(ctx context.Context, arg *GetDocumentCustomMetadataByVersionParams) (*GetDocumentCustomMetadataByVersionRow, error) {
row := q.db.QueryRow(ctx, getDocumentCustomMetadataByVersion, arg.DocumentID, arg.Version)
var i GetDocumentCustomMetadataByVersionRow
err := row.Scan(
&i.ID,
&i.DocumentID,
&i.Metadata,
&i.Version,
&i.CreatedAt,
&i.CreatedBy,
&i.SchemaID,
&i.SchemaName,
&i.SchemaVersion,
)
return &i, err
}
const getDocumentCustomMetadataHistory = `-- name: GetDocumentCustomMetadataHistory :many
SELECT
version,
created_at,
created_by
FROM document_custom_metadata
WHERE document_id = $1
ORDER BY version DESC
LIMIT $3 OFFSET $2
`
type GetDocumentCustomMetadataHistoryParams struct {
DocumentID uuid.UUID `db:"document_id"`
OffsetVal int64 `db:"offset_val"`
LimitVal int64 `db:"limit_val"`
}
type GetDocumentCustomMetadataHistoryRow struct {
Version int32 `db:"version"`
CreatedAt pgtype.Timestamptz `db:"created_at"`
CreatedBy string `db:"created_by"`
}
// Return a paged list of version summaries for a document, newest first.
// No schema join: the history response is a terse list of
// (version, created_at, created_by) tuples.
//
// SELECT
// version,
// created_at,
// created_by
// FROM document_custom_metadata
// WHERE document_id = $1
// ORDER BY version DESC
// LIMIT $3 OFFSET $2
func (q *Queries) GetDocumentCustomMetadataHistory(ctx context.Context, arg *GetDocumentCustomMetadataHistoryParams) ([]*GetDocumentCustomMetadataHistoryRow, error) {
rows, err := q.db.Query(ctx, getDocumentCustomMetadataHistory, arg.DocumentID, arg.OffsetVal, arg.LimitVal)
if err != nil {
return nil, err
}
defer rows.Close()
items := []*GetDocumentCustomMetadataHistoryRow{}
for rows.Next() {
var i GetDocumentCustomMetadataHistoryRow
if err := rows.Scan(&i.Version, &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 getDocumentCustomSchemaId = `-- name: GetDocumentCustomSchemaId :one
SELECT custom_schema_id
FROM documents
WHERE id = $1
`
// Read a document's current schema binding. Returns NULL when unbound.
//
// SELECT custom_schema_id
// FROM documents
// WHERE id = $1
func (q *Queries) GetDocumentCustomSchemaId(ctx context.Context, id uuid.UUID) (*uuid.UUID, error) {
row := q.db.QueryRow(ctx, getDocumentCustomSchemaId, id)
var custom_schema_id *uuid.UUID
err := row.Scan(&custom_schema_id)
return custom_schema_id, err
}
const getDocumentSchemaBindingForReset = `-- name: GetDocumentSchemaBindingForReset :one
SELECT
d.custom_schema_id,
cms.name AS schema_name,
cms.version AS schema_version
FROM documents d
LEFT JOIN client_metadata_schemas cms ON cms.id = d.custom_schema_id
WHERE d.id = $1
`
type GetDocumentSchemaBindingForResetRow struct {
CustomSchemaID *uuid.UUID `db:"custom_schema_id"`
SchemaName *string `db:"schema_name"`
SchemaVersion *int32 `db:"schema_version"`
}
// Read the document's current schema binding along with the joined
// schema row's name and version so ResetDocumentMetadata can populate the
// previousSchema* fields on its response. The LEFT JOIN returns NULL for
// name/version when the document has no schema bound — the reset endpoint
// treats that as the idempotent no-op case and still succeeds.
//
// SELECT
// d.custom_schema_id,
// cms.name AS schema_name,
// cms.version AS schema_version
// FROM documents d
// LEFT JOIN client_metadata_schemas cms ON cms.id = d.custom_schema_id
// WHERE d.id = $1
func (q *Queries) GetDocumentSchemaBindingForReset(ctx context.Context, documentID uuid.UUID) (*GetDocumentSchemaBindingForResetRow, error) {
row := q.db.QueryRow(ctx, getDocumentSchemaBindingForReset, documentID)
var i GetDocumentSchemaBindingForResetRow
err := row.Scan(&i.CustomSchemaID, &i.SchemaName, &i.SchemaVersion)
return &i, err
}
const getMaxDocumentMetadataVersion = `-- name: GetMaxDocumentMetadataVersion :one
SELECT COALESCE(MAX(version), 0)::int AS max_version
FROM document_custom_metadata
WHERE document_id = $1
`
// Return MAX(version) for a document's metadata rows, or 0 when none
// exist. Caller adds 1 to compute the next version to insert. Must run
// inside the transaction that holds the LockDocumentForMetadataWrite
// lock; otherwise two concurrent writers can both see the same max.
//
// SELECT COALESCE(MAX(version), 0)::int AS max_version
// FROM document_custom_metadata
// WHERE document_id = $1
func (q *Queries) GetMaxDocumentMetadataVersion(ctx context.Context, documentID uuid.UUID) (int32, error) {
row := q.db.QueryRow(ctx, getMaxDocumentMetadataVersion, documentID)
var max_version int32
err := row.Scan(&max_version)
return max_version, err
}
const hasDocumentCustomMetadata = `-- name: HasDocumentCustomMetadata :one
SELECT EXISTS(
SELECT 1 FROM document_custom_metadata WHERE document_id = $1
) AS has_metadata
`
// EXISTS probe for any metadata row belonging to this document. Used by
// AssignSchema to reject re-binding once metadata has been written.
//
// SELECT EXISTS(
// SELECT 1 FROM document_custom_metadata WHERE document_id = $1
// ) AS has_metadata
func (q *Queries) HasDocumentCustomMetadata(ctx context.Context, documentID uuid.UUID) (bool, error) {
row := q.db.QueryRow(ctx, hasDocumentCustomMetadata, documentID)
var has_metadata bool
err := row.Scan(&has_metadata)
return has_metadata, err
}
const lockDocumentForMetadataWrite = `-- name: LockDocumentForMetadataWrite :one
SELECT id FROM documents
WHERE id = $1
FOR UPDATE
`
// Parent-row lock for metadata write ordering. Must be called inside a
// transaction as the FIRST DML statement. Serializes with AssignSchema
// (which also takes FOR UPDATE on documents) so the mutual-exclusivity
// invariant in plan Section 7.3 holds.
//
// SELECT id FROM documents
// WHERE id = $1
// FOR UPDATE
func (q *Queries) LockDocumentForMetadataWrite(ctx context.Context, documentID uuid.UUID) (uuid.UUID, error) {
row := q.db.QueryRow(ctx, lockDocumentForMetadataWrite, documentID)
var id uuid.UUID
err := row.Scan(&id)
return id, err
}
const lockDocumentForReset = `-- name: LockDocumentForReset :one
SELECT id, clientId, custom_schema_id
FROM documents
WHERE id = $1
FOR UPDATE
`
type LockDocumentForResetRow struct {
ID uuid.UUID `db:"id"`
Clientid string `db:"clientid"`
CustomSchemaID *uuid.UUID `db:"custom_schema_id"`
}
// Milestone 3 parent-row lock acquired by ResetDocumentMetadata as the
// FIRST DML statement inside its transaction. Same FOR UPDATE shape as
// LockDocumentForMetadataWrite / LockDocumentForLegacyExtractionWrite so
// the reset path serializes correctly against concurrent assign and write
// paths for the mutual-exclusivity invariant (plan Section 7.3).
// Returns id, clientid, and current custom_schema_id so the caller can
// short-circuit and/or decorate the result without a second round-trip.
//
// SELECT id, clientId, custom_schema_id
// FROM documents
// WHERE id = $1
// FOR UPDATE
func (q *Queries) LockDocumentForReset(ctx context.Context, documentID uuid.UUID) (*LockDocumentForResetRow, error) {
row := q.db.QueryRow(ctx, lockDocumentForReset, documentID)
var i LockDocumentForResetRow
err := row.Scan(&i.ID, &i.Clientid, &i.CustomSchemaID)
return &i, err
}
const nullifyDocumentCustomSchemaIdForReset = `-- name: NullifyDocumentCustomSchemaIdForReset :exec
UPDATE documents SET custom_schema_id = NULL WHERE id = $1
`
// Clear the document's schema binding as the final DML of the reset
// transaction. Trigger 1 (trg_prevent_schema_reassignment) fires on this
// UPDATE; the preceding DeleteDocumentCustomMetadataForReset has already
// removed every metadata row and the transaction has already rejected any
// document carrying legacy field extractions, so the trigger's EXISTS
// checks both return false and the UPDATE succeeds.
//
// UPDATE documents SET custom_schema_id = NULL WHERE id = $1
func (q *Queries) NullifyDocumentCustomSchemaIdForReset(ctx context.Context, documentID uuid.UUID) error {
_, err := q.db.Exec(ctx, nullifyDocumentCustomSchemaIdForReset, documentID)
return err
}
const setDocumentCustomSchemaId = `-- name: SetDocumentCustomSchemaId :exec
UPDATE documents
SET custom_schema_id = $1
WHERE id = $2
`
type SetDocumentCustomSchemaIdParams struct {
CustomSchemaID *uuid.UUID `db:"custom_schema_id"`
ID uuid.UUID `db:"id"`
}
// Bind (or clear) a document's schema. Nullable parameter: passing NULL
// clears the binding. The composite FK in migration 128 prevents binding
// a schema owned by a different client, so this query does not re-check.
//
// UPDATE documents
// SET custom_schema_id = $1
// WHERE id = $2
func (q *Queries) SetDocumentCustomSchemaId(ctx context.Context, arg *SetDocumentCustomSchemaIdParams) error {
_, err := q.db.Exec(ctx, setDocumentCustomSchemaId, arg.CustomSchemaID, arg.ID)
return err
}