mirror of
https://codeberg.org/forgejo/forgejo
synced 2025-10-19 10:50:52 +02:00
Adds four foreign keys: - stopwatch -- issue_id -> issue, user_id -> user - tracked_time -- issue_id -> issue, user_id -> user The majority of work encompassed in this PR is updating testing and support infrastructure to support foreign keys: - `models/db/foreign_keys.go` adds new capabilities to sort registered tables into the right insertion order to avoid violating foreign keys - `RecreateTables`, used by migration testing and the `doctor recreate-table` CLI, has been updated to support tables with foreign keys; new restrictions require that FK-related tables be rebuilt at the same time - test fixture data is inserted in foreign-key order, and deleted in the reverse An upgrade to xorm v1.3.9-forgejo.2 is incorporated in this PR, as two unexpected behaviors in the foreign key schema management were discovered during development of the updated `RecreateTables` routine. Work in this PR is laid out to be reviewed easier commit-by-commit. ## 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. - [x] 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. - [ ] I want the title to show in the release notes with a link to this pull request. - [x] 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/9373 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
157 lines
3.9 KiB
Go
157 lines
3.9 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//nolint:forbidigo
|
|
package unittest
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
|
|
"forgejo.org/models/db"
|
|
"forgejo.org/modules/auth/password/hash"
|
|
"forgejo.org/modules/container"
|
|
"forgejo.org/modules/setting"
|
|
|
|
"xorm.io/xorm"
|
|
"xorm.io/xorm/schemas"
|
|
)
|
|
|
|
var fixturesLoader *loader
|
|
|
|
// GetXORMEngine gets the XORM engine
|
|
func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine, err error) {
|
|
if len(engine) == 1 {
|
|
return engine[0], nil
|
|
}
|
|
return db.GetMasterEngine(db.DefaultContext.(*db.Context).Engine())
|
|
}
|
|
|
|
func OverrideFixtures(dir string) func() {
|
|
old := fixturesLoader
|
|
|
|
opts := FixturesOptions{
|
|
Dir: filepath.Join(setting.AppWorkPath, "models/fixtures/"),
|
|
Base: setting.AppWorkPath,
|
|
Dirs: []string{dir},
|
|
}
|
|
if err := InitFixtures(opts); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return func() {
|
|
fixturesLoader = old
|
|
}
|
|
}
|
|
|
|
var allTableNames = sync.OnceValue(db.GetTableNames)
|
|
|
|
// InitFixtures initialize test fixtures for a test database
|
|
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
|
|
e, err := GetXORMEngine(engine...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fixturePaths := []string{}
|
|
if opts.Dir != "" {
|
|
fixturePaths = append(fixturePaths, opts.Dir)
|
|
} else {
|
|
fixturePaths = append(fixturePaths, opts.Files...)
|
|
}
|
|
if opts.Dirs != nil {
|
|
for _, dir := range opts.Dirs {
|
|
fixturePaths = append(fixturePaths, filepath.Join(opts.Base, dir))
|
|
}
|
|
}
|
|
|
|
var dialect string
|
|
switch e.Dialect().URI().DBType {
|
|
case schemas.POSTGRES:
|
|
dialect = "postgres"
|
|
case schemas.MYSQL:
|
|
dialect = "mysql"
|
|
case schemas.SQLITE:
|
|
dialect = "sqlite3"
|
|
default:
|
|
panic("Unsupported RDBMS for test")
|
|
}
|
|
|
|
var allTables container.Set[string]
|
|
if opts.OnlyAffectModels == nil {
|
|
allTables = allTableNames().Clone()
|
|
} else {
|
|
allTables = make(container.Set[string])
|
|
for _, bean := range opts.OnlyAffectModels {
|
|
allTables.Add(e.TableName(bean))
|
|
}
|
|
}
|
|
|
|
fixturesLoader, err = newFixtureLoader(e.DB().DB, dialect, fixturePaths, allTables)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// register the dummy hash algorithm function used in the test fixtures
|
|
_ = hash.Register("dummy", hash.NewDummyHasher)
|
|
|
|
setting.PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
|
|
|
|
return err
|
|
}
|
|
|
|
// LoadFixtures load fixtures for a test database
|
|
func LoadFixtures(engine ...*xorm.Engine) error {
|
|
e, err := GetXORMEngine(engine...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// (doubt) database transaction conflicts could occur and result in ROLLBACK? just try for a few times.
|
|
for range 5 {
|
|
if err = fixturesLoader.Load(); err == nil {
|
|
break
|
|
}
|
|
time.Sleep(200 * time.Millisecond)
|
|
}
|
|
if err != nil {
|
|
fmt.Printf("LoadFixtures failed after retries: %v\n", err)
|
|
}
|
|
// Now if we're running postgres we need to tell it to update the sequences
|
|
if e.Dialect().URI().DBType == schemas.POSTGRES {
|
|
results, err := e.QueryString(`SELECT 'SELECT SETVAL(' ||
|
|
quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
|
|
', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM ' ||
|
|
quote_ident(PGT.schemaname)|| '.'||quote_ident(T.relname)|| ';'
|
|
FROM pg_class AS S,
|
|
pg_depend AS D,
|
|
pg_class AS T,
|
|
pg_attribute AS C,
|
|
pg_tables AS PGT
|
|
WHERE S.relkind = 'S'
|
|
AND S.oid = D.objid
|
|
AND D.refobjid = T.oid
|
|
AND D.refobjid = C.attrelid
|
|
AND D.refobjsubid = C.attnum
|
|
AND T.relname = PGT.tablename
|
|
ORDER BY S.relname;`)
|
|
if err != nil {
|
|
fmt.Printf("Failed to generate sequence update: %v\n", err)
|
|
return err
|
|
}
|
|
for _, r := range results {
|
|
for _, value := range r {
|
|
_, err = e.Exec(value)
|
|
if err != nil {
|
|
fmt.Printf("Failed to update sequence: %s Error: %v\n", value, err)
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
_ = hash.Register("dummy", hash.NewDummyHasher)
|
|
setting.PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
|
|
|
|
return err
|
|
}
|