Skip to content

Commit

Permalink
feat: creating CLI to create and run migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldyzdk committed May 18, 2024
1 parent ee573c2 commit e0e9198
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 22 deletions.
5 changes: 0 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@ REDIS_HOST="cache"
REDIS_PORT="6379"
REDIS_PASSWORD="123"

DATABASE_URL=maria:123@tcp(localhost:3306)/petdex?multiStatements=true
PORT=3000
ENVIROMENT=DEVELOPMENT
MIGRATIONS_PATH=migrations

JWT_SECRET=MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgHQ5BWxB9NlRB89pOVY320IISVsSCOLKtGy0nVybrHNdzIhQW64XN8af66SvAN4CG9ZuzH73iGOFUXoMTwy1bDKsoxrRgybEoHA8wZxw0yOItYoQ8HY6fzLJTEmHtMTPLKCqhZe70vEBK/N69dJhZWL0MH4UeQwLgibIrCoPQuhbAgMBAAE

2 changes: 1 addition & 1 deletion api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
}

config.InitConfigs()
sqlxDb, err := sqlx.Connect("mysql", env.DBUrl)
sqlxDb, err := sqlx.Open("mysql", env.DBUrl)

if err != nil {
panic(err)
Expand Down
44 changes: 34 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
package main

import (
"pet-dex-backend/v2/infra/db"
"pet-dex-backend/v2/usecase"

"github.com/jmoiron/sqlx"
"fmt"
"pet-dex-backend/v2/pkg/migration"
)

func main() {
var number string
fmt.Println("Migrations CLI")
fmt.Println("Type the number of the command desired:\n1-Migrations UP\n2-Migrations DOWN\n3-Create a new migration\n")
_, err := fmt.Scan(&number)
if err != nil {
fmt.Println("Error while reading the values", err)
}

sqlxDb, err := sqlx.Connect("mysql", "dellis:@/shud")
if number == "1" {
fmt.Println("Running Migrations UP...")
migration.Up()
fmt.Println("Migrations executed!")
return
}

if err != nil {
panic(err)
if number == "2" {
fmt.Println("Running Migrations DOWN...")
migration.Down()
fmt.Println("Migrations executed!")
return
}

if number == "3" {
fmt.Println("Type the name of the migration desired:")
var name string
_, err := fmt.Scan(&name)
if err != nil {
fmt.Println("Error while reading the values", err)
}
fmt.Println("Creating a new migration...")
migration.Create(name)
fmt.Println("Migration created!")
}
pr := db.NewPetRepository(sqlxDb)
adoptUseCase := usecase.NewAdoptUseCase(pr)

adoptUseCase.Do()
fmt.Println("No valid commands identified")
return
}
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ go 1.21

require (
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/golang-migrate/migrate/v4 v4.17.1
github.com/google/uuid v1.4.0
github.com/jmoiron/sqlx v1.3.5
github.com/stretchr/testify v1.9.0
)

require (
github.com/lib/pq v1.10.9 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
)

require (
github.com/stretchr/objx v0.5.2 // indirect
golang.org/x/crypto v0.21.0
Expand All @@ -29,6 +25,8 @@ require (

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrt
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang-migrate/migrate/v4 v4.17.1 h1:4zQ6iqL6t6AiItphxJctQb3cFqWiSpMnX7wLTPnnYO4=
github.com/golang-migrate/migrate/v4 v4.17.1/go.mod h1:m8hinFyWBn0SA4QKHuKh175Pm9wjmxj3S2Mia7dbXzM=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
Expand Down
3 changes: 2 additions & 1 deletion infra/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ var env *envconfig

type envconfig struct {
DBUrl string `mapstructure:"DATABASE_URL"`
DBUrl_Migration string `mapstructure:"MIGRATION_DATABASE_URL"`
PORT string `mapstructure:"PORT"`
ENV string `mapstructure:"ENVIRONMENT"`
MIGRATIONS_PATH string `mapstructure:"MIGRATIONS_PATH"`
JWT_SECRET string `mapstructure:"JWT_SECRET"`
JWT_SECRET string `mapstructure:"JWT_SECRET"`
}

func GetEnvConfig() *envconfig {
Expand Down
29 changes: 29 additions & 0 deletions pkg/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
"log"
"os"
"pet-dex-backend/v2/infra/config"
"time"
)

func Up() {
Expand Down Expand Up @@ -71,4 +73,31 @@ func Down() {
log.Fatalf("Failed on running migrations down: %v\n", err)
return
}

}

func Create(name string) {
path, err := config.LoadEnv("../../")
if err != nil {
fmt.Println("Error loading the .env file:", err)
}
data := time.Now()
timestamp := data.Format("20060102150405")
fmt.Println("Current date and time: ", timestamp)
fileNameDown := fmt.Sprintf("%s/%s_%s.down.sql", path.MIGRATIONS_PATH, timestamp, name)
fileNameUp := fmt.Sprintf("%s/%s_%s.up.sql", path.MIGRATIONS_PATH, timestamp, name)
// Create the file
fileDown, err := os.Create(fileNameDown)
if err != nil {
fmt.Println("Error creating down file:", err)
return
}
defer fileDown.Close()

fileUp, err := os.Create(fileNameUp)
if err != nil {
fmt.Println("Error creating up file:", err)
return
}
defer fileUp.Close()
}

0 comments on commit e0e9198

Please sign in to comment.