Skip to content

Commit 28a2b76

Browse files
committed
Auto Migrate Database
1 parent 9a60c5f commit 28a2b76

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

book/entity.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Book struct {
88
Description string
99
Price int
1010
Rating int
11+
Discount int
1112
CreatedAt time.Time
1213
UpdatedAt time.Time
1314
}

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package main
22

33
import (
4-
"fmt"
54
"log"
65
"net/http"
76

7+
"pustaka-api/book"
88
"pustaka-api/handler"
99

1010
"github.com/gin-gonic/gin"
@@ -16,13 +16,14 @@ import (
1616
func main() {
1717
// refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details
1818
dsn := "root:@tcp(127.0.0.1:3306)/pustaka-api?charset=utf8mb4&parseTime=True&loc=Local"
19-
_, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
19+
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
2020

2121
if err != nil {
2222
log.Fatal("Db Erorr")
2323
}
2424

25-
fmt.Println("Database Berhasil Terhubung")
25+
//fmt.Println("Database Berhasil Terhubung")
26+
db.AutoMigrate(&book.Book{})
2627

2728
router := gin.Default()
2829

0 commit comments

Comments
 (0)