mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-05-12 22:10:25 +00:00
Project columns and cards use a sorting field for ordering, but nothing prevents duplicate values from being inserted. This causes unpredictable ordering and makes swap-based reordering unsafe. Additionally, the same issue can be added to a project multiple times, which shouldn't be possible. This PR adds a migration to clean up existing data and enforce three unique constraints: - (project_id, sorting) on project_board — one sorting value per column per project - (project_id, issue_id) on project_issue — one card per issue per project - (project_board_id, sorting) on project_issue — one sorting value per card per column The migration deduplicates existing rows and reassigns sequential sorting values before adding the constraints. Changes - Migration: fix duplicate sorting values in project_board and project_issue, remove duplicate (project_id, issue_id) rows, add three unique constraints - MoveColumnsOnProject: two-phase swap (negate then set) to avoid constraint collisions - MoveIssuesOnProjectColumn: three-phase approach with duplicate validation and sorted lock ordering - UpdateColumn: always persist sorting field (allows setting to 0) - GetDefaultColumn: query max sorting before auto-creating - createDefaultColumnsForProject: explicit sequential sorting - changeProjectStatus: only set ClosedDateUnix when closing ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change. - [x] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change. *The decision if the pull request will be shown in the release notes is up to the mergers / release team.* The content of the `release-notes/<pull request number>.md` file will serve as the basis for the release notes. If the file does not exist, the title of the pull request will be used instead. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11334 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org> Co-authored-by: Myers Carpenter <myers@maski.org> Co-committed-by: Myers Carpenter <myers@maski.org>
218 lines
6.6 KiB
Go
218 lines
6.6 KiB
Go
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package forgejo_migrations
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"forgejo.org/models/gitea_migrations/base"
|
|
"forgejo.org/modules/setting"
|
|
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
func init() {
|
|
registerMigration(&Migration{
|
|
Description: "Fix duplicate project sorting values and add unique constraints",
|
|
Upgrade: fixProjectSortingUniqueConstraints,
|
|
})
|
|
}
|
|
|
|
func fixProjectSortingUniqueConstraints(x *xorm.Engine) error {
|
|
// Step 1: Fix existing duplicates in project_issue (cards)
|
|
// Reassign sequential sorting values within each column
|
|
if err := fixProjectIssueDuplicates(x); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Step 2: Fix existing duplicates in project_board (columns)
|
|
// Reassign sequential sorting values within each project
|
|
if err := fixProjectBoardDuplicates(x); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Step 3: Remove duplicate (project_id, issue_id) rows keeping the lowest id
|
|
if err := fixProjectIssueDuplicateAssignments(x); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Step 4: Add unique constraints (idempotent — skip if already exists)
|
|
if err := createUniqueIndexIfNotExists(x, "project_issue", "UQE_project_issue_column_sorting", "project_board_id, sorting"); err != nil {
|
|
return err
|
|
}
|
|
if err := createUniqueIndexIfNotExists(x, "project_issue", "UQE_project_issue_project_issue", "project_id, issue_id"); err != nil {
|
|
return err
|
|
}
|
|
if err := createUniqueIndexIfNotExists(x, "project_board", "UQE_project_board_project_sorting", "project_id, sorting"); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Step 5: Enforce NOT NULL on project_issue columns.
|
|
// The struct tags declare NOT NULL but the DB may not enforce it.
|
|
// On SQLite, RecreateTables rebuilds the table with NOT NULL and unique constraints.
|
|
return enforceProjectIssueNotNull(x)
|
|
}
|
|
|
|
func createUniqueIndexIfNotExists(x *xorm.Engine, tableName, indexName, columns string) error {
|
|
exists, err := indexExists(x, tableName, indexName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if exists {
|
|
return nil
|
|
}
|
|
_, err = x.Exec(fmt.Sprintf("CREATE UNIQUE INDEX %s ON %s (%s)", indexName, tableName, columns))
|
|
return err
|
|
}
|
|
|
|
func fixProjectIssueDuplicates(x *xorm.Engine) error {
|
|
switch {
|
|
case setting.Database.Type.IsSQLite3():
|
|
// SQLite: Use UPDATE with subquery
|
|
_, err := x.Exec(`
|
|
UPDATE project_issue SET sorting = (
|
|
SELECT new_sort FROM (
|
|
SELECT id, ROW_NUMBER() OVER (PARTITION BY project_board_id ORDER BY sorting, id) - 1 as new_sort
|
|
FROM project_issue
|
|
) ranked WHERE ranked.id = project_issue.id
|
|
)
|
|
`)
|
|
return err
|
|
|
|
case setting.Database.Type.IsPostgreSQL():
|
|
// PostgreSQL: Use UPDATE FROM with subquery
|
|
_, err := x.Exec(`
|
|
UPDATE project_issue pi SET sorting = ranked.new_sort
|
|
FROM (
|
|
SELECT id, ROW_NUMBER() OVER (PARTITION BY project_board_id ORDER BY sorting, id) - 1 as new_sort
|
|
FROM project_issue
|
|
) ranked
|
|
WHERE pi.id = ranked.id
|
|
`)
|
|
return err
|
|
|
|
case setting.Database.Type.IsMySQL():
|
|
// MySQL: Use UPDATE with JOIN
|
|
_, err := x.Exec(`
|
|
UPDATE project_issue pi
|
|
INNER JOIN (
|
|
SELECT id, ROW_NUMBER() OVER (PARTITION BY project_board_id ORDER BY sorting, id) - 1 as new_sort
|
|
FROM project_issue
|
|
) ranked ON pi.id = ranked.id
|
|
SET pi.sorting = ranked.new_sort
|
|
`)
|
|
return err
|
|
|
|
default:
|
|
return fmt.Errorf("unsupported db dialect type %v", x.Dialect().URI().DBType)
|
|
}
|
|
}
|
|
|
|
func fixProjectBoardDuplicates(x *xorm.Engine) error {
|
|
switch {
|
|
case setting.Database.Type.IsSQLite3():
|
|
// SQLite: Use UPDATE with subquery
|
|
_, err := x.Exec(`
|
|
UPDATE project_board SET sorting = (
|
|
SELECT new_sort FROM (
|
|
SELECT id, ROW_NUMBER() OVER (PARTITION BY project_id ORDER BY sorting, id) - 1 as new_sort
|
|
FROM project_board
|
|
) ranked WHERE ranked.id = project_board.id
|
|
)
|
|
`)
|
|
return err
|
|
|
|
case setting.Database.Type.IsPostgreSQL():
|
|
// PostgreSQL: Use UPDATE FROM with subquery
|
|
_, err := x.Exec(`
|
|
UPDATE project_board pb SET sorting = ranked.new_sort
|
|
FROM (
|
|
SELECT id, ROW_NUMBER() OVER (PARTITION BY project_id ORDER BY sorting, id) - 1 as new_sort
|
|
FROM project_board
|
|
) ranked
|
|
WHERE pb.id = ranked.id
|
|
`)
|
|
return err
|
|
|
|
case setting.Database.Type.IsMySQL():
|
|
// MySQL: Use UPDATE with JOIN
|
|
_, err := x.Exec(`
|
|
UPDATE project_board pb
|
|
INNER JOIN (
|
|
SELECT id, ROW_NUMBER() OVER (PARTITION BY project_id ORDER BY sorting, id) - 1 as new_sort
|
|
FROM project_board
|
|
) ranked ON pb.id = ranked.id
|
|
SET pb.sorting = ranked.new_sort
|
|
`)
|
|
return err
|
|
|
|
default:
|
|
return fmt.Errorf("unsupported db dialect type %v", x.Dialect().URI().DBType)
|
|
}
|
|
}
|
|
|
|
func enforceProjectIssueNotNull(x *xorm.Engine) error {
|
|
switch {
|
|
case setting.Database.Type.IsSQLite3():
|
|
type ProjectIssue struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
IssueID int64 `xorm:"INDEX NOT NULL unique(project_issue)"`
|
|
ProjectID int64 `xorm:"INDEX NOT NULL unique(project_issue)"`
|
|
ProjectColumnID int64 `xorm:"'project_board_id' INDEX NOT NULL unique(column_sorting)"`
|
|
Sorting int64 `xorm:"NOT NULL DEFAULT 0 unique(column_sorting)"`
|
|
}
|
|
return base.RecreateTables(new(ProjectIssue))(x)
|
|
|
|
case setting.Database.Type.IsPostgreSQL():
|
|
for _, col := range []string{"issue_id", "project_id", "project_board_id"} {
|
|
if _, err := x.Exec(fmt.Sprintf("ALTER TABLE project_issue ALTER COLUMN %s SET NOT NULL", col)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
|
|
case setting.Database.Type.IsMySQL():
|
|
for _, col := range []string{"issue_id", "project_id", "project_board_id"} {
|
|
if _, err := x.Exec(fmt.Sprintf("ALTER TABLE project_issue MODIFY COLUMN %s BIGINT NOT NULL", col)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
|
|
default:
|
|
return fmt.Errorf("unsupported db dialect type %v", x.Dialect().URI().DBType)
|
|
}
|
|
}
|
|
|
|
// fixProjectIssueDuplicateAssignments removes duplicate (project_id, issue_id) rows,
|
|
// keeping only the row with the lowest id for each pair.
|
|
func fixProjectIssueDuplicateAssignments(x *xorm.Engine) error {
|
|
switch {
|
|
case setting.Database.Type.IsSQLite3():
|
|
_, err := x.Exec(`
|
|
DELETE FROM project_issue WHERE id NOT IN (
|
|
SELECT MIN(id) FROM project_issue GROUP BY project_id, issue_id
|
|
)
|
|
`)
|
|
return err
|
|
|
|
case setting.Database.Type.IsPostgreSQL():
|
|
_, err := x.Exec(`
|
|
DELETE FROM project_issue pi USING project_issue pi2
|
|
WHERE pi.project_id = pi2.project_id AND pi.issue_id = pi2.issue_id AND pi.id > pi2.id
|
|
`)
|
|
return err
|
|
|
|
case setting.Database.Type.IsMySQL():
|
|
_, err := x.Exec(`
|
|
DELETE pi FROM project_issue pi
|
|
INNER JOIN project_issue pi2
|
|
ON pi.project_id = pi2.project_id AND pi.issue_id = pi2.issue_id AND pi.id > pi2.id
|
|
`)
|
|
return err
|
|
|
|
default:
|
|
return fmt.Errorf("unsupported db dialect type %v", x.Dialect().URI().DBType)
|
|
}
|
|
}
|