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

migration_test.go 6.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Copyright 2019 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 migrations
  5. import (
  6. "compress/gzip"
  7. "database/sql"
  8. "fmt"
  9. "io/ioutil"
  10. "os"
  11. "path"
  12. "regexp"
  13. "sort"
  14. "testing"
  15. "code.gitea.io/gitea/integrations"
  16. "code.gitea.io/gitea/models"
  17. "code.gitea.io/gitea/models/migrations"
  18. "code.gitea.io/gitea/modules/base"
  19. "code.gitea.io/gitea/modules/setting"
  20. "github.com/go-xorm/xorm"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. var currentEngine *xorm.Engine
  24. func initMigrationTest(t *testing.T) {
  25. integrations.PrintCurrentTest(t, 2)
  26. giteaRoot := base.SetupGiteaRoot()
  27. if giteaRoot == "" {
  28. integrations.Printf("Environment variable $GITEA_ROOT not set\n")
  29. os.Exit(1)
  30. }
  31. setting.AppPath = path.Join(giteaRoot, "gitea")
  32. if _, err := os.Stat(setting.AppPath); err != nil {
  33. integrations.Printf("Could not find gitea binary at %s\n", setting.AppPath)
  34. os.Exit(1)
  35. }
  36. giteaConf := os.Getenv("GITEA_CONF")
  37. if giteaConf == "" {
  38. integrations.Printf("Environment variable $GITEA_CONF not set\n")
  39. os.Exit(1)
  40. } else if !path.IsAbs(giteaConf) {
  41. setting.CustomConf = path.Join(giteaRoot, giteaConf)
  42. } else {
  43. setting.CustomConf = giteaConf
  44. }
  45. setting.NewContext()
  46. setting.CheckLFSVersion()
  47. models.LoadConfigs()
  48. setting.NewLogServices(true)
  49. }
  50. func getDialect() string {
  51. dialect := "sqlite"
  52. switch {
  53. case setting.UseSQLite3:
  54. dialect = "sqlite"
  55. case setting.UseMySQL:
  56. dialect = "mysql"
  57. case setting.UsePostgreSQL:
  58. dialect = "pgsql"
  59. case setting.UseMSSQL:
  60. dialect = "mssql"
  61. }
  62. return dialect
  63. }
  64. func availableVersions() ([]string, error) {
  65. migrationsDir, err := os.Open("integrations/migration-test")
  66. if err != nil {
  67. return nil, err
  68. }
  69. defer migrationsDir.Close()
  70. versionRE, err := regexp.Compile("gitea-v(?P<version>.+)\\." + regexp.QuoteMeta(models.DbCfg.Type) + "\\.sql.gz")
  71. if err != nil {
  72. return nil, err
  73. }
  74. filenames, err := migrationsDir.Readdirnames(-1)
  75. if err != nil {
  76. return nil, err
  77. }
  78. versions := []string{}
  79. for _, filename := range filenames {
  80. if versionRE.MatchString(filename) {
  81. substrings := versionRE.FindStringSubmatch(filename)
  82. versions = append(versions, substrings[1])
  83. }
  84. }
  85. sort.Strings(versions)
  86. return versions, nil
  87. }
  88. func readSQLFromFile(version string) (string, error) {
  89. filename := fmt.Sprintf("integrations/migration-test/gitea-v%s.%s.sql.gz", version, models.DbCfg.Type)
  90. if _, err := os.Stat(filename); os.IsNotExist(err) {
  91. return "", nil
  92. }
  93. file, err := os.Open(filename)
  94. if err != nil {
  95. return "", err
  96. }
  97. defer file.Close()
  98. gr, err := gzip.NewReader(file)
  99. if err != nil {
  100. return "", err
  101. }
  102. defer gr.Close()
  103. bytes, err := ioutil.ReadAll(gr)
  104. if err != nil {
  105. return "", err
  106. }
  107. return string(bytes), nil
  108. }
  109. func restoreOldDB(t *testing.T, version string) bool {
  110. data, err := readSQLFromFile(version)
  111. assert.NoError(t, err)
  112. if len(data) == 0 {
  113. integrations.Printf("No db found to restore for %s version: %s\n", models.DbCfg.Type, version)
  114. return false
  115. }
  116. switch {
  117. case setting.UseSQLite3:
  118. os.Remove(models.DbCfg.Path)
  119. err := os.MkdirAll(path.Dir(models.DbCfg.Path), os.ModePerm)
  120. assert.NoError(t, err)
  121. db, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d", models.DbCfg.Path, models.DbCfg.Timeout))
  122. assert.NoError(t, err)
  123. defer db.Close()
  124. _, err = db.Exec(data)
  125. assert.NoError(t, err)
  126. db.Close()
  127. case setting.UseMySQL:
  128. db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/",
  129. models.DbCfg.User, models.DbCfg.Passwd, models.DbCfg.Host))
  130. assert.NoError(t, err)
  131. defer db.Close()
  132. _, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", models.DbCfg.Name))
  133. assert.NoError(t, err)
  134. _, err = db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", models.DbCfg.Name))
  135. assert.NoError(t, err)
  136. db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?multiStatements=true",
  137. models.DbCfg.User, models.DbCfg.Passwd, models.DbCfg.Host, models.DbCfg.Name))
  138. assert.NoError(t, err)
  139. defer db.Close()
  140. _, err = db.Exec(data)
  141. assert.NoError(t, err)
  142. db.Close()
  143. case setting.UsePostgreSQL:
  144. db, err := sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/?sslmode=%s",
  145. models.DbCfg.User, models.DbCfg.Passwd, models.DbCfg.Host, models.DbCfg.SSLMode))
  146. assert.NoError(t, err)
  147. defer db.Close()
  148. _, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", models.DbCfg.Name))
  149. assert.NoError(t, err)
  150. _, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", models.DbCfg.Name))
  151. assert.NoError(t, err)
  152. db.Close()
  153. db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s",
  154. models.DbCfg.User, models.DbCfg.Passwd, models.DbCfg.Host, models.DbCfg.Name, models.DbCfg.SSLMode))
  155. assert.NoError(t, err)
  156. defer db.Close()
  157. _, err = db.Exec(data)
  158. assert.NoError(t, err)
  159. db.Close()
  160. case setting.UseMSSQL:
  161. host, port := models.ParseMSSQLHostPort(models.DbCfg.Host)
  162. db, err := sql.Open("mssql", fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;",
  163. host, port, "master", models.DbCfg.User, models.DbCfg.Passwd))
  164. assert.NoError(t, err)
  165. defer db.Close()
  166. _, err = db.Exec("DROP DATABASE IF EXISTS gitea")
  167. assert.NoError(t, err)
  168. _, err = db.Exec("CREATE DATABASE gitea")
  169. assert.NoError(t, err)
  170. _, err = db.Exec(data)
  171. assert.NoError(t, err)
  172. db.Close()
  173. }
  174. return true
  175. }
  176. func wrappedMigrate(x *xorm.Engine) error {
  177. currentEngine = x
  178. return migrations.Migrate(x)
  179. }
  180. func doMigrationTest(t *testing.T, version string) {
  181. integrations.PrintCurrentTest(t)
  182. integrations.Printf("Performing migration test for %s version: %s\n", models.DbCfg.Type, version)
  183. if !restoreOldDB(t, version) {
  184. return
  185. }
  186. setting.NewXORMLogService(false)
  187. err := models.SetEngine()
  188. assert.NoError(t, err)
  189. err = models.NewEngine(wrappedMigrate)
  190. assert.NoError(t, err)
  191. currentEngine.Close()
  192. }
  193. func TestMigrations(t *testing.T) {
  194. initMigrationTest(t)
  195. dialect := models.DbCfg.Type
  196. versions, err := availableVersions()
  197. assert.NoError(t, err)
  198. if len(versions) == 0 {
  199. integrations.Printf("No old database versions available to migration test for %s\n", dialect)
  200. return
  201. }
  202. integrations.Printf("Preparing to test %d migrations for %s\n", len(versions), dialect)
  203. for _, version := range versions {
  204. t.Run(fmt.Sprintf("Migrate-%s-%s", dialect, version), func(t *testing.T) {
  205. doMigrationTest(t, version)
  206. })
  207. }
  208. }