migration

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package migration provides a lightweight SQL migration runner for cleat.

Migration files follow the naming convention NNN_name.sql and live in a configurable directory (default "migrations/"). Applied versions are tracked in a schema_migrations table. On each Run call, only pending (unapplied) migrations are executed in version order, each within its own transaction.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SplitMSSQL

func SplitMSSQL(sql string) []string

SplitMSSQL splits a MSSQL SQL string into batches on GO lines.

It is exported so that other packages needing MSSQL-correct statement splitting -- notably tests/plugin-harness's migration test setup, which used to carry its own copy that also split on every semicolon inside a batch -- can reuse this rather than diverging from it again. Splitting a stored procedure body's internal semicolons the same way a top-level statement separator would is the MSSQL analogue of what MySQL's DELIMITER directive guards against: it cuts CREATE OR ALTER PROCEDURE dbo.finalize_workflow_status (migrations/mssql/003_procedures.sql) into fragments and sends them to the server individually, which fails with "Incorrect syntax" partway through the body. GO is the only batch separator MSSQL recognises; everything else inside a batch, semicolons included, is the server's job to parse as one unit.

func SplitSQL

func SplitSQL(sql string) []string

SplitSQL splits a multi-statement MySQL script into individual statements, honouring DELIMITER directives, quoted strings/identifiers, and comments. See the unexported splitSQL below for the parsing rules.

It is exported so that other packages needing MySQL-correct statement splitting -- notably tests/plugin-harness's migration test setup, which used to carry its own copy that did not understand DELIMITER and silently mangled migrations/mysql/003_procedures.sql -- can reuse this rather than diverging from it again.

Types

type Dialect

type Dialect string

Dialect identifies the SQL dialect a Runner applies migrations for.

Deliberately not engine.Dialect: this package is meant to be a leaf that anything bootstrapping a database schema can depend on, including engine/testutil. engine/testutil is imported by many of engine's, migration's and plugin's own *_test.go files (internal tests, same package as the code under test), and Go refuses to build a package whose internal test files import something that imports the package itself ("import cycle not allowed in test") -- so if this package imported engine.Dialect, engine/testutil could not import this package without breaking every one of those. A three-value string enum is cheap enough to declare twice; callers that already have an engine.Dialect (e.g. cmd/cleat-worker/main.go) convert with a plain string conversion, since the two types share the same underlying values by construction -- TestDialectValuesMatchEngine below pins that.

const (
	DialectPostgres Dialect = "postgres"
	DialectMySQL    Dialect = "mysql"
	DialectMSSQL    Dialect = "mssql"
)

type Runner

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

Runner applies pending SQL migrations to a database.

func NewRunner

func NewRunner(db *sql.DB, dialect Dialect, dir string) *Runner

NewRunner creates a migration runner that reads .sql files from the dialect-specific subdirectory under dir and applies pending ones against db.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context) error

Run applies all pending migrations in version order within individual transactions. It creates the schema_migrations tracking table if it does not already exist. Run returns the first error encountered; no further migrations are attempted after a failure.

Jump to

Keyboard shortcuts

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