Skip to content

Commit 055df98

Browse files
[management] add gorm tag for primary key for the networks objects (#3758)
1 parent 12f883b commit 055df98

File tree

9 files changed

+63
-7
lines changed

9 files changed

+63
-7
lines changed

management/server/migration/migration.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,24 @@ func MigrateNewField[T any](ctx context.Context, db *gorm.DB, columnName string,
352352
log.WithContext(ctx).Infof("Migration of empty %s to default value in table %s completed", columnName, tableName)
353353
return nil
354354
}
355+
356+
func DropIndex[T any](ctx context.Context, db *gorm.DB, indexName string) error {
357+
var model T
358+
359+
if !db.Migrator().HasTable(&model) {
360+
log.WithContext(ctx).Debugf("table for %T does not exist, no migration needed", model)
361+
return nil
362+
}
363+
364+
if !db.Migrator().HasIndex(&model, indexName) {
365+
log.WithContext(ctx).Debugf("index %s does not exist in table %T, no migration needed", indexName, model)
366+
return nil
367+
}
368+
369+
if err := db.Migrator().DropIndex(&model, indexName); err != nil {
370+
return fmt.Errorf("failed to drop index %s: %w", indexName, err)
371+
}
372+
373+
log.WithContext(ctx).Infof("dropped index %s from table %T", indexName, model)
374+
return nil
375+
}

management/server/migration/migration_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,25 @@ func TestMigrateSetupKeyToHashedSetupKey_ForAlreadyMigratedKey_Case2(t *testing.
227227

228228
assert.Equal(t, "9+FQcmNd2GCxIK+SvHmtp6PPGV4MKEicDS+xuSQmvlE=", key.Key, "Key should be hashed")
229229
}
230+
231+
func TestDropIndex(t *testing.T) {
232+
db := setupDatabase(t)
233+
234+
err := db.AutoMigrate(&types.SetupKey{})
235+
require.NoError(t, err, "Failed to auto-migrate tables")
236+
237+
err = db.Save(&types.SetupKey{
238+
Id: "1",
239+
Key: "9+FQcmNd2GCxIK+SvHmtp6PPGV4MKEicDS+xuSQmvlE=",
240+
}).Error
241+
require.NoError(t, err, "Failed to insert setup key")
242+
243+
exist := db.Migrator().HasIndex(&types.SetupKey{}, "idx_setup_keys_account_id")
244+
assert.True(t, exist, "Should have the index")
245+
246+
err = migration.DropIndex[types.SetupKey](context.Background(), db, "idx_setup_keys_account_id")
247+
require.NoError(t, err, "Migration should not fail to remove index")
248+
249+
exist = db.Migrator().HasIndex(&types.SetupKey{}, "idx_setup_keys_account_id")
250+
assert.False(t, exist, "Should not have the index")
251+
}

management/server/networks/resources/types/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (p NetworkResourceType) String() string {
3030
}
3131

3232
type NetworkResource struct {
33-
ID string `gorm:"index"`
33+
ID string `gorm:"primaryKey"`
3434
NetworkID string `gorm:"index"`
3535
AccountID string `gorm:"index"`
3636
Name string

management/server/networks/routers/types/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
type NetworkRouter struct {
13-
ID string `gorm:"index"`
13+
ID string `gorm:"primaryKey"`
1414
NetworkID string `gorm:"index"`
1515
AccountID string `gorm:"index"`
1616
Peer string

management/server/networks/types/network.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
type Network struct {
10-
ID string `gorm:"index"`
10+
ID string `gorm:"primaryKey"`
1111
AccountID string `gorm:"index"`
1212
Name string
1313
Description string

management/server/store/sql_store.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func NewSqlStore(ctx context.Context, db *gorm.DB, storeEngine types.Engine, met
8282
log.WithContext(ctx).Warnf("setting NB_SQL_MAX_OPEN_CONNS is not supported for sqlite, using default value 1")
8383
}
8484
conns = 1
85+
_, err = sql.Exec("PRAGMA foreign_keys = ON")
86+
if err != nil {
87+
return nil, fmt.Errorf("failed to set foreign keys for sqlite: %w", err)
88+
}
8589
}
8690

8791
sql.SetMaxOpenConns(conns)

management/server/store/sql_store_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func Test_NewStore(t *testing.T) {
6060

6161
runTestForAllEngines(t, "", func(t *testing.T, store Store) {
6262
if store == nil {
63-
t.Errorf("expected to create a new Store")
63+
t.Fatalf("expected to create a new Store")
6464
}
6565
if len(store.GetAllAccounts(context.Background())) != 0 {
66-
t.Errorf("expected to create a new empty Accounts map when creating a new FileStore")
66+
t.Fatalf("expected to create a new empty Accounts map when creating a new FileStore")
6767
}
6868
})
6969
}
@@ -1115,7 +1115,7 @@ func TestSqlite_CreateAndGetObjectInTransaction(t *testing.T) {
11151115

11161116
group := &types.Group{
11171117
ID: "group-id",
1118-
AccountID: "account-id",
1118+
AccountID: "bf1c8084-ba50-4ce7-9439-34653001fc3b",
11191119
Name: "group-name",
11201120
Issued: "api",
11211121
Peers: nil,

management/server/store/store.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,15 @@ func getMigrations(ctx context.Context) []migrationFunc {
315315
func(db *gorm.DB) error {
316316
return migration.MigrateNewField[routerTypes.NetworkRouter](ctx, db, "enabled", true)
317317
},
318+
func(db *gorm.DB) error {
319+
return migration.DropIndex[networkTypes.Network](ctx, db, "idx_networks_id")
320+
},
321+
func(db *gorm.DB) error {
322+
return migration.DropIndex[resourceTypes.NetworkResource](ctx, db, "idx_network_resources_id")
323+
},
324+
func(db *gorm.DB) error {
325+
return migration.DropIndex[routerTypes.NetworkRouter](ctx, db, "idx_network_routers_id")
326+
},
318327
}
319328
}
320329

management/server/types/group.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const (
1414
// Group of the peers for ACL
1515
type Group struct {
1616
// ID of the group
17-
ID string
17+
ID string `gorm:"primaryKey"`
1818

1919
// AccountID is a reference to Account that this object belongs
2020
AccountID string `json:"-" gorm:"index"`

0 commit comments

Comments
 (0)