Documentation
¶
Overview ¶
Package gomigration provides a PostgreSQL migration driver for managing and applying SQL migrations.
Package gomigration provides tools for managing database migrations in Go projects. It supports creating, applying, rolling back, and listing migrations using a customizable driver interface.
Index ¶
- Variables
- type Cli
- func (c *Cli) CleanCommand(ctx context.Context) *cobra.Command
- func (c *Cli) CreateCommand(ctx context.Context) *cobra.Command
- func (c *Cli) Execute(ctx context.Context) error
- func (c *Cli) ListCommand(ctx context.Context) *cobra.Command
- func (c *Cli) MigrateCommand(ctx context.Context) *cobra.Command
- func (c *Cli) ResetCommand(ctx context.Context) *cobra.Command
- func (c *Cli) RollbackCommand(ctx context.Context) *cobra.Command
- type CliConfig
- type Config
- type Driver
- type ExecutedMigration
- type GoMigration
- func (q *GoMigration) Clean(ctx context.Context) error
- func (q *GoMigration) Create(fileName string) error
- func (q *GoMigration) Fresh(ctx context.Context) error
- func (q *GoMigration) List(ctx context.Context) (RegisteredMigrationList, error)
- func (q *GoMigration) Migrate(ctx context.Context) error
- func (q *GoMigration) Register(migrations ...Migration) error
- func (q *GoMigration) Reset(ctx context.Context) error
- func (q *GoMigration) Rollback(ctx context.Context, step int) error
- func (q *GoMigration) SetMigrationFilesDir(dir string) *GoMigration
- type Migration
- type MySqlDriver
- func (b *MySqlDriver) ApplyMigrations(ctx context.Context, migrations []Migration, ...) error
- func (m *MySqlDriver) CleanDatabase(ctx context.Context) error
- func (b *MySqlDriver) Close() error
- func (b *MySqlDriver) CreateMigrationsTable(ctx context.Context) error
- func (b *MySqlDriver) GetExecutedMigrations(ctx context.Context, reverse bool) ([]ExecutedMigration, error)
- func (b *MySqlDriver) SetMigrationTableName(name string)
- func (b *MySqlDriver) UnapplyMigrations(ctx context.Context, migrations []Migration, ...) error
- type PostgresDriver
- func (b *PostgresDriver) ApplyMigrations(ctx context.Context, migrations []Migration, ...) error
- func (p *PostgresDriver) CleanDatabase(ctx context.Context) error
- func (b *PostgresDriver) Close() error
- func (b *PostgresDriver) CreateMigrationsTable(ctx context.Context) error
- func (b *PostgresDriver) GetExecutedMigrations(ctx context.Context, reverse bool) ([]ExecutedMigration, error)
- func (b *PostgresDriver) SetMigrationTableName(name string)
- func (b *PostgresDriver) UnapplyMigrations(ctx context.Context, migrations []Migration, ...) error
- type RegisteredMigration
- type RegisteredMigrationList
- type SqliteDriver
- func (b *SqliteDriver) ApplyMigrations(ctx context.Context, migrations []Migration, ...) error
- func (d *SqliteDriver) CleanDatabase(ctx context.Context) error
- func (b *SqliteDriver) Close() error
- func (b *SqliteDriver) CreateMigrationsTable(ctx context.Context) error
- func (b *SqliteDriver) GetExecutedMigrations(ctx context.Context, reverse bool) ([]ExecutedMigration, error)
- func (b *SqliteDriver) SetMigrationTableName(name string)
- func (b *SqliteDriver) UnapplyMigrations(ctx context.Context, migrations []Migration, ...) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrConfigNotProvided = errors.New("config not provided") ErrDriverNotProvided = errors.New("driver not provided") ErrMigrationDirNotProvided = errors.New("migration directory not provided") ErrMigrationDirNotExists = errors.New("migration directory does not exist") ErrMigrationNameNotProvided = errors.New("migration name not provided") ErrMigrationFileAlreadyExists = errors.New("migration file already exists") ErrMigrationFileNotFound = errors.New("migration file not found") ErrInvalidRollbackStep = errors.New("invalid rollback step") ErrEmbeddedFSNotProvided = errors.New("embedded fs not provided") ErrGoMigrationNotProvided = errors.New("gomigration instance not provided") )
Functions ¶
This section is empty.
Types ¶
type Cli ¶
type Cli struct {
// contains filtered or unexported fields
}
func (*Cli) CleanCommand ¶ added in v1.3.0
func (*Cli) CreateCommand ¶ added in v1.3.0
func (*Cli) ListCommand ¶ added in v1.3.0
func (*Cli) MigrateCommand ¶ added in v1.3.0
func (*Cli) ResetCommand ¶ added in v1.3.0
type CliConfig ¶
type CliConfig struct {
GoMigration *GoMigration
CliName string
}
type Driver ¶
type Driver interface {
// SetMigrationTableName sets the name of the table that stores executed migration records.
SetMigrationTableName(name string)
// CreateMigrationsTable creates the migration history table if it does not already exist.
CreateMigrationsTable(ctx context.Context) error
// GetExecutedMigrations returns the list of already executed migrations.
// If reverse is true, the list is returned in descending order (most recent first).
GetExecutedMigrations(ctx context.Context, reverse bool) ([]ExecutedMigration, error)
// CleanDatabase drops or truncates all user tables in the database.
CleanDatabase(ctx context.Context) error
// ApplyMigrations applies a list of "up" migrations in sequence.
// The onRunning, onSuccess, and onFailed callbacks are triggered accordingly for each migration.
ApplyMigrations(
ctx context.Context,
migrations []Migration,
onRunning func(migration *Migration),
onSuccess func(migration *Migration),
onFailed func(migration *Migration, err error),
) error
// UnapplyMigrations rolls back a list of "down" migrations in sequence.
// The onRunning, onSuccess, and onFailed callbacks are triggered accordingly for each migration.
UnapplyMigrations(
ctx context.Context,
migrations []Migration,
onRunning func(migration *Migration),
onSuccess func(migration *Migration),
onFailed func(migration *Migration, err error),
) error
// Close gracefully closes the connection to the database or releases resources.
Close() error
}
Driver defines the contract for a migration driver implementation.
type ExecutedMigration ¶
type GoMigration ¶
type GoMigration struct {
// contains filtered or unexported fields
}
GoMigration is the main struct for managing and executing database migrations.
func New ¶
func New(config *Config) (*GoMigration, error)
New creates a new instance of GoMigration using the provided configuration. It validates and sets defaults for missing fields, checks for the migration directory, and applies configuration to the driver.
func (*GoMigration) Clean ¶
func (q *GoMigration) Clean(ctx context.Context) error
Clean drops all database tables and objects managed by the migration system.
func (*GoMigration) Create ¶
func (q *GoMigration) Create(fileName string) error
Create generates a new migration file using the given name. The generated file includes a timestamp prefix and basic template content.
func (*GoMigration) Fresh ¶
func (q *GoMigration) Fresh(ctx context.Context) error
Fresh wipes the database clean and reapplies all registered migrations from scratch.
func (*GoMigration) List ¶
func (q *GoMigration) List(ctx context.Context) (RegisteredMigrationList, error)
List returns all registered migrations along with their execution status.
func (*GoMigration) Migrate ¶
func (q *GoMigration) Migrate(ctx context.Context) error
Migrate applies all pending migrations in the correct order. It skips migrations that have already been executed.
func (*GoMigration) Register ¶
func (q *GoMigration) Register(migrations ...Migration) error
Register adds one or more Migration instances to the internal registry. It ensures no duplicate migration names are registered.
func (*GoMigration) Reset ¶
func (q *GoMigration) Reset(ctx context.Context) error
Reset rolls back all applied migrations and reapplies them from scratch.
func (*GoMigration) Rollback ¶
func (q *GoMigration) Rollback(ctx context.Context, step int) error
Rollback undoes the last `step` number of executed migrations.
func (*GoMigration) SetMigrationFilesDir ¶ added in v1.1.0
func (q *GoMigration) SetMigrationFilesDir(dir string) *GoMigration
Set migration files directory.
type MySqlDriver ¶
type MySqlDriver struct {
// contains filtered or unexported fields
}
MySqlDriver implements the Driver interface for MySQL.
func NewMySqlDriver ¶
func NewMySqlDriver( host string, port string, user string, password string, database string, charset string, ) (*MySqlDriver, error)
NewMySqlDriver initializes a new MySqlDriver with the given DB config.
func (*MySqlDriver) ApplyMigrations ¶
func (b *MySqlDriver) ApplyMigrations( ctx context.Context, migrations []Migration, onRunning func(migration *Migration), onSuccess func(migration *Migration), onFailed func(migration *Migration, err error), ) error
ApplyMigrations applies a batch of "up" migrations with optional callbacks. Each migration's UpScript and bookkeeping insert run inside a single transaction, and the whole batch is guarded by a cross-process lock where the dialect supports one.
func (*MySqlDriver) CleanDatabase ¶
func (m *MySqlDriver) CleanDatabase(ctx context.Context) error
CleanDatabase drops all tables from the current database, except the migrations tracking table itself.
func (*MySqlDriver) Close ¶
func (b *MySqlDriver) Close() error
Close closes the database connection.
func (*MySqlDriver) CreateMigrationsTable ¶
CreateMigrationsTable creates the migration table if it doesn't exist.
func (*MySqlDriver) GetExecutedMigrations ¶
func (b *MySqlDriver) GetExecutedMigrations(ctx context.Context, reverse bool) ([]ExecutedMigration, error)
GetExecutedMigrations returns a list of previously executed migrations, optionally in reverse order.
func (*MySqlDriver) SetMigrationTableName ¶
func (b *MySqlDriver) SetMigrationTableName(name string)
SetMigrationTableName sets the name of the migration tracking table. An invalid name (see sanitizeTableName) is rejected in favor of the default, since this method has no error return in the Driver interface.
func (*MySqlDriver) UnapplyMigrations ¶
func (b *MySqlDriver) UnapplyMigrations( ctx context.Context, migrations []Migration, onRunning func(migration *Migration), onSuccess func(migration *Migration), onFailed func(migration *Migration, err error), ) error
UnapplyMigrations rolls back a batch of "down" migrations with optional callbacks. Each migration's DownScript and bookkeeping delete run inside a single transaction, and the whole batch is guarded by a cross-process lock where the dialect supports one.
type PostgresDriver ¶
type PostgresDriver struct {
// contains filtered or unexported fields
}
PostgresDriver manages database connections and migration operations for PostgreSQL.
func NewPostgresDriver ¶
func NewPostgresDriver( host string, port string, user string, password string, database string, schema string, ) (*PostgresDriver, error)
NewPostgresDriver creates and returns a new instance of PostgresDriver. It opens a connection to the given PostgreSQL database using the provided credentials and schema.
func (*PostgresDriver) ApplyMigrations ¶
func (b *PostgresDriver) ApplyMigrations( ctx context.Context, migrations []Migration, onRunning func(migration *Migration), onSuccess func(migration *Migration), onFailed func(migration *Migration, err error), ) error
ApplyMigrations applies a batch of "up" migrations with optional callbacks. Each migration's UpScript and bookkeeping insert run inside a single transaction, and the whole batch is guarded by a cross-process lock where the dialect supports one.
func (*PostgresDriver) CleanDatabase ¶
func (p *PostgresDriver) CleanDatabase(ctx context.Context) error
CleanDatabase drops all tables in the "public" schema, except the migrations tracking table itself.
func (*PostgresDriver) Close ¶
func (b *PostgresDriver) Close() error
Close closes the database connection.
func (*PostgresDriver) CreateMigrationsTable ¶
CreateMigrationsTable creates the migration table if it doesn't exist.
func (*PostgresDriver) GetExecutedMigrations ¶
func (b *PostgresDriver) GetExecutedMigrations(ctx context.Context, reverse bool) ([]ExecutedMigration, error)
GetExecutedMigrations returns a list of previously executed migrations, optionally in reverse order.
func (*PostgresDriver) SetMigrationTableName ¶
func (b *PostgresDriver) SetMigrationTableName(name string)
SetMigrationTableName sets the name of the migration tracking table. An invalid name (see sanitizeTableName) is rejected in favor of the default, since this method has no error return in the Driver interface.
func (*PostgresDriver) UnapplyMigrations ¶
func (b *PostgresDriver) UnapplyMigrations( ctx context.Context, migrations []Migration, onRunning func(migration *Migration), onSuccess func(migration *Migration), onFailed func(migration *Migration, err error), ) error
UnapplyMigrations rolls back a batch of "down" migrations with optional callbacks. Each migration's DownScript and bookkeeping delete run inside a single transaction, and the whole batch is guarded by a cross-process lock where the dialect supports one.
type RegisteredMigration ¶
type RegisteredMigrationList ¶
type RegisteredMigrationList []RegisteredMigration
func (RegisteredMigrationList) Print ¶
func (m RegisteredMigrationList) Print()
type SqliteDriver ¶ added in v1.2.0
type SqliteDriver struct {
// contains filtered or unexported fields
}
SqliteDriver is a driver for sqlite
func NewSqliteDriver ¶ added in v1.2.0
func NewSqliteDriver( database string, ) (*SqliteDriver, error)
NewSqliteDriver creates a new SqliteDriver
func (*SqliteDriver) ApplyMigrations ¶ added in v1.2.0
func (b *SqliteDriver) ApplyMigrations( ctx context.Context, migrations []Migration, onRunning func(migration *Migration), onSuccess func(migration *Migration), onFailed func(migration *Migration, err error), ) error
ApplyMigrations applies a batch of "up" migrations with optional callbacks. Each migration's UpScript and bookkeeping insert run inside a single transaction, and the whole batch is guarded by a cross-process lock where the dialect supports one.
func (*SqliteDriver) CleanDatabase ¶ added in v1.2.0
func (d *SqliteDriver) CleanDatabase(ctx context.Context) error
CleanDatabase drops all tables from the current database, except the migrations tracking table itself.
func (*SqliteDriver) Close ¶ added in v1.2.0
func (b *SqliteDriver) Close() error
Close closes the database connection.
func (*SqliteDriver) CreateMigrationsTable ¶ added in v1.2.0
CreateMigrationsTable creates the migration table if it doesn't exist.
func (*SqliteDriver) GetExecutedMigrations ¶ added in v1.2.0
func (b *SqliteDriver) GetExecutedMigrations(ctx context.Context, reverse bool) ([]ExecutedMigration, error)
GetExecutedMigrations returns a list of previously executed migrations, optionally in reverse order.
func (*SqliteDriver) SetMigrationTableName ¶ added in v1.2.0
func (b *SqliteDriver) SetMigrationTableName(name string)
SetMigrationTableName sets the name of the migration tracking table. An invalid name (see sanitizeTableName) is rejected in favor of the default, since this method has no error return in the Driver interface.
func (*SqliteDriver) UnapplyMigrations ¶ added in v1.2.0
func (b *SqliteDriver) UnapplyMigrations( ctx context.Context, migrations []Migration, onRunning func(migration *Migration), onSuccess func(migration *Migration), onFailed func(migration *Migration, err error), ) error
UnapplyMigrations rolls back a batch of "down" migrations with optional callbacks. Each migration's DownScript and bookkeeping delete run inside a single transaction, and the whole batch is guarded by a cross-process lock where the dialect supports one.