2019-01-28 21:48:52 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-01-28 21:48:52 +05:30
|
|
|
|
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
2019-12-15 15:21:28 +05:30
|
|
|
"context"
|
2019-01-28 21:48:52 +05:30
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2021-09-22 11:08:34 +05:30
|
|
|
"io"
|
2019-01-28 21:48:52 +05:30
|
|
|
"os"
|
|
|
|
"path"
|
2020-04-14 19:23:34 +05:30
|
|
|
"path/filepath"
|
2019-01-28 21:48:52 +05:30
|
|
|
"regexp"
|
|
|
|
"sort"
|
2019-05-06 05:12:29 +05:30
|
|
|
"strings"
|
2019-01-28 21:48:52 +05:30
|
|
|
"testing"
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2019-01-28 21:48:52 +05:30
|
|
|
"code.gitea.io/gitea/models/migrations"
|
2022-11-02 14:24:36 +05:30
|
|
|
migrate_base "code.gitea.io/gitea/models/migrations/base"
|
2022-04-01 22:04:57 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2019-04-17 21:36:35 +05:30
|
|
|
"code.gitea.io/gitea/modules/base"
|
2019-08-15 17:37:28 +05:30
|
|
|
"code.gitea.io/gitea/modules/charset"
|
2021-06-26 16:58:55 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-01-28 21:48:52 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-08-12 01:35:34 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-09-03 00:48:23 +05:30
|
|
|
"code.gitea.io/gitea/tests"
|
2019-01-28 21:48:52 +05:30
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-10-17 14:56:49 +05:30
|
|
|
"xorm.io/xorm"
|
2019-01-28 21:48:52 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
var currentEngine *xorm.Engine
|
|
|
|
|
2019-11-26 04:51:37 +05:30
|
|
|
func initMigrationTest(t *testing.T) func() {
|
2022-09-03 00:48:23 +05:30
|
|
|
deferFn := tests.PrintCurrentTest(t, 2)
|
2019-04-17 21:36:35 +05:30
|
|
|
giteaRoot := base.SetupGiteaRoot()
|
2019-01-28 21:48:52 +05:30
|
|
|
if giteaRoot == "" {
|
2022-09-03 00:48:23 +05:30
|
|
|
tests.Printf("Environment variable $GITEA_ROOT not set\n")
|
2019-01-28 21:48:52 +05:30
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
setting.AppPath = path.Join(giteaRoot, "gitea")
|
|
|
|
if _, err := os.Stat(setting.AppPath); err != nil {
|
2022-09-03 00:48:23 +05:30
|
|
|
tests.Printf("Could not find gitea binary at %s\n", setting.AppPath)
|
2019-01-28 21:48:52 +05:30
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
giteaConf := os.Getenv("GITEA_CONF")
|
|
|
|
if giteaConf == "" {
|
2022-09-03 00:48:23 +05:30
|
|
|
tests.Printf("Environment variable $GITEA_CONF not set\n")
|
2019-01-28 21:48:52 +05:30
|
|
|
os.Exit(1)
|
|
|
|
} else if !path.IsAbs(giteaConf) {
|
|
|
|
setting.CustomConf = path.Join(giteaRoot, giteaConf)
|
|
|
|
} else {
|
|
|
|
setting.CustomConf = giteaConf
|
|
|
|
}
|
|
|
|
|
2023-02-19 21:42:01 +05:30
|
|
|
setting.InitProviderAndLoadCommonSettingsForTest()
|
2020-04-14 19:23:34 +05:30
|
|
|
|
|
|
|
assert.True(t, len(setting.RepoRootPath) != 0)
|
2020-08-12 01:35:34 +05:30
|
|
|
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
|
2022-09-03 00:48:23 +05:30
|
|
|
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
|
2021-12-17 00:31:14 +05:30
|
|
|
ownerDirs, err := os.ReadDir(setting.RepoRootPath)
|
|
|
|
if err != nil {
|
|
|
|
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
|
|
|
|
}
|
|
|
|
for _, ownerDir := range ownerDirs {
|
|
|
|
if !ownerDir.Type().IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
repoDirs, err := os.ReadDir(filepath.Join(setting.RepoRootPath, ownerDir.Name()))
|
|
|
|
if err != nil {
|
|
|
|
assert.NoError(t, err, "unable to read the new repo root: %v\n", err)
|
|
|
|
}
|
|
|
|
for _, repoDir := range repoDirs {
|
2022-01-20 23:16:10 +05:30
|
|
|
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "pack"), 0o755)
|
|
|
|
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "objects", "info"), 0o755)
|
|
|
|
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "heads"), 0o755)
|
|
|
|
_ = os.MkdirAll(filepath.Join(setting.RepoRootPath, ownerDir.Name(), repoDir.Name(), "refs", "tag"), 0o755)
|
2021-12-17 00:31:14 +05:30
|
|
|
}
|
|
|
|
}
|
2020-04-14 19:23:34 +05:30
|
|
|
|
2022-08-09 08:52:24 +05:30
|
|
|
assert.NoError(t, git.InitFull(context.Background()))
|
2023-02-19 21:42:01 +05:30
|
|
|
setting.LoadDBSetting()
|
|
|
|
setting.InitLogs(true)
|
2019-11-26 04:51:37 +05:30
|
|
|
return deferFn
|
2019-01-28 21:48:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func availableVersions() ([]string, error) {
|
2022-09-03 00:48:23 +05:30
|
|
|
migrationsDir, err := os.Open("tests/integration/migration-test")
|
2019-01-28 21:48:52 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer migrationsDir.Close()
|
2019-08-24 14:54:45 +05:30
|
|
|
versionRE, err := regexp.Compile("gitea-v(?P<version>.+)\\." + regexp.QuoteMeta(setting.Database.Type) + "\\.sql.gz")
|
2019-01-28 21:48:52 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
filenames, err := migrationsDir.Readdirnames(-1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
versions := []string{}
|
|
|
|
for _, filename := range filenames {
|
|
|
|
if versionRE.MatchString(filename) {
|
|
|
|
substrings := versionRE.FindStringSubmatch(filename)
|
|
|
|
versions = append(versions, substrings[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(versions)
|
|
|
|
return versions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readSQLFromFile(version string) (string, error) {
|
2022-09-03 00:48:23 +05:30
|
|
|
filename := fmt.Sprintf("tests/integration/migration-test/gitea-v%s.%s.sql.gz", version, setting.Database.Type)
|
2019-01-28 21:48:52 +05:30
|
|
|
|
|
|
|
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
gr, err := gzip.NewReader(file)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer gr.Close()
|
|
|
|
|
2021-09-22 11:08:34 +05:30
|
|
|
bytes, err := io.ReadAll(gr)
|
2019-01-28 21:48:52 +05:30
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-08-15 17:37:28 +05:30
|
|
|
return string(charset.RemoveBOMIfPresent(bytes)), nil
|
2019-01-28 21:48:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func restoreOldDB(t *testing.T, version string) bool {
|
|
|
|
data, err := readSQLFromFile(version)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if len(data) == 0 {
|
2022-09-03 00:48:23 +05:30
|
|
|
tests.Printf("No db found to restore for %s version: %s\n", setting.Database.Type, version)
|
2019-01-28 21:48:52 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2019-08-24 14:54:45 +05:30
|
|
|
case setting.Database.UseSQLite3:
|
2020-08-12 01:35:34 +05:30
|
|
|
util.Remove(setting.Database.Path)
|
2019-08-24 14:54:45 +05:30
|
|
|
err := os.MkdirAll(path.Dir(setting.Database.Path), os.ModePerm)
|
2019-01-28 21:48:52 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-02-27 05:21:37 +05:30
|
|
|
db, err := sql.Open("sqlite3", fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d&_txlock=immediate", setting.Database.Path, setting.Database.Timeout))
|
2019-01-28 21:48:52 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2019-08-24 14:54:45 +05:30
|
|
|
case setting.Database.UseMySQL:
|
2019-01-28 21:48:52 +05:30
|
|
|
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/",
|
2019-08-24 14:54:45 +05:30
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host))
|
2019-01-28 21:48:52 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
2019-08-24 14:54:45 +05:30
|
|
|
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", setting.Database.Name))
|
2019-01-28 21:48:52 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-08-24 14:54:45 +05:30
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", setting.Database.Name))
|
2019-01-28 21:48:52 +05:30
|
|
|
assert.NoError(t, err)
|
2020-09-18 07:06:14 +05:30
|
|
|
db.Close()
|
2019-01-28 21:48:52 +05:30
|
|
|
|
|
|
|
db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?multiStatements=true",
|
2019-08-24 14:54:45 +05:30
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name))
|
2019-01-28 21:48:52 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2019-08-24 14:54:45 +05:30
|
|
|
case setting.Database.UsePostgreSQL:
|
2022-06-03 11:44:09 +05:30
|
|
|
var db *sql.DB
|
|
|
|
var err error
|
|
|
|
if setting.Database.Host[0] == '/' {
|
|
|
|
db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@/?sslmode=%s&host=%s",
|
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.SSLMode, setting.Database.Host))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/?sslmode=%s",
|
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.SSLMode))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2019-01-28 21:48:52 +05:30
|
|
|
defer db.Close()
|
|
|
|
|
2019-08-24 14:54:45 +05:30
|
|
|
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", setting.Database.Name))
|
2019-01-28 21:48:52 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-08-24 14:54:45 +05:30
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", setting.Database.Name))
|
2019-01-28 21:48:52 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2020-01-20 21:15:14 +05:30
|
|
|
// Check if we need to setup a specific schema
|
|
|
|
if len(setting.Database.Schema) != 0 {
|
2022-06-03 11:44:09 +05:30
|
|
|
if setting.Database.Host[0] == '/' {
|
|
|
|
db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@/%s?sslmode=%s&host=%s",
|
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Name, setting.Database.SSLMode, setting.Database.Host))
|
|
|
|
} else {
|
|
|
|
db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s",
|
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name, setting.Database.SSLMode))
|
|
|
|
}
|
2020-01-20 21:15:14 +05:30
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return false
|
|
|
|
}
|
2020-09-18 07:06:14 +05:30
|
|
|
defer db.Close()
|
|
|
|
|
2020-01-20 21:15:14 +05:30
|
|
|
schrows, err := db.Query(fmt.Sprintf("SELECT 1 FROM information_schema.schemata WHERE schema_name = '%s'", setting.Database.Schema))
|
|
|
|
if !assert.NoError(t, err) || !assert.NotEmpty(t, schrows) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !schrows.Next() {
|
|
|
|
// Create and setup a DB schema
|
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE SCHEMA %s", setting.Database.Schema))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
schrows.Close()
|
|
|
|
|
|
|
|
// Make the user's default search path the created schema; this will affect new connections
|
|
|
|
_, err = db.Exec(fmt.Sprintf(`ALTER USER "%s" SET search_path = %s`, setting.Database.User, setting.Database.Schema))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
db.Close()
|
|
|
|
}
|
|
|
|
|
2022-06-03 11:44:09 +05:30
|
|
|
if setting.Database.Host[0] == '/' {
|
|
|
|
db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@/%s?sslmode=%s&host=%s",
|
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Name, setting.Database.SSLMode, setting.Database.Host))
|
|
|
|
} else {
|
|
|
|
db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s",
|
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name, setting.Database.SSLMode))
|
|
|
|
}
|
2019-01-28 21:48:52 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2019-08-24 14:54:45 +05:30
|
|
|
case setting.Database.UseMSSQL:
|
|
|
|
host, port := setting.ParseMSSQLHostPort(setting.Database.Host)
|
2019-01-28 21:48:52 +05:30
|
|
|
db, err := sql.Open("mssql", fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;",
|
2019-08-24 14:54:45 +05:30
|
|
|
host, port, "master", setting.Database.User, setting.Database.Passwd))
|
2019-01-28 21:48:52 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
2019-07-26 09:40:20 +05:30
|
|
|
_, err = db.Exec("DROP DATABASE IF EXISTS [gitea]")
|
2019-01-28 21:48:52 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-05-06 05:12:29 +05:30
|
|
|
statements := strings.Split(data, "\nGO\n")
|
|
|
|
for _, statement := range statements {
|
2019-07-26 09:40:20 +05:30
|
|
|
if len(statement) > 5 && statement[:5] == "USE [" {
|
|
|
|
dbname := statement[5 : len(statement)-1]
|
|
|
|
db.Close()
|
|
|
|
db, err = sql.Open("mssql", fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;",
|
2019-08-24 14:54:45 +05:30
|
|
|
host, port, dbname, setting.Database.User, setting.Database.Passwd))
|
2019-07-26 09:40:20 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
}
|
2019-05-06 05:12:29 +05:30
|
|
|
_, err = db.Exec(statement)
|
|
|
|
assert.NoError(t, err, "Failure whilst running: %s\nError: %v", statement, err)
|
|
|
|
}
|
2019-01-28 21:48:52 +05:30
|
|
|
db.Close()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrappedMigrate(x *xorm.Engine) error {
|
|
|
|
currentEngine = x
|
|
|
|
return migrations.Migrate(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doMigrationTest(t *testing.T, version string) {
|
2022-09-03 00:48:23 +05:30
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
tests.Printf("Performing migration test for %s version: %s\n", setting.Database.Type, version)
|
2019-01-28 21:48:52 +05:30
|
|
|
if !restoreOldDB(t, version) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-19 21:42:01 +05:30
|
|
|
setting.InitSQLLog(false)
|
2019-01-28 21:48:52 +05:30
|
|
|
|
2021-10-30 20:02:11 +05:30
|
|
|
err := db.InitEngineWithMigration(context.Background(), wrappedMigrate)
|
2019-01-28 21:48:52 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
currentEngine.Close()
|
2020-09-07 03:22:01 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
beans, _ := db.NamesToBean()
|
2020-09-07 03:22:01 +05:30
|
|
|
|
2021-10-30 20:02:11 +05:30
|
|
|
err = db.InitEngineWithMigration(context.Background(), func(x *xorm.Engine) error {
|
2020-09-07 03:22:01 +05:30
|
|
|
currentEngine = x
|
2022-11-02 14:24:36 +05:30
|
|
|
return migrate_base.RecreateTables(beans...)(x)
|
2020-09-07 03:22:01 +05:30
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
currentEngine.Close()
|
|
|
|
|
|
|
|
// We do this a second time to ensure that there is not a problem with retained indices
|
2021-10-30 20:02:11 +05:30
|
|
|
err = db.InitEngineWithMigration(context.Background(), func(x *xorm.Engine) error {
|
2020-09-07 03:22:01 +05:30
|
|
|
currentEngine = x
|
2022-11-02 14:24:36 +05:30
|
|
|
return migrate_base.RecreateTables(beans...)(x)
|
2020-09-07 03:22:01 +05:30
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
currentEngine.Close()
|
2019-01-28 21:48:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrations(t *testing.T) {
|
2019-11-26 04:51:37 +05:30
|
|
|
defer initMigrationTest(t)()
|
2019-01-28 21:48:52 +05:30
|
|
|
|
2019-08-24 14:54:45 +05:30
|
|
|
dialect := setting.Database.Type
|
2019-01-28 21:48:52 +05:30
|
|
|
versions, err := availableVersions()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
if len(versions) == 0 {
|
2022-09-03 00:48:23 +05:30
|
|
|
tests.Printf("No old database versions available to migration test for %s\n", dialect)
|
2019-01-28 21:48:52 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-03 00:48:23 +05:30
|
|
|
tests.Printf("Preparing to test %d migrations for %s\n", len(versions), dialect)
|
2019-01-28 21:48:52 +05:30
|
|
|
for _, version := range versions {
|
2019-04-07 05:55:14 +05:30
|
|
|
t.Run(fmt.Sprintf("Migrate-%s-%s", dialect, version), func(t *testing.T) {
|
|
|
|
doMigrationTest(t, version)
|
|
|
|
})
|
2019-01-28 21:48:52 +05:30
|
|
|
}
|
|
|
|
}
|