forgejo/.deadcode-out
Mathieu Fenniak fe4a2ae7c1 fix: empty DBs create tables in an ungoverned order resulting in future foreign key errors (#9709)
Discovered while testing #9708:

Fresh install databases, which is also a process used by the integration tests, create tables by using `SyncAllTables`.  The order that tables are created is ungoverned -- it occurs based upon the order that Go calls each module's `init` to register their schema models.  With the current foreign keys in the database, this does not yet cause an error.  But it will shortly.

I've manually tested this fix in this way:
- The correct order to create tables is that indicated by `foreignKeySortInsert`.  This creates tables that are *referenced* by foreign keys before creating tables that do the referencing (eg. `user` before `tracked_time`).
- If I modify this PR and sort the keys the opposite way `foreignKeySortDelete`, then integration tests fail:

```
??? [TestLogger] 2025/10/15 22:37:58 models/db/engine.go:270:InitEngineWithMigration() [E] [Error SQL Query] CREATE TABLE IF NOT EXISTS "gtestschema"."tracked_time" ("id" BIGSERIAL PRIMARY KEY  NOT NULL, "issue_id" BIGINT NULL, "user_id" BIGINT NULL, "created_unix" BIGINT NULL, "time" BIGINT NOT NULL, "deleted" BOOL DEFAULT false NOT NULL, CONSTRAINT "tracked_time_issue_id_fkey" FOREIGN KEY ("issue_id") REFERENCES "gtestschema"."issue" ("id"), CONSTRAINT "tracked_time_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "gtestschema"."user" ("id"));  [] - pq: relation "gtestschema.issue" does not exist
??? [TestLogger] 2025/10/15 22:37:58 routers/common/db.go:36:InitDBEngine() [E] ORM engine initialization attempt #1/10 failed.
```

Therefore this PR which doesn't appear to fix anything today fixes a latent bug that will occur shortly (possibly in #9397, possibly when another foreign key is added, possibly if Go changes the order in which `init` functions are invoked).

## 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

- [x] I do not want this change to show in the release notes.
- [ ] 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/9709
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2025-10-16 16:02:49 +02:00

249 lines
4.2 KiB
Text

forgejo.org/cmd
NoMainListener
forgejo.org/cmd/forgejo
ContextSetNoInit
ContextSetNoExit
ContextSetStderr
ContextGetStderr
ContextSetStdout
ContextSetStdin
forgejo.org/models
IsErrSHANotFound
IsErrMergeDivergingFastForwardOnly
forgejo.org/models/auth
WebAuthnCredentials
forgejo.org/models/db
TruncateBeans
TruncateBeansCascade
InTransaction
DumpTables
GetTableNames
extendBeansForCascade
forgejo.org/models/dbfs
file.renameTo
Create
Rename
forgejo.org/models/forgejo/semver
GetVersion
SetVersionString
SetVersion
forgejo.org/models/forgejo_migrations
resetMigrations
forgejo.org/models/git
RemoveDeletedBranchByID
forgejo.org/models/issues
IsErrUnknownDependencyType
IsErrIssueWasClosed
forgejo.org/models/organization
SearchMembersOptions.ToConds
forgejo.org/models/perm/access
GetRepoWriters
forgejo.org/models/repo
WatchRepoMode
forgejo.org/models/user
IsErrExternalLoginUserAlreadyExist
IsErrExternalLoginUserNotExist
NewFederatedUser
IsErrUserSettingIsNotExist
GetUserAllSettings
DeleteUserSetting
GetFederatedUser
forgejo.org/modules/activitypub
NewContext
Context.APClientFactory
forgejo.org/modules/assetfs
Bindata
forgejo.org/modules/auth/password/hash
DummyHasher.HashWithSaltBytes
NewDummyHasher
forgejo.org/modules/auth/password/pwn
WithHTTP
forgejo.org/modules/base
SetupGiteaRoot
forgejo.org/modules/cache
GetInt
WithNoCacheContext
RemoveContextData
forgejo.org/modules/emoji
ReplaceCodes
forgejo.org/modules/eventsource
Event.String
forgejo.org/modules/forgefed
NewForgeFollow
NewForgeUndoLike
ForgeUndoLike.UnmarshalJSON
ForgeUndoLike.Validate
NewPersonIDFromModel
GetItemByType
JSONUnmarshalerFn
NotEmpty
ToRepository
OnRepository
forgejo.org/modules/git
AllowLFSFiltersArgs
AddChanges
AddChangesWithArgs
CommitChanges
CommitChangesWithArgs
SetUpdateHook
openRepositoryWithDefaultContext
ToEntryMode
forgejo.org/modules/gitrepo
GetBranchCommitID
GetWikiDefaultBranch
forgejo.org/modules/graceful
Manager.TerminateContext
Manager.Err
Manager.Value
Manager.Deadline
forgejo.org/modules/hcaptcha
WithHTTP
forgejo.org/modules/hostmatcher
HostMatchList.AppendPattern
forgejo.org/modules/json
StdJSON.Marshal
StdJSON.Unmarshal
StdJSON.NewEncoder
StdJSON.NewDecoder
StdJSON.Indent
forgejo.org/modules/log
NewEventWriterBuffer
forgejo.org/modules/markup
GetRendererByType
RenderString
IsMarkupFile
forgejo.org/modules/markup/console
Render
RenderString
forgejo.org/modules/markup/markdown
RenderRawString
forgejo.org/modules/markup/mdstripper
stripRenderer.AddOptions
StripMarkdown
forgejo.org/modules/markup/orgmode
RenderString
forgejo.org/modules/process
Manager.ExecTimeout
forgejo.org/modules/queue
newBaseChannelSimple
newBaseChannelUnique
newBaseRedisSimple
newBaseRedisUnique
testStateRecorder.Records
testStateRecorder.Reset
newWorkerPoolQueueForTest
forgejo.org/modules/queue/lqinternal
QueueItemIDBytes
QueueItemKeyBytes
ListLevelQueueKeys
forgejo.org/modules/setting
NewConfigProviderFromData
GitConfigType.GetOption
InitLoggersForTest
forgejo.org/modules/sync
StatusTable.Start
StatusTable.IsRunning
forgejo.org/modules/timeutil
GetExecutableModTime
MockSet
MockUnset
forgejo.org/modules/translation
MockLocale.Language
MockLocale.TrString
MockLocale.Tr
MockLocale.TrN
MockLocale.TrPluralString
MockLocale.TrPluralStringAllForms
MockLocale.TrSize
MockLocale.HasKey
MockLocale.PrettyNumber
forgejo.org/modules/translation/localeiter
IterateMessagesContent
forgejo.org/modules/util
OptionalArg
forgejo.org/modules/util/filebuffer
CreateFromReader
forgejo.org/modules/validation
IsErrNotValid
forgejo.org/modules/web
RouteMock
RouteMockReset
forgejo.org/modules/zstd
NewWriter
Writer.Write
Writer.Close
forgejo.org/routers/web/org
MustEnableProjects
forgejo.org/services/context
GetPrivateContext
forgejo.org/services/federation
FollowRemoteActor
forgejo.org/services/mailer
ActionCloseIssueByCommit.isActionAdditionalData
forgejo.org/services/notify
UnregisterNotifier
forgejo.org/services/repository
IsErrForkAlreadyExist
forgejo.org/services/repository/files
ContentType.String
forgejo.org/services/repository/gitgraph
Parser.Reset
forgejo.org/services/webhook
NewNotifier