mirror of
https://github.com/yanislav-igonin/micrach
synced 2024-12-22 22:32:33 +03:00
feat: little refactoring
This commit is contained in:
parent
62be228308
commit
cb4eaa9fc2
25
db/db.go
25
db/db.go
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user