Mhamdans17 commited on
Commit
11fca28
·
1 Parent(s): 4800900

fix: entirely remove PointHistory Member relation to avoid GORM auto-migrate bugs

Browse files
Files changed (4) hide show
  1. drop_table.go +19 -0
  2. kill_locks.go +23 -0
  3. models/models.go +0 -1
  4. test_migrate.go +35 -0
drop_table.go ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package main
2
+
3
+ import (
4
+ "log"
5
+ "service-warungpos-go/config"
6
+ )
7
+
8
+ func main() {
9
+ config.LoadConfig()
10
+ config.ConnectDatabase()
11
+
12
+ log.Println("Dropping point_histories table...")
13
+ err := config.DB.Exec(`DROP TABLE IF EXISTS point_histories CASCADE;`).Error
14
+ if err != nil {
15
+ log.Println("Error dropping table:", err)
16
+ }
17
+
18
+ log.Println("Done.")
19
+ }
kill_locks.go ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package main
2
+
3
+ import (
4
+ "log"
5
+ "service-warungpos-go/config"
6
+ )
7
+
8
+ func main() {
9
+ config.LoadConfig()
10
+ config.ConnectDatabase()
11
+
12
+ log.Println("Killing ALL other connections to this database...")
13
+ err := config.DB.Exec(`
14
+ SELECT pg_terminate_backend(pid)
15
+ FROM pg_stat_activity
16
+ WHERE datname = current_database() AND pid <> pg_backend_pid() AND usename = current_user;
17
+ `).Error
18
+ if err != nil {
19
+ log.Println("Error killing connections:", err)
20
+ }
21
+
22
+ log.Println("Done killing all connections.")
23
+ }
models/models.go CHANGED
@@ -71,7 +71,6 @@ type PointHistory struct {
71
  CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
72
 
73
  Company *Company `gorm:"foreignKey:CompanyID" json:"-"`
74
- Member *Member `gorm:"foreignKey:MemberID;references:ID" json:"-"`
75
  }
76
 
77
  type Branch struct {
 
71
  CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
72
 
73
  Company *Company `gorm:"foreignKey:CompanyID" json:"-"`
 
74
  }
75
 
76
  type Branch struct {
test_migrate.go ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package main
2
+
3
+ import (
4
+ "log"
5
+ "service-warungpos-go/config"
6
+ "service-warungpos-go/models"
7
+ )
8
+
9
+ func main() {
10
+ config.LoadConfig()
11
+ config.ConnectDatabase()
12
+
13
+ log.Println("Running Auto-Migration Test...")
14
+ err := config.DB.AutoMigrate(
15
+ &models.Company{},
16
+ &models.Branch{},
17
+ &models.User{},
18
+ &models.Category{},
19
+ &models.Product{},
20
+ &models.BranchProduct{},
21
+ &models.Order{},
22
+ &models.OrderItem{},
23
+ &models.OTP{},
24
+ &models.Setting{},
25
+ &models.Member{},
26
+ &models.PointRule{},
27
+ &models.WalletTransaction{},
28
+ &models.TopupRequest{},
29
+ &models.PointHistory{},
30
+ )
31
+ if err != nil {
32
+ log.Fatalf("Gagal melakukan Auto-Migration: %v", err)
33
+ }
34
+ log.Println("Auto-Migration Selesai dengan Sukses!")
35
+ }