Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename all files and separate migrations #59

Merged
merged 1 commit into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading