Documentation
¶
Overview ¶
Package migrataur is a simple migration tool for the Go language. It's written as a library that needs an adapter to work. It has been build with simplicity in mind.
Index ¶
- Variables
- func GetCurrentTimeFormatted() string
- type Adapter
- type Logger
- type MarshalOptions
- type Migrataur
- func (m *Migrataur) GetAll() ([]*Migration, error)
- func (m *Migrataur) Init() (*Migration, error)
- func (m *Migrataur) Migrate(rangeOrName string) ([]*Migration, error)
- func (m *Migrataur) MigrateToLatest() ([]*Migration, error)
- func (m *Migrataur) New(name string) (*Migration, error)
- func (m *Migrataur) Printf(format string, args ...interface{})
- func (m *Migrataur) Remove(rangeOrName string) ([]*Migration, error)
- func (m *Migrataur) Reset() ([]*Migration, error)
- func (m *Migrataur) Rollback(rangeOrName string) ([]*Migration, error)
- type Migration
- type Options
Constants ¶
This section is empty.
Variables ¶
var DefaultMarshalOptions = MarshalOptions{
UpStart: "-- +migrataur up",
UpEnd: "-- -migrataur up",
DownStart: "-- +migrataur down",
DownEnd: "-- -migrataur down",
}
DefaultMarshalOptions holds default marshal options for the migration used when writing or reading migration files to the filesystem.
var DefaultOptions = Options{ Logger: log.New(os.Stdout, "", log.LstdFlags), Directory: "./migrations", Extension: ".sql", InitialMigrationName: "createMigrationHistory", SequenceGenerator: GetCurrentTimeFormatted, MarshalOptions: DefaultMarshalOptions, }
DefaultOptions represents the default migrataur options
Functions ¶
func GetCurrentTimeFormatted ¶
func GetCurrentTimeFormatted() string
GetCurrentTimeFormatted retrieves the current time formatted. It's used as the default sequence generator since a linux timestamp goes up to 2038 :)
Types ¶
type Adapter ¶
type Adapter interface {
// GetInitialMigration retrieves the migration up and down code and is used to populate
// the migrations history table.
GetInitialMigration() (up, down string)
// MigrationApplied is called when the migration has been successfully applied by the
// adapter. This is where you should insert the migration in the history.
MigrationApplied(migration *Migration) error
// MigrationRollbacked is called when the migration has been successfully rolled back.
// This is where you should remove the migration from the history.
MigrationRollbacked(migration *Migration) error
// Exec the given commands. This is call by Migrataur to apply or rollback a migration
// with the corresponding code.
Exec(command string) error
// GetAll retrieves all migrations for this adapter
GetAll() ([]*Migration, error)
}
Adapter is the interface needed to access the underlying database. This is where you should implements the desired behavior. Built-in adapters are found in the subpackage /adapters.
type Logger ¶
type Logger interface {
Print(...interface{})
Printf(string, ...interface{})
Println(...interface{})
Fatal(...interface{})
Fatalf(string, ...interface{})
Fatalln(...interface{})
Panic(...interface{})
Panicf(string, ...interface{})
Panicln(...interface{})
}
Logger is the interface to be implemented by a logger since golang does not exposes a common interface. This interface is taken from the logrus library.
type MarshalOptions ¶
MarshalOptions holds configuration for the migration marshaling & unmarshaling
type Migrataur ¶
type Migrataur struct {
// contains filtered or unexported fields
}
Migrataur represents an instance configurated for a particular use. This is the main object you will use.
func (*Migrataur) GetAll ¶
GetAll retrieve all migrations for the current instance. It will list applied and pending migrations
func (*Migrataur) Init ¶
Init writes the initial migration provided by the adapter to create the needed migrations table, you should call it at the start of your project.
func (*Migrataur) Migrate ¶
Migrate migrates the database and returns an array of effectively applied migrations (it will not contains those that were already applied. rangeOrName can be the exact migration name or a range such as <migration>..<another migration name>
func (*Migrataur) MigrateToLatest ¶
MigrateToLatest migrates the database to the latest version
func (*Migrataur) New ¶
New creates a new migration in the configured folder and returns the instance of the migration attached to the newly created file
func (*Migrataur) Remove ¶
Remove one or many migrations given a name or a range. It will rollbacks them and delete needed files.
type Migration ¶
type Migration struct {
Name string
AppliedAt *time.Time
// contains filtered or unexported fields
}
Migration represents a database migration, nothing more.
func (*Migration) HasBeenApplied ¶
HasBeenApplied checks if the migration has already been applied in the database.
type Options ¶
type Options struct {
Logger Logger
Directory string
Extension string
InitialMigrationName string
SequenceGenerator func() string
MarshalOptions MarshalOptions
}
Options represents migrataur options to give to an instance
func (Options) ExtendWith ¶
ExtendWith extends self with the given Options. It means that if a field is not present in this option, it will be replaced by the one in the other Options. This will not work for the Logger field since a user may want to not provide it.