mirror of
https://github.com/pressly/goose.git
synced 2025-05-31 11:42:04 +00:00
Update formatting per PR comments
Also rename `VersionId` to `VersionID`
This commit is contained in:
parent
5ee045e0f9
commit
abb2957c5d
@ -6,7 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Create : Writes a new blank migration file.
|
// Create writes a new blank migration file.
|
||||||
func Create(db *sql.DB, dir, name, migrationType string) error {
|
func Create(db *sql.DB, dir, name, migrationType string) error {
|
||||||
path, err := CreateMigration(name, migrationType, dir, time.Now())
|
path, err := CreateMigration(name, migrationType, dir, time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
14
dialect.go
14
dialect.go
@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SQLDialect : abstracts the details of specific SQL dialects
|
// SQLDialect abstracts the details of specific SQL dialects
|
||||||
// for goose's few SQL specific statements
|
// for goose's few SQL specific statements
|
||||||
type SQLDialect interface {
|
type SQLDialect interface {
|
||||||
createVersionTableSQL() string // sql string to create the goose_db_version table
|
createVersionTableSQL() string // sql string to create the goose_db_version table
|
||||||
@ -15,12 +15,12 @@ type SQLDialect interface {
|
|||||||
|
|
||||||
var dialect SQLDialect = &PostgresDialect{}
|
var dialect SQLDialect = &PostgresDialect{}
|
||||||
|
|
||||||
// GetDialect : Get the SQLDialect
|
// GetDialect gets the SQLDialect
|
||||||
func GetDialect() SQLDialect {
|
func GetDialect() SQLDialect {
|
||||||
return dialect
|
return dialect
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDialect : Set the SQLDialect
|
// SetDialect sets the SQLDialect
|
||||||
func SetDialect(d string) error {
|
func SetDialect(d string) error {
|
||||||
switch d {
|
switch d {
|
||||||
case "postgres":
|
case "postgres":
|
||||||
@ -42,7 +42,7 @@ func SetDialect(d string) error {
|
|||||||
// Postgres
|
// Postgres
|
||||||
////////////////////////////
|
////////////////////////////
|
||||||
|
|
||||||
// PostgresDialect : The PostgreSQL dialect struct.
|
// PostgresDialect struct.
|
||||||
type PostgresDialect struct{}
|
type PostgresDialect struct{}
|
||||||
|
|
||||||
func (pg PostgresDialect) createVersionTableSQL() string {
|
func (pg PostgresDialect) createVersionTableSQL() string {
|
||||||
@ -72,7 +72,7 @@ func (pg PostgresDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) {
|
|||||||
// MySQL
|
// MySQL
|
||||||
////////////////////////////
|
////////////////////////////
|
||||||
|
|
||||||
// MySQLDialect : The MySQLDialect struct.
|
// MySQLDialect struct.
|
||||||
type MySQLDialect struct{}
|
type MySQLDialect struct{}
|
||||||
|
|
||||||
func (m MySQLDialect) createVersionTableSQL() string {
|
func (m MySQLDialect) createVersionTableSQL() string {
|
||||||
@ -102,7 +102,7 @@ func (m MySQLDialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) {
|
|||||||
// sqlite3
|
// sqlite3
|
||||||
////////////////////////////
|
////////////////////////////
|
||||||
|
|
||||||
// Sqlite3Dialect : The sqlite dialect struct.
|
// Sqlite3Dialect struct.
|
||||||
type Sqlite3Dialect struct{}
|
type Sqlite3Dialect struct{}
|
||||||
|
|
||||||
func (m Sqlite3Dialect) createVersionTableSQL() string {
|
func (m Sqlite3Dialect) createVersionTableSQL() string {
|
||||||
@ -131,7 +131,7 @@ func (m Sqlite3Dialect) dbVersionQuery(db *sql.DB) (*sql.Rows, error) {
|
|||||||
// Redshift
|
// Redshift
|
||||||
////////////////////////////
|
////////////////////////////
|
||||||
|
|
||||||
// RedshiftDialect : The Redshift dialect struct.
|
// RedshiftDialect struct.
|
||||||
type RedshiftDialect struct{}
|
type RedshiftDialect struct{}
|
||||||
|
|
||||||
func (rs RedshiftDialect) createVersionTableSQL() string {
|
func (rs RedshiftDialect) createVersionTableSQL() string {
|
||||||
|
4
down.go
4
down.go
@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Down : Roll back a single migration from the current version.
|
// Down rolls back a single migration from the current version.
|
||||||
func Down(db *sql.DB, dir string) error {
|
func Down(db *sql.DB, dir string) error {
|
||||||
currentVersion, err := GetDBVersion(db)
|
currentVersion, err := GetDBVersion(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -25,7 +25,7 @@ func Down(db *sql.DB, dir string) error {
|
|||||||
return current.Down(db)
|
return current.Down(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DownTo : Roll back migrations to a specific version.
|
// DownTo rolls back migrations to a specific version.
|
||||||
func DownTo(db *sql.DB, dir string, version int64) error {
|
func DownTo(db *sql.DB, dir string, version int64) error {
|
||||||
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
|
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
2
goose.go
2
goose.go
@ -13,7 +13,7 @@ var (
|
|||||||
maxVersion = int64((1 << 63) - 1)
|
maxVersion = int64((1 << 63) - 1)
|
||||||
)
|
)
|
||||||
|
|
||||||
// Run : Run a goose command.
|
// Run runs a goose command.
|
||||||
func Run(command string, db *sql.DB, dir string, args ...string) error {
|
func Run(command string, db *sql.DB, dir string, args ...string) error {
|
||||||
switch command {
|
switch command {
|
||||||
case "up":
|
case "up":
|
||||||
|
30
migrate.go
30
migrate.go
@ -11,17 +11,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrNoCurrentVersion : Error when not find a current migration version.
|
// ErrNoCurrentVersion when a current migration version is not found.
|
||||||
ErrNoCurrentVersion = errors.New("no current version found")
|
ErrNoCurrentVersion = errors.New("no current version found")
|
||||||
// ErrNoNextVersion : Error when not find a next migration version.
|
// ErrNoNextVersion when the next migration version is not found.
|
||||||
ErrNoNextVersion = errors.New("no next version found")
|
ErrNoNextVersion = errors.New("no next version found")
|
||||||
// MaxVersion : The maximum allowed version.
|
// MaxVersion is the maximum allowed version.
|
||||||
MaxVersion int64 = 9223372036854775807 // max(int64)
|
MaxVersion int64 = 9223372036854775807 // max(int64)
|
||||||
|
|
||||||
goMigrations []*Migration
|
goMigrations []*Migration
|
||||||
)
|
)
|
||||||
|
|
||||||
// Migrations : Slice of migrations.
|
// Migrations slice.
|
||||||
type Migrations []*Migration
|
type Migrations []*Migration
|
||||||
|
|
||||||
// helpers so we can use pkg sort
|
// helpers so we can use pkg sort
|
||||||
@ -34,7 +34,7 @@ func (ms Migrations) Less(i, j int) bool {
|
|||||||
return ms[i].Version < ms[j].Version
|
return ms[i].Version < ms[j].Version
|
||||||
}
|
}
|
||||||
|
|
||||||
// Current : Get the current migration.
|
// Current gets the current migration.
|
||||||
func (ms Migrations) Current(current int64) (*Migration, error) {
|
func (ms Migrations) Current(current int64) (*Migration, error) {
|
||||||
for i, migration := range ms {
|
for i, migration := range ms {
|
||||||
if migration.Version == current {
|
if migration.Version == current {
|
||||||
@ -45,7 +45,7 @@ func (ms Migrations) Current(current int64) (*Migration, error) {
|
|||||||
return nil, ErrNoCurrentVersion
|
return nil, ErrNoCurrentVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next : Get the next migration.
|
// Next gets the next migration.
|
||||||
func (ms Migrations) Next(current int64) (*Migration, error) {
|
func (ms Migrations) Next(current int64) (*Migration, error) {
|
||||||
for i, migration := range ms {
|
for i, migration := range ms {
|
||||||
if migration.Version > current {
|
if migration.Version > current {
|
||||||
@ -67,7 +67,7 @@ func (ms Migrations) Previous(current int64) (*Migration, error) {
|
|||||||
return nil, ErrNoNextVersion
|
return nil, ErrNoNextVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
// Last : Get the last migration.
|
// Last gets the last migration.
|
||||||
func (ms Migrations) Last() (*Migration, error) {
|
func (ms Migrations) Last() (*Migration, error) {
|
||||||
if len(ms) == 0 {
|
if len(ms) == 0 {
|
||||||
return nil, ErrNoNextVersion
|
return nil, ErrNoNextVersion
|
||||||
@ -84,7 +84,7 @@ func (ms Migrations) String() string {
|
|||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddMigration : Add a migration.
|
// AddMigration adds a migration.
|
||||||
func AddMigration(up func(*sql.Tx) error, down func(*sql.Tx) error) {
|
func AddMigration(up func(*sql.Tx) error, down func(*sql.Tx) error) {
|
||||||
_, filename, _, _ := runtime.Caller(1)
|
_, filename, _, _ := runtime.Caller(1)
|
||||||
AddNamedMigration(filename, up, down)
|
AddNamedMigration(filename, up, down)
|
||||||
@ -98,7 +98,7 @@ func AddNamedMigration(filename string, up func(*sql.Tx) error, down func(*sql.T
|
|||||||
goMigrations = append(goMigrations, migration)
|
goMigrations = append(goMigrations, migration)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CollectMigrations : Returns all the valid looking migration scripts in the
|
// CollectMigrations returns all the valid looking migration scripts in the
|
||||||
// migrations folder and go func registry, and key them by version.
|
// migrations folder and go func registry, and key them by version.
|
||||||
func CollectMigrations(dirpath string, current, target int64) (Migrations, error) {
|
func CollectMigrations(dirpath string, current, target int64) (Migrations, error) {
|
||||||
var migrations Migrations
|
var migrations Migrations
|
||||||
@ -170,7 +170,7 @@ func versionFilter(v, current, target int64) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnsureDBVersion: Retrieve the current version for this DB.
|
// EnsureDBVersion retrieves the current version for this DB.
|
||||||
// Create and initialize the DB version table if it doesn't exist.
|
// Create and initialize the DB version table if it doesn't exist.
|
||||||
func EnsureDBVersion(db *sql.DB) (int64, error) {
|
func EnsureDBVersion(db *sql.DB) (int64, error) {
|
||||||
rows, err := GetDialect().dbVersionQuery(db)
|
rows, err := GetDialect().dbVersionQuery(db)
|
||||||
@ -187,14 +187,14 @@ func EnsureDBVersion(db *sql.DB) (int64, error) {
|
|||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var row MigrationRecord
|
var row MigrationRecord
|
||||||
if err = rows.Scan(&row.VersionId, &row.IsApplied); err != nil {
|
if err = rows.Scan(&row.VersionID, &row.IsApplied); err != nil {
|
||||||
log.Fatal("error scanning rows:", err)
|
log.Fatal("error scanning rows:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// have we already marked this version to be skipped?
|
// have we already marked this version to be skipped?
|
||||||
skip := false
|
skip := false
|
||||||
for _, v := range toSkip {
|
for _, v := range toSkip {
|
||||||
if v == row.VersionId {
|
if v == row.VersionID {
|
||||||
skip = true
|
skip = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -206,11 +206,11 @@ func EnsureDBVersion(db *sql.DB) (int64, error) {
|
|||||||
|
|
||||||
// if version has been applied we're done
|
// if version has been applied we're done
|
||||||
if row.IsApplied {
|
if row.IsApplied {
|
||||||
return row.VersionId, nil
|
return row.VersionID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// latest version of migration has not been applied.
|
// latest version of migration has not been applied.
|
||||||
toSkip = append(toSkip, row.VersionId)
|
toSkip = append(toSkip, row.VersionID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, ErrNoNextVersion
|
return 0, ErrNoNextVersion
|
||||||
@ -241,7 +241,7 @@ func createVersionTable(db *sql.DB) error {
|
|||||||
return txn.Commit()
|
return txn.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDBVersion : Wrapper for EnsureDBVersion for callers that don't already
|
// GetDBVersion is a wrapper for EnsureDBVersion for callers that don't already
|
||||||
// have their own DB instance
|
// have their own DB instance
|
||||||
func GetDBVersion(db *sql.DB) (int64, error) {
|
func GetDBVersion(db *sql.DB) (int64, error) {
|
||||||
version, err := EnsureDBVersion(db)
|
version, err := EnsureDBVersion(db)
|
||||||
|
16
migration.go
16
migration.go
@ -12,14 +12,14 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MigrationRecord : A migration record struct.
|
// MigrationRecord struct.
|
||||||
type MigrationRecord struct {
|
type MigrationRecord struct {
|
||||||
VersionId int64
|
VersionID int64
|
||||||
TStamp time.Time
|
TStamp time.Time
|
||||||
IsApplied bool // was this a result of up() or down()
|
IsApplied bool // was this a result of up() or down()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Migration : The migration struct.
|
// Migration struct.
|
||||||
type Migration struct {
|
type Migration struct {
|
||||||
Version int64
|
Version int64
|
||||||
Next int64 // next version, or -1 if none
|
Next int64 // next version, or -1 if none
|
||||||
@ -33,12 +33,12 @@ func (m *Migration) String() string {
|
|||||||
return fmt.Sprintf(m.Source)
|
return fmt.Sprintf(m.Source)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Up : Run an up migration.
|
// Up runs an up migration.
|
||||||
func (m *Migration) Up(db *sql.DB) error {
|
func (m *Migration) Up(db *sql.DB) error {
|
||||||
return m.run(db, true)
|
return m.run(db, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Down : Run a down migration.
|
// Down runs a down migration.
|
||||||
func (m *Migration) Down(db *sql.DB) error {
|
func (m *Migration) Down(db *sql.DB) error {
|
||||||
return m.run(db, false)
|
return m.run(db, false)
|
||||||
}
|
}
|
||||||
@ -78,7 +78,7 @@ func (m *Migration) run(db *sql.DB, direction bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NumericComponent : look for migration scripts with names in the form:
|
// NumericComponent looks for migration scripts with names in the form:
|
||||||
// XXX_descriptivename.ext where XXX specifies the version number
|
// XXX_descriptivename.ext where XXX specifies the version number
|
||||||
// and ext specifies the type of migration
|
// and ext specifies the type of migration
|
||||||
func NumericComponent(name string) (int64, error) {
|
func NumericComponent(name string) (int64, error) {
|
||||||
@ -102,7 +102,7 @@ func NumericComponent(name string) (int64, error) {
|
|||||||
return n, e
|
return n, e
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateMigration : Create a migration.
|
// CreateMigration creates a migration.
|
||||||
func CreateMigration(name, migrationType, dir string, t time.Time) (path string, err error) {
|
func CreateMigration(name, migrationType, dir string, t time.Time) (path string, err error) {
|
||||||
|
|
||||||
if migrationType != "go" && migrationType != "sql" {
|
if migrationType != "go" && migrationType != "sql" {
|
||||||
@ -123,7 +123,7 @@ func CreateMigration(name, migrationType, dir string, t time.Time) (path string,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// FinalizeMigration : Update the version table for the given migration,
|
// FinalizeMigration updates the version table for the given migration,
|
||||||
// and finalize the transaction.
|
// and finalize the transaction.
|
||||||
func FinalizeMigration(tx *sql.Tx, direction bool, v int64) error {
|
func FinalizeMigration(tx *sql.Tx, direction bool, v int64) error {
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Version : Print the current version of the database.
|
// Version prints the current version of the database.
|
||||||
func Version(db *sql.DB, dir string) error {
|
func Version(db *sql.DB, dir string) error {
|
||||||
current, err := GetDBVersion(db)
|
current, err := GetDBVersion(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user