Documentation ¶
Overview ¶
Package sqlxmigrate is a migration helper for sqlx (https://github.com/jmoiron/sqlx/). Enables schema versioning and rollback cababilities.
Example:
package main
import (
"database/sql" "log" "github.com/geeks-accelerator/sqlxmigrate" "github.com/jmoiron/sqlx" _ "github.com/lib/pq"
)
func main() { // this Pings the database trying to connect, panics on error // use sqlx.Open() for sql.Open() semantics db, err := sqlx.Connect("postgres", "host=127.0.0.1 user=postgres dbname=sqlxmigrate_test port=5433 sslmode=disable password=postgres") if err != nil { log.Fatalf("main : Register DB : %v", err) } defer db.Close() m := sqlxmigrate.New(db, sqlxmigrate.DefaultOptions, []*sqlxmigrate.Migration{ // create persons table { ID: "201608301400", Migrate: func(tx *sql.Tx) error { q := `CREATE TABLE "people" ( "id" serial, "created_at" timestamp with time zone, "updated_at" timestamp with time zone, "deleted_at" timestamp with time zone, "name" text , PRIMARY KEY ("id") )` _, err = tx.Exec(q) return err }, Rollback: func(tx *sql.Tx) error { q := `DROP TABLE IF EXISTS people` _, err = tx.Exec(q) return err }, }, // add age column to persons { ID: "201608301415", Migrate: func(tx *sql.Tx) error { q := `ALTER TABLE people ADD column age int ` _, err = tx.Exec(q) return err }, Rollback: func(tx *sql.Tx) error { q := `ALTER TABLE people DROP column age` _, err = tx.Exec(q) return err }, }, }) if err = m.Migrate(); err != nil { log.Fatalf("Could not migrate: %v", err) } log.Printf("Migration did run successfully") }
Index ¶
- Variables
- type DuplicatedIDError
- type InitSchemaFunc
- type MigrateFunc
- type Migration
- type Options
- type ReservedIDError
- type RollbackFunc
- type Sqlxmigrate
- func (g *Sqlxmigrate) HasTable(tableName string) (bool, error)
- func (g *Sqlxmigrate) InitSchema(initSchema InitSchemaFunc)
- func (g *Sqlxmigrate) Migrate() error
- func (g *Sqlxmigrate) MigrateTo(migrationID string) error
- func (g *Sqlxmigrate) RollbackLast() error
- func (g *Sqlxmigrate) RollbackMigration(m *Migration) error
- func (g *Sqlxmigrate) RollbackTo(migrationID string) error
- func (g *Sqlxmigrate) SetLogger(logger *log.Logger)
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultOptions can be used if you don't want to think about options. DefaultOptions = &Options{ TableName: "migrations", IDColumnName: "id", IDColumnSize: 255, } // ErrRollbackImpossible is returned when trying to rollback a migration // that has no rollback function. ErrRollbackImpossible = errors.New("sqlxmigrate: It's impossible to rollback this migration") // ErrNoMigrationDefined is returned when no migration is defined. ErrNoMigrationDefined = errors.New("sqlxmigrate: No migration defined") // ErrMissingID is returned when the ID od migration is equal to "" ErrMissingID = errors.New("sqlxmigrate: Missing ID in migration") // ErrNoRunMigration is returned when any run migration was found while // running RollbackLast ErrNoRunMigration = errors.New("sqlxmigrate: Could not find last run migration") // ErrMigrationIDDoesNotExist is returned when migrating or rolling back to a migration ID that // does not exist in the list of migrations ErrMigrationIDDoesNotExist = errors.New("sqlxmigrate: Tried to migrate to an ID that doesn't exist") )
Functions ¶
This section is empty.
Types ¶
type DuplicatedIDError ¶
type DuplicatedIDError struct {
ID string
}
DuplicatedIDError is returned when more than one migration have the same ID
func (*DuplicatedIDError) Error ¶
func (e *DuplicatedIDError) Error() string
type InitSchemaFunc ¶
InitSchemaFunc is the func signature for initializing the schema.
type MigrateFunc ¶
MigrateFunc is the func signature for migrating.
type Migration ¶
type Migration struct { // ID is the migration identifier. Usually a timestamp like "201601021504". ID string // Migrate is a function that will br executed while running this migration. Migrate MigrateFunc // Rollback will be executed on rollback. Can be nil. Rollback RollbackFunc }
Migration represents a database migration (a modification to be made on the database).
type Options ¶
type Options struct { // TableName is the migration table. TableName string // IDColumnName is the name of column where the migration id will be stored. IDColumnName string // IDColumnSize is the length of the migration id column IDColumnSize int }
Options define options for all migrations.
type ReservedIDError ¶
type ReservedIDError struct {
ID string
}
ReservedIDError is returned when a migration is using a reserved ID
func (*ReservedIDError) Error ¶
func (e *ReservedIDError) Error() string
type RollbackFunc ¶
RollbackFunc is the func signature for rollbacking.
type Sqlxmigrate ¶
type Sqlxmigrate struct {
// contains filtered or unexported fields
}
Sqlxmigrate represents a collection of all migrations of a database schema.
func New ¶
func New(db *sqlx.DB, options *Options, migrations []*Migration) *Sqlxmigrate
New returns a new Sqlxmigrate.
func (*Sqlxmigrate) InitSchema ¶
func (g *Sqlxmigrate) InitSchema(initSchema InitSchemaFunc)
InitSchema sets a function that is run if no migration is found. The idea is preventing to run all migrations when a new clean database is being migrating. In this function you should create all tables and foreign key necessary to your application.
func (*Sqlxmigrate) Migrate ¶
func (g *Sqlxmigrate) Migrate() error
Migrate executes all migrations that did not run yet.
func (*Sqlxmigrate) MigrateTo ¶
func (g *Sqlxmigrate) MigrateTo(migrationID string) error
MigrateTo executes all migrations that did not run yet up to the migration that matches `migrationID`.
func (*Sqlxmigrate) RollbackLast ¶
func (g *Sqlxmigrate) RollbackLast() error
RollbackLast undo the last migration
func (*Sqlxmigrate) RollbackMigration ¶
func (g *Sqlxmigrate) RollbackMigration(m *Migration) error
RollbackMigration undo a migration.
func (*Sqlxmigrate) RollbackTo ¶
func (g *Sqlxmigrate) RollbackTo(migrationID string) error
RollbackTo undoes migrations up to the given migration that matches the `migrationID`. Migration with the matching `migrationID` is not rolled back.
func (*Sqlxmigrate) SetLogger ¶
func (g *Sqlxmigrate) SetLogger(logger *log.Logger)
SetLogger allows the default logger to be overwritten