Skip to content

Commit 18f7f52

Browse files
committed
Add verbose flag
1 parent 543d75f commit 18f7f52

File tree

6 files changed

+79
-40
lines changed

6 files changed

+79
-40
lines changed

builder/mysql/key/unique.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package key
33
import (
44
"fmt"
55
"github.com/ShkrutDenis/go-migrations/builder/contract"
6+
"github.com/ShkrutDenis/go-migrations/config"
67
"github.com/jmoiron/sqlx"
8+
"log"
79
)
810

911
type UniqueKey struct {
@@ -44,11 +46,17 @@ func (uk *UniqueKey) GetSQL() string {
4446
}
4547

4648
func (uk *UniqueKey) Exec(con *sqlx.DB) error {
49+
if config.GetConfig().Verbose {
50+
log.Println(uk.GetSQL())
51+
}
4752
_, err := con.Exec(uk.GetSQL())
4853
return err
4954
}
5055

5156
func (uk *UniqueKey) MustExec(con *sqlx.DB) {
57+
if config.GetConfig().Verbose {
58+
log.Println(uk.GetSQL())
59+
}
5260
con.MustExec(uk.GetSQL())
5361
}
5462

builder/mysql/table/table.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/ShkrutDenis/go-migrations/builder/mysql/column"
77
"github.com/ShkrutDenis/go-migrations/builder/mysql/info"
88
"github.com/ShkrutDenis/go-migrations/builder/mysql/key"
9+
"github.com/ShkrutDenis/go-migrations/config"
910
"github.com/jmoiron/sqlx"
1011
"log"
1112
"strconv"
@@ -223,6 +224,9 @@ func (t *Table) Exec() error {
223224
if i == len(queries) {
224225
break
225226
}
227+
if config.GetConfig().Verbose {
228+
log.Println(q + ";")
229+
}
226230
_, err := t.connect.Exec(q + ";")
227231
if err != nil {
228232
return err
@@ -244,7 +248,9 @@ func (t *Table) MustExec() {
244248
if q == "" {
245249
break
246250
}
247-
log.Println(q)
251+
if config.GetConfig().Verbose {
252+
log.Println(q + ";")
253+
}
248254
t.connect.MustExec(q + ";")
249255
}
250256

builder/postgress/key/unique.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package key
33
import (
44
"fmt"
55
"github.com/ShkrutDenis/go-migrations/builder/contract"
6+
"github.com/ShkrutDenis/go-migrations/config"
67
"github.com/jmoiron/sqlx"
78
"log"
89
)
@@ -47,12 +48,17 @@ func (uk *UniqueKey) GetSQL() string {
4748
}
4849

4950
func (uk *UniqueKey) Exec(con *sqlx.DB) error {
51+
if config.GetConfig().Verbose {
52+
log.Println(uk.GetSQL())
53+
}
5054
_, err := con.Exec(uk.GetSQL())
5155
return err
5256
}
5357

5458
func (uk *UniqueKey) MustExec(con *sqlx.DB) {
55-
log.Println(uk.GetSQL())
59+
if config.GetConfig().Verbose {
60+
log.Println(uk.GetSQL())
61+
}
5662
con.MustExec(uk.GetSQL())
5763
}
5864

builder/postgress/table/table.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/ShkrutDenis/go-migrations/builder/postgress/column"
77
"github.com/ShkrutDenis/go-migrations/builder/postgress/info"
88
"github.com/ShkrutDenis/go-migrations/builder/postgress/key"
9+
"github.com/ShkrutDenis/go-migrations/config"
910
"github.com/jmoiron/sqlx"
1011
"log"
1112
"strconv"
@@ -217,6 +218,9 @@ func (t *Table) Exec() error {
217218
if i == len(queries) {
218219
break
219220
}
221+
if config.GetConfig().Verbose {
222+
log.Println(q + ";")
223+
}
220224
_, err := t.connect.Exec(q + ";")
221225
if err != nil {
222226
return err
@@ -238,7 +242,9 @@ func (t *Table) MustExec() {
238242
if q == "" {
239243
break
240244
}
241-
log.Println(q)
245+
if config.GetConfig().Verbose {
246+
log.Println(q + ";")
247+
}
242248
t.connect.MustExec(q + ";")
243249
}
244250

config/config.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package config
2+
3+
import "os"
4+
5+
var cfg *Config
6+
7+
type Config struct {
8+
IsRollback bool
9+
EnvPath string
10+
EnvFile string
11+
Verbose bool
12+
13+
LastBatch int
14+
FirstRun bool
15+
}
16+
17+
func (c *Config) GetEnvFullPath() string {
18+
if c.EnvPath == "" {
19+
return c.EnvFile
20+
}
21+
_, err := os.Stat(c.EnvPath + "/" + c.EnvFile)
22+
if os.IsNotExist(err) {
23+
return c.EnvPath + "\\" + c.EnvFile
24+
}
25+
return c.EnvPath + "/" + c.EnvFile
26+
}
27+
28+
func GetConfig() *Config {
29+
if cfg == nil {
30+
cfg = &Config{}
31+
}
32+
return cfg
33+
}

entry.go

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,22 @@ package go_migrations
22

33
import (
44
"flag"
5+
"github.com/ShkrutDenis/go-migrations/config"
56
"github.com/ShkrutDenis/go-migrations/db"
67
"github.com/ShkrutDenis/go-migrations/model"
78
"github.com/ShkrutDenis/go-migrations/store"
89
"github.com/jmoiron/sqlx"
910
"github.com/joho/godotenv"
1011
"log"
11-
"os"
1212
)
1313

1414
var connection *sqlx.DB
15-
var config *Config
16-
17-
type Config struct {
18-
IsRollback bool
19-
EnvPath string
20-
EnvFile string
21-
22-
lastBatch int
23-
firstRun bool
24-
}
25-
26-
func (c *Config) GetEnvFullPath() string {
27-
if c.EnvPath == "" {
28-
return c.EnvFile
29-
}
30-
_, err := os.Stat(c.EnvPath + "/" + c.EnvFile)
31-
if os.IsNotExist(err) {
32-
return c.EnvPath + "\\" + c.EnvFile
33-
}
34-
return c.EnvPath + "/" + c.EnvFile
35-
}
36-
37-
func init() {
38-
config = &Config{}
39-
}
4015

4116
func Run(migs []store.Migratable) {
4217
parseFlags()
4318
prepare()
4419

45-
if config.IsRollback {
20+
if config.GetConfig().IsRollback {
4621
store.RegisterMigrations(migs)
4722
rollBack()
4823
return
@@ -54,7 +29,7 @@ func Run(migs []store.Migratable) {
5429
}
5530

5631
func rollBack() {
57-
forRollback := model.GetLastMigrations(connection, config.lastBatch)
32+
forRollback := model.GetLastMigrations(connection, config.GetConfig().LastBatch)
5833
for _, m := range forRollback {
5934
forRun := store.FindMigration(m.Name)
6035
if forRun == nil {
@@ -64,31 +39,36 @@ func rollBack() {
6439
forRun.Down(connection)
6540
log.Println("Rolled back", forRun.GetName())
6641
}
67-
model.RemoveLastBatch(connection, config.lastBatch)
42+
model.RemoveLastBatch(connection, config.GetConfig().LastBatch)
6843
}
6944

7045
func upOrIgnore(migration store.Migratable) {
71-
if !config.firstRun && model.MigrationExist(connection, migration.GetName()) {
46+
if !config.GetConfig().FirstRun && model.MigrationExist(connection, migration.GetName()) {
7247
return
7348
}
7449
log.Println("Migrating", migration.GetName())
7550
migration.Up(connection)
76-
model.AddMigrationRaw(connection, migration.GetName(), config.lastBatch+1)
51+
model.AddMigrationRaw(connection, migration.GetName(), config.GetConfig().LastBatch+1)
7752
log.Println("Migrated", migration.GetName())
7853
}
7954

8055
func parseFlags() {
8156
isRollback := flag.Bool("rollback", false, "Flag for init rollback.")
8257
envPath := flag.String("env-path", "", "Path to .env file.")
8358
envFile := flag.String("env-file", ".env", "Env file name.")
59+
verbose := flag.Bool("verbose", false, "Flag for show more info.")
8460
flag.Parse()
85-
config.IsRollback = *isRollback
86-
config.EnvPath = *envPath
87-
config.EnvFile = *envFile
61+
config.GetConfig().IsRollback = *isRollback
62+
config.GetConfig().EnvPath = *envPath
63+
config.GetConfig().EnvFile = *envFile
64+
config.GetConfig().Verbose = *verbose
8865
}
8966

9067
func prepare() {
91-
err := godotenv.Load(config.GetEnvFullPath())
68+
if config.GetConfig().Verbose {
69+
log.Println("load env file from:", config.GetConfig().GetEnvFullPath())
70+
}
71+
err := godotenv.Load(config.GetConfig().GetEnvFullPath())
9272
if err != nil {
9373
log.Println("Error loading .env file")
9474
}
@@ -98,6 +78,6 @@ func prepare() {
9878
if err != nil {
9979
log.Fatal(err)
10080
}
101-
config.lastBatch = model.GetLastBatch(connection)
102-
config.firstRun = model.CreateMigrationsTable(connection)
81+
config.GetConfig().LastBatch = model.GetLastBatch(connection)
82+
config.GetConfig().FirstRun = model.CreateMigrationsTable(connection)
10383
}

0 commit comments

Comments
 (0)