Documentation
¶
Index ¶
- func Apply(ctx context.Context, dbConn *sql.DB, dir string) error
- func GenerateFromSnapshot(newSnap Snapshot, dir, name string, prompt PromptFunc) (string, error)
- func GenerateFromSnapshotOpts(newSnap Snapshot, dir, name string, prompt PromptFunc, opts GenerateOptions) (string, error)
- func ImportPathForDir(moduleDir, targetDir string) (string, error)
- func Init(ctx context.Context, dbConn *sql.DB, dir string) error
- func RollbackLast(ctx context.Context, dbConn *sql.DB, dir string) error
- func RollbackN(ctx context.Context, dbConn *sql.DB, dir string, n int) error
- func SaveSnapshot(dir, version, name string, snap Snapshot) error
- type AmbiguityKind
- type Change
- type ChangeKind
- type ColumnSnapshot
- type ConstraintSnapshot
- type EnumSnapshot
- type GenerateOptions
- type IndexSnapshot
- type ManualReviewRequiredError
- type MigrationFile
- type MigrationStatus
- type PromptContext
- type PromptFunc
- type Resolution
- type Snapshot
- type TableSnapshot
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 Init ¶ added in v0.1.3
Init ensures the migrations directory and the grain.schema_migrations tracking table exist, without applying anything.
func RollbackLast ¶
RollbackLast rolls back the single most recent applied migration.
func RollbackN ¶ added in v0.1.3
RollbackN rolls back the n most recent applied migrations (or all of them if fewer than n are applied).
func SaveSnapshot ¶
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
}
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 EnumSnapshot ¶
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 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
}
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 LoadSnapshotFromDir ¶
func LoadSnapshotFromPackage ¶
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"`
}