jojo/models/gitea_migrations/fixtures
Myers Carpenter 2d4a3e5658 fix(models): deduplicate project sorting values and add unique constraints (#11334)
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>
2026-02-27 16:25:20 +01:00
..
Test_AddCombinedIndexToIssueUser chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_AddConfidentialClientColumnToOAuth2ApplicationTable chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_AddForeignKeysAccess migration: update existing foreign key migrations to automatically fix inconsistencies (#10568) 2025-12-29 02:50:20 +01:00
Test_addForeignKeysActionRunnerToken feat: add foreign keys to the action_runner_token table (#10756) 2026-01-12 21:59:40 +01:00
Test_addForeignKeysCollaboration migration: update existing foreign key migrations to automatically fix inconsistencies (#10568) 2025-12-29 02:50:20 +01:00
Test_addForeignKeysForgejoAuthToken migration: update existing foreign key migrations to automatically fix inconsistencies (#10568) 2025-12-29 02:50:20 +01:00
Test_addForeignKeysPullRequest1 migration: update existing foreign key migrations to automatically fix inconsistencies (#10568) 2025-12-29 02:50:20 +01:00
Test_AddForeignKeysStopwatchTrackedTime migration: update existing foreign key migrations to automatically fix inconsistencies (#10568) 2025-12-29 02:50:20 +01:00
Test_AddHeaderAuthorizationEncryptedColWebhook chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_AddIssueResourceIndexTable chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_AddPayloadVersionToHookTaskTable chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_AddRepoIDForAttachment chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_AddUniqueIndexForProjectIssue chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_ChangeMavenArtifactConcatenation refactor: migrate from lib/pq to jackc/pgx (#10219) 2025-11-30 17:47:45 +01:00
Test_CheckProjectColumnsConsistency fix(models): deduplicate project sorting values and add unique constraints (#11334) 2026-02-27 16:25:20 +01:00
Test_DeleteOrphanedIssueLabels chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_MigrateActionSecretToKeying chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_MigrateNormalizedFederatedURI chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_MigrateTaskSecretsToKeying feat: use keying for task secrets (#9923) 2025-11-03 13:42:32 +01:00
Test_MigrateTwoFactorToKeying chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_MigrateWebhookSecrets feat: use keying for webhook secrets (#10059) 2025-12-22 15:51:37 +01:00
Test_RemigrateU2FCredentials chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_RemoveInvalidLabels chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_removeSoftDeleteActionRunnerToken feat: add foreign keys to the action_runner_token table (#10756) 2026-01-12 21:59:40 +01:00
Test_RemoveSSHSignaturesFromReleaseNotes chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_RepositoryFormat chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_SetTopicsAsEmptySlice chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_StoreWebauthnCredentialIDAsBytes chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_UnwrapLDAPSourceCfg chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_UpdateBadgeColName chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_UpdateOpenMilestoneCounts chore: rename 'migrations' to 'gitea_migrations' 2025-10-14 14:40:49 -06:00
Test_v14ActionsApprovalAndTrustPopulateTableActionUser refactor: migrate from lib/pq to jackc/pgx (#10219) 2025-11-30 17:47:45 +01:00