You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
migrate/migrate.go

114 lines
2.6 KiB

7 years ago
// Package migrate allows you to update your database from your application
package migrate
import (
"database/sql"
"errors"
"fmt"
2 years ago
"io/fs"
7 years ago
"strconv"
)
// Options contains all settings
type Options struct {
2 years ago
TableName string // Name used for version info table; defaults to DefaultTableName if not set
Schema string // Schema used for version info table; In PostgreSQL the current schema is changed to the one specified here
7 years ago
}
// DefaultTableName is the name used when no TableName is specified in Options
const DefaultTableName = `version`
// ErrUpdatesMissing indicates an update is missing, making it impossible to execute the migration
var ErrUpdatesMissing = errors.New(`Missing migration files`)
const fileFormat = `%04d.sql`
// AssetFunc is a function that returns the data for the given name
type AssetFunc func(string) ([]byte, error)
2 years ago
// Migrate executes all migrations
// Filenames need to have incrementing numbers
// Downgrading is not supported as it could result in data loss
2 years ago
func Migrate(db *sql.DB, o Options, assets fs.FS) error {
entries, err := fs.ReadDir(assets, `.`)
if err != nil {
panic(`failed to read list of files`)
7 years ago
}
2 years ago
version := len(entries)
tx, err := db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
2 years ago
dbType := getDbType(db)
searchPath := `public`
if o.TableName == `` {
o.TableName = DefaultTableName
}
if o.Schema != `` {
err = createSchemaIfNotExists(tx, o.Schema)
if err != nil {
return err
}
if dbType == `pq` {
_ = tx.QueryRow(`SHOW search_path`).Scan(&searchPath)
_, err = tx.Exec(`SET search_path TO ` + o.Schema)
if err != nil {
return err
}
}
7 years ago
}
versionTable, err := createTableIfNotExists(tx, o.Schema, o.TableName)
7 years ago
if err != nil {
return err
}
row := tx.QueryRow(`SELECT version FROM ` + versionTable + ` ORDER BY Version DESC`)
7 years ago
var v int
err = row.Scan(&v)
if err != sql.ErrNoRows && err != nil {
return err
}
for i := v + 1; i <= version; i++ {
2 years ago
fileName := fmt.Sprintf(fileFormat, i)
errorf := func(e error) error { return fmt.Errorf(`migration "%s" failed: %w`, fileName, e) }
2 years ago
script, err := fs.ReadFile(assets, fileName)
7 years ago
if err != nil {
return errorf(ErrUpdatesMissing)
7 years ago
}
_, err = tx.Exec(string(script))
7 years ago
if err != nil {
return errorf(err)
7 years ago
}
_, err = tx.Exec(`INSERT INTO ` + versionTable + ` (version) VALUES (` + strconv.Itoa(i) + `)`)
7 years ago
if err != nil {
return errorf(err)
7 years ago
}
}
if dbType == `pq` && o.Schema != `` {
_, err = tx.Exec(`SET search_path TO ` + searchPath)
if err != nil {
return err
}
}
7 years ago
err = tx.Commit()
if err != nil {
return err
}
return nil
}