-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Fix migration v292 #30153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix migration v292 #30153
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0a5e4fc
Improve migration v292
lunny 788c512
Use local struct
lunny 3fb007c
Remove multiple defaults of projects and keep the last one
lunny 0963761
Fix bug
lunny 48338b1
Merge branch 'main' into lunny/improve_migration_v292
lunny bb2d1a0
Fix test
lunny 9544b70
Fix broken migration
lunny cc0b580
revert unnecessary change
lunny 4455719
fix migration
lunny 0c28c14
Fix test
lunny 7bd2cc1
Add transaction for projects operations
lunny 94ad45c
Fix bug
lunny 69625ea
Fix test
lunny 296387c
Fix test
lunny 96d1248
Merge branch 'main' into lunny/improve_migration_v292
lunny eb686b1
Merge branch 'main' into lunny/improve_migration_v292
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package v1_22 //nolint | ||
|
||
import ( | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/timeutil" | ||
|
||
"xorm.io/xorm" | ||
) | ||
|
||
// CheckProjectColumnsConsistency ensures there is exactly one default board per project present | ||
func CheckProjectColumnsConsistency(x *xorm.Engine) error { | ||
sess := x.NewSession() | ||
defer sess.Close() | ||
|
||
limit := setting.Database.IterateBufferSize | ||
if limit <= 0 { | ||
limit = 50 | ||
} | ||
|
||
type Project struct { | ||
ID int64 | ||
CreatorID int64 | ||
BoardID int64 | ||
} | ||
|
||
type ProjectBoard struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
Title string | ||
Default bool `xorm:"NOT NULL DEFAULT false"` // issues not assigned to a specific board will be assigned to this board | ||
Sorting int8 `xorm:"NOT NULL DEFAULT 0"` | ||
Color string `xorm:"VARCHAR(7)"` | ||
|
||
ProjectID int64 `xorm:"INDEX NOT NULL"` | ||
CreatorID int64 `xorm:"NOT NULL"` | ||
|
||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` | ||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` | ||
} | ||
|
||
for { | ||
if err := sess.Begin(); err != nil { | ||
return err | ||
} | ||
|
||
// all these projects without defaults will be fixed in the same loop, so | ||
// we just need to always get projects without defaults until no such project | ||
var projects []*Project | ||
if err := sess.Select("project.id as id, project.creator_id, project_board.id as board_id"). | ||
Join("LEFT", "project_board", "project_board.project_id = project.id AND project_board.`default`=?", true). | ||
Where("project_board.id is NULL OR project_board.id = 0"). | ||
Limit(limit). | ||
Find(&projects); err != nil { | ||
return err | ||
} | ||
|
||
for _, p := range projects { | ||
if _, err := sess.Insert(ProjectBoard{ | ||
ProjectID: p.ID, | ||
Default: true, | ||
Title: "Uncategorized", | ||
CreatorID: p.CreatorID, | ||
}); err != nil { | ||
return err | ||
} | ||
} | ||
if err := sess.Commit(); err != nil { | ||
return err | ||
} | ||
|
||
if len(projects) == 0 { | ||
break | ||
} | ||
} | ||
sess.Close() | ||
|
||
return removeDuplicatedBoardDefault(x) | ||
} | ||
|
||
func removeDuplicatedBoardDefault(x *xorm.Engine) error { | ||
type ProjectInfo struct { | ||
ProjectID int64 | ||
DefaultNum int | ||
} | ||
var projects []ProjectInfo | ||
if err := x.Select("project_id, count(*) AS default_num"). | ||
Table("project_board"). | ||
Where("`default` = ?", true). | ||
GroupBy("project_id"). | ||
Having("count(*) > 1"). | ||
Find(&projects); err != nil { | ||
return err | ||
} | ||
|
||
for _, project := range projects { | ||
if _, err := x.Where("project_id=?", project.ProjectID). | ||
Table("project_board"). | ||
Limit(project.DefaultNum - 1). | ||
Update(map[string]bool{ | ||
"`default`": false, | ||
}); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.