You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.1 KiB
Go
120 lines
3.1 KiB
Go
3 years ago
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||
7 years ago
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
3 years ago
|
package unittest
|
||
7 years ago
|
|
||
|
import (
|
||
6 years ago
|
"fmt"
|
||
4 years ago
|
"os"
|
||
6 years ago
|
"time"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
|
|
||
4 years ago
|
"github.com/go-testfixtures/testfixtures/v3"
|
||
3 years ago
|
"xorm.io/xorm"
|
||
4 years ago
|
"xorm.io/xorm/schemas"
|
||
7 years ago
|
)
|
||
|
|
||
4 years ago
|
var fixtures *testfixtures.Loader
|
||
7 years ago
|
|
||
3 years ago
|
func getXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) {
|
||
3 years ago
|
if len(engine) == 1 {
|
||
3 years ago
|
return engine[0]
|
||
3 years ago
|
}
|
||
3 years ago
|
return db.DefaultContext.(*db.Context).Engine().(*xorm.Engine)
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
// InitFixtures initialize test fixtures for a test database
|
||
|
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
|
||
|
e := getXORMEngine(engine...)
|
||
3 years ago
|
var testfiles func(*testfixtures.Loader) error
|
||
|
if opts.Dir != "" {
|
||
|
testfiles = testfixtures.Directory(opts.Dir)
|
||
|
} else {
|
||
|
testfiles = testfixtures.Files(opts.Files...)
|
||
|
}
|
||
4 years ago
|
dialect := "unknown"
|
||
3 years ago
|
switch e.Dialect().URI().DBType {
|
||
4 years ago
|
case schemas.POSTGRES:
|
||
|
dialect = "postgres"
|
||
|
case schemas.MYSQL:
|
||
|
dialect = "mysql"
|
||
|
case schemas.MSSQL:
|
||
|
dialect = "mssql"
|
||
|
case schemas.SQLITE:
|
||
|
dialect = "sqlite3"
|
||
|
default:
|
||
|
fmt.Println("Unsupported RDBMS for integration tests")
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
loaderOptions := []func(loader *testfixtures.Loader) error{
|
||
3 years ago
|
testfixtures.Database(e.DB().DB),
|
||
4 years ago
|
testfixtures.Dialect(dialect),
|
||
|
testfixtures.DangerousSkipTestDatabaseCheck(),
|
||
|
testfiles,
|
||
|
}
|
||
|
|
||
3 years ago
|
if e.Dialect().URI().DBType == schemas.POSTGRES {
|
||
4 years ago
|
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())
|
||
|
}
|
||
|
|
||
|
fixtures, err = testfixtures.New(loaderOptions...)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
7 years ago
|
return err
|
||
|
}
|
||
|
|
||
|
// LoadFixtures load fixtures for a test database
|
||
3 years ago
|
func LoadFixtures(engine ...*xorm.Engine) error {
|
||
3 years ago
|
e := getXORMEngine(engine...)
|
||
6 years ago
|
var err error
|
||
|
// Database transaction conflicts could occur and result in ROLLBACK
|
||
6 years ago
|
// As a simple workaround, we just retry 20 times.
|
||
|
for i := 0; i < 20; i++ {
|
||
6 years ago
|
err = fixtures.Load()
|
||
|
if err == nil {
|
||
|
break
|
||
|
}
|
||
6 years ago
|
time.Sleep(200 * time.Millisecond)
|
||
|
}
|
||
|
if err != nil {
|
||
|
fmt.Printf("LoadFixtures failed after retries: %v\n", err)
|
||
6 years ago
|
}
|
||
5 years ago
|
// Now if we're running postgres we need to tell it to update the sequences
|
||
3 years ago
|
if e.Dialect().URI().DBType == schemas.POSTGRES {
|
||
|
results, err := e.QueryString(`SELECT 'SELECT SETVAL(' ||
|
||
5 years ago
|
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 {
|
||
3 years ago
|
_, err = e.Exec(value)
|
||
5 years ago
|
if err != nil {
|
||
|
fmt.Printf("Failed to update sequence: %s Error: %v\n", value, err)
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
6 years ago
|
return err
|
||
7 years ago
|
}
|