migrate

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

ddl.go generates dialect-specific DDL SQL from a migration changeset.

Package migrate implements the `gco migrate` commands for database migration management.

planner.go implements the schema diff engine that compares two IR schemas and produces a structured changeset describing the migration steps.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateMigrationsTable

func CreateMigrationsTable(dialect string) string

CreateMigrationsTable returns the SQL to create the migrations metadata table.

func Run

func Run(args []string) error

Run executes the migrate subcommand.

Types

type Change

type Change struct {
	Type     ChangeType
	Model    string            // Table/model name.
	Field    string            // Column name (if applicable).
	OldValue string            // Previous value (for renames, type changes).
	NewValue string            // New value.
	Details  map[string]string // Additional context.
	Rollback RollbackSafety
}

Change represents a single schema change.

type ChangeRecord

type ChangeRecord struct {
	Type     string `json:"type"`
	Model    string `json:"model"`
	Field    string `json:"field,omitempty"`
	Rollback string `json:"rollback"`
}

ChangeRecord stores a single schema change in the migration manifest.

type ChangeType

type ChangeType string

ChangeType categorizes a schema change.

const (
	CreateTable  ChangeType = "CreateTable"
	DropTable    ChangeType = "DropTable"
	RenameTable  ChangeType = "RenameTable"
	AddColumn    ChangeType = "AddColumn"
	DropColumn   ChangeType = "DropColumn"
	RenameColumn ChangeType = "RenameColumn"
	AlterType    ChangeType = "AlterType"
	AlterNull    ChangeType = "AlterNullability"
	AlterDefault ChangeType = "AlterDefault"
	AddIndex     ChangeType = "AddIndex"
	DropIndex    ChangeType = "DropIndex"
	AddUnique    ChangeType = "AddUnique"
	DropUnique   ChangeType = "DropUnique"
	ChangePK     ChangeType = "ChangePrimaryKey"
	AddFK        ChangeType = "AddForeignKey"
	DropFK       ChangeType = "DropForeignKey"
)

type Changeset

type Changeset struct {
	Changes []Change
	Old     *ir.Schema // Source schema (may be nil for initial migration).
	New     *ir.Schema // Target schema (may be nil for teardown).
}

Changeset is an ordered list of changes from old to new schema.

func Diff

func Diff(old, new *ir.Schema) *Changeset

Diff compares two schemas and returns the changes needed to go from old to new.

type DDLGenerator

type DDLGenerator struct {
	Dialect string     // "postgresql", "mysql", "sqlite"
	Schema  *ir.Schema // Target schema for type lookups.
}

DDLGenerator generates dialect-specific DDL from a changeset.

func (DDLGenerator) GenerateDown

func (g DDLGenerator) GenerateDown(cs *Changeset) string

GenerateDown produces the best-effort down migration SQL.

func (DDLGenerator) GenerateUp

func (g DDLGenerator) GenerateUp(cs *Changeset) string

GenerateUp produces the up migration SQL.

type MigrationManifest

type MigrationManifest struct {
	ID             string         `json:"id"`
	Description    string         `json:"description"`
	Checksum       string         `json:"checksum"`
	CreatedAt      time.Time      `json:"createdAt"`
	ToolVersion    string         `json:"toolVersion"`
	DestructiveOps []string       `json:"destructiveOps,omitempty"`
	ReviewRequired bool           `json:"reviewRequired"`
	Changes        []ChangeRecord `json:"changes,omitempty"`
	Models         []string       `json:"models,omitempty"`
}

MigrationManifest describes a single migration.

type MigrationRecord

type MigrationRecord struct {
	ID              string `json:"id"`
	Checksum        string `json:"checksum"`
	AppliedAt       string `json:"appliedAt"`
	ExecutionTimeMs int64  `json:"executionTimeMs"`
	Status          string `json:"status"` // "applied", "failed", "rolled_back"
	ToolVersion     string `json:"toolVersion"`
}

MigrationRecord tracks an applied migration in the database.

type RollbackSafety

type RollbackSafety string

RollbackSafety indicates how safe it is to reverse a change.

const (
	SafeRollback        RollbackSafety = "safe"
	DestructiveRollback RollbackSafety = "destructive"
	ReviewRequired      RollbackSafety = "review"
)

Jump to

Keyboard shortcuts

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