-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: creating CLI to create and run migrations
- Loading branch information
Showing
8 changed files
with
193 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,44 @@ | ||
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() | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
drop table if exists addresses cascade; | ||
SET FOREIGN_KEY_CHECKS = 0; | ||
|
||
drop table if exists breeds cascade; | ||
drop table if exists vaccines cascade; | ||
|
||
drop table if exists legal_person cascade; | ||
drop table if exists pets_image cascade; | ||
|
||
drop table if exists person cascade; | ||
drop table if exists pets cascade; | ||
|
||
drop table if exists breeds cascade; | ||
|
||
drop table if exists pet_image cascade; | ||
drop table if exists addresses cascade; | ||
|
||
drop table if exists vaccines cascade; | ||
drop table if exists legal_persons cascade; | ||
|
||
drop table if exists pets cascade; | ||
drop table if exists users cascade; | ||
|
||
drop table if exists person cascade; | ||
|
||
drop table if exists users cascade; | ||
SET FOREIGN_KEY_CHECKS = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package migration | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/golang-migrate/migrate/v4" | ||
"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() { | ||
env, err := config.LoadEnv("../") | ||
if err != nil { | ||
log.Fatalf("Failed to load .env file: %v\n", err) | ||
} | ||
fmt.Println(env.DBUrl_Migration) | ||
db, err := sql.Open("mysql", env.DBUrl_Migration) | ||
if err != nil { | ||
log.Fatalf("Failed connecting to the database: %v\n", err) | ||
} | ||
defer func() { | ||
if err := db.Close(); err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
driver, _ := mysql.WithInstance(db, &mysql.Config{}) | ||
migration, err := migrate.NewWithDatabaseInstance( | ||
"file://migrations", | ||
"mysql", | ||
driver, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = migration.Up() | ||
if err != nil { | ||
log.Fatalf("Failed on running migrations up: %v\n", err) | ||
return | ||
} | ||
} | ||
|
||
func Down() { | ||
env, err := config.LoadEnv("../") | ||
if err != nil { | ||
log.Fatalf("Failed to load .env file: %v\n", err) | ||
} | ||
fmt.Println(env.DBUrl_Migration) | ||
db, err := sql.Open("mysql", env.DBUrl_Migration) | ||
if err != nil { | ||
log.Fatalf("Failed connecting to the database: %v\n", err) | ||
} | ||
defer func() { | ||
if err := db.Close(); err != nil { | ||
log.Fatal(err) | ||
} | ||
}() | ||
driver, _ := mysql.WithInstance(db, &mysql.Config{}) | ||
migration, err := migrate.NewWithDatabaseInstance( | ||
"file://migrations", | ||
"mysql", | ||
driver, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = migration.Down() | ||
if err != nil { | ||
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() | ||
} |