migrator

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package migrator provides schema snapshot, diff, and SQL migration helpers for the Rain CLI.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplySQLMigrations

func ApplySQLMigrations(ctx context.Context, db *sql.DB, dialectName, tableName string, migrationsOnDisk []DiskMigration) (migrate.ApplyResult, error)

ApplySQLMigrations applies ordered SQL migrations using the existing migration table tracking.

func MarshalSnapshot

func MarshalSnapshot(snapshot Snapshot) ([]byte, error)

MarshalSnapshot produces stable, indented JSON for disk storage.

func SplitSQLStatements

func SplitSQLStatements(sqlText string) ([]string, error)

SplitSQLStatements splits one migration file into executable statements.

Types

type ColumnSnapshot

type ColumnSnapshot struct {
	Name          string            `json:"name"`
	Type          schema.ColumnType `json:"type"`
	Nullable      bool              `json:"nullable"`
	DefaultSQL    string            `json:"default_sql,omitempty"`
	HasDefault    bool              `json:"has_default"`
	PrimaryKey    bool              `json:"primary_key"`
	AutoIncrement bool              `json:"auto_increment"`
	Unique        bool              `json:"unique"`
	DefinitionSQL string            `json:"definition_sql"`
}

ColumnSnapshot stores one column definition and its additive DDL fragment.

type ConstraintSnapshot

type ConstraintSnapshot struct {
	Name string `json:"name"`
	Type string `json:"type"`
	SQL  string `json:"sql"`
}

ConstraintSnapshot stores one table-level constraint.

type DiskMigration

type DiskMigration struct {
	ID           string
	Name         string
	DirPath      string
	SQLPath      string
	SnapshotPath string
	SQL          string
	Snapshot     Snapshot
}

DiskMigration represents one migration folder on disk.

func LoadDiskMigrations

func LoadDiskMigrations(dir string) ([]DiskMigration, error)

LoadDiskMigrations reads ordered migration folders and validates required files.

func WriteMigrationFiles

func WriteMigrationFiles(dir, name string, plan Plan, snapshot Snapshot, now time.Time) (DiskMigration, error)

WriteMigrationFiles writes one Drizzle-style migration folder containing migration.sql and snapshot.json.

type ForeignKeySnapshot

type ForeignKeySnapshot struct {
	Name string `json:"name"`
	SQL  string `json:"sql"`
}

ForeignKeySnapshot stores one single-column foreign key.

type IndexSnapshot

type IndexSnapshot struct {
	Name string `json:"name"`
	SQL  string `json:"sql"`
}

IndexSnapshot stores one standalone index.

type Plan

type Plan struct {
	Statements []string
}

Plan summarizes one additive migration diff.

func DiffSnapshots

func DiffSnapshots(previous *Snapshot, current Snapshot) (Plan, error)

DiffSnapshots creates an additive-only migration plan.

func (Plan) Empty

func (p Plan) Empty() bool

Empty reports whether the plan contains no SQL statements.

type Registry

type Registry func() []schema.TableReference

Registry returns the managed tables that participate in migration generation.

type Snapshot

type Snapshot struct {
	Version int             `json:"version"`
	Dialect string          `json:"dialect"`
	Tables  []TableSnapshot `json:"tables"`
}

Snapshot captures a deterministic schema view and the SQL fragments needed for additive diffs.

func BuildSnapshot

func BuildSnapshot(dialectName string, tables []schema.TableReference) (Snapshot, error)

BuildSnapshot compiles managed tables into a deterministic snapshot.

func ReadLatestSnapshotFromMigrations

func ReadLatestSnapshotFromMigrations(dir string) (*Snapshot, error)

ReadLatestSnapshotFromMigrations returns the newest migration snapshot or nil when no migrations exist.

func UnmarshalSnapshot

func UnmarshalSnapshot(data []byte) (Snapshot, error)

UnmarshalSnapshot parses one JSON snapshot file.

type TableSnapshot

type TableSnapshot struct {
	Name           string               `json:"name"`
	CreateTableSQL string               `json:"create_table_sql"`
	Columns        []ColumnSnapshot     `json:"columns"`
	Constraints    []ConstraintSnapshot `json:"constraints"`
	ForeignKeys    []ForeignKeySnapshot `json:"foreign_keys"`
	Indexes        []IndexSnapshot      `json:"indexes"`
}

TableSnapshot stores a portable, deterministic representation of one table.

Jump to

Keyboard shortcuts

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