From cb4eaa9fc28b0ace6bf0e8bcfee61accddb01832 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Fri, 21 Jan 2022 01:24:30 +0200 Subject: [PATCH] feat: little refactoring --- db/db.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) 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)