Skip to content

Commit 473486c

Browse files
committed
feat(orm): migrate ResourceReference model to GORM v2
1 parent 8179651 commit 473486c

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

backend/src/apiserver/model/resource_reference.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,27 +147,29 @@ type ResourceType string
147147
type Relationship string
148148

149149
// Resource reference table models the relationship between resources in a loosely coupled way.
150+
// This model has a composite primary key consisting of ResourceUUID, ResourceType, and ReferenceType.
151+
// Updated for GORM v2 compatibility.
150152
type ResourceReference struct {
151153
// ID of the resource object
152-
ResourceUUID string `gorm:"column:ResourceUUID; not null; primary_key;"`
154+
ResourceUUID string `gorm:"column:ResourceUUID; not null; primaryKey;"`
153155

154156
// The type of the resource object
155-
ResourceType ResourceType `gorm:"column:ResourceType; not null; primary_key; index:referencefilter;"`
157+
ResourceType ResourceType `gorm:"column:ResourceType; not null; primaryKey; index:referencefilter;"`
156158

157-
// The ID of the resource that been referenced to.
159+
// The ID of the referenced resource.
158160
ReferenceUUID string `gorm:"column:ReferenceUUID; not null; index:referencefilter;"`
159161

160-
// The name of the resource that been referenced to.
162+
// The name of the referenced resource.
161163
ReferenceName string `gorm:"column:ReferenceName; not null;"`
162164

163-
// The type of the resource that been referenced to.
164-
ReferenceType ResourceType `gorm:"column:ReferenceType; not null; primary_key; index:referencefilter;"`
165+
// The type of the referenced resource.
166+
ReferenceType ResourceType `gorm:"column:ReferenceType; not null; primaryKey; index:referencefilter;"`
165167

166-
// The relationship between the resource object and the resource that been referenced to.
168+
// The relationship between the resource object and the referenced resource.
167169
Relationship Relationship `gorm:"column:Relationship; not null;"`
168170

169-
// The json formatted blob of the resource reference.
170-
Payload string `gorm:"column:Payload; not null; size:65535;"`
171+
// JSON-encoded metadata blob about the reference
172+
Payload string `gorm:"column:Payload; not null; size:65535"`
171173
}
172174

173175
type ReferenceKey struct {

playground/gorm_v2_migration/test_gorm_v2.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ type User struct {
3232
Email string
3333
}
3434

35+
36+
type ResourceReference struct {
37+
ID uint64 `gorm:"primaryKey;autoIncrement"`
38+
ResourceUUID string `gorm:"column:ResourceUUID"`
39+
ResourceType string `gorm:"column:ResourceType"`
40+
ReferenceUUID string `gorm:"column:ReferenceUUID"`
41+
ReferenceType string `gorm:"column:ReferenceType"`
42+
Relationship string `gorm:"column:Relationship"`
43+
CreatedAtInSec int64 `gorm:"column:CreatedAtInSec"`
44+
UpdatedAtInSec int64 `gorm:"column:UpdatedAtInSec"`
45+
}
46+
3547
func main() {
3648
// Initialize GORM v2 with MySQL
3749
dsn := "testuser:testpw@tcp(127.0.0.1:3306)/testdb?charset=utf8mb4&parseTime=True&loc=Local"
@@ -44,6 +56,10 @@ func main() {
4456
if err := db.AutoMigrate(&User{}); err != nil {
4557
panic("failed to migrate")
4658
}
59+
if err := db.AutoMigrate(&ResourceReference{}); err != nil {
60+
panic("failed to migrate ResourceReference")
61+
}
62+
fmt.Println("ResourceReference AutoMigrate successful.")
4763

4864
// Insert data
4965
db.Create(&User{Name: "Alice", Email: "[email protected]"})
@@ -75,4 +91,5 @@ func main() {
7591
var tableNames []string
7692
db.Raw("SHOW TABLES").Scan(&tableNames)
7793
fmt.Printf("Tables in DB (via Raw): %v\n", tableNames)
94+
7895
}

0 commit comments

Comments
 (0)