1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

refactor: move migrations to separate method

This commit is contained in:
William Flores 2020-04-25 13:53:55 -07:00
parent 7a339d77d3
commit 7f548dabeb
No known key found for this signature in database
GPG Key ID: E4E51CFD36F843ED

53
main.go
View File

@ -121,32 +121,14 @@ func main() {
// Read Config // Read Config
config := readConfig() config := readConfig()
// Enable line numbers in logging // Enable line numbers in logging
log.SetFlags(log.LstdFlags | log.Lshortfile) if config.IsDev() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
// Connect to database // Connect to database
db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config)) db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config))
var migrateDo func()
if config.DbDialect == "sqlite3" { if config.DbDialect == "sqlite3" {
db.DB().Exec("PRAGMA foreign_keys = ON;") db.DB().Exec("PRAGMA foreign_keys = ON;")
migrations := &migrate.PackrMigrationSource{
Box: packr.New("migrations", "./migrations/sqlite3"),
}
migrateDo = func() {
n, err := migrate.Exec(db.DB(), "sqlite3", migrations, migrate.Up)
if err != nil {
log.Fatal(err)
}
log.Printf("Applied %d migrations!\n", n)
}
} else {
migrateDo = func() {
db.AutoMigrate(&models.Alias{})
db.AutoMigrate(&models.Summary{})
db.AutoMigrate(&models.SummaryItem{})
db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.Heartbeat{}).AddForeignKey("user_id", "users(id)", "RESTRICT", "RESTRICT")
db.AutoMigrate(&models.SummaryItem{}).AddForeignKey("summary_id", "summaries(id)", "CASCADE", "CASCADE")
}
} }
db.LogMode(config.IsDev()) db.LogMode(config.IsDev())
db.DB().SetMaxIdleConns(int(config.DbMaxConn)) db.DB().SetMaxIdleConns(int(config.DbMaxConn))
@ -159,7 +141,8 @@ func main() {
defer db.Close() defer db.Close()
// Migrate database schema // Migrate database schema
migrateDo() migrateDo := databaseMigrateActions(config.DbDialect)
migrateDo(db)
// Custom migrations and initial data // Custom migrations and initial data
addDefaultUser(db, config) addDefaultUser(db, config)
@ -243,6 +226,32 @@ func main() {
s.ListenAndServe() s.ListenAndServe()
} }
func databaseMigrateActions(dbDialect string) func(db *gorm.DB) {
var migrateDo func(db *gorm.DB)
if dbDialect == "sqlite3" {
migrations := &migrate.PackrMigrationSource{
Box: packr.New("migrations", "./migrations/sqlite3"),
}
migrateDo = func(db *gorm.DB) {
n, err := migrate.Exec(db.DB(), "sqlite3", migrations, migrate.Up)
if err != nil {
log.Fatal(err)
}
log.Printf("Applied %d migrations!\n", n)
}
} else {
migrateDo = func(db *gorm.DB) {
db.AutoMigrate(&models.Alias{})
db.AutoMigrate(&models.Summary{})
db.AutoMigrate(&models.SummaryItem{})
db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.Heartbeat{}).AddForeignKey("user_id", "users(id)", "RESTRICT", "RESTRICT")
db.AutoMigrate(&models.SummaryItem{}).AddForeignKey("summary_id", "summaries(id)", "CASCADE", "CASCADE")
}
}
return migrateDo
}
func migrateLanguages(db *gorm.DB, cfg *models.Config) { func migrateLanguages(db *gorm.DB, cfg *models.Config) {
for k, v := range cfg.CustomLanguages { for k, v := range cfg.CustomLanguages {
result := db.Model(models.Heartbeat{}). result := db.Model(models.Heartbeat{}).