Documentation
¶
Overview ¶
Package migrations provides a simple and flexible SQL migration system. It supports creating, applying, rolling back, and managing database migrations using plain SQL files organized in numbered directories.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Create ¶
Create creates a new migration directory with up and down SQL files. It automatically generates the next sequential index or uses the provided index.
Parameters:
- dir: base directory where migrations are stored
- name: descriptive name for the migration
- index: optional index number (if not provided, uses next available number)
func Down ¶
Down rolls back the most recently applied migration. It executes the corresponding .down.sql file and removes the migration record.
Parameters:
- db: database connection
- baseDir: base directory containing migration folders
func Insert ¶
Insert creates a new migration at a specific index, renumbering existing migrations as needed. All migrations with index >= insertIndex will be incremented by 1.
Parameters:
- dir: base directory where migrations are stored
- name: descriptive name for the migration
- insertIndex: the index number where the new migration should be inserted
func Reset ¶
Reset rolls back all applied migrations in reverse order. It executes all .down.sql files and removes all migration records.
Parameters:
- db: database connection
- baseDir: base directory containing migration folders
func ResetAndDrop ¶
ResetAndDrop rolls back all migrations and drops the migrations table.
Parameters:
- db: database connection
- baseDir: base directory containing migration folders
Types ¶
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator encapsulates the migration configuration and provides convenient methods for running migrations without repeatedly passing the same parameters.
func New ¶
New creates a new Migrator instance with the given database connection and base directory for migrations.
Parameters:
- db: database connection
- baseDir: base directory containing migration folders
Example:
m := miggo.New(db, "./migrations")
m.Up()
m.Create("add_users_table")
func (*Migrator) Create ¶
Create creates a new migration with the given name. Optionally accepts an index number.
func (*Migrator) Down ¶
func (m *Migrator) Down()
Down rolls back the most recently applied migration.
func (*Migrator) Insert ¶
Insert creates a new migration at a specific index, renumbering existing migrations as needed.
func (*Migrator) ResetAndDrop ¶
func (m *Migrator) ResetAndDrop()
ResetAndDrop rolls back all migrations and drops the migrations table.