Help us shape the future of Go! Take the 2026 Go Developer Survey

migrations

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMigrationConfigPath = "dbdiff.yaml"
View Source
const PostgresTestConnectionString = "postgres://user:password@localhost:5432/dbdiff?sslmode=disable"

Variables

This section is empty.

Functions

func ApplyMigrations

func ApplyMigrations(ctx context.Context, migrator Migrator, set *MigrationSet,
	lastVersion string, output io.Writer) error

func GenerateSQLiteMigration

func GenerateSQLiteMigration(tb testing.TB, target string, migrations string) []string

func MigrationChecksum

func MigrationChecksum(content []byte) string

func MigrationSlug

func MigrationSlug(text string) string

func MigrationVerifyDirectory

func MigrationVerifyDirectory(set *MigrationSet) (string, func(), error)

func MigrationVersionOfTime

func MigrationVersionOfTime(moment time.Time) string

func RenderMigrationFile

func RenderMigrationFile(toolVersion string, generatedAt time.Time, instructions []drivers.Instruction) string

func RenderMigrationPreview

func RenderMigrationPreview(set *MigrationSet) (string, error)

func RenderMigrationStatus

func RenderMigrationStatus(set *MigrationSet) string

func RunMigrationPreview

func RunMigrationPreview(ctx context.Context, migrator Migrator, set *MigrationSet, output io.Writer) error

One transaction holds every pending file, because a file can read an object of an earlier file.

func RunMigrationRepair

func RunMigrationRepair(ctx context.Context, migrator Migrator, set *MigrationSet,
	output io.Writer) error

RunMigrationRepair makes the record agree with the files. A changed row takes the checksum of its file. A missing row and a dirty row go away. An out of order file needs a new generate, so repair does not touch it.

func StepMigration

func StepMigration(ctx context.Context, migrator Migrator, set *MigrationSet,
	input io.Reader, output io.Writer) error

func WriteMigrationFiles

func WriteMigrationFiles(directory string, slug string, generatedAt time.Time,
	toolVersion string, instructions []drivers.Instruction) ([]string, error)

func WriteSQLFile

func WriteSQLFile(tb testing.TB, directory string, name string, content string) string

Types

type AppliedMigration

type AppliedMigration struct {
	Version   string
	Name      string
	Checksum  string
	AppliedAt time.Time
	Dirty     bool
}

type Migration

type Migration struct {
	Version  string
	Name     string
	Path     string
	Checksum string
}

func ParseMigrationFileName

func ParseMigrationFileName(fileName string) (*Migration, error)

func ReadMigrationDirectory

func ReadMigrationDirectory(directory string) ([]*Migration, error)

type MigrationConfig

type MigrationConfig struct {
	Driver  drivers.DriverName `yaml:"driver"`
	Source  string             `yaml:"source"`
	Target  string             `yaml:"target"`
	Schema  string             `yaml:"schema"`
	Version string             `yaml:"version"`
}

func ReadMigrationConfig

func ReadMigrationConfig(path string, optional bool) (*MigrationConfig, error)

type MigrationEntry

type MigrationEntry struct {
	Migration *Migration
	State     MigrationState
	AppliedAt time.Time
}

func (*MigrationEntry) Content

func (e *MigrationEntry) Content() (string, error)

LoadMigrationSet reads the checksum, and this method reads the file again at the moment of the apply. An edit between the two reads gives a history row that names text that no engine ran.

func (*MigrationEntry) FileName

func (e *MigrationEntry) FileName() string

type MigrationSet

type MigrationSet struct {
	Entries []*MigrationEntry
}

func LoadMigrationSet

func LoadMigrationSet(ctx context.Context, migrator Migrator, directory string) (*MigrationSet, error)

func NewMigrationSet

func NewMigrationSet(files []*Migration, applied []AppliedMigration) *MigrationSet

func (*MigrationSet) Pending

func (s *MigrationSet) Pending() []*MigrationEntry

func (*MigrationSet) ProblemError

func (s *MigrationSet) ProblemError() error

func (*MigrationSet) Problems

func (s *MigrationSet) Problems() []*MigrationEntry

func (*MigrationSet) RecordError

func (s *MigrationSet) RecordError() error

type MigrationState

type MigrationState string
const (
	MigrationApplied    MigrationState = "applied"
	MigrationPending    MigrationState = "pending"
	MigrationChanged    MigrationState = "changed"
	MigrationMissing    MigrationState = "missing"
	MigrationOutOfOrder MigrationState = "out of order"
	MigrationDirty      MigrationState = "dirty"
)

type MigrationTransaction

type MigrationTransaction interface {
	Apply(ctx context.Context, statement string) error
	Record(ctx context.Context, migration *Migration) error
	Commit() error
	Rollback() error
}

type Migrator

type Migrator interface {
	Close() error
	EnsureHistoryTable(ctx context.Context) error
	AppliedMigrations(ctx context.Context) ([]AppliedMigration, error)
	Lock(ctx context.Context) error
	TryLock(ctx context.Context) (bool, error)
	Unlock(ctx context.Context) error
	Begin(ctx context.Context, useTransaction bool) (MigrationTransaction, error)
	RecordDirty(ctx context.Context, migration *Migration) error
	ClearDirty(ctx context.Context, migration *Migration) error
	UpdateChecksum(ctx context.Context, migration *Migration) error
	DeleteRecord(ctx context.Context, version string) error
}

func NewMigrator

func NewMigrator(ctx context.Context, driverName drivers.DriverName, target string,
	schema string) (Migrator, error)

type PostgresMigrationTransaction

type PostgresMigrationTransaction struct {
	Executor migrationExecutor

	// This field is nil when the file holds the no-transaction directive. Commit and
	// Rollback then do nothing, so the apply loop holds one path.
	Transaction *sql.Tx
}

func (*PostgresMigrationTransaction) Apply

func (t *PostgresMigrationTransaction) Apply(ctx context.Context, statement string) error

func (*PostgresMigrationTransaction) Commit

func (t *PostgresMigrationTransaction) Commit() error

func (*PostgresMigrationTransaction) Record

func (t *PostgresMigrationTransaction) Record(ctx context.Context, migration *Migration) error

func (*PostgresMigrationTransaction) Rollback

func (t *PostgresMigrationTransaction) Rollback() error

type PostgresMigrator

type PostgresMigrator struct {
	Database   *sql.DB
	Connection *sql.Conn
}

func NewPostgresMigrator

func NewPostgresMigrator(ctx context.Context, connectionString string, schema string) (*PostgresMigrator, error)

pg_advisory_lock holds a lock of the session, so every statement runs on one connection. A lock that one pooled connection takes, and that another releases, does nothing.

func (*PostgresMigrator) AppliedMigrations

func (m *PostgresMigrator) AppliedMigrations(ctx context.Context) ([]AppliedMigration, error)

A read must stay a read, so this method creates no table. The command status calls it against a database that never ran a migration.

func (*PostgresMigrator) Begin

func (m *PostgresMigrator) Begin(ctx context.Context, useTransaction bool) (MigrationTransaction, error)

func (*PostgresMigrator) ClearDirty

func (m *PostgresMigrator) ClearDirty(ctx context.Context, migration *Migration) error

func (*PostgresMigrator) Close

func (m *PostgresMigrator) Close() error

func (*PostgresMigrator) DeleteRecord

func (m *PostgresMigrator) DeleteRecord(ctx context.Context, version string) error

func (*PostgresMigrator) EnsureHistoryTable

func (m *PostgresMigrator) EnsureHistoryTable(ctx context.Context) error

func (*PostgresMigrator) Lock

func (m *PostgresMigrator) Lock(ctx context.Context) error

func (*PostgresMigrator) RecordDirty

func (m *PostgresMigrator) RecordDirty(ctx context.Context, migration *Migration) error

func (*PostgresMigrator) TryLock

func (m *PostgresMigrator) TryLock(ctx context.Context) (bool, error)

func (*PostgresMigrator) Unlock

func (m *PostgresMigrator) Unlock(ctx context.Context) error

func (*PostgresMigrator) UpdateChecksum

func (m *PostgresMigrator) UpdateChecksum(ctx context.Context, migration *Migration) error

type SQLiteMigrationTransaction

type SQLiteMigrationTransaction struct {
	Executor migrationExecutor

	Transaction *sql.Tx
}

func (*SQLiteMigrationTransaction) Apply

func (t *SQLiteMigrationTransaction) Apply(ctx context.Context, statement string) error

func (*SQLiteMigrationTransaction) Commit

func (t *SQLiteMigrationTransaction) Commit() error

func (*SQLiteMigrationTransaction) Record

func (t *SQLiteMigrationTransaction) Record(ctx context.Context, migration *Migration) error

func (*SQLiteMigrationTransaction) Rollback

func (t *SQLiteMigrationTransaction) Rollback() error

type SQLiteMigrator

type SQLiteMigrator struct {
	Connection *sql.DB
}

func NewSQLiteMigrator

func NewSQLiteMigrator(path string) (*SQLiteMigrator, error)

func (*SQLiteMigrator) AppliedMigrations

func (m *SQLiteMigrator) AppliedMigrations(ctx context.Context) ([]AppliedMigration, error)

func (*SQLiteMigrator) Begin

func (m *SQLiteMigrator) Begin(ctx context.Context, useTransaction bool) (MigrationTransaction, error)

func (*SQLiteMigrator) ClearDirty

func (m *SQLiteMigrator) ClearDirty(ctx context.Context, migration *Migration) error

func (*SQLiteMigrator) Close

func (m *SQLiteMigrator) Close() error

func (*SQLiteMigrator) DeleteRecord

func (m *SQLiteMigrator) DeleteRecord(ctx context.Context, version string) error

func (*SQLiteMigrator) EnsureHistoryTable

func (m *SQLiteMigrator) EnsureHistoryTable(ctx context.Context) error

func (*SQLiteMigrator) Lock

func (m *SQLiteMigrator) Lock(ctx context.Context) error

A write transaction of SQLite takes the lock of the database already, and a second process then reads the error SQLITE_BUSY. A lock table adds a failure mode and no safety.

func (*SQLiteMigrator) RecordDirty

func (m *SQLiteMigrator) RecordDirty(ctx context.Context, migration *Migration) error

func (*SQLiteMigrator) TryLock

func (m *SQLiteMigrator) TryLock(ctx context.Context) (bool, error)

func (*SQLiteMigrator) Unlock

func (m *SQLiteMigrator) Unlock(ctx context.Context) error

func (*SQLiteMigrator) UpdateChecksum

func (m *SQLiteMigrator) UpdateChecksum(ctx context.Context, migration *Migration) error

type TestingPostgresMigrator

type TestingPostgresMigrator struct {
	*PostgresMigrator
	// contains filtered or unexported fields
}

func NewTestPostgresMigrator

func NewTestPostgresMigrator(tb testing.TB) *TestingPostgresMigrator

func (*TestingPostgresMigrator) Schema

func (m *TestingPostgresMigrator) Schema() string

func (*TestingPostgresMigrator) TableNames

func (m *TestingPostgresMigrator) TableNames() []string

type TestingSQLiteMigrator

type TestingSQLiteMigrator struct {
	*SQLiteMigrator

	Path string
	// contains filtered or unexported fields
}

func NewTestSQLiteMigrator

func NewTestSQLiteMigrator(tb testing.TB) *TestingSQLiteMigrator

func (*TestingSQLiteMigrator) TableNames

func (m *TestingSQLiteMigrator) TableNames() []string

Jump to

Keyboard shortcuts

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