{"modulePath":"github.com/GoCodeAlone/workflow","version":"v0.87.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package migration\n\nPackage migration provides a migration management system with schema providers, distributed locking, and a migration runner for applying database schema changes.\n\n## Functions\n\n```go\nfunc DiffSchemas(oldDDL, newDDL string) (upSQL string, downSQL string)\n```\nDiffSchemas compares an old and new schema DDL and generates ALTER TABLE statements to migrate from old to new. It handles added columns, dropped columns, and added/dropped indexes. It does NOT handle column type changes or renames (those require manual migration).\n\n## Types\n\n```go\ntype AppliedMigration struct {\n\tSchemaName string\n\tVersion    int\n\tChecksum   string\n\tAppliedAt  time.Time\n}\n```\nAppliedMigration records a migration that has already been applied.\n\n```go\ntype ColumnDef struct {\n\tName       string\n\tDefinition string // full column definition (type + constraints)\n}\n```\nColumnDef represents a column within a CREATE TABLE statement.\n\n```go\ntype DistributedLock interface {\n\t// Acquire obtains the lock for the given key. The returned release function\n\t// must be called to release the lock. The lock is held until release is called\n\t// or the context is cancelled.\n\tAcquire(ctx context.Context, key string) (release func(), err error)\n}\n```\nDistributedLock provides mutual exclusion for migration operations across multiple processes or nodes.\n\n```go\ntype MigrationRunner struct {\n\t// contains filtered or unexported fields\n}\n```\nMigrationRunner coordinates schema migrations across multiple providers.\n\n```go\nfunc NewMigrationRunner(store MigrationStore, locker DistributedLock, logger *slog.Logger) *MigrationRunner\n```\nNewMigrationRunner creates a new MigrationRunner.\n\n```go\nfunc (r *MigrationRunner) Pending(ctx context.Context, providers ...SchemaProvider) ([]PendingMigration, error)\n```\nPending returns all pending migrations for the given providers without applying them.\n\n```go\nfunc (r *MigrationRunner) Run(ctx context.Context, db *sql.DB, providers ...SchemaProvider) error\n```\nRun applies all pending migrations for the given providers. It acquires a distributed lock, queries the store for applied versions, compares against providers' current versions, runs pending diffs in order, and records each applied migration.\n\n```go\nfunc (r *MigrationRunner) Status(ctx context.Context, providers ...SchemaProvider) (map[string][]AppliedMigration, error)\n```\nStatus returns the applied migrations for each provider.\n\n```go\ntype MigrationStore interface {\n\t// Applied returns all applied migrations for the given schema, ordered by version.\n\tApplied(ctx context.Context, schemaName string) ([]AppliedMigration, error)\n\t// Record stores an applied migration record.\n\tRecord(ctx context.Context, schemaName string, version int, checksum string) error\n}\n```\nMigrationStore persists records of applied migrations.\n\n```go\ntype PendingMigration struct {\n\tSchemaName  string\n\tFromVersion int\n\tToVersion   int\n\tUpSQL       string\n}\n```\nPendingMigration describes a migration that has not yet been applied.\n\n```go\ntype PostgresLock struct {\n\t// contains filtered or unexported fields\n}\n```\nPostgresLock implements DistributedLock using PostgreSQL advisory locks.\n\n```go\nfunc NewPostgresLock(db *sql.DB) *PostgresLock\n```\nNewPostgresLock creates a new PostgresLock.\n\n```go\nfunc (l *PostgresLock) Acquire(ctx context.Context, key string) (func(), error)\n```\nAcquire obtains a PostgreSQL advisory lock. The lock key is hashed to an int64 from the string key. The returned release function unlocks the advisory lock.\n\n```go\ntype SQLiteLock struct {\n\t// contains filtered or unexported fields\n}\n```\nSQLiteLock implements DistributedLock using a process-local mutex. SQLite is inherently single-writer, so a mutex is sufficient for ensuring only one migration runs at a time within a process. For cross-process safety, SQLite's built-in file locking provides protection.\n\n```go\nfunc NewSQLiteLock(_ *sql.DB) *SQLiteLock\n```\nNewSQLiteLock creates a new SQLiteLock. The db parameter is accepted for interface consistency but the lock uses an in-process mutex since SQLite does not support advisory locks.\n\n```go\nfunc (l *SQLiteLock) Acquire(ctx context.Context, _ string) (func(), error)\n```\nAcquire obtains the mutex lock. Returns an error if the context is already cancelled.\n\n```go\ntype SQLiteMigrationStore struct {\n\t// contains filtered or unexported fields\n}\n```\nSQLiteMigrationStore implements MigrationStore using SQLite.\n\n```go\nfunc NewSQLiteMigrationStore(db *sql.DB) (*SQLiteMigrationStore, error)\n```\nNewSQLiteMigrationStore creates a new SQLiteMigrationStore and ensures the \\_migrations table exists.\n\n```go\nfunc (s *SQLiteMigrationStore) Applied(ctx context.Context, schemaName string) ([]AppliedMigration, error)\n```\nApplied returns all applied migrations for the given schema, ordered by version.\n\n```go\nfunc (s *SQLiteMigrationStore) Record(ctx context.Context, schemaName string, version int, checksum string) error\n```\nRecord stores an applied migration record.\n\n```go\ntype SchemaDiff struct {\n\tFromVersion int\n\tToVersion   int\n\tUpSQL       string\n\tDownSQL     string\n}\n```\nSchemaDiff represents a single migration step between two schema versions.\n\n```go\ntype SchemaProvider interface {\n\t// SchemaName returns a unique identifier for this schema (e.g., \"workflow_executions\").\n\tSchemaName() string\n\t// SchemaVersion returns the current version of the schema.\n\tSchemaVersion() int\n\t// SchemaSQL returns the full DDL for the current schema version.\n\tSchemaSQL() string\n\t// SchemaDiffs returns ordered diffs for migrating between versions.\n\tSchemaDiffs() []SchemaDiff\n}\n```\nSchemaProvider defines the interface for components that own database schemas. Each provider declares its schema name, current version, full DDL, and ordered diffs.\n\n```go\ntype TableDef struct {\n\tName    string\n\tColumns []ColumnDef\n\tIndexes []string // raw CREATE INDEX statements\n}\n```\nTableDef represents a parsed CREATE TABLE statement.\n\n","path":"github.com/GoCodeAlone/workflow/migration","name":"migration","synopsis":"Package migration provides a migration management system with schema providers, distributed locking, and a migration runner for applying database schema changes.","isRedistributable":true}
