Skip to content

Commit 3b9c845

Browse files
authored
Merge pull request #872 from Vafilor/fix/migrations.configmap
2 parents 63afd31 + f88a98c commit 3b9c845

1 file changed

Lines changed: 27 additions & 19 deletions

File tree

main.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@ func main() {
5656
log.Fatalf("Failed to connect to Kubernetes cluster: %v", err)
5757
}
5858

59+
client.ClearSystemConfigCache()
60+
sysConfig, err := client.GetSystemConfig()
61+
if err != nil {
62+
log.Fatalf("Failed to get system config: %v", err)
63+
}
64+
65+
dbDriverName, databaseDataSourceName := sysConfig.DatabaseConnection()
66+
// sqlx.MustConnect will panic when it can't connect to DB. In that case, this whole application will crash.
67+
// This is okay, as the pod will restart and try connecting to DB again.
68+
// dbDriverName may be nil, but sqlx will then panic.
69+
db := sqlx.MustConnect(dbDriverName, databaseDataSourceName)
70+
goose.SetTableName("goose_db_version")
71+
if err := goose.Run("up", db.DB, filepath.Join("db", "sql")); err != nil {
72+
log.Fatalf("Failed to run database sql migrations: %v", err)
73+
db.Close()
74+
}
75+
76+
goose.SetTableName("goose_db_go_version")
77+
migrations.Initialize()
78+
if err := goose.Run("up", db.DB, filepath.Join("db", "go")); err != nil {
79+
log.Fatalf("Failed to run database go migrations: %v", err)
80+
db.Close()
81+
}
82+
5983
go watchConfigmapChanges("onepanel", stopCh, func(configMap *corev1.ConfigMap) error {
6084
log.Printf("Configmap changed")
6185
stopCh <- struct{}{}
@@ -65,36 +89,20 @@ func main() {
6589

6690
for {
6791
client.ClearSystemConfigCache()
68-
sysConfig, err := client.GetSystemConfig()
92+
sysConfig, err = client.GetSystemConfig()
6993
if err != nil {
7094
log.Fatalf("Failed to get system config: %v", err)
7195
}
7296

73-
dbDriverName, databaseDataSourceName := sysConfig.DatabaseConnection()
74-
// sqlx.MustConnect will panic when it can't connect to DB. In that case, this whole application will crash.
75-
// This is okay, as the pod will restart and try connecting to DB again.
76-
// dbDriverName may be nil, but sqlx will then panic.
77-
db := sqlx.MustConnect(dbDriverName, databaseDataSourceName)
78-
goose.SetTableName("goose_db_version")
79-
if err := goose.Run("up", db.DB, filepath.Join("db", "sql")); err != nil {
80-
log.Fatalf("Failed to run database sql migrations: %v", err)
81-
db.Close()
82-
}
83-
84-
goose.SetTableName("goose_db_go_version")
85-
migrations.Initialize()
86-
if err := goose.Run("up", db.DB, filepath.Join("db", "go")); err != nil {
87-
log.Fatalf("Failed to run database go migrations: %v", err)
88-
db.Close()
89-
}
97+
dbDriverName, databaseDataSourceName = sysConfig.DatabaseConnection()
9098

9199
s := startRPCServer(v1.NewDB(db), kubeConfig, sysConfig, stopCh)
92100

93101
<-stopCh
94102

95103
s.Stop()
96104
if err := db.Close(); err != nil {
97-
log.Printf("[error] closing db connection")
105+
log.Printf("[error] closing db connection %v", err.Error())
98106
}
99107
}
100108
}()

0 commit comments

Comments
 (0)