Documentation ¶
Overview ¶
Package dbmigrate is a sql database migration tool.
dbmigrate can be used both as a CLI application and as a Go package, does not use any DSL for migrations, just plain old SQL we all know and love so it is compatible with any framework and programming language.
Index ¶
- Constants
- func DirExists(dirpath string) bool
- func Engines() []string
- func FileExists(fpath string) bool
- func FindProjectDir(fromDir string) (string, error)
- type Direction
- type Migration
- type Migrator
- func (m *Migrator) Close() error
- func (m *Migrator) GenerateMigration(descr string, engines ...string) ([]string, error)
- func (m *Migrator) LastAppliedMigration() (*Migration, error)
- func (m *Migrator) LatestVersionMigration() (*Migration, error)
- func (m *Migrator) Migrate() (int, error)
- func (m *Migrator) MigrateSteps(steps int) (int, error)
- func (m *Migrator) Rollback() (int, error)
- func (m *Migrator) RollbackSteps(steps int) (int, error)
- func (m *Migrator) Status() ([]*Migration, error)
- type Settings
Constants ¶
const ( // TimestampFormat defines format for migration versioning // and is used in migrations file names and db table TimestampFormat = "20060102150405" // PrintTimestampFormat defines format for printing timestamps PrintTimestampFormat = "2006.01.02 15:04:05" )
const ( // DirectionUp is the direction to migrate schema DirectionUp // DirectionDown is the direction to rollback schema DirectionDown )
const AllSteps = 0
AllSteps specifies that all migrations should be applied
const MigrationsDir = "dbmigrations"
MigrationsDir is the directory to store migrations
Variables ¶
This section is empty.
Functions ¶
func FindProjectDir ¶
FindProjectDir recursively finds project dir (the one that has dbmigrations subdir)
Types ¶
type Direction ¶
type Direction int
Direction specifies if migration is used to migrate or rollback schema
func DirectionFromString ¶
DirectionFromString tries to build Direction from string, checking for valid ones and returning an error if check was unsuccessful
type Migration ¶
type Migration struct { // Version holds the created at timestamp Version time.Time Name string AppliedAt time.Time Direction Direction Engine string }
Migration holds metadata of migration
type Migrator ¶
type Migrator struct { // Settings used by migrator *Settings // contains filtered or unexported fields }
Migrator is the end user interface for all library operations
func NewMigrator ¶
NewMigrator creates new Migrator instance
func (*Migrator) GenerateMigration ¶
GenerateMigration generates up and down migrations with given name for given engine
func (*Migrator) LastAppliedMigration ¶
LastAppliedMigration returns the migration which was applied last
func (*Migrator) LatestVersionMigration ¶
LatestVersionMigration returns the migration that has the most recent version (which is not necessarily the last applied one)
func (*Migrator) MigrateSteps ¶
MigrateSteps applies the number of migrations specified by the steps variable, returning number of applied migrations
func (*Migrator) RollbackSteps ¶
RollbackSteps rolls back the number of migrations specified by the steps variable
type Settings ¶
type Settings struct { // Engines is the used RDBMS. Currently PostgreSQL, MySQL and SQLite are supported Engine string Database string User string Password string Host string Port int // MigrationsTable is the database table to store applied migrations data MigrationsTable string // AllowMissingDowns flag specifies if Migrator should allow empty or missing down migrations files // which means that there will be no rollback for the corresponding up migrations and that this is ok AllowMissingDowns bool // MigrationsCh is the channel for applied migrations MigrationsCh chan *Migration // ErrorsChan is the channel for errors that happened during the work but are not fatal ErrorsCh chan error }
Settings used by Migrator