Documentation
¶
Overview ¶
Package qafoia provides a PostgreSQL migration driver for managing and applying SQL migrations.
Package qafoia 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
- type CliConfig
- type Config
- type Driver
- type ExecutedMigration
- type Migration
- type MySqlDriver
- func (m *MySqlDriver) ApplyMigrations(ctx context.Context, migrations []Migration, ...) error
- func (m *MySqlDriver) CleanDatabase(ctx context.Context) error
- func (m *MySqlDriver) Close() error
- func (m *MySqlDriver) CreateMigrationsTable(ctx context.Context) error
- func (m *MySqlDriver) GetExecutedMigrations(ctx context.Context, reverse bool) ([]ExecutedMigration, error)
- func (m *MySqlDriver) SetMigrationTableName(name string)
- func (m *MySqlDriver) UnapplyMigrations(ctx context.Context, migrations []Migration, ...) error
- type PostgresDriver
- func (p *PostgresDriver) ApplyMigrations(ctx context.Context, migrations []Migration, ...) error
- func (p *PostgresDriver) CleanDatabase(ctx context.Context) error
- func (p *PostgresDriver) Close() error
- func (p *PostgresDriver) CreateMigrationsTable(ctx context.Context) error
- func (p *PostgresDriver) GetExecutedMigrations(ctx context.Context, reverse bool) ([]ExecutedMigration, error)
- func (p *PostgresDriver) SetMigrationTableName(name string)
- func (p *PostgresDriver) UnapplyMigrations(ctx context.Context, migrations []Migration, ...) error
- type Qafoia
- func (q *Qafoia) Clean(ctx context.Context) error
- func (q *Qafoia) Create(fileName string) error
- func (q *Qafoia) Fresh(ctx context.Context) error
- func (q *Qafoia) List(ctx context.Context) (RegisteredMigrationList, error)
- func (q *Qafoia) Migrate(ctx context.Context) error
- func (q *Qafoia) Register(migrations ...Migration) error
- func (q *Qafoia) Reset(ctx context.Context) error
- func (q *Qafoia) Rollback(ctx context.Context, step int) error
- type RegisteredMigration
- type RegisteredMigrationList
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") ErrQafoiaNotProvided = errors.New("qafoia instance not provided") )
Functions ¶
This section is empty.
Types ¶
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 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 (m *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.
func (*MySqlDriver) CleanDatabase ¶
func (m *MySqlDriver) CleanDatabase(ctx context.Context) error
CleanDatabase drops all tables from the current database.
func (*MySqlDriver) Close ¶
func (m *MySqlDriver) Close() error
Close closes the database connection.
func (*MySqlDriver) CreateMigrationsTable ¶
func (m *MySqlDriver) CreateMigrationsTable(ctx context.Context) error
CreateMigrationsTable creates the migration table if it doesn't exist.
func (*MySqlDriver) GetExecutedMigrations ¶
func (m *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 (m *MySqlDriver) SetMigrationTableName(name string)
SetMigrationTableName sets the name of the migration tracking table.
func (*MySqlDriver) UnapplyMigrations ¶
func (m *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.
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 (p *PostgresDriver) ApplyMigrations( ctx context.Context, migrations []Migration, onRunning func(migration *Migration), onSuccess func(migration *Migration), onFailed func(migration *Migration, err error), ) error
ApplyMigrations runs the "up" SQL scripts for the given migrations. Optional callbacks can be provided to track the progress of each migration.
func (*PostgresDriver) CleanDatabase ¶
func (p *PostgresDriver) CleanDatabase(ctx context.Context) error
CleanDatabase drops all tables in the "public" schema.
func (*PostgresDriver) Close ¶
func (p *PostgresDriver) Close() error
Close closes the database connection.
func (*PostgresDriver) CreateMigrationsTable ¶
func (p *PostgresDriver) CreateMigrationsTable(ctx context.Context) error
CreateMigrationsTable creates the migration tracking table if it does not exist.
func (*PostgresDriver) GetExecutedMigrations ¶
func (p *PostgresDriver) GetExecutedMigrations(ctx context.Context, reverse bool) ([]ExecutedMigration, error)
GetExecutedMigrations returns a list of executed migrations from the tracking table. If reverse is true, the list is ordered descending by name.
func (*PostgresDriver) SetMigrationTableName ¶
func (p *PostgresDriver) SetMigrationTableName(name string)
SetMigrationTableName sets the name of the table used to track executed migrations. If the provided name is empty, the default "migrations" is used.
func (*PostgresDriver) UnapplyMigrations ¶
func (p *PostgresDriver) UnapplyMigrations( ctx context.Context, migrations []Migration, onRunning func(migration *Migration), onSuccess func(migration *Migration), onFailed func(migration *Migration, err error), ) error
UnapplyMigrations runs the "down" SQL scripts for the given migrations in reverse order. Optional callbacks can be provided to track the progress of each migration.
type Qafoia ¶
type Qafoia struct {
// contains filtered or unexported fields
}
Qafoia is the main struct for managing and executing database migrations.
func New ¶
New creates a new instance of Qafoia using the provided configuration. It validates and sets defaults for missing fields, checks for the migration directory, and applies configuration to the driver.
func (*Qafoia) Create ¶
Create generates a new migration file using the given name. The generated file includes a timestamp prefix and basic template content.
func (*Qafoia) Fresh ¶
Fresh wipes the database clean and reapplies all registered migrations from scratch.
func (*Qafoia) List ¶
func (q *Qafoia) List(ctx context.Context) (RegisteredMigrationList, error)
List returns all registered migrations along with their execution status.
func (*Qafoia) Migrate ¶
Migrate applies all pending migrations in the correct order. It skips migrations that have already been executed.
func (*Qafoia) Register ¶
Register adds one or more Migration instances to the internal registry. It ensures no duplicate migration names are registered.
type RegisteredMigration ¶
type RegisteredMigrationList ¶
type RegisteredMigrationList []RegisteredMigration
func (RegisteredMigrationList) Print ¶
func (m RegisteredMigrationList) Print()