feat: little refactoring

This commit is contained in:
Yanislav Igonin 2022-01-21 01:24:30 +02:00
parent 62be228308
commit cb4eaa9fc2

View File

@ -47,16 +47,8 @@ func Migrate() {
_, isMigrationInDb := dbMigrations[id] _, isMigrationInDb := dbMigrations[id]
if !isMigrationInDb { if !isMigrationInDb {
_, err := Pool.Exec(context.TODO(), Files.ReadFileText(m)) sql := Files.ReadFileText(m)
if err != nil { runMigration(id, name, sql)
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)
}
log.Println("database migration - " + name + " - online") log.Println("database migration - " + name + " - online")
} }
} }
@ -64,6 +56,19 @@ func Migrate() {
log.Println("database migrations - online") 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 { func getDbMigrations() MigrationsMap {
sql := `SELECT id, name FROM migrations` sql := `SELECT id, name FROM migrations`
rows, err := Pool.Query(context.TODO(), sql) rows, err := Pool.Query(context.TODO(), sql)