From 18f7f52e02ff6b538ea64dede7228c7d75496bb2 Mon Sep 17 00:00:00 2001 From: Denis Shkrut Date: Mon, 27 Apr 2020 20:48:21 +0300 Subject: [PATCH] Add verbose flag --- builder/mysql/key/unique.go | 8 +++++ builder/mysql/table/table.go | 8 ++++- builder/postgress/key/unique.go | 8 ++++- builder/postgress/table/table.go | 8 ++++- config/config.go | 33 +++++++++++++++++++ entry.go | 54 ++++++++++---------------------- 6 files changed, 79 insertions(+), 40 deletions(-) create mode 100644 config/config.go diff --git a/builder/mysql/key/unique.go b/builder/mysql/key/unique.go index a1d638a..eb30765 100644 --- a/builder/mysql/key/unique.go +++ b/builder/mysql/key/unique.go @@ -3,7 +3,9 @@ package key import ( "fmt" "github.com/ShkrutDenis/go-migrations/builder/contract" + "github.com/ShkrutDenis/go-migrations/config" "github.com/jmoiron/sqlx" + "log" ) type UniqueKey struct { @@ -44,11 +46,17 @@ func (uk *UniqueKey) GetSQL() string { } func (uk *UniqueKey) Exec(con *sqlx.DB) error { + if config.GetConfig().Verbose { + log.Println(uk.GetSQL()) + } _, err := con.Exec(uk.GetSQL()) return err } func (uk *UniqueKey) MustExec(con *sqlx.DB) { + if config.GetConfig().Verbose { + log.Println(uk.GetSQL()) + } con.MustExec(uk.GetSQL()) } diff --git a/builder/mysql/table/table.go b/builder/mysql/table/table.go index ca589a8..ada615d 100644 --- a/builder/mysql/table/table.go +++ b/builder/mysql/table/table.go @@ -6,6 +6,7 @@ import ( "github.com/ShkrutDenis/go-migrations/builder/mysql/column" "github.com/ShkrutDenis/go-migrations/builder/mysql/info" "github.com/ShkrutDenis/go-migrations/builder/mysql/key" + "github.com/ShkrutDenis/go-migrations/config" "github.com/jmoiron/sqlx" "log" "strconv" @@ -223,6 +224,9 @@ func (t *Table) Exec() error { if i == len(queries) { break } + if config.GetConfig().Verbose { + log.Println(q + ";") + } _, err := t.connect.Exec(q + ";") if err != nil { return err @@ -244,7 +248,9 @@ func (t *Table) MustExec() { if q == "" { break } - log.Println(q) + if config.GetConfig().Verbose { + log.Println(q + ";") + } t.connect.MustExec(q + ";") } diff --git a/builder/postgress/key/unique.go b/builder/postgress/key/unique.go index 4db2197..3741861 100644 --- a/builder/postgress/key/unique.go +++ b/builder/postgress/key/unique.go @@ -3,6 +3,7 @@ package key import ( "fmt" "github.com/ShkrutDenis/go-migrations/builder/contract" + "github.com/ShkrutDenis/go-migrations/config" "github.com/jmoiron/sqlx" "log" ) @@ -47,12 +48,17 @@ func (uk *UniqueKey) GetSQL() string { } func (uk *UniqueKey) Exec(con *sqlx.DB) error { + if config.GetConfig().Verbose { + log.Println(uk.GetSQL()) + } _, err := con.Exec(uk.GetSQL()) return err } func (uk *UniqueKey) MustExec(con *sqlx.DB) { - log.Println(uk.GetSQL()) + if config.GetConfig().Verbose { + log.Println(uk.GetSQL()) + } con.MustExec(uk.GetSQL()) } diff --git a/builder/postgress/table/table.go b/builder/postgress/table/table.go index 30998d7..1aff33e 100644 --- a/builder/postgress/table/table.go +++ b/builder/postgress/table/table.go @@ -6,6 +6,7 @@ import ( "github.com/ShkrutDenis/go-migrations/builder/postgress/column" "github.com/ShkrutDenis/go-migrations/builder/postgress/info" "github.com/ShkrutDenis/go-migrations/builder/postgress/key" + "github.com/ShkrutDenis/go-migrations/config" "github.com/jmoiron/sqlx" "log" "strconv" @@ -217,6 +218,9 @@ func (t *Table) Exec() error { if i == len(queries) { break } + if config.GetConfig().Verbose { + log.Println(q + ";") + } _, err := t.connect.Exec(q + ";") if err != nil { return err @@ -238,7 +242,9 @@ func (t *Table) MustExec() { if q == "" { break } - log.Println(q) + if config.GetConfig().Verbose { + log.Println(q + ";") + } t.connect.MustExec(q + ";") } diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..16a0544 --- /dev/null +++ b/config/config.go @@ -0,0 +1,33 @@ +package config + +import "os" + +var cfg *Config + +type Config struct { + IsRollback bool + EnvPath string + EnvFile string + Verbose bool + + LastBatch int + FirstRun bool +} + +func (c *Config) GetEnvFullPath() string { + if c.EnvPath == "" { + return c.EnvFile + } + _, err := os.Stat(c.EnvPath + "/" + c.EnvFile) + if os.IsNotExist(err) { + return c.EnvPath + "\\" + c.EnvFile + } + return c.EnvPath + "/" + c.EnvFile +} + +func GetConfig() *Config { + if cfg == nil { + cfg = &Config{} + } + return cfg +} diff --git a/entry.go b/entry.go index 54202fa..0a4177a 100644 --- a/entry.go +++ b/entry.go @@ -2,47 +2,22 @@ package go_migrations import ( "flag" + "github.com/ShkrutDenis/go-migrations/config" "github.com/ShkrutDenis/go-migrations/db" "github.com/ShkrutDenis/go-migrations/model" "github.com/ShkrutDenis/go-migrations/store" "github.com/jmoiron/sqlx" "github.com/joho/godotenv" "log" - "os" ) var connection *sqlx.DB -var config *Config - -type Config struct { - IsRollback bool - EnvPath string - EnvFile string - - lastBatch int - firstRun bool -} - -func (c *Config) GetEnvFullPath() string { - if c.EnvPath == "" { - return c.EnvFile - } - _, err := os.Stat(c.EnvPath + "/" + c.EnvFile) - if os.IsNotExist(err) { - return c.EnvPath + "\\" + c.EnvFile - } - return c.EnvPath + "/" + c.EnvFile -} - -func init() { - config = &Config{} -} func Run(migs []store.Migratable) { parseFlags() prepare() - if config.IsRollback { + if config.GetConfig().IsRollback { store.RegisterMigrations(migs) rollBack() return @@ -54,7 +29,7 @@ func Run(migs []store.Migratable) { } func rollBack() { - forRollback := model.GetLastMigrations(connection, config.lastBatch) + forRollback := model.GetLastMigrations(connection, config.GetConfig().LastBatch) for _, m := range forRollback { forRun := store.FindMigration(m.Name) if forRun == nil { @@ -64,16 +39,16 @@ func rollBack() { forRun.Down(connection) log.Println("Rolled back", forRun.GetName()) } - model.RemoveLastBatch(connection, config.lastBatch) + model.RemoveLastBatch(connection, config.GetConfig().LastBatch) } func upOrIgnore(migration store.Migratable) { - if !config.firstRun && model.MigrationExist(connection, migration.GetName()) { + if !config.GetConfig().FirstRun && model.MigrationExist(connection, migration.GetName()) { return } log.Println("Migrating", migration.GetName()) migration.Up(connection) - model.AddMigrationRaw(connection, migration.GetName(), config.lastBatch+1) + model.AddMigrationRaw(connection, migration.GetName(), config.GetConfig().LastBatch+1) log.Println("Migrated", migration.GetName()) } @@ -81,14 +56,19 @@ func parseFlags() { isRollback := flag.Bool("rollback", false, "Flag for init rollback.") envPath := flag.String("env-path", "", "Path to .env file.") envFile := flag.String("env-file", ".env", "Env file name.") + verbose := flag.Bool("verbose", false, "Flag for show more info.") flag.Parse() - config.IsRollback = *isRollback - config.EnvPath = *envPath - config.EnvFile = *envFile + config.GetConfig().IsRollback = *isRollback + config.GetConfig().EnvPath = *envPath + config.GetConfig().EnvFile = *envFile + config.GetConfig().Verbose = *verbose } func prepare() { - err := godotenv.Load(config.GetEnvFullPath()) + if config.GetConfig().Verbose { + log.Println("load env file from:", config.GetConfig().GetEnvFullPath()) + } + err := godotenv.Load(config.GetConfig().GetEnvFullPath()) if err != nil { log.Println("Error loading .env file") } @@ -98,6 +78,6 @@ func prepare() { if err != nil { log.Fatal(err) } - config.lastBatch = model.GetLastBatch(connection) - config.firstRun = model.CreateMigrationsTable(connection) + config.GetConfig().LastBatch = model.GetLastBatch(connection) + config.GetConfig().FirstRun = model.CreateMigrationsTable(connection) }