You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.6 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "os"
  8. "time"
  9. "github.com/go-testfixtures/testfixtures/v3"
  10. "xorm.io/xorm/schemas"
  11. )
  12. var fixtures *testfixtures.Loader
  13. // InitFixtures initialize test fixtures for a test database
  14. func InitFixtures(dir string) (err error) {
  15. testfiles := testfixtures.Directory(dir)
  16. dialect := "unknown"
  17. switch x.Dialect().URI().DBType {
  18. case schemas.POSTGRES:
  19. dialect = "postgres"
  20. case schemas.MYSQL:
  21. dialect = "mysql"
  22. case schemas.MSSQL:
  23. dialect = "mssql"
  24. case schemas.SQLITE:
  25. dialect = "sqlite3"
  26. default:
  27. fmt.Println("Unsupported RDBMS for integration tests")
  28. os.Exit(1)
  29. }
  30. loaderOptions := []func(loader *testfixtures.Loader) error{
  31. testfixtures.Database(x.DB().DB),
  32. testfixtures.Dialect(dialect),
  33. testfixtures.DangerousSkipTestDatabaseCheck(),
  34. testfiles,
  35. }
  36. if x.Dialect().URI().DBType == schemas.POSTGRES {
  37. loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())
  38. }
  39. fixtures, err = testfixtures.New(loaderOptions...)
  40. if err != nil {
  41. return err
  42. }
  43. return err
  44. }
  45. // LoadFixtures load fixtures for a test database
  46. func LoadFixtures() error {
  47. var err error
  48. // Database transaction conflicts could occur and result in ROLLBACK
  49. // As a simple workaround, we just retry 20 times.
  50. for i := 0; i < 20; i++ {
  51. err = fixtures.Load()
  52. if err == nil {
  53. break
  54. }
  55. time.Sleep(200 * time.Millisecond)
  56. }
  57. if err != nil {
  58. fmt.Printf("LoadFixtures failed after retries: %v\n", err)
  59. }
  60. // Now if we're running postgres we need to tell it to update the sequences
  61. if x.Dialect().URI().DBType == schemas.POSTGRES {
  62. results, err := x.QueryString(`SELECT 'SELECT SETVAL(' ||
  63. quote_literal(quote_ident(PGT.schemaname) || '.' || quote_ident(S.relname)) ||
  64. ', COALESCE(MAX(' ||quote_ident(C.attname)|| '), 1) ) FROM ' ||
  65. quote_ident(PGT.schemaname)|| '.'||quote_ident(T.relname)|| ';'
  66. FROM pg_class AS S,
  67. pg_depend AS D,
  68. pg_class AS T,
  69. pg_attribute AS C,
  70. pg_tables AS PGT
  71. WHERE S.relkind = 'S'
  72. AND S.oid = D.objid
  73. AND D.refobjid = T.oid
  74. AND D.refobjid = C.attrelid
  75. AND D.refobjsubid = C.attnum
  76. AND T.relname = PGT.tablename
  77. ORDER BY S.relname;`)
  78. if err != nil {
  79. fmt.Printf("Failed to generate sequence update: %v\n", err)
  80. return err
  81. }
  82. for _, r := range results {
  83. for _, value := range r {
  84. _, err = x.Exec(value)
  85. if err != nil {
  86. fmt.Printf("Failed to update sequence: %s Error: %v\n", value, err)
  87. return err
  88. }
  89. }
  90. }
  91. }
  92. return err
  93. }