diff --git a/README.md b/README.md index 08b0588..857662e 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,27 @@ Clean Architecture proposes a structured application design with several layers go run main.go ``` +## Run Migrations and Seeder +To run migrations and seed the database, use the following commands: + +```bash +go run main.go --migrate --seed +``` + +### Migrate Database +To migrate the database schema +```bash +go run main.go --migrate +``` +This command will apply all pending migrations to your PostgreSQL database specified in `.env` + +### Seeder Database +To seed the database with initial data: +```bash +go run main.go --seed +``` +This command will populate the database with initial data using the seeders defined in your application. + ## What did you get? By using this template, you get a ready-to-go architecture with pre-configured endpoints. The template provides a structured foundation for building your application using Golang with Clean Architecture principles. diff --git a/cmd/command.go b/cmd/command.go new file mode 100644 index 0000000..06753bd --- /dev/null +++ b/cmd/command.go @@ -0,0 +1,37 @@ +package cmd + +import ( + "log" + "os" + + "github.com/Caknoooo/go-gin-clean-starter/migrations" + "gorm.io/gorm" +) + +func Commands(db *gorm.DB) { + migrate := false + seed := false + + for _, arg := range os.Args[1:] { + if arg == "--migrate" { + migrate = true + } + if arg == "--seed" { + seed = true + } + } + + if migrate { + if err := migrations.Migrate(db); err != nil { + log.Fatalf("error migration: %v", err) + } + log.Println("migration completed successfully") + } + + if seed { + if err := migrations.Seeder(db); err != nil { + log.Fatalf("error migration seeder: %v", err) + } + log.Println("seeder completed successfully") + } +} diff --git a/config/database.go b/config/database.go index 58d28a9..4d911ac 100644 --- a/config/database.go +++ b/config/database.go @@ -37,7 +37,7 @@ func SetUpDatabaseConnection() *gorm.DB { return db } -func ClosDatabaseConnection(db *gorm.DB) { +func CloseDatabaseConnection(db *gorm.DB) { dbSQL, err := db.DB() if err != nil { panic(err) diff --git a/main.go b/main.go index 0a554ef..247c2c4 100644 --- a/main.go +++ b/main.go @@ -3,23 +3,28 @@ package main import ( "log" "os" - "sync" + "github.com/Caknoooo/go-gin-clean-starter/cmd" "github.com/Caknoooo/go-gin-clean-starter/config" "github.com/Caknoooo/go-gin-clean-starter/controller" "github.com/Caknoooo/go-gin-clean-starter/middleware" - "github.com/Caknoooo/go-gin-clean-starter/migrations" "github.com/Caknoooo/go-gin-clean-starter/repository" "github.com/Caknoooo/go-gin-clean-starter/routes" "github.com/Caknoooo/go-gin-clean-starter/service" "github.com/gin-gonic/gin" - "gorm.io/gorm" ) func main() { + db := config.SetUpDatabaseConnection() + defer config.CloseDatabaseConnection(db) + + if len(os.Args) > 1 { + cmd.Commands(db) + return + } + var ( - db *gorm.DB = config.SetUpDatabaseConnection() jwtService service.JWTService = service.NewJWTService() // Implementation Dependency Injection @@ -39,33 +44,13 @@ func main() { // routes routes.User(server, userController, jwtService) - var wg sync.WaitGroup - var serve string - wg.Add(2) - - go func() { - defer wg.Done() - if err := migrations.Migrate(db); err != nil { - log.Fatalf("error migration: %v", err) - } - }() - - go func() { - defer wg.Done() - if err := migrations.Seeder(db); err != nil { - log.Fatalf("error migration seeder: %v", err) - } - }() - - wg.Wait() - server.Static("/assets", "./assets") - port := os.Getenv("PORT") if port == "" { port = "8888" } + var serve string if os.Getenv("APP_ENV") == "localhost" { serve = "127.0.0.1:" + port } else {