diff --git a/db/db.go b/db/db.go index f43d287..240fc1d 100644 --- a/db/db.go +++ b/db/db.go @@ -47,16 +47,8 @@ func Migrate() { _, isMigrationInDb := dbMigrations[id] if !isMigrationInDb { - _, err := Pool.Exec(context.TODO(), Files.ReadFileText(m)) - if err != nil { - log.Panicln(err) - } - - sql := `INSERT INTO migrations (id, name) VALUES ($1, $2)` - _, err = Pool.Query(context.TODO(), sql, id, name) - if err != nil { - log.Panicln(err) - } + sql := Files.ReadFileText(m) + runMigration(id, name, sql) log.Println("database migration - " + name + " - online") } } @@ -64,6 +56,19 @@ func Migrate() { log.Println("database migrations - online") } +func runMigration(mid int, mname, msql string) { + _, err := Pool.Exec(context.TODO(), msql) + if err != nil { + log.Panicln(err) + } + + sql := `INSERT INTO migrations (id, name) VALUES ($1, $2)` + _, err = Pool.Query(context.TODO(), sql, mid, mname) + if err != nil { + log.Panicln(err) + } +} + func getDbMigrations() MigrationsMap { sql := `SELECT id, name FROM migrations` rows, err := Pool.Query(context.TODO(), sql)