Documentation
¶
Index ¶
- Variables
- func AppliedMigrations(ctx context.Context, sqlDB *sql.DB) (map[MigrationKey]bool, error)
- func ApplyPending(ctx context.Context, sqlDB *sql.DB, dialect db.Dialect, migrations []Migration) error
- func ApplyStep(ctx context.Context, sqlDB *sql.DB, dialect db.Dialect, step Step) error
- func EnsureTrackingTable(ctx context.Context, sqlDB *sql.DB, dialect db.Dialect) error
- func IsMissingTrackingTable(err error) bool
- func RollbackLast(ctx context.Context, sqlDB *sql.DB, dialect db.Dialect, migrations []Migration) error
- type AddColumn
- type AlterColumnUnique
- type Column
- type ColumnState
- type CreateIndex
- type CreateTable
- type DropColumn
- type DropIndex
- type DropTable
- type Migration
- type MigrationKey
- type Model
- type SchemaState
- type Step
- type TableState
Constants ¶
This section is empty.
Variables ¶
var ErrIrreversibleMigration = errors.New("tango migration: migration is irreversible")
ErrIrreversibleMigration is returned by RollbackLast when the most recently applied migration is marked irreversible.
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 ¶
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 ¶
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 ¶
EnsureTrackingTable creates the tango_migrations table if it doesn't already exist.
func IsMissingTrackingTable ¶
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 ¶
AddColumn adds a column to an existing table. It is exported for generated migration files; application code should use the tango CLI.
type AlterColumnUnique ¶
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 ¶
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 ¶
CreateTable creates a table with the provided columns. It is exported for generated migration files; application code should use the tango CLI.
type DropColumn ¶
DropColumn removes a column from an existing table. It is exported for generated migration files; application code should use the tango CLI.
type DropIndex ¶
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 ¶
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 ¶
MigrationKey identifies one app-scoped migration row.
type Model ¶
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 ¶
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.