mirror of
https://github.com/pressly/goose.git
synced 2025-05-01 21:19:45 +00:00
Reset rolls back all migrations Refresh rolls back all migrations and applies all available migrations again it's useful for development to put db in latest state without full recreation of db.
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package goose
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
"sort"
|
|
)
|
|
|
|
// Reset rolls back all migrations
|
|
func Reset(db *sql.DB, dir string) error {
|
|
migrations, err := CollectMigrations(dir, minVersion, maxVersion)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
statuses, err := dbMigrationsStatus(db)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sort.Sort(sort.Reverse(migrations))
|
|
|
|
for _, migration := range migrations {
|
|
if !statuses[migration.Version] {
|
|
continue
|
|
}
|
|
if err = migration.Down(db); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func dbMigrationsStatus(db *sql.DB) (map[int64]bool, error) {
|
|
rows, err := GetDialect().dbVersionQuery(db)
|
|
if err != nil {
|
|
return map[int64]bool{}, createVersionTable(db)
|
|
}
|
|
defer rows.Close()
|
|
|
|
// The most recent record for each migration specifies
|
|
// whether it has been applied or rolled back.
|
|
|
|
result := make(map[int64]bool)
|
|
|
|
for rows.Next() {
|
|
var row MigrationRecord
|
|
if err = rows.Scan(&row.VersionID, &row.IsApplied); err != nil {
|
|
log.Fatal("error scanning rows:", err)
|
|
}
|
|
|
|
if _, ok := result[row.VersionID]; ok {
|
|
continue
|
|
}
|
|
|
|
result[row.VersionID] = row.IsApplied
|
|
}
|
|
|
|
return result, nil
|
|
}
|