migrate

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(ctx context.Context, dbConn *sql.DB, dir string) error

func GenerateFromSnapshot

func GenerateFromSnapshot(newSnap Snapshot, dir, name string, prompt PromptFunc) (string, error)

func GenerateFromSnapshotOpts added in v0.1.3

func GenerateFromSnapshotOpts(newSnap Snapshot, dir, name string, prompt PromptFunc, opts GenerateOptions) (string, error)

func ImportPathForDir

func ImportPathForDir(moduleDir, targetDir string) (string, error)

func Init added in v0.1.3

func Init(ctx context.Context, dbConn *sql.DB, dir string) error

Init ensures the migrations directory and the grain.schema_migrations tracking table exist, without applying anything.

func RollbackLast

func RollbackLast(ctx context.Context, dbConn *sql.DB, dir string) error

RollbackLast rolls back the single most recent applied migration.

func RollbackN added in v0.1.3

func RollbackN(ctx context.Context, dbConn *sql.DB, dir string, n int) error

RollbackN rolls back the n most recent applied migrations (or all of them if fewer than n are applied).

func SaveSnapshot

func SaveSnapshot(dir, version, name string, snap Snapshot) error

Types

type AmbiguityKind

type AmbiguityKind string
const (
	AmbiguousTable  AmbiguityKind = "table"
	AmbiguousColumn AmbiguityKind = "column"
	AmbiguousEnum   AmbiguityKind = "enum"
)

type Change

type Change struct {
	Kind    ChangeKind
	UpSQL   string
	DownSQL string

	// Destructive means this change can lose data in a way no amount of user
	// confirmation resolves — e.g. a column type or a NOT NULL column with no default.
	Destructive bool

	// RequiresNoTx means this statement cannot run inside the transaction
	// Apply() wraps each migration file in — e.g. ALTER TYPE ... ADD VALUE.
	// The generate step emits these into dedicated `-- +RequiresNoTx Up|Down`
	// sections that the apply step runs outside the migration transaction.
	RequiresNoTx bool
	Note         string
}

func Diff

func Diff(old, new Snapshot, prompt PromptFunc) ([]Change, error)

type ChangeKind

type ChangeKind string
const (
	CreateTable     ChangeKind = "create_table"
	DropTable       ChangeKind = "drop_table"
	RenameTable     ChangeKind = "rename_table"
	AddColumn       ChangeKind = "add_column"
	DropColumn      ChangeKind = "drop_column"
	RenameColumn    ChangeKind = "rename_column"
	AlterColumnType ChangeKind = "alter_column_type"

	AlterColumnIdentity    ChangeKind = "alter_column_identity"
	AlterColumnDefault     ChangeKind = "alter_column_default"
	AlterColumnNullability ChangeKind = "alter_column_nullability"
	AlterColumnGenerated   ChangeKind = "alter_column_generated"
	AlterColumnCheck       ChangeKind = "alter_column_check"
	AddColumnUnique        ChangeKind = "add_column_unique"
	DropColumnUnique       ChangeKind = "drop_column_unique"
	AddPrimaryKey          ChangeKind = "add_primary_key"
	DropPrimaryKey         ChangeKind = "drop_primary_key"
	AddForeignKey          ChangeKind = "add_foreign_key"
	DropForeignKey         ChangeKind = "drop_foreign_key"
	AddUniqueConstraint    ChangeKind = "add_unique_constraint"
	DropUniqueConstraint   ChangeKind = "drop_unique_constraint"
	AddConstraint          ChangeKind = "add_constraint"
	DropConstraint         ChangeKind = "drop_constraint"

	CreateEnum   ChangeKind = "create_enum"
	DropEnum     ChangeKind = "drop_enum"
	RenameEnum   ChangeKind = "rename_enum"
	AddEnumValue ChangeKind = "add_enum_value"
	AddIndex     ChangeKind = "add_index"
	DropIndex    ChangeKind = "drop_index"
)

type ColumnSnapshot

type ColumnSnapshot struct {
	Name        string `json:"name"`
	Type        string `json:"type"`
	NotNull     bool   `json:"not_null,omitempty"`
	Unique      bool   `json:"unique,omitempty"`
	PK          bool   `json:"pk,omitempty"`
	Identity    bool   `json:"identity,omitempty"`
	Default     any    `json:"default,omitempty"`
	DefaultExpr string `json:"default_expr,omitempty"`
	Generated   string `json:"generated,omitempty"`
	Check       string `json:"check,omitempty"`
	Collation   string `json:"collation,omitempty"`
	RefTable    string `json:"ref_table,omitempty"`
	RefColumn   string `json:"ref_column,omitempty"`
	OnDelete    string `json:"on_delete,omitempty"`
	OnUpdate    string `json:"on_update,omitempty"`
}

type ConstraintSnapshot added in v0.1.3

type ConstraintSnapshot struct {
	Name string `json:"name"`
	Expr string `json:"expr"`
}

type EnumSnapshot

type EnumSnapshot struct {
	Name   string   `json:"name"`
	Values []string `json:"values"`
}

type GenerateOptions added in v0.1.3

type GenerateOptions struct {
	// Force bypasses the ManualReviewRequiredError guard and writes destructive
	// changes (type casts without USING, SET NOT NULL without backfill, etc.)
	// into the migration file. Use only when you know the data is safe.
	Force bool
}

type IndexSnapshot added in v0.1.3

type IndexSnapshot struct {
	Name      string   `json:"name"`
	Cols      []string `json:"cols"`
	Unique    bool     `json:"unique,omitempty"`
	Predicate string   `json:"predicate,omitempty"`
}

type ManualReviewRequiredError

type ManualReviewRequiredError struct {
	Changes []Change
}

func (*ManualReviewRequiredError) Error

func (e *ManualReviewRequiredError) Error() string

type MigrationFile

type MigrationFile struct {
	Version     int64
	Name        string
	UpSQL       string
	DownSQL     string
	NoTxUpSQL   string
	NoTxDownSQL string
	Path        string
	Checksum    string
}

func LoadMigrations

func LoadMigrations(dir string) ([]MigrationFile, error)

type MigrationStatus

type MigrationStatus struct {
	MigrationFile
	Applied          bool
	ChecksumVerified bool
	Orphaned         bool
}

func Status

func Status(ctx context.Context, dbConn *sql.DB, dir string) ([]MigrationStatus, error)

type PromptContext

type PromptContext struct {
	Kind       AmbiguityKind
	Table      string
	OldName    string
	Candidates []string
}

type PromptFunc

type PromptFunc func(ctx PromptContext) (Resolution, error)

type Resolution

type Resolution struct {
	Action string // "rename", "delete", or "ignore"
	Target string // the new name, when Action == "rename"
}

func AutoResolvePrompt added in v0.1.3

func AutoResolvePrompt(ctx PromptContext) (Resolution, error)

AutoResolvePrompt drives rename/delete decisions without a human. It only renames when there is exactly one candidate; an ambiguous set of candidates is ignored rather than guessing wrong silently.

func CLIPrompt

func CLIPrompt(ctx PromptContext) (Resolution, error)

type Snapshot

type Snapshot struct {
	FormatVersion int             `json:"format_version"`
	Tables        []TableSnapshot `json:"tables"`
	Enums         []EnumSnapshot  `json:"enums"`
}

func BuildSnapshot

func BuildSnapshot() Snapshot

BuildSnapshot walks schema.Registry and schema.EnumRegistry to produce the full schema state. It is also called from the dynamically generated loader program in LoadSnapshotFromPackage, so don't change the Snapshot shape without bumping snapshotFormatVersion.

func LoadLatestSnapshot

func LoadLatestSnapshot(dir string) (Snapshot, error)

func LoadSnapshotFromDir

func LoadSnapshotFromDir(moduleDir, schemaDir string) (Snapshot, error)

func LoadSnapshotFromPackage

func LoadSnapshotFromPackage(moduleDir, schemaImportPath string) (Snapshot, error)

type TableSnapshot

type TableSnapshot struct {
	Name        string               `json:"name"`
	Columns     []ColumnSnapshot     `json:"columns"`
	Indexes     []IndexSnapshot      `json:"indexes"`
	Uniques     []IndexSnapshot      `json:"uniques"`
	Constraints []ConstraintSnapshot `json:"constraints"`
}

Jump to

Keyboard shortcuts

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