Skip to content

Commit

Permalink
Update migration model for using with different drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
ShkrutDenis committed Apr 20, 2020
1 parent 9809bad commit 111fbaf
Showing 1 changed file with 108 additions and 7 deletions.
115 changes: 108 additions & 7 deletions model/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package model

import (
"github.com/jmoiron/sqlx"
"os"
"time"
)

Expand All @@ -13,34 +14,134 @@ type Migration struct {
}

func CreateMigrationsTable(connection *sqlx.DB) bool {
_, err := connection.Exec("SELECT * FROM migrations LIMIT 1")
_, err := connection.Exec(checkTableExitSQL())
if err != nil {
connection.MustExec("CREATE TABLE migrations (id int auto_increment, name varchar(255) not null, batch int not null, created_at datetime default current_timestamp not null, constraint migrations_pk primary key (id));")
connection.MustExec(creteTableSQL())
return true
}
return false
}

func AddMigrationRaw(connection *sqlx.DB, migration string, lastBatch int) {
connection.MustExec("INSERT INTO migrations (name, batch) VALUES (?, ?)", migration, lastBatch)
connection.MustExec(addRawSQL(), migration, lastBatch)
}

func MigrationExist(connection *sqlx.DB, name string) bool {
var raw Migration
err := connection.Get(&raw, getRawSQL(), name)
if err != nil {
return false
}
return true
}

func GetLastBatch(connection *sqlx.DB) int {
var raw Migration
_ = connection.Get(&raw, "SELECT * FROM migrations ORDER BY batch DESC LIMIT 1")
_ = connection.Get(&raw, getLastBatchSQL())
return raw.Batch
}

func GetLastMigrations(connection *sqlx.DB, lastBatch int) []*Migration {
var list []*Migration
_ = connection.Select(&list, "SELECT * FROM migrations WHERE batch=? ORDER BY created_at DESC, id DESC;", lastBatch)
err := connection.Select(&list, getLastRawSQL(), lastBatch)
if err != nil {
panic(err)
}
return list
}

func RemoveMigrationRaw(connection *sqlx.DB, migration string) {
connection.MustExec("DELETE FROM migrations WHERE name=?", migration)
connection.MustExec(removeMigrationSQL(), migration)
}

func RemoveLastBatch(connection *sqlx.DB, lastBatch int) {
connection.MustExec("DELETE FROM migrations WHERE batch=?", lastBatch)
connection.MustExec(removeBatchSQL(), lastBatch)
}

func checkTableExitSQL() string {
switch os.Getenv("DB_DRIVER") {
case "mysql":
return "SELECT * FROM migrations LIMIT 1;"
case "postgres":
return "SELECT * FROM migrations LIMIT 1;"
default:
panic("Not supported DB driver: " + os.Getenv("DB_DRIVER"))
}
}

func creteTableSQL() string {
switch os.Getenv("DB_DRIVER") {
case "mysql":
return "CREATE TABLE migrations (id int auto_increment, name varchar(255) not null, batch int not null, created_at datetime default current_timestamp not null, constraint migrations_pk primary key (id));"
case "postgres":
return "CREATE TABLE migrations (id serial PRIMARY KEY, name varchar(255) not null, batch int not null, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP not null);"
default:
panic("Not supported DB driver: " + os.Getenv("DB_DRIVER"))
}
}

func getRawSQL() string {
switch os.Getenv("DB_DRIVER") {
case "mysql":
return "SELECT * FROM migrations WHERE name=?;"
case "postgres":
return "SELECT * FROM migrations WHERE name=$1;"
default:
panic("Not supported DB driver: " + os.Getenv("DB_DRIVER"))
}
}

func addRawSQL() string {
switch os.Getenv("DB_DRIVER") {
case "mysql":
return "INSERT INTO migrations (name, batch) VALUES (?, ?);"
case "postgres":
return "INSERT INTO migrations (name, batch) VALUES ($1, $2);"
default:
panic("Not supported DB driver: " + os.Getenv("DB_DRIVER"))
}
}

func getLastBatchSQL() string {
switch os.Getenv("DB_DRIVER") {
case "mysql":
return "SELECT * FROM migrations ORDER BY batch DESC LIMIT 1;"
case "postgres":
return "SELECT * FROM migrations ORDER BY batch DESC LIMIT 1;"
default:
panic("Not supported DB driver: " + os.Getenv("DB_DRIVER"))
}
}

func getLastRawSQL() string {
switch os.Getenv("DB_DRIVER") {
case "mysql":
return "SELECT * FROM migrations WHERE batch=? ORDER BY created_at DESC, id DESC;"
case "postgres":
return "SELECT * FROM migrations WHERE batch=$1 ORDER BY created_at DESC, id DESC;"
default:
panic("Not supported DB driver: " + os.Getenv("DB_DRIVER"))
}
}

func removeBatchSQL() string {
switch os.Getenv("DB_DRIVER") {
case "mysql":
return "DELETE FROM migrations WHERE batch=?;"
case "postgres":
return "DELETE FROM migrations WHERE batch=$1;"
default:
panic("Not supported DB driver: " + os.Getenv("DB_DRIVER"))
}
}

func removeMigrationSQL() string {
switch os.Getenv("DB_DRIVER") {
case "mysql":
return "DELETE FROM migrations WHERE name=$1"
case "postgres":
return ""
default:
panic("Not supported DB driver: " + os.Getenv("DB_DRIVER"))
}
}

0 comments on commit 111fbaf

Please sign in to comment.