Documentation
¶
Overview ¶
Package coremigrate holds implementation primitives shared by migratekit's Postgres migrator (the root package) and its ClickHouse migrator (migratekit/chmigrate): the migrations-tracking table DDL and environment template substitution. It is not part of migratekit's public API.
Index ¶
- Constants
- func AdvisoryLockKey(s string) int64
- func Contains(slice []string, item string) bool
- func EnsurePublicMigrationsTable(ctx context.Context, db *sql.DB) error
- func SplitStatements(sql string) []string
- func SubstituteTemplates(sql string) (string, error)
- type TrackedMigration
- type Tracker
- func (t *Tracker) Lock(ctx context.Context, key int64) error
- func (t *Tracker) RecordState(ctx context.Context, app, database, schema string, r TrackedMigration) error
- func (t *Tracker) Records(ctx context.Context, app, database, schema string) ([]TrackedMigration, error)
- func (t *Tracker) Setup(ctx context.Context) error
- func (t *Tracker) Unlock(ctx context.Context, key int64) error
Constants ¶
const MigrationSetupLockKey int64 = 7592348109
MigrationSetupLockKey serializes initialization of the shared public.migrations tracker. Postgres migrations use the same key for their application lock, but setup must acquire it first because the tracker may not exist yet.
Variables ¶
This section is empty.
Functions ¶
func AdvisoryLockKey ¶
AdvisoryLockKey derives a stable advisory-lock key from an arbitrary string.
func EnsurePublicMigrationsTable ¶
EnsurePublicMigrationsTable atomically creates the tracker tables under the shared bootstrap advisory lock. This ledger requires a fresh database; no legacy tables are upgraded. The tracker identity includes `schema` because WithSchema places tables in different schemas of the SAME database: without it, the same app applied to two schemas (e.g. doujins.* and hentai0.* sharing one DB) would record under one identity and the second schema would never get its tables. schema=” is the stamp for no-WithSchema groups only — Applied() matches schemas exactly, no wildcard.
func SplitStatements ¶
SplitStatements splits SQL into statements and strips comments. It is quote-aware: semicolons and comment markers inside '...' strings, "..." identifiers, and `...` identifiers are preserved verbatim (including ” doubled-quote and backslash escapes inside strings).
Two callers need it, for the same reason: the statements must reach the server one at a time. ClickHouse has no transactional DDL, and Postgres wraps a multi-statement simple-protocol Exec in an IMPLICIT transaction — which is exactly what `-- migratekit:no-transaction` exists to avoid.
It does NOT understand dollar-quoting ($$ ... $$); callers that can meet one must reject it rather than mis-split it.
func SubstituteTemplates ¶
SubstituteTemplates replaces template variables in SQL with environment variable values. Supports two template formats:
- {{VAR_NAME}} (Handlebars/Mustache style)
- ${VAR_NAME} (Shell/JS template literal style)
A referenced variable that is NOT SET in the environment is an error — a silent empty-string substitution would ship e.g. an empty password into DDL on a typo'd name. A variable explicitly set to the empty string is substituted as-is (assumed intentional).
Empty templates like ${} or {{}} are skipped (no substitution), and ON_CLUSTER placeholders are left intact for the ClickHouse driver to expand from its own Cluster config.
Types ¶
type TrackedMigration ¶ added in v1.0.5
TrackedMigration is the identity and completion state of a non-Postgres migration.
type Tracker ¶
type Tracker struct {
// contains filtered or unexported fields
}
Tracker records applied migrations and takes advisory locks in Postgres, for migrators whose target database is not Postgres itself (e.g. ClickHouse): the durable record and locking live in Postgres regardless of what's being migrated.
func NewTracker ¶
func (*Tracker) Lock ¶
Lock acquires the advisory lock for key on a dedicated, pinned connection (session advisory locks belong to the connection that took them; going through the pool would acquire and release on different connections).
func (*Tracker) RecordState ¶ added in v1.0.5
func (t *Tracker) RecordState(ctx context.Context, app, database, schema string, r TrackedMigration) error
RecordState is called under the target's advisory lock, after identity validation.