Skip to content

Commit fbc800f

Browse files
committed
fix: site and stream unique ke y create issue #1017
1 parent 7c791f3 commit fbc800f

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

internal/migrate/2.fix_site_and_stream_unique.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
var FixSiteAndStreamPathUnique = &gormigrate.Migration{
10-
ID: "20250405000003",
10+
ID: "202505070000001",
1111
Migrate: func(tx *gorm.DB) error {
1212
// Check if sites table exists
1313
if tx.Migrator().HasTable(&model.Site{}) {
@@ -19,36 +19,38 @@ var FixSiteAndStreamPathUnique = &gormigrate.Migration{
1919

2020
if err := tx.Model(&model.Site{}).
2121
Select("path, count(*) as count").
22-
Group("path").
23-
Having("count(*) > 1").
24-
Find(&siteDuplicates).Error; err != nil {
25-
return err
26-
}
22+
Group("path").
23+
Having("count(*) > 1").
24+
Unscoped().
25+
Find(&siteDuplicates).Error; err != nil {
26+
return err
27+
}
2728

28-
// For each duplicated path, delete all but the one with max id
29-
for _, dup := range siteDuplicates {
30-
if err := tx.Exec(`DELETE FROM sites WHERE path = ? AND id NOT IN
29+
// For each duplicated path, delete all but the one with max id
30+
for _, dup := range siteDuplicates {
31+
if err := tx.Exec(`DELETE FROM sites WHERE path = ? AND id NOT IN
3132
(SELECT max(id) FROM sites WHERE path = ?)`, dup.Path, dup.Path).Error; err != nil {
32-
return err
33+
return err
34+
}
3335
}
3436
}
35-
}
3637

3738
// Check if streams table exists
3839
if tx.Migrator().HasTable(&model.Stream{}) {
3940
// Find duplicated paths in streams table
4041
var streamDuplicates []struct {
4142
Path string
42-
Count int
43-
}
43+
Count int
44+
}
4445

45-
if err := tx.Model(&model.Stream{}).
46-
Select("path, count(*) as count").
47-
Group("path").
48-
Having("count(*) > 1").
49-
Find(&streamDuplicates).Error; err != nil {
50-
return err
51-
}
46+
if err := tx.Model(&model.Stream{}).
47+
Select("path, count(*) as count").
48+
Group("path").
49+
Having("count(*) > 1").
50+
Unscoped().
51+
Find(&streamDuplicates).Error; err != nil {
52+
return err
53+
}
5254

5355
// For each duplicated path, delete all but the one with max id
5456
for _, dup := range streamDuplicates {

0 commit comments

Comments
 (0)