diff --git a/initializers/database.go b/initializers/database.go index 80a45c2..25d4bfd 100644 --- a/initializers/database.go +++ b/initializers/database.go @@ -1,7 +1,6 @@ package initializers import ( - "log" "os" "gorm.io/driver/postgres" @@ -12,10 +11,10 @@ var DB *gorm.DB func ConnectToDB() { var err error - dsn := os.Getenv("DB_URL") + dsn := os.Getenv("DB") DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { - log.Fatal("Failed to connect to database") + panic("Failed to connect to database") } } diff --git a/initializers/syncDatabase.go b/initializers/syncDatabase.go new file mode 100644 index 0000000..67dcb0f --- /dev/null +++ b/initializers/syncDatabase.go @@ -0,0 +1,7 @@ +package initializers + +import "tugas1/models" + +func SyncDatabase() { + DB.AutoMigrate(&models.Post{}) +}