migration

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2025 License: ISC Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateMigrationFile

func CreateMigrationFile(root, name, ext string, stages ...string) error

CreateMigrationFile creates a migration file in the specified root directory with the given name, extension, and optional stages. It also accepts optional stages to include in the migration file.

func NewMigrationCLI

func NewMigrationCLI(m Migration, options ...CLIOptions) *cobra.Command

NewMigrationCLI creates a new cobra command for database migration with the provided options.

Types

type CLIOptions

type CLIOptions func(*cliOption)

func WithCallback added in v0.0.2

func WithCallback(cb func()) CLIOptions

WithCallback register a callback function to call after command finished.

func WithDefaultStages

func WithDefaultStages(stages ...string) CLIOptions

WithDefaultStages adds stages to auto-run, generate, and rollback on CLI.

func WithNewCMD

func WithNewCMD(enabled bool) CLIOptions

WithNewCMD enables a new command to create a migration file in development mode.

func WithOnlyFiles

func WithOnlyFiles(files ...string) CLIOptions

WithOnlyFiles specifies the files to include in the migration.

func WithOutputPath

func WithOutputPath(path string) CLIOptions

WithOutputPath sets the output file root path for the new command.

func WithRefreshStages

func WithRefreshStages(stages ...string) CLIOptions

WithRefreshStages adds the stages to run on refresh.

func WithSkipFiles

func WithSkipFiles(files ...string) CLIOptions

WithSkipFiles specifies the files to exclude from the migration.

type ExecutableScanner

type ExecutableScanner interface {
	// Exec executes a SQL command with the provided arguments.
	// Returns an error if the execution fails.
	Exec(ctx context.Context, sql string, arguments ...any) error

	// Scan executes a SQL query with the provided arguments and returns the result rows.
	// Returns an error if the query fails or if scanning the results encounters an issue.
	Scan(ctx context.Context, sql string, arguments ...any) (Rows, error)
}

ExecutableScanner represents an entity capable of executing SQL commands and scanning results.

type Migrated

type Migrated struct {
	Name      string    `db:"name"`
	Stage     string    `db:"stage"`
	CreatedAt time.Time `db:"created_at"`
}

type Migration

type Migration interface {
	// Load loads migration stages from the filesystem and caches them.
	Load() error

	// Root returns the root directory of migration files.
	Root() string

	// Extension returns the file extension for migration files.
	Extension() string

	// IsDev indicates if it is in development mode.
	IsDev() bool

	// Initialize sets up the database migration table.
	Initialize() error

	// Summary returns an overview of the migration.
	Summary() (Summary, error)

	// Up applies migration stages.
	Up(stages []string, options ...MigrationOption) (Summary, error)

	// Down rolls back migration stages.
	Down(stages []string, options ...MigrationOption) (Summary, error)

	// Refresh rolls back and reapplies migration stages.
	Refresh(stages []string, options ...MigrationOption) (Summary, error)
}

Migration defines the interface for managing database migrations. It includes methods for loading migration stages, initializing the migration table, retrieving summaries, and applying or rolling back migration stages.

func NewMigration

func NewMigration(db MigrationSource, fs fs.FlexibleFS, options ...Option) (Migration, error)

NewMigration initializes a migration with the specified database source, filesystem, and options.

type MigrationOption

type MigrationOption func(*migrationOption)

func OnlyFiles

func OnlyFiles(files ...string) MigrationOption

OnlyFiles specifies the files to include in the migration.

func SkipFiles

func SkipFiles(files ...string) MigrationOption

SkipFiles specifies the files to exclude from the migration.

type MigrationSource

type MigrationSource interface {
	// Transaction runs a function within a transaction context.
	// The transaction is committed if the function succeeds, or rolled back in case of an error.
	Transaction(ctx context.Context, callback func(ExecutableScanner) error) error

	// Exec executes a SQL command with the provided arguments.
	// Returns an error if the execution fails.
	Exec(ctx context.Context, sql string, arguments ...any) error

	// Scan executes a SQL query with the provided arguments and returns the result rows.
	// Returns an error if the query fails or if scanning the results encounters an issue.
	Scan(ctx context.Context, sql string, arguments ...any) (Rows, error)
}

MigrationSource defines methods for running database migrations within a transaction.

func NewMySQLSource

func NewMySQLSource(conn mysql.Connection) MigrationSource

NewMySQLSource creates a new MySQL migration source using the provided connection.

func NewPostgresSource

func NewPostgresSource(conn postgres.Connection) MigrationSource

NewPostgresSource creates a new PostgreSQL migration source using the provided connection.

type Option

type Option func(*migration)

func WithEnv

func WithEnv(isDev bool) Option

WithEnv sets the environment mode for migrations. Enables development mode if true, causing Load() to be called on each migration run.

func WithExtension

func WithExtension(ext string) Option

WithExtension sets the file extension for migration files.

func WithRoot

func WithRoot(root string) Option

WithRoot sets the root directory for migration files.

type Rows

type Rows interface {
	// Next prepares the next row for reading.
	// Returns true if there is a next row, false if there are no more rows.
	Next() bool

	// Scan reads the current row's columns into the provided destination variables.
	// Returns an error if scanning the row fails.
	Scan(dest ...any) error

	// Close releases resources associated with the Rows.
	// It prevents further row enumeration after being called.
	Close()
}

Rows represents the set of results from a SQL query.

type Summary

type Summary []Migrated

func (Summary) ForStage

func (s Summary) ForStage(stage string) Summary

func (Summary) GroupByFile

func (s Summary) GroupByFile() map[string][]Migrated

func (Summary) GroupByStage

func (s Summary) GroupByStage() map[string][]Migrated

func (Summary) IsEmpty

func (s Summary) IsEmpty() bool

func (Summary) Names

func (s Summary) Names() []string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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