Documentation
¶
Index ¶
Constants ¶
const HelpText = "Please specify up, down, redo, rollback, status, or create."
HelpText is printed when no command is specified.
Variables ¶
var CLIErr error = errors.New(HelpText)
CLIErr is returned when no command is specified.
var Logger = log.Default()
Functions ¶
func CLI ¶
CLI parses os.Args and runs the appropriate migration command. Commands available are up, down, redo, rollback, status, and create. Most commands accept a "steps" flag which is parsed as an int. Use -steps=1 to set it. Up, down, and redo accept a "version" flag which is parsed as int64. Use --version=1610069160 to set it.
Types ¶
type Executor ¶
type Executor interface { Exec(query string, args ...interface{}) (sql.Result, error) GetVersions(query string, args ...interface{}) ([]int64, error) }
Executor is the interface to executing SQL
Exec executes a SQL query returning a sql.Result. Use this for mutation queries like CREATE, INSERT, UPDATE, DELETE, etc.
GetVersions returns the timestamped versions that have been migrated thus far. Timestamps are stored in Unix time, that is seconds since epoch, stored in an int64.
type IMigrator ¶
type IMigrator struct { DB Executor FS http.FileSystem Dirname string // The directory where migrations are stored. UpKey *regexp.Regexp // The Regexp to detecth the up migration SQL. DnKey *regexp.Regexp // The Regexp to detecth the down migration SQL. TableName string // The table where migration info is stored. VersionColumn string // The version column in the migrations table. CreateTableSQL string // The SQL to create the migrations table. Migrations []Migration FileVersionRegexp *regexp.Regexp // The Regexp to detect a migration file. TemplateUp string // The SQL to place in the UP section of a generated file. TemplateDn string // The SQL to place in the DOWN section of a generated file. // contains filtered or unexported fields }
IMigrator is the default migrator that satisfies the Migrator interface.
func NewIMigrator ¶
func NewIMigrator(db Executor, fs http.FileSystem) *IMigrator
NewIMigrator returns a default migrator with the SQLite dialect.
func (IMigrator) Create ¶
Create generates a new migration file in the Dirname directory. The file is prefixed with the current time as a unix timestamp, followed by the provided name. It will insert the provided TemplateUp and TemplateDn strings into the appropriate sections of the migration file.
func (*IMigrator) Down ¶
Down runs all migrations in descending order. If steps is greater than -1, it will step down that many migrations. If version is greater than 0, it will only migrate down that specific version.
func (*IMigrator) Rollback ¶
Rollback runs the down SQL for the most recent migration. If steps is greater than 1, it will run that many migrations down.
type Migrator ¶
type Migrator interface { Create(string) Up(int, int64) Down(int, int64) Redo(int, int64) Rollback(int) Status() }
Migrator is the interface for running migrations.
Create is used to create a new migration file. The file should be prefixed with a unix timestamp and stored in the migrations directory.
Up runs the UP migration for every migration file.
Down runs the DOWN migration for every migration file.
Redo runs the DOWN then UP migration for the most recently created migration.
Rollback runs the DOWN migration for the most recenlty created migration.
Status prints out which migrations have been run thus far.