Skip to content

Commit

Permalink
feat: create go command migrate and seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
Caknoooo committed Jul 13, 2024
1 parent 012d6a7 commit d9f8d0c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 26 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions cmd/command.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
2 changes: 1 addition & 1 deletion config/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 10 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit d9f8d0c

Please sign in to comment.