From 84eb766e35c44607fa8a82ac13e541a55a924322 Mon Sep 17 00:00:00 2001 From: Mathieu Fenniak Date: Wed, 29 Oct 2025 01:09:06 +0100 Subject: [PATCH] feat: add foreign keys to forgejo_auth_token (#9886) Adds a foreign key to the table `forgejo_auth_token` (`AuthorizationToken` in go code). A review has shown that most capabilities around auth tokens seem to be well covered by test automation, but supplemented with manual testing around user deletion as well. ## 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... - [ ] 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/.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9886 Reviewed-by: Earl Warren Co-authored-by: Mathieu Fenniak Co-committed-by: Mathieu Fenniak --- models/auth/auth_token.go | 2 +- ...14a_add-foreign-keys-forgejo_auth_token.go | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 models/forgejo_migrations/v14a_add-foreign-keys-forgejo_auth_token.go diff --git a/models/auth/auth_token.go b/models/auth/auth_token.go index d09aebcf85..d01ddbca1e 100644 --- a/models/auth/auth_token.go +++ b/models/auth/auth_token.go @@ -36,7 +36,7 @@ func EmailActivation(email string) AuthorizationPurpose { // AuthorizationToken represents a authorization token to a user. type AuthorizationToken struct { ID int64 `xorm:"pk autoincr"` - UID int64 `xorm:"INDEX"` + UID int64 `xorm:"INDEX REFERENCES(user, id)"` LookupKey string `xorm:"INDEX UNIQUE"` HashedValidator string Purpose AuthorizationPurpose `xorm:"NOT NULL DEFAULT 'long_term_authorization'"` diff --git a/models/forgejo_migrations/v14a_add-foreign-keys-forgejo_auth_token.go b/models/forgejo_migrations/v14a_add-foreign-keys-forgejo_auth_token.go new file mode 100644 index 0000000000..a5d8126d91 --- /dev/null +++ b/models/forgejo_migrations/v14a_add-foreign-keys-forgejo_auth_token.go @@ -0,0 +1,24 @@ +// Copyright 2025 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: GPL-3.0-or-later + +package forgejo_migrations + +import ( + "xorm.io/xorm" +) + +func init() { + registerMigration(&Migration{ + Description: "add foreign keys to table forgejo_auth_token", + Upgrade: addForeignKeysForgejoAuthToken, + }) +} + +func addForeignKeysForgejoAuthToken(x *xorm.Engine) error { + type ForgejoAuthToken struct { + UID int64 `xorm:"INDEX REFERENCES(user, id)"` + } + return syncDoctorForeignKey(x, []any{ + new(ForgejoAuthToken), + }) +}