sqlmigrate

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package sqlmigrate is the shared forward-only SQL migration runner for Harbor's SQLite and Postgres persistence drivers. It is the single home of the migration contract that was previously copy-pasted per driver: the `NNNN_<slug>.sql` filename rule, the partial-apply precheck against `schema_migrations`, the per-migration transaction, and (Postgres) the FNV-64a advisory-key derivation that serialises concurrent boots.

Forward-only contract (unchanged from the per-driver runners): migrations are numbered monotonically; editing a merged migration is forbidden; future schema changes land as new files. A bad filename in the embed set is a build-time bug and is surfaced loudly, never skipped.

SQLite and Postgres keep deliberately distinct runners — they differ in the migrations-table DDL (`TIMESTAMP`/`CURRENT_TIMESTAMP` vs `TIMESTAMPTZ`/`NOW()`), in who records the applied version (the SQLite runner inserts it; the Postgres migration body inserts its own row), and in concurrency control (Postgres takes a session `pg_advisory_lock`, SQLite needs none). Each driver passes its own embedded `migrations/` FS and error prefix so wrapped errors read exactly as before.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunPostgres

func RunPostgres(ctx context.Context, db *sql.DB, migrationsFS fs.FS, errPrefix, advisoryLockName string) error

RunPostgres applies any forward-only migrations in migrationsFS not yet recorded in `schema_migrations` to db, wrapping the run in a session-level `pg_advisory_lock` derived from advisoryLockName so concurrent New() calls across replicas don't race. errPrefix is the driver's error-wrap prefix (e.g. "postgres", "memory/postgres"). The migration body records its own schema_migrations row.

func RunSQLite

func RunSQLite(ctx context.Context, db *sql.DB, migrationsFS fs.FS, errPrefix string) error

RunSQLite applies any forward-only migrations in migrationsFS whose version is not already present in `schema_migrations` to db. errPrefix is the driver's error-wrap prefix (e.g. "state/sqlite"). Each migration runs inside a single transaction; the runner records the applied version with `INSERT OR IGNORE` so a partially-applied DB (DDL applied but the trailing INSERT lost) is still recoverable.

Types

This section is empty.

Jump to

Keyboard shortcuts

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