migrator

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidFormat        = errors.New("invalid_format")
	ErrParsingMigrationType = errors.New("invalid_migration_type")

	ErrReadingFileNames      = errors.New("error_reading_file_names")
	ErrParsingMigrations     = errors.New("error_parsing_migrations")
	ErrCreatingMigrationFile = errors.New("error_creating_migration_file")

	ErrNoMigrationsToApply         = errors.New("error_no_migrations_to_apply")
	ErrReadingLastAppliedMigration = errors.New("error_reading_last_applied_migration")

	// Storage
	ErrLoadingMigrations = errors.New("error_loading_migrations")

	ErrMigrationTableDoesNotExist = errors.New("error_migration_table_does_not_exist")
	ErrDatabaseNotInitialized     = errors.New("error_database_not_initialized")
)

Functions

func ApplyMigration

func ApplyMigration(config *MigratorConfig, migration *Migration) error

TODO: apply all as one transaction ?. if one fails, rollback all

func ApplyMigrations

func ApplyMigrations(config *MigratorConfig, migrationsToApply []*Migration) error

func DryApplyMigrations added in v0.6.1

func DryApplyMigrations(migrationsToApply []*Migration)

func FillMigrationFiles

func FillMigrationFiles(fsRepo FileRepo, config GeneratorConfig, migrations []*Migration) error

func Generate

func Generate(config GeneratorConfig) error

func GenerateMigrationFiles

func GenerateMigrationFiles(fsRepo FileRepo, migrations []*Migration) error

func LoadMigrations

func LoadMigrations(config *MigratorConfig, migrationType MigrationType) ([]*Migration, *Migration, error)

func MigrationHistory

func MigrationHistory(dbRepo DatabaseRepo) error

func MigrationToFilename

func MigrationToFilename(migration *Migration) string

func SelectExtention

func SelectExtention(database Database) string

func SelectSubDirectory

func SelectSubDirectory(database Database) string

Types

type Database

type Database string
const (
	PostgresDB Database = "postgres"
	MySQLDB    Database = "mysql"
	SQLiteDB   Database = "sqlite"
)

func SelectDatabase

func SelectDatabase(database string) Database

type DatabaseRepo

type DatabaseRepo interface {
	// Load last applied migration entry from the migration table
	// - Creates migration table if not exists
	// - Returns nil if no entry found
	LoadLastAppliedMigration() (*Migration, error)
	// Load all migration entries from the migration table
	LoadMigrations() ([]*Migration, error)
	// Create a migration table if not exists
	CreateMigrationTableIfNotExists() error
	// Get the last applied migration from the migration table
	GetLastAppliedMigration() (*Migration, error)
	// Apply a migration to the database and add an entry to the migration table
	ApplyMigration(migration *Migration) error
	// Delete the migration table
	DeleteMigrationTable() error
}

type FileRepo

type FileRepo interface {
	// Create a migration directory if not exists
	OpenDirectory() error
	// Load migrations from directoryy
	LoadMigrationsFromFile(migrationType MigrationType) ([]*Migration, error)
	// Read all the files from the migration directory
	GetMigrationFilePathsByType(migrationType MigrationType) ([]string, error)
	// Create a migration file
	CreateMigrationFile(migration *Migration) error
	// Write to File
	WriteMigrationToFile(migration *Migration) error
	// Read Query from File
	LoadMigrationQuery(migration *Migration) error
	// Delete a migration file
	DeleteMigrationFile(migration *Migration) error
	// Delete the migration directory
	DeleteMigrationDirectory() error
}

type GeneratorConfig

type GeneratorConfig struct {
	Name          string
	NumToGenerate int
	DryRun        bool
	Fill          bool
	FSRepo        FileRepo
}

type Migration

type Migration struct {
	Number  int
	Name    string
	Type    MigrationType
	Path    string
	Query   string
	Created time.Time
}

func CalculateDownMigrationsToApply

func CalculateDownMigrationsToApply(lastMigration *Migration, migrations []*Migration, numberToMigrate int) []*Migration

It will accept a reversed list of migrations

func CalculateUpMigrationsToApply

func CalculateUpMigrationsToApply(lastMigration *Migration, migrations []*Migration, numberToMigrate int) []*Migration

func Clean

func Clean(config *MigratorConfig) ([]*Migration, error)

func GenerateNextMigrations

func GenerateNextMigrations(lastNumber int, name string, total int) []*Migration

func MigrateDown

func MigrateDown(config *MigratorConfig) ([]*Migration, error)

func MigrateUp

func MigrateUp(config *MigratorConfig) ([]*Migration, error)

func ParseMigrationFromString

func ParseMigrationFromString(s string) (*Migration, error)

type MigrationType

type MigrationType string
const (
	MigrationUp   MigrationType = "up"
	MigrationDown MigrationType = "down"
)

type MigratorConfig

type MigratorConfig struct {
	NumToMigrate int
	DryRun       bool
	DBRepo       DatabaseRepo
	FSRepo       FileRepo
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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