Skip to content

Commit

Permalink
Merge pull request #59 from Caknoooo/refactor/change-all-files-and-se…
Browse files Browse the repository at this point in the history
…parate-migrations

refactor: rename all files and separate migrations
  • Loading branch information
Caknoooo authored Jan 20, 2024
2 parents 325db9f + 3e1e68e commit a45a81e
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 12 deletions.
11 changes: 0 additions & 11 deletions config/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"

"github.com/Caknoooo/go-gin-clean-template/constants"
"github.com/Caknoooo/go-gin-clean-template/entity"
"github.com/joho/godotenv"
"gorm.io/driver/postgres"
"gorm.io/gorm"
Expand All @@ -15,7 +14,6 @@ func SetUpDatabaseConnection() *gorm.DB {
if os.Getenv("APP_ENV") != constants.ENUM_RUN_PRODUCTION {
err := godotenv.Load(".env")
if err != nil {
fmt.Println(err)
panic(err)
}
}
Expand All @@ -30,27 +28,18 @@ func SetUpDatabaseConnection() *gorm.DB {

db, err := gorm.Open(postgres.New(postgres.Config{
DSN: dsn,
// Menambahkan opsi berikut akan memungkinkan driver database
// untuk mendukung tipe data UUID secara bawaan.
PreferSimpleProtocol: true,
}), &gorm.Config{})
if err != nil {
panic(err)
}

if err := db.AutoMigrate(
&entity.User{},
); err != nil {
panic(err)
}

return db
}

func ClosDatabaseConnection(db *gorm.DB) {
dbSQL, err := db.DB()
if err != nil {
fmt.Println(err)
panic(err)
}
dbSQL.Close()
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func main() {
log.Fatalf("error migration seeder: %v", err)
}

server.RedirectTrailingSlash = false
if err := migrations.Migrate(db); err != nil {
log.Fatalf("error migration: %v", err)
}

server.Static("/assets", "./assets")

port := os.Getenv("PORT")
Expand Down
16 changes: 16 additions & 0 deletions migrations/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package migrations

import (
"github.com/Caknoooo/go-gin-clean-template/entity"
"gorm.io/gorm"
)

func Migrate(db *gorm.DB) error {
if err := db.AutoMigrate(
&entity.User{},
); err != nil {
return err
}

return nil
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit a45a81e

Please sign in to comment.