forgejo/models/forgejo_migrations_legacy/v41.go
forgejo-backport-action 763547f43f [v14.0/forgejo] migration: update existing foreign key migrations to automatically fix inconsistencies (#10621)
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/10568

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

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.

Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10621
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
2025-12-29 03:49:03 +01:00

88 lines
3 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package forgejo_migrations_legacy
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
}
func AddForeignKeysStopwatchTrackedTime(x *xorm.Engine) error {
type Stopwatch struct {
IssueID int64 `xorm:"INDEX REFERENCES(issue, id)"`
UserID int64 `xorm:"INDEX REFERENCES(user, id)"`
}
type TrackedTime struct {
ID int64 `xorm:"pk autoincr"`
IssueID int64 `xorm:"INDEX REFERENCES(issue, id)"`
UserID int64 `xorm:"INDEX REFERENCES(user, id)"`
}
// TrackedTime.UserID used to be an intentionally dangling reference if a user was deleted, in order to maintain the
// time that was tracked against an issue. With the addition of a foreign key, we set UserID to NULL where the user
// doesn't exist instead of leaving it pointing to an invalid record:
var trackedTime []TrackedTime
err := x.Table("tracked_time").
Join("LEFT", "`user`", "`tracked_time`.user_id = `user`.id").
Where(builder.IsNull{"`user`.id"}).
Where(builder.NotNull{"tracked_time.user_id"}).
Find(&trackedTime)
if err != nil {
return err
}
for _, tt := range trackedTime {
affected, err := x.Table(&TrackedTime{}).Where("id = ?", tt.ID).Update(map[string]any{"user_id": nil})
if err != nil {
return err
} else if affected != 1 {
return fmt.Errorf("expected to update 1 tracked_time record with ID %d, but actually affected %d records", tt.ID, affected)
}
}
err = syncForeignKeyWithDelete(x,
new(Stopwatch),
builder.Or(
builder.Expr("NOT EXISTS (SELECT id FROM issue WHERE issue.id = stopwatch.issue_id)"),
builder.Expr("NOT EXISTS (SELECT id FROM `user` WHERE `user`.id = stopwatch.user_id)"),
),
)
if err != nil {
return err
}
return syncForeignKeyWithDelete(x,
new(TrackedTime),
builder.Or(
builder.And(
builder.Expr("user_id IS NOT NULL"),
builder.Expr("NOT EXISTS (SELECT id FROM `user` WHERE `user`.id = tracked_time.user_id)"),
),
builder.Expr("NOT EXISTS (SELECT id FROM issue WHERE issue.id = tracked_time.issue_id)"),
),
)
}