Skip to content

Commit 31a82e8

Browse files
DugowitchMichaelMraka
authored andcommitted
RHINENG-21214: fix migration logging
1 parent e75e37d commit 31a82e8

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

database_admin/migrate.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"app/base/utils"
55
"database/sql"
66
"fmt"
7+
"log"
78
"os"
89
"path/filepath"
910
"strconv"
@@ -30,7 +31,7 @@ func NewConn(databaseURL string) (database.Driver, *sql.DB, error) {
3031
}
3132

3233
loggingConn := pq.ConnectorWithNoticeHandler(baseConn, func(e *pq.Error) {
33-
fmt.Printf("Notice: %v\n", e)
34+
utils.LogInfo("Notice: " + e.Error())
3435
})
3536

3637
db := sql.OpenDB(loggingConn)
@@ -61,12 +62,12 @@ func MigrateUp(conn database.Driver, sourceURL string) {
6162
}
6263

6364
if err != nil && err.Error() == "no change" {
64-
fmt.Println("no change")
65+
utils.LogInfo("no change")
6566
return
6667
}
6768

6869
if err != nil {
69-
fmt.Fprintf(os.Stderr, "Error upgrading the database: %v", err.Error())
70+
utils.LogError("err", err.Error(), "error upgrading the database")
7071
panic(err)
7172
}
7273
}
@@ -109,44 +110,44 @@ func dbSchemaVersion(conn database.Driver, sourceURL string) (int, error) {
109110

110111
func migrateAction(conn database.Driver, sourceURL string) int {
111112
expectedSchema := schemaMigration
112-
fmt.Printf("DB migration in progress, waiting for schema=%d\n", expectedSchema)
113+
utils.LogInfo("schema", expectedSchema, "DB migration in progress, waiting for schema")
113114
dbSchema, err := dbSchemaVersion(conn, sourceURL)
114115
if err != nil {
115116
if errors.As(err, &migrate.ErrDirty{}) && forceMigrationVersion > 0 {
116117
return MIGRATE
117118
}
118-
fmt.Fprintf(os.Stderr, "Error getting current DB version: %v\n", err.Error())
119+
utils.LogError("err", err.Error(), "error getting current DB version")
119120
return BLOCK
120121
}
121122
migrationSchema, err := latestSchemaMigrationFileVersion(sourceURL)
122123
if err != nil {
123-
fmt.Fprint(os.Stderr, err.Error())
124+
utils.LogError("err", err.Error(), "failed to load latestSchemaMigrationFileVersion")
124125
return BLOCK
125126
}
126127

127128
if migrationSchema < expectedSchema || migrationSchema < dbSchema {
128129
// some migration files are missing
129-
fmt.Fprintf(os.Stderr, "Missing migration files for schema %d and newer\n", migrationSchema)
130+
utils.LogError("schema", migrationSchema, "missing migration files for schema")
130131
return BLOCK
131132
}
132133
if dbSchema == expectedSchema && migrationSchema > dbSchema {
133134
// schema can be upgraded but is intentionaly blocked by SCHEMA_MIGRATION
134-
fmt.Println("Deployment blocked, enable migrations to proceed")
135+
utils.LogWarn("deployment blocked, enable migrations to proceed")
135136
return BLOCK
136137
}
137138
if dbSchema == migrationSchema &&
138139
(dbSchema == expectedSchema || expectedSchema == -1) {
139-
fmt.Println("DB is upgraded")
140+
utils.LogInfo("DB is upgraded")
140141
return CONTINUE
141142
}
142-
fmt.Printf("current version: %d, expected: %d\n", dbSchema, expectedSchema)
143+
utils.LogInfo("current version:", dbSchema, "expected: ", expectedSchema, "migrateAction")
143144
return MIGRATE
144145
}
145146

146147
type logger struct{}
147148

148149
func (t logger) Printf(format string, v ...interface{}) {
149-
fmt.Printf(format, v...)
150+
log.Printf(format, v...)
150151
}
151152

152153
func (t logger) Verbose() bool {

0 commit comments

Comments
 (0)