sqlmeta

package
v0.35.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package sqlmeta validates SQL Store tables and metadata on borrowed pools.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnsureSQLTable

func EnsureSQLTable(ctx context.Context, db *sqlx.DB, table SQLTable, statements ...string) error

EnsureSQLTable directly executes idempotent table/index initialization statements. It records no version or history and leaves the borrowed pool open.

PostgreSQL can reject concurrent CREATE ... IF NOT EXISTS statements for the same relation with catalog conflicts, so PostgreSQL statements run in one transaction serialized by LockPostgreSQLTable. SQLite executes them directly.

func ExternalSQLError

func ExternalSQLError(operation string, err error) error

ExternalSQLError preserves error identity while keeping driver text, which can contain bound values or connection details, out of the public error string. A driver SQLSTATE code carries no bound data and is kept for diagnosis.

func LockPostgreSQLTable

func LockPostgreSQLTable(ctx context.Context, tx *sqlx.Tx, table SQLTable) error

LockPostgreSQLTable takes the transaction advisory lock that serializes DDL for one PostgreSQL table name in the current schema. Every Store kind uses the same key, so initialization and other DDL on that table cannot interleave.

func QuoteSQLIdentifier

func QuoteSQLIdentifier(dialect SQLDialect, value string) (string, error)

QuoteSQLIdentifier returns a validated identifier quoted for SQLite or PostgreSQL.

func QuoteSQLTableName

func QuoteSQLTableName(dialect SQLDialect, value string) (string, error)

QuoteSQLTableName returns a validated table name quoted for SQLite or PostgreSQL.

func SQLColumns

func SQLColumns(ctx context.Context, db *sqlx.DB, backend SQLTable) (map[string]SQLColumn, error)

SQLColumns returns the exact current-schema columns for a validated table.

func SQLIndexName

func SQLIndexName(table SQLTable, purpose string) (string, error)

SQLIndexName derives a stable, bounded identifier for one table index.

func SQLUnixNano

func SQLUnixNano(value time.Time) (int64, error)

SQLUnixNano returns a lossless signed nanosecond representation.

func ValidateSQLColumns

func ValidateSQLColumns(got, want map[string]SQLColumn) error

ValidateSQLColumns requires the exact named column contract.

func ValidateSQLIdentifier

func ValidateSQLIdentifier(value string) error

ValidateSQLIdentifier validates an unqualified, portable SQL identifier.

func ValidateSQLIndexColumns

func ValidateSQLIndexColumns(ctx context.Context, db *sqlx.DB, backend SQLTable, name string, want []string) error

ValidateSQLIndexColumns requires an exact, non-unique, non-partial index column order.

func ValidateSQLSequenceIdentity

func ValidateSQLSequenceIdentity(ctx context.Context, db *sqlx.DB, backend SQLTable, column string) error

ValidateSQLSequenceIdentity requires the dialect-specific generated sequence contract used to break equal-timestamp Metrics ties.

func ValidateSQLTableName

func ValidateSQLTableName(value string) error

ValidateSQLTableName validates an unqualified SQL table name. KV prefixes may contain hyphens, which remain safe because every accepted name is quoted.

Types

type SQLColumn

type SQLColumn struct {
	Type               string
	Nullable           bool
	PrimaryKeyPosition int
}

SQLColumn describes one persisted SQL column for constructor-time validation.

type SQLDailyPartition

type SQLDailyPartition struct {
	Name         string
	Day          time.Time
	Lower, Upper int64
}

SQLDailyPartition is a managed UTC day with nanosecond range bounds.

type SQLDailyPartitions

type SQLDailyPartitions struct {
	Table  SQLTable
	Column string
	Prefix string
}

SQLDailyPartitions maintains day children of a PostgreSQL range parent. Prefix is owned by the caller so existing relation names remain stable.

func (SQLDailyPartitions) Check

func (p SQLDailyPartitions) Check(ctx context.Context, db *sqlx.DB) error

Check verifies the parent partition key and every attached managed child.

func (SQLDailyPartitions) List

func (p SQLDailyPartitions) List(ctx context.Context, q sqlPartitionQueryer) ([]SQLDailyPartition, error)

List verifies attached child names and exact bounds before returning them.

func (SQLDailyPartitions) Maintain

func (p SQLDailyPartitions) Maintain(ctx context.Context, tx *sqlx.Tx, cutoff time.Time, required []time.Time, beforeDrop func(SQLDailyPartition) error) error

Maintain runs under the parent's transaction advisory lock. Children wholly before cutoff are dropped; beforeDrop can clean caller-owned auxiliary rows in the same transaction. Required days and each following day are prepared.

func (SQLDailyPartitions) Partition

func (p SQLDailyPartitions) Partition(at time.Time) (SQLDailyPartition, error)

Partition returns the managed child for the UTC day containing at.

func (SQLDailyPartitions) Prepare

func (p SQLDailyPartitions) Prepare(ctx context.Context, tx *sqlx.Tx, required []time.Time) error

Prepare creates only missing required days and their following days. Call it outside a write transaction that has reserved caller-owned identities. The advisory lock coordinates DDL, while ordinary writes remain concurrent.

type SQLDialect

type SQLDialect string

SQLDialect identifies a supported SQL Store dialect.

const (
	// SQLDialectSQLite is the modernc.org/sqlite dialect.
	SQLDialectSQLite SQLDialect = "sqlite"
	// SQLDialectPostgreSQL is the lib/pq dialect.
	SQLDialectPostgreSQL SQLDialect = "postgres"
)

type SQLTable

type SQLTable struct {
	// contains filtered or unexported fields
}

SQLTable describes one validated logical table on a borrowed SQL pool.

func PrepareSQLTable

func PrepareSQLTable(db *sqlx.DB, kind, table string) (SQLTable, error)

PrepareSQLTable validates a borrowed pool, Store kind, and logical table.

func (SQLTable) Dialect

func (table SQLTable) Dialect() SQLDialect

Dialect returns the table's validated SQL dialect.

func (SQLTable) Name

func (table SQLTable) Name() string

Name returns the validated unqualified table name.

func (SQLTable) Quoted

func (table SQLTable) Quoted() string

Quoted returns the validated dialect-safe table identifier.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址