Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3ffa80a

Browse files
committed
fix: migration issues
1 parenta98b024 commit3ffa80a

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

‎internal/migrate/5.rename_env_groups_to_namespaces.go‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package migrate
22

33
import (
4+
"github.com/0xJacky/Nginx-UI/model"
45
"github.com/go-gormigrate/gormigrate/v2"
56
"gorm.io/gorm"
67
)
@@ -22,19 +23,32 @@ var RenameEnvGroupsToNamespaces = &gormigrate.Migration{
2223
}
2324
}else {
2425
// namespaces 表已存在,迁移数据后删除旧表
25-
iferr:=tx.Exec(`
26-
INSERT OR IGNORE INTO namespaces (id, created_at, updated_at, deleted_at, name, sync_node_ids, order_id, post_sync_action, upstream_test_type)
27-
SELECT id, created_at, updated_at, deleted_at,
28-
COALESCE(name, 'Default') as name,
29-
COALESCE(sync_node_ids, '[]') as sync_node_ids,
30-
COALESCE(order_id, 0) as order_id,
31-
COALESCE(post_sync_action, 'reload_nginx') as post_sync_action,
32-
COALESCE(upstream_test_type, 'local') as upstream_test_type
33-
FROM env_groups
34-
`).Error;err!=nil {
26+
// 使用 GORM 查询和创建来迁移数据
27+
varenvGroups []model.Namespace
28+
iferr:=tx.Table("env_groups").Find(&envGroups).Error;err!=nil {
3529
returnerr
3630
}
3731

32+
// 为每个 env_group 创建对应的 namespace
33+
for_,envGroup:=rangeenvGroups {
34+
// 设置默认值
35+
ifenvGroup.Name=="" {
36+
envGroup.Name="Default"
37+
}
38+
ifenvGroup.PostSyncAction=="" {
39+
envGroup.PostSyncAction="reload_nginx"
40+
}
41+
ifenvGroup.UpstreamTestType=="" {
42+
envGroup.UpstreamTestType="local"
43+
}
44+
45+
// 使用 FirstOrCreate 避免重复插入
46+
varexistingNamespace model.Namespace
47+
iferr:=tx.Where("id = ?",envGroup.ID).FirstOrCreate(&existingNamespace,&envGroup).Error;err!=nil {
48+
returnerr
49+
}
50+
}
51+
3852
// 删除旧表
3953
iferr:=tx.Migrator().DropTable("env_groups");err!=nil {
4054
returnerr

‎internal/migrate/6.rename_environments_to_nodes.go‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package migrate
22

33
import (
4+
"github.com/0xJacky/Nginx-UI/model"
45
"github.com/go-gormigrate/gormigrate/v2"
56
"gorm.io/gorm"
67
)
@@ -22,18 +23,32 @@ var RenameEnvironmentsToNodes = &gormigrate.Migration{
2223
}
2324
}else {
2425
// nodes 表已存在,迁移数据后删除旧表
25-
iferr:=tx.Exec(`
26-
INSERT OR IGNORE INTO nodes (id, created_at, updated_at, deleted_at, name, url, token, enabled)
27-
SELECT id, created_at, updated_at, deleted_at,
28-
COALESCE(name, 'Unknown Node') as name,
29-
COALESCE(url, '') as url,
30-
COALESCE(token, '') as token,
31-
COALESCE(enabled, false) as enabled
32-
FROM environments
33-
`).Error;err!=nil {
26+
// 使用 GORM 查询和创建来迁移数据
27+
varenvironments []model.Node
28+
iferr:=tx.Table("environments").Find(&environments).Error;err!=nil {
3429
returnerr
3530
}
3631

32+
// 为每个 environment 创建对应的 node
33+
for_,environment:=rangeenvironments {
34+
// 设置默认值
35+
ifenvironment.Name=="" {
36+
environment.Name="Unknown Node"
37+
}
38+
ifenvironment.URL=="" {
39+
environment.URL=""
40+
}
41+
ifenvironment.Token=="" {
42+
environment.Token=""
43+
}
44+
45+
// 使用 FirstOrCreate 避免重复插入
46+
varexistingNode model.Node
47+
iferr:=tx.Where("id = ?",environment.ID).FirstOrCreate(&existingNode,&environment).Error;err!=nil {
48+
returnerr
49+
}
50+
}
51+
3752
// 删除旧表
3853
iferr:=tx.Migrator().DropTable("environments");err!=nil {
3954
returnerr

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp