coremigrate

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 8 Imported by: 0

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

View Source
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

func AdvisoryLockKey(s string) int64

AdvisoryLockKey derives a stable advisory-lock key from an arbitrary string.

func Contains

func Contains(slice []string, item string) bool

Contains reports whether item is present in slice.

func EnsurePublicMigrationsTable

func EnsurePublicMigrationsTable(ctx context.Context, db *sql.DB) error

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

func SplitStatements(sql string) []string

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

func SubstituteTemplates(sql string) (string, error)

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

type TrackedMigration struct {
	Sequence                 int64
	Filename, Digest, Status string
}

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 NewTracker(db *sql.DB) *Tracker

func (*Tracker) Lock

func (t *Tracker) Lock(ctx context.Context, key int64) error

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.

func (*Tracker) Records added in v1.0.5

func (t *Tracker) Records(ctx context.Context, app, database, schema string) ([]TrackedMigration, error)

Records reads only. Empty-schema rows are refused because their target is unknown.

func (*Tracker) Setup

func (t *Tracker) Setup(ctx context.Context) error

func (*Tracker) Unlock

func (t *Tracker) Unlock(ctx context.Context, key int64) error

Unlock releases the advisory lock on the pinned connection, then closes it. Runs with a non-cancellable context; closing the connection releases the session lock even if pg_advisory_unlock fails.

Jump to

Keyboard shortcuts

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