migration

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIrreversibleMigration = errors.New("tango migration: migration is irreversible")

ErrIrreversibleMigration is returned by RollbackLast when the most recently applied migration is marked irreversible.

View Source
var ErrNoAppliedMigrations = errors.New("tango migration: no applied migrations to roll back")

ErrNoAppliedMigrations is returned by RollbackLast when tango_migrations has nothing recorded to roll back.

Functions

func AppliedMigrations

func AppliedMigrations(ctx context.Context, sqlDB *sql.DB) (map[MigrationKey]bool, error)

AppliedMigrations returns the set of applied app-scoped migration keys.

func ApplyPending

func ApplyPending(ctx context.Context, sqlDB *sql.DB, dialect db.Dialect, migrations []Migration) error

ApplyPending applies every migration in migrations whose Name is not already recorded in tango_migrations, in slice order, translating each Up step via ApplyStep and recording one tango_migrations row per (app, name) pair the migration touches.

func ApplyStep

func ApplyStep(ctx context.Context, sqlDB *sql.DB, dialect db.Dialect, step Step) error

ApplyStep executes step against sqlDB, translating it into the DDL appropriate for dialect. SQLite steps it cannot express via a direct ALTER TABLE (DropColumn, AlterColumnUnique) are applied via the standard table-rebuild pattern: create a new table with the desired shape, copy data across, drop the old table, and rename the new one into place. It is exported so ApplyPending and the tango CLI can share DDL translation; application code should use tango migrate rather than calling ApplyStep directly.

func EnsureTrackingTable

func EnsureTrackingTable(ctx context.Context, sqlDB *sql.DB, dialect db.Dialect) error

EnsureTrackingTable creates the tango_migrations table if it doesn't already exist.

func IsMissingTrackingTable

func IsMissingTrackingTable(err error) bool

IsMissingTrackingTable reports whether err means the tango_migrations table does not exist yet. Status uses this to stay read-only: creating the tracking table remains ApplyPending's responsibility.

func RollbackLast

func RollbackLast(ctx context.Context, sqlDB *sql.DB, dialect db.Dialect, migrations []Migration) error

RollbackLast rolls back the most recently applied migration (by applied_at) by running its Down steps, then removes its tango_migrations row(s). It fails, changing nothing, if that migration is marked irreversible in migrations, or if migrations doesn't contain it.

Types

type AddColumn

type AddColumn struct {
	Table  string
	Column Column
}

AddColumn adds a column to an existing table. It is exported for generated migration files; application code should use the tango CLI.

type AlterColumnUnique

type AlterColumnUnique struct {
	Table  string
	Column string
	Unique bool
}

AlterColumnUnique changes a column's uniqueness constraint. It is exported for generated migration files; application code should use the tango CLI.

type Column

type Column struct {
	Name       string
	Type       string
	PrimaryKey bool
	Unique     bool
	Indexed    bool
	// References is the target table name for a foreign key column (from a
	// tango:"fk=X" tag), or "" for an ordinary column. The generated
	// constraint is always RESTRICT/NO ACTION: cascade delete is
	// implemented in application code by Store.Delete, never by the
	// database itself.
	References string
	// Default is a raw SQL literal (e.g. "TRUE") the column is given as a
	// NOT NULL DEFAULT when adding it to an existing table, so pre-existing
	// rows backfill to that value instead of NULL. Empty means no default:
	// the column is added exactly as before this field existed. There is no
	// struct-tag-driven way to set this — it is only ever hand-authored on a
	// specific generated migration where an existing model needs it.
	Default string
}

Column describes the SQL-relevant shape of one table column. It is exported for generated migration files and CLI diffing, not as a hand-authored v0.0.1 application API.

type ColumnState

type ColumnState struct {
	Name       string
	Type       string
	PrimaryKey bool
	Unique     bool
	Indexed    bool
	References string
}

ColumnState is a column's shape as reconstructed by Replay. It is exported only for the tango CLI's migration replay/diff machinery.

type CreateIndex

type CreateIndex struct {
	Table  string
	Column string
}

CreateIndex creates a plain index for a table column. It is exported for generated migration files; application code should use the tango CLI.

type CreateTable

type CreateTable struct {
	Table   string
	Columns []Column
}

CreateTable creates a table with the provided columns. It is exported for generated migration files; application code should use the tango CLI.

type DropColumn

type DropColumn struct {
	Table  string
	Column string
}

DropColumn removes a column from an existing table. It is exported for generated migration files; application code should use the tango CLI.

type DropIndex

type DropIndex struct {
	Table  string
	Column string
}

DropIndex removes a plain index for a table column. It is exported for generated migration files; application code should use the tango CLI.

type DropTable

type DropTable struct {
	Table string
}

DropTable removes a table. It is exported for generated migration files; application code should use the tango CLI.

type Migration

type Migration struct {
	App        string
	Name       string
	Up         []Step
	Down       []Step
	Reversible bool
}

Migration groups the up/down steps for one generated migration file. It is part of the runtime contract accepted by ApplyPending and RollbackLast, but application code should normally use values generated by tango makemigrations.

func Diff

func Diff(models []model.ModelMeta, state SchemaState) []Migration

Diff compares the desired schema (derived from models) against state (the replayed result of existing migrations) and returns one Migration per app with detected changes, keyed by ModelMeta.App. A model whose App is empty is grouped under the empty-string key. Returns no migrations if nothing changed. It is exported only for the tango CLI's makemigrations machinery.

func DiffModels

func DiffModels(models []Model, state SchemaState) []Migration

DiffModels compares desired model shapes against a replayed migration state. It is exported only for the tango CLI's makemigrations machinery.

type MigrationKey

type MigrationKey struct {
	App  string
	Name string
}

MigrationKey identifies one app-scoped migration row.

type Model

type Model struct {
	App     string
	Name    string
	Columns []Column
}

Model is the SQL-relevant model shape makemigrations needs from an app. It is exported so tango's app-side "-tango-dump-models" JSON payload and CLI can share a type; do not treat it as a hand-authored application API.

func ModelsFromMeta

func ModelsFromMeta(models []model.ModelMeta) []Model

ModelsFromMeta converts registered model metadata into the dialect- and reflect-free Model shape the CLI works with (e.g. to serialize as JSON for the "-tango-dump-models" flag convention `tango makemigrations` relies on). It is exported only for tango's CLI/app-side convention.

type SchemaState

type SchemaState struct {
	Tables map[string]TableState
}

SchemaState is the reconstructed shape of every table after replaying a sequence of migrations. It is exported only for the tango CLI's migration replay/diff machinery.

func Replay

func Replay(migrations []Migration) (SchemaState, error)

Replay applies every migration's Up steps, in order, starting from an empty schema, and returns the resulting state. It is exported only for the tango CLI's makemigrations machinery.

type Step

type Step interface {
	// contains filtered or unexported methods
}

Step is a typed, dialect-agnostic schema migration operation. It is exported so generated migration files and the tango CLI can share one representation; do not hand-author Step values directly in application code.

type TableState

type TableState struct {
	Name    string
	App     string
	Columns []ColumnState
}

TableState is a table's shape as reconstructed by Replay. It is exported only for the tango CLI's migration replay/diff machinery.

Jump to

Keyboard shortcuts

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