mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-04-21 03:16:56 -04:00
Changes foreign key database inconsistency handling so that inconsistent records are automatically deleted with an administrator warning during migration. As noted in discussion: https://codeberg.org/forgejo/discussions/issues/385#issuecomment-9175566 Intended for backport to v14 to eliminate pain during upgrades. Because these migrations are now deleting data, rather than allowing the administrator to do it, all migrations have been covered with an integration test that verifies expected data is deleted. This is particularly interesting with nullable fields. ## 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 - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10568 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
33 lines
1.1 KiB
Go
33 lines
1.1 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/modules/log"
|
|
|
|
"xorm.io/builder"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
// syncForeignKeyWithDelete will delete any records that match `cond`, and if present, log and warn to the
|
|
// administrator; then it will perform an `xorm.Sync()` in order to create foreign keys on the table definition.
|
|
func syncForeignKeyWithDelete(x *xorm.Engine, bean any, cond builder.Cond) error {
|
|
rowsDeleted, err := x.Where(cond).Delete(bean)
|
|
if err != nil {
|
|
return fmt.Errorf("failure to delete inconsistent records before foreign key sync: %w", err)
|
|
}
|
|
if rowsDeleted > 0 {
|
|
tableName := x.TableName(bean)
|
|
log.Warn(
|
|
"Foreign key creation on table %s required deleting %d records with inconsistent foreign key values.",
|
|
tableName, rowsDeleted)
|
|
}
|
|
|
|
// Sync() drops indexes by default, which will cause unnecessary rebuilding of indexes when syncForeignKeyWithDelete
|
|
// is used with partial bean definitions; so we disable that option
|
|
_, err = x.SyncWithOptions(xorm.SyncOptions{IgnoreDropIndices: true}, bean)
|
|
return err
|
|
}
|