Linting.
This commit is contained in:
@@ -47,6 +47,7 @@ func startConnectionWatcher() {
|
||||
|
||||
ticker.Stop()
|
||||
log.Println("Connection watcher stopped and connection to database was shutted down")
|
||||
|
||||
connWatcherStopped = true
|
||||
}
|
||||
|
||||
@@ -56,11 +57,14 @@ func watcher() bool {
|
||||
// If we're shutting down - stop connection watcher.
|
||||
if weAreShuttingDown {
|
||||
log.Println("Closing database connection...")
|
||||
|
||||
err := Conn.Close()
|
||||
if err != nil {
|
||||
log.Println("Failed to close database connection")
|
||||
}
|
||||
|
||||
Conn = nil
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
@@ -14,7 +14,7 @@ var (
|
||||
Conn *sqlx.DB
|
||||
|
||||
// Shutdown flags.
|
||||
// Sets to true when Shutdown() is called to indicate other subsystes
|
||||
// Sets to true when Shutdown() is called to indicate other subsystems
|
||||
// that we're shutting down.
|
||||
weAreShuttingDown bool
|
||||
// Sets to true when connection watcher will be stopped.
|
||||
@@ -35,6 +35,7 @@ func Initialize() {
|
||||
|
||||
func Shutdown() {
|
||||
weAreShuttingDown = true
|
||||
|
||||
for {
|
||||
if connWatcherStopped {
|
||||
break
|
||||
|
@@ -11,21 +11,23 @@ import (
|
||||
"github.com/pressly/goose"
|
||||
)
|
||||
|
||||
// Initialize initializes database migrations. This is the function
|
||||
// where migrations should be registered.
|
||||
func Initialize() {
|
||||
log.Println("Initializing database migrations...")
|
||||
|
||||
_ = goose.SetDialect("postgres")
|
||||
|
||||
goose.AddNamedMigration("1_create_users_table.go", CreateUsersTableUp, CreateUsersTableDown)
|
||||
|
||||
// Migrations should be registered here.
|
||||
}
|
||||
|
||||
// Migrate parses environment for necessary parameters and starts
|
||||
// database migration process.
|
||||
func Migrate(db *sql.DB) {
|
||||
log.Println("Starting database migration procedure...")
|
||||
|
||||
// Prepare migrations configuration.
|
||||
var action = "UP"
|
||||
|
||||
actionFromEnv, actionFound := os.LookupEnv("DATABASE_ACTION")
|
||||
if actionFound {
|
||||
log.Println("Migration action override: " + actionFromEnv)
|
||||
@@ -35,13 +37,16 @@ func Migrate(db *sql.DB) {
|
||||
}
|
||||
|
||||
var count int64
|
||||
|
||||
countFromEnv, countFound := os.LookupEnv("DATABASE_COUNT")
|
||||
if countFound {
|
||||
log.Println("Migration count override: " + countFromEnv)
|
||||
|
||||
countAsInt, err := strconv.ParseInt(countFromEnv, 10, 64)
|
||||
if err != nil {
|
||||
log.Fatalln("Failed to convert count gathered from DATABASE_COUNT to integer")
|
||||
}
|
||||
|
||||
count = countAsInt
|
||||
} else {
|
||||
log.Println("Applying or rollback this count of migrations: " + countFromEnv + ". 0 - all.")
|
||||
@@ -49,27 +54,35 @@ func Migrate(db *sql.DB) {
|
||||
|
||||
// Execute migrations.
|
||||
var err error
|
||||
|
||||
currentDBVersion, gooseerr := goose.GetDBVersion(db)
|
||||
|
||||
if gooseerr != nil {
|
||||
log.Fatalln("Failed to get database version: " + gooseerr.Error())
|
||||
}
|
||||
|
||||
log.Println("Current database version obtained: " + strconv.Itoa(int(currentDBVersion)))
|
||||
if action == "UP" && count == 0 {
|
||||
|
||||
switch {
|
||||
case action == "UP" && count == 0:
|
||||
log.Println("Applying all unapplied migrations...")
|
||||
|
||||
err = goose.Up(db, ".")
|
||||
} else if action == "UP" && count != 0 {
|
||||
case action == "UP" && count != 0:
|
||||
newVersion := currentDBVersion + count
|
||||
log.Println("Migrating database to specific version: " + strconv.Itoa(int(newVersion)))
|
||||
err = goose.UpTo(db, ".", newVersion)
|
||||
} else if action == "DOWN" && count == 0 {
|
||||
case action == "DOWN" && count == 0:
|
||||
log.Println("Downgrading database to zero state, you'll need to re-apply migrations!")
|
||||
err = goose.Down(db, ".")
|
||||
|
||||
_ = goose.Down(db, ".")
|
||||
|
||||
log.Fatalln("Database downgraded to zero state. You have to re-apply migrations")
|
||||
} else if action == "DOWN" && count != 0 {
|
||||
case action == "DOWN" && count != 0:
|
||||
newVersion := currentDBVersion - count
|
||||
log.Println("Downgrading database to specific version: " + strconv.Itoa(int(newVersion)))
|
||||
err = goose.DownTo(db, ".", newVersion)
|
||||
} else {
|
||||
default:
|
||||
log.Fatalln("Unsupported set of migration parameters, cannot continue: " + action + "/" + countFromEnv)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user