migration

package
v0.0.0-...-6a3e998 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutoMigrate

func AutoMigrate(ctx context.Context, db *sqlx.DB, driverName string, options AutoMigrateOptions) error

AutoMigrate performs automatic database migration on startup

func CreateMigration

func CreateMigration(dir, name string) error

CreateMigration creates a new migration file with the given name

Types

type AutoMigrateOptions

type AutoMigrateOptions struct {
	// Whether to automatically run migrations on startup
	Enabled bool

	// Path to migration files
	Path string

	// Whether to fail startup if migrations fail
	FailOnError bool

	// Timeout for migration operations
	Timeout time.Duration

	// Whether to validate migrations without applying them
	ValidateOnly bool

	// Logger to use for migration messages
	Logger *log.Logger
}

AutoMigrateOptions contains options for automatic migration

func DefaultOptions

func DefaultOptions() AutoMigrateOptions

DefaultOptions returns the default migration options

type Config

type Config struct {
	// Path to migration files directory
	MigrationsPath string `json:"migrations_path" yaml:"migrations_path"`

	// Whether to automatically run migrations on startup
	AutoMigrate bool `json:"auto_migrate" yaml:"auto_migrate"`

	// Timeout for migration operations
	MigrationTimeout time.Duration `json:"migration_timeout" yaml:"migration_timeout"`

	// Whether to validate migrations without applying them
	ValidateOnly bool `json:"validate_only" yaml:"validate_only"`

	// Use a specific number of steps for migration (0 means all)
	Steps int `json:"steps" yaml:"steps"`
}

Config holds the migration configuration

type DataMigration

type DataMigration struct {
	// Name is a descriptive name for the migration
	Name string

	// Version is the migration version number
	Version uint

	// Execute is the function that performs the data migration
	Execute func(ctx context.Context, tx *sqlx.Tx) error

	// Rollback is the function that reverts the data migration (optional)
	Rollback func(ctx context.Context, tx *sqlx.Tx) error
}

DataMigration represents a migration that transforms data using Go code

type DataMigrator

type DataMigrator struct {
	// contains filtered or unexported fields
}

DataMigrator handles data migrations using Go code

func NewDataMigrator

func NewDataMigrator(db *sqlx.DB, logger *log.Logger) *DataMigrator

NewDataMigrator creates a new data migrator

func (*DataMigrator) Register

func (m *DataMigrator) Register(migration DataMigration)

Register adds a migration to the registry

func (*DataMigrator) RollbackMigration

func (m *DataMigrator) RollbackMigration(ctx context.Context, version uint) error

RollbackMigration rolls back a specific data migration

func (*DataMigrator) RunMigration

func (m *DataMigrator) RunMigration(ctx context.Context, version uint) error

RunMigration applies a specific data migration

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager handles database migrations

func NewManager

func NewManager(db *sqlx.DB, config Config, driverName string) (*Manager, error)

NewManager creates a new migration manager

func (*Manager) Close

func (m *Manager) Close() error

Close releases resources used by the migration manager

func (*Manager) ForceVersion

func (m *Manager) ForceVersion(version uint) error

ForceVersion forces the database to a specific version

func (*Manager) GetVersion

func (m *Manager) GetVersion() (uint, bool, error)

GetVersion returns the current migration version

func (*Manager) Init

func (m *Manager) Init(ctx context.Context) error

Init initializes the migration manager

func (*Manager) Rollback

func (m *Manager) Rollback(ctx context.Context) error

Rollback rolls back the last applied migration

func (*Manager) RollbackAll

func (m *Manager) RollbackAll(ctx context.Context) error

RollbackAll rolls back all migrations

func (*Manager) RunMigrations

func (m *Manager) RunMigrations(ctx context.Context) error

RunMigrations applies all pending migrations

func (*Manager) ValidateMigrations

func (m *Manager) ValidateMigrations(ctx context.Context) error

ValidateMigrations checks if migrations are valid but doesn't apply them

func (*Manager) WithTransaction

func (m *Manager) WithTransaction(ctx context.Context, fn func(*sql.Tx) error) error

WithTransaction executes a function within a transaction This is useful for data migrations that are not handled by SQL files

Jump to

Keyboard shortcuts

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