2019-01-28 11:18:52 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-01-28 11:18:52 -05:00
|
|
|
|
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
2019-12-15 04:51:28 -05:00
|
|
|
"context"
|
2019-01-28 11:18:52 -05:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2021-09-22 01:38:34 -04:00
|
|
|
"io"
|
2019-01-28 11:18:52 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-04-14 09:53:34 -04:00
|
|
|
"path/filepath"
|
2019-01-28 11:18:52 -05:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
2019-05-05 19:42:29 -04:00
|
|
|
"strings"
|
2019-01-28 11:18:52 -05:00
|
|
|
"testing"
|
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2019-01-28 11:18:52 -05:00
|
|
|
"code.gitea.io/gitea/models/migrations"
|
2022-11-02 04:54:36 -04:00
|
|
|
migrate_base "code.gitea.io/gitea/models/migrations/base"
|
2022-04-01 12:34:57 -04:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2019-04-17 12:06:35 -04:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2019-08-15 08:07:28 -04:00
|
|
|
"code.gitea.io/gitea/modules/charset"
|
2021-06-26 07:28:55 -04:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-01-28 11:18:52 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-08-11 16:05:34 -04:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-09-02 15:18:23 -04:00
|
|
|
"code.gitea.io/gitea/tests"
|
2019-01-28 11:18:52 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-10-17 05:26:49 -04:00
|
|
|
"xorm.io/xorm"
|
2019-01-28 11:18:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var currentEngine *xorm.Engine
|
|
|
|
|
2019-11-25 18:21:37 -05:00
|
|
|
func initMigrationTest(t *testing.T) func() {
|
2022-09-02 15:18:23 -04:00
|
|
|
deferFn := tests.PrintCurrentTest(t, 2)
|
2019-04-17 12:06:35 -04:00
|
|
|
giteaRoot := base.SetupGiteaRoot()
|
2019-01-28 11:18:52 -05:00
|
|
|
if giteaRoot == "" {
|
2022-09-02 15:18:23 -04:00
|
|
|
tests.Printf("Environment variable $GITEA_ROOT not set\n")
|
2019-01-28 11:18:52 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
setting.AppPath = path.Join(giteaRoot, "gitea")
|
|
|
|
if _, err := os.Stat(setting.AppPath); err != nil {
|
2022-09-02 15:18:23 -04:00
|
|
|
tests.Printf("Could not find gitea binary at %s\n", setting.AppPath)
|
2019-01-28 11:18:52 -05:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
giteaConf := os.Getenv("GITEA_CONF")
|
|
|
|
if giteaConf == "" {
|
2022-09-02 15:18:23 -04:00
|
|
|
tests.Printf("Environment variable $GITEA_CONF not set\n")
|
2019-01-28 11:18:52 -05:00
|
|
|
os.Exit(1)
|
|
|
|
} else if !path.IsAbs(giteaConf) {
|
|
|
|
setting.CustomConf = path.Join(giteaRoot, giteaConf)
|
|
|
|
} else {
|
|
|
|
setting.CustomConf = giteaConf
|
|
|
|
}
|
|
|
|
|
2023-05-03 23:55:35 -04:00
|
|
|
unittest.InitSettings()
|
2020-04-14 09:53:34 -04:00
|
|
|
|
|
|
|
assert.True(t, len(setting.RepoRootPath) != 0)
|
2020-08-11 16:05:34 -04:00
|
|
|
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
|
2022-09-02 15:18:23 -04:00
|
|
|
assert.NoError(t, unittest.CopyDir(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
|
2021-12-16 14:01:14 -05:00
|
|
|
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 12:46:10 -05:00
|
|
|
_ = 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-16 14:01:14 -05:00
|
|
|
}
|
|
|
|
}
|
2020-04-14 09:53:34 -04:00
|
|
|
|
2022-08-08 23:22:24 -04:00
|
|
|
assert.NoError(t, git.InitFull(context.Background()))
|
2023-02-19 11:12:01 -05:00
|
|
|
setting.LoadDBSetting()
|
|
|
|
setting.InitLogs(true)
|
2019-11-25 18:21:37 -05:00
|
|
|
return deferFn
|
2019-01-28 11:18:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func availableVersions() ([]string, error) {
|
2022-09-02 15:18:23 -04:00
|
|
|
migrationsDir, err := os.Open("tests/integration/migration-test")
|
2019-01-28 11:18:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer migrationsDir.Close()
|
2023-03-07 05:51:06 -05:00
|
|
|
versionRE, err := regexp.Compile("gitea-v(?P<version>.+)\\." + regexp.QuoteMeta(setting.Database.Type.String()) + "\\.sql.gz")
|
2019-01-28 11:18:52 -05:00
|
|
|
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-02 15:18:23 -04:00
|
|
|
filename := fmt.Sprintf("tests/integration/migration-test/gitea-v%s.%s.sql.gz", version, setting.Database.Type)
|
2019-01-28 11:18:52 -05:00
|
|
|
|
|
|
|
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 01:38:34 -04:00
|
|
|
bytes, err := io.ReadAll(gr)
|
2019-01-28 11:18:52 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-08-15 08:07:28 -04:00
|
|
|
return string(charset.RemoveBOMIfPresent(bytes)), nil
|
2019-01-28 11:18:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func restoreOldDB(t *testing.T, version string) bool {
|
|
|
|
data, err := readSQLFromFile(version)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if len(data) == 0 {
|
2022-09-02 15:18:23 -04:00
|
|
|
tests.Printf("No db found to restore for %s version: %s\n", setting.Database.Type, version)
|
2019-01-28 11:18:52 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2023-03-07 05:51:06 -05:00
|
|
|
case setting.Database.Type.IsSQLite3():
|
2020-08-11 16:05:34 -04:00
|
|
|
util.Remove(setting.Database.Path)
|
2019-08-24 05:24:45 -04:00
|
|
|
err := os.MkdirAll(path.Dir(setting.Database.Path), os.ModePerm)
|
2019-01-28 11:18:52 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-02-26 18:51:37 -05:00
|
|
|
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 11:18:52 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2023-03-07 05:51:06 -05:00
|
|
|
case setting.Database.Type.IsMySQL():
|
2019-01-28 11:18:52 -05:00
|
|
|
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/",
|
2019-08-24 05:24:45 -04:00
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host))
|
2019-01-28 11:18:52 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
2019-08-24 05:24:45 -04:00
|
|
|
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", setting.Database.Name))
|
2019-01-28 11:18:52 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-08-24 05:24:45 -04:00
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", setting.Database.Name))
|
2019-01-28 11:18:52 -05:00
|
|
|
assert.NoError(t, err)
|
2020-09-17 21:36:14 -04:00
|
|
|
db.Close()
|
2019-01-28 11:18:52 -05:00
|
|
|
|
|
|
|
db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?multiStatements=true",
|
2019-08-24 05:24:45 -04:00
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name))
|
2019-01-28 11:18:52 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2023-03-07 05:51:06 -05:00
|
|
|
case setting.Database.Type.IsPostgreSQL():
|
2022-06-03 02:14:09 -04:00
|
|
|
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 11:18:52 -05:00
|
|
|
defer db.Close()
|
|
|
|
|
2019-08-24 05:24:45 -04:00
|
|
|
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", setting.Database.Name))
|
2019-01-28 11:18:52 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-08-24 05:24:45 -04:00
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", setting.Database.Name))
|
2019-01-28 11:18:52 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2020-01-20 10:45:14 -05:00
|
|
|
// Check if we need to setup a specific schema
|
|
|
|
if len(setting.Database.Schema) != 0 {
|
2022-06-03 02:14:09 -04:00
|
|
|
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 10:45:14 -05:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return false
|
|
|
|
}
|
2020-09-17 21:36:14 -04:00
|
|
|
defer db.Close()
|
|
|
|
|
2020-01-20 10:45:14 -05:00
|
|
|
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 02:14:09 -04:00
|
|
|
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 11:18:52 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2023-03-07 05:51:06 -05:00
|
|
|
case setting.Database.Type.IsMSSQL():
|
2019-08-24 05:24:45 -04:00
|
|
|
host, port := setting.ParseMSSQLHostPort(setting.Database.Host)
|
2019-01-28 11:18:52 -05:00
|
|
|
db, err := sql.Open("mssql", fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;",
|
2019-08-24 05:24:45 -04:00
|
|
|
host, port, "master", setting.Database.User, setting.Database.Passwd))
|
2019-01-28 11:18:52 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
2019-07-26 00:10:20 -04:00
|
|
|
_, err = db.Exec("DROP DATABASE IF EXISTS [gitea]")
|
2019-01-28 11:18:52 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-05-05 19:42:29 -04:00
|
|
|
statements := strings.Split(data, "\nGO\n")
|
|
|
|
for _, statement := range statements {
|
2019-07-26 00:10:20 -04:00
|
|
|
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 05:24:45 -04:00
|
|
|
host, port, dbname, setting.Database.User, setting.Database.Passwd))
|
2019-07-26 00:10:20 -04:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
}
|
2019-05-05 19:42:29 -04:00
|
|
|
_, err = db.Exec(statement)
|
|
|
|
assert.NoError(t, err, "Failure whilst running: %s\nError: %v", statement, err)
|
|
|
|
}
|
2019-01-28 11:18:52 -05:00
|
|
|
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-02 15:18:23 -04:00
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
tests.Printf("Performing migration test for %s version: %s\n", setting.Database.Type, version)
|
2019-01-28 11:18:52 -05:00
|
|
|
if !restoreOldDB(t, version) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-19 11:12:01 -05:00
|
|
|
setting.InitSQLLog(false)
|
2019-01-28 11:18:52 -05:00
|
|
|
|
2021-10-30 10:32:11 -04:00
|
|
|
err := db.InitEngineWithMigration(context.Background(), wrappedMigrate)
|
2019-01-28 11:18:52 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
currentEngine.Close()
|
2020-09-06 17:52:01 -04:00
|
|
|
|
2021-09-19 07:49:59 -04:00
|
|
|
beans, _ := db.NamesToBean()
|
2020-09-06 17:52:01 -04:00
|
|
|
|
2021-10-30 10:32:11 -04:00
|
|
|
err = db.InitEngineWithMigration(context.Background(), func(x *xorm.Engine) error {
|
2020-09-06 17:52:01 -04:00
|
|
|
currentEngine = x
|
2022-11-02 04:54:36 -04:00
|
|
|
return migrate_base.RecreateTables(beans...)(x)
|
2020-09-06 17:52:01 -04:00
|
|
|
})
|
|
|
|
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 10:32:11 -04:00
|
|
|
err = db.InitEngineWithMigration(context.Background(), func(x *xorm.Engine) error {
|
2020-09-06 17:52:01 -04:00
|
|
|
currentEngine = x
|
2022-11-02 04:54:36 -04:00
|
|
|
return migrate_base.RecreateTables(beans...)(x)
|
2020-09-06 17:52:01 -04:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
currentEngine.Close()
|
2019-01-28 11:18:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrations(t *testing.T) {
|
2019-11-25 18:21:37 -05:00
|
|
|
defer initMigrationTest(t)()
|
2019-01-28 11:18:52 -05:00
|
|
|
|
2019-08-24 05:24:45 -04:00
|
|
|
dialect := setting.Database.Type
|
2019-01-28 11:18:52 -05:00
|
|
|
versions, err := availableVersions()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
if len(versions) == 0 {
|
2022-09-02 15:18:23 -04:00
|
|
|
tests.Printf("No old database versions available to migration test for %s\n", dialect)
|
2019-01-28 11:18:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 15:18:23 -04:00
|
|
|
tests.Printf("Preparing to test %d migrations for %s\n", len(versions), dialect)
|
2019-01-28 11:18:52 -05:00
|
|
|
for _, version := range versions {
|
2019-04-06 20:25:14 -04:00
|
|
|
t.Run(fmt.Sprintf("Migrate-%s-%s", dialect, version), func(t *testing.T) {
|
|
|
|
doMigrationTest(t, version)
|
|
|
|
})
|
2019-01-28 11:18:52 -05:00
|
|
|
}
|
|
|
|
}
|