gonews/database/migrations/exported.go

95 lines
2.6 KiB
Go
Raw Normal View History

2019-09-09 21:52:32 +05:00
package migrations
import (
// stdlib
"database/sql"
"log"
"os"
"strconv"
// other
"github.com/pressly/goose"
)
2019-10-22 04:38:40 +05:00
// Initialize initializes database migrations. This is the function
// where migrations should be registered.
2019-09-09 21:52:32 +05:00
func Initialize() {
log.Println("Initializing database migrations...")
_ = goose.SetDialect("postgres")
goose.AddNamedMigration("1_create_users_table.go", CreateUsersTableUp, CreateUsersTableDown)
}
2019-10-22 04:38:40 +05:00
// Migrate parses environment for necessary parameters and starts
// database migration process.
2019-09-09 21:52:32 +05:00
func Migrate(db *sql.DB) {
log.Println("Starting database migration procedure...")
var action = "UP"
2019-10-22 04:38:40 +05:00
2019-09-09 21:52:32 +05:00
actionFromEnv, actionFound := os.LookupEnv("DATABASE_ACTION")
if actionFound {
log.Println("Migration action override: " + actionFromEnv)
action = actionFromEnv
} else {
log.Println("Executing default migration action (UP)")
}
var count int64
2019-10-22 04:38:40 +05:00
2019-09-09 21:52:32 +05:00
countFromEnv, countFound := os.LookupEnv("DATABASE_COUNT")
if countFound {
log.Println("Migration count override: " + countFromEnv)
2019-10-22 04:38:40 +05:00
2019-09-09 21:52:32 +05:00
countAsInt, err := strconv.ParseInt(countFromEnv, 10, 64)
if err != nil {
log.Fatalln("Failed to convert count gathered from DATABASE_COUNT to integer")
}
2019-10-22 04:38:40 +05:00
2019-09-09 21:52:32 +05:00
count = countAsInt
} else {
log.Println("Applying or rollback this count of migrations: " + countFromEnv + ". 0 - all.")
}
// Execute migrations.
var err error
2019-10-22 04:38:40 +05:00
2019-09-09 21:52:32 +05:00
currentDBVersion, gooseerr := goose.GetDBVersion(db)
2019-10-22 04:38:40 +05:00
2019-09-09 21:52:32 +05:00
if gooseerr != nil {
log.Fatalln("Failed to get database version: " + gooseerr.Error())
}
2019-10-22 04:38:40 +05:00
2019-09-09 21:52:32 +05:00
log.Println("Current database version obtained: " + strconv.Itoa(int(currentDBVersion)))
2019-10-22 04:38:40 +05:00
switch {
case action == "UP" && count == 0:
2019-09-09 21:52:32 +05:00
log.Println("Applying all unapplied migrations...")
2019-10-22 04:38:40 +05:00
2019-09-09 21:52:32 +05:00
err = goose.Up(db, ".")
2019-10-22 04:38:40 +05:00
case action == "UP" && count != 0:
2019-09-09 21:52:32 +05:00
newVersion := currentDBVersion + count
log.Println("Migrating database to specific version: " + strconv.Itoa(int(newVersion)))
err = goose.UpTo(db, ".", newVersion)
2019-10-22 04:38:40 +05:00
case action == "DOWN" && count == 0:
2019-09-09 21:52:32 +05:00
log.Println("Downgrading database to zero state, you'll need to re-apply migrations!")
2019-10-22 04:38:40 +05:00
_ = goose.Down(db, ".")
2019-09-09 21:52:32 +05:00
log.Fatalln("Database downgraded to zero state. You have to re-apply migrations")
2019-10-22 04:38:40 +05:00
case action == "DOWN" && count != 0:
2019-09-09 21:52:32 +05:00
newVersion := currentDBVersion - count
log.Println("Downgrading database to specific version: " + strconv.Itoa(int(newVersion)))
err = goose.DownTo(db, ".", newVersion)
2019-10-22 04:38:40 +05:00
default:
2019-09-09 21:52:32 +05:00
log.Fatalln("Unsupported set of migration parameters, cannot continue: " + action + "/" + countFromEnv)
}
if err != nil {
log.Fatalln("Failed to execute migration sequence: " + err.Error())
}
log.Println("Database migrated successfully")
}