Skip to content

Commit

Permalink
Merge pull request #8 from ShkrutDenis/develop
Browse files Browse the repository at this point in the history
Add verbose flag
  • Loading branch information
ShkrutDenis authored Apr 27, 2020
2 parents 343a7d1 + 18f7f52 commit 8196f4f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 40 deletions.
8 changes: 8 additions & 0 deletions builder/mysql/key/unique.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}

Expand Down
8 changes: 7 additions & 1 deletion builder/mysql/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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 + ";")
}

Expand Down
8 changes: 7 additions & 1 deletion builder/postgress/key/unique.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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())
}

Expand Down
8 changes: 7 additions & 1 deletion builder/postgress/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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 + ";")
}

Expand Down
33 changes: 33 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -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
}
54 changes: 17 additions & 37 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -64,31 +39,36 @@ 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())
}

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")
}
Expand All @@ -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)
}

0 comments on commit 8196f4f

Please sign in to comment.