2019-01-29 00:18:52 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
2019-12-15 17:51:28 +08:00
|
|
|
"context"
|
2019-01-29 00:18:52 +08:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-04-14 21:53:34 +08:00
|
|
|
"path/filepath"
|
2019-01-29 00:18:52 +08:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
2019-05-06 07:42:29 +08:00
|
|
|
"strings"
|
2019-01-29 00:18:52 +08:00
|
|
|
"testing"
|
|
|
|
|
2019-04-07 08:25:14 +08:00
|
|
|
"code.gitea.io/gitea/integrations"
|
2019-01-29 00:18:52 +08:00
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/models/migrations"
|
2019-04-18 00:06:35 +08:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2019-08-15 20:07:28 +08:00
|
|
|
"code.gitea.io/gitea/modules/charset"
|
2019-01-29 00:18:52 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-08-12 04:05:34 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2019-01-29 00:18:52 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-04-14 21:53:34 +08:00
|
|
|
"github.com/unknwon/com"
|
2019-10-17 17:26:49 +08:00
|
|
|
"xorm.io/xorm"
|
2019-01-29 00:18:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var currentEngine *xorm.Engine
|
|
|
|
|
2019-11-26 07:21:37 +08:00
|
|
|
func initMigrationTest(t *testing.T) func() {
|
|
|
|
deferFn := integrations.PrintCurrentTest(t, 2)
|
2019-04-18 00:06:35 +08:00
|
|
|
giteaRoot := base.SetupGiteaRoot()
|
2019-01-29 00:18:52 +08:00
|
|
|
if giteaRoot == "" {
|
2019-04-07 08:25:14 +08:00
|
|
|
integrations.Printf("Environment variable $GITEA_ROOT not set\n")
|
2019-01-29 00:18:52 +08:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
setting.AppPath = path.Join(giteaRoot, "gitea")
|
|
|
|
if _, err := os.Stat(setting.AppPath); err != nil {
|
2019-04-07 08:25:14 +08:00
|
|
|
integrations.Printf("Could not find gitea binary at %s\n", setting.AppPath)
|
2019-01-29 00:18:52 +08:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
giteaConf := os.Getenv("GITEA_CONF")
|
|
|
|
if giteaConf == "" {
|
2019-04-07 08:25:14 +08:00
|
|
|
integrations.Printf("Environment variable $GITEA_CONF not set\n")
|
2019-01-29 00:18:52 +08:00
|
|
|
os.Exit(1)
|
|
|
|
} else if !path.IsAbs(giteaConf) {
|
|
|
|
setting.CustomConf = path.Join(giteaRoot, giteaConf)
|
|
|
|
} else {
|
|
|
|
setting.CustomConf = giteaConf
|
|
|
|
}
|
|
|
|
|
|
|
|
setting.NewContext()
|
2020-04-14 21:53:34 +08:00
|
|
|
|
|
|
|
assert.True(t, len(setting.RepoRootPath) != 0)
|
2020-08-12 04:05:34 +08:00
|
|
|
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
|
2020-04-14 21:53:34 +08:00
|
|
|
assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
|
|
|
|
|
2019-01-29 00:18:52 +08:00
|
|
|
setting.CheckLFSVersion()
|
2019-08-24 17:24:45 +08:00
|
|
|
setting.InitDBConfig()
|
2019-04-07 08:25:14 +08:00
|
|
|
setting.NewLogServices(true)
|
2019-11-26 07:21:37 +08:00
|
|
|
return deferFn
|
2019-01-29 00:18:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func availableVersions() ([]string, error) {
|
|
|
|
migrationsDir, err := os.Open("integrations/migration-test")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer migrationsDir.Close()
|
2019-08-24 17:24:45 +08:00
|
|
|
versionRE, err := regexp.Compile("gitea-v(?P<version>.+)\\." + regexp.QuoteMeta(setting.Database.Type) + "\\.sql.gz")
|
2019-01-29 00:18:52 +08: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) {
|
2019-08-24 17:24:45 +08:00
|
|
|
filename := fmt.Sprintf("integrations/migration-test/gitea-v%s.%s.sql.gz", version, setting.Database.Type)
|
2019-01-29 00:18:52 +08: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()
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadAll(gr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-08-15 20:07:28 +08:00
|
|
|
return string(charset.RemoveBOMIfPresent(bytes)), nil
|
2019-01-29 00:18:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func restoreOldDB(t *testing.T, version string) bool {
|
|
|
|
data, err := readSQLFromFile(version)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if len(data) == 0 {
|
2019-08-24 17:24:45 +08:00
|
|
|
integrations.Printf("No db found to restore for %s version: %s\n", setting.Database.Type, version)
|
2019-01-29 00:18:52 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2019-08-24 17:24:45 +08:00
|
|
|
case setting.Database.UseSQLite3:
|
2020-08-12 04:05:34 +08:00
|
|
|
util.Remove(setting.Database.Path)
|
2019-08-24 17:24:45 +08:00
|
|
|
err := os.MkdirAll(path.Dir(setting.Database.Path), os.ModePerm)
|
2019-01-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-02-27 07:51:37 +08: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-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2019-08-24 17:24:45 +08:00
|
|
|
case setting.Database.UseMySQL:
|
2019-01-29 00:18:52 +08:00
|
|
|
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/",
|
2019-08-24 17:24:45 +08:00
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host))
|
2019-01-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
2019-08-24 17:24:45 +08:00
|
|
|
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", setting.Database.Name))
|
2019-01-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-08-24 17:24:45 +08:00
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", setting.Database.Name))
|
2019-01-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?multiStatements=true",
|
2019-08-24 17:24:45 +08:00
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name))
|
2019-01-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2019-08-24 17:24:45 +08:00
|
|
|
case setting.Database.UsePostgreSQL:
|
2019-01-29 00:18:52 +08:00
|
|
|
db, err := sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/?sslmode=%s",
|
2019-08-24 17:24:45 +08:00
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.SSLMode))
|
2019-01-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
2019-08-24 17:24:45 +08:00
|
|
|
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", setting.Database.Name))
|
2019-01-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-08-24 17:24:45 +08:00
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", setting.Database.Name))
|
2019-01-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2020-01-20 23:45:14 +08:00
|
|
|
// Check if we need to setup a specific schema
|
|
|
|
if len(setting.Database.Schema) != 0 {
|
|
|
|
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))
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-01-29 00:18:52 +08:00
|
|
|
db, err = sql.Open("postgres", fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s",
|
2019-08-24 17:24:45 +08:00
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name, setting.Database.SSLMode))
|
2019-01-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2019-08-24 17:24:45 +08:00
|
|
|
case setting.Database.UseMSSQL:
|
|
|
|
host, port := setting.ParseMSSQLHostPort(setting.Database.Host)
|
2019-01-29 00:18:52 +08:00
|
|
|
db, err := sql.Open("mssql", fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;",
|
2019-08-24 17:24:45 +08:00
|
|
|
host, port, "master", setting.Database.User, setting.Database.Passwd))
|
2019-01-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
2019-07-26 12:10:20 +08:00
|
|
|
_, err = db.Exec("DROP DATABASE IF EXISTS [gitea]")
|
2019-01-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-05-06 07:42:29 +08:00
|
|
|
statements := strings.Split(data, "\nGO\n")
|
|
|
|
for _, statement := range statements {
|
2019-07-26 12:10:20 +08: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 17:24:45 +08:00
|
|
|
host, port, dbname, setting.Database.User, setting.Database.Passwd))
|
2019-07-26 12:10:20 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
}
|
2019-05-06 07:42:29 +08:00
|
|
|
_, err = db.Exec(statement)
|
|
|
|
assert.NoError(t, err, "Failure whilst running: %s\nError: %v", statement, err)
|
|
|
|
}
|
2019-01-29 00:18:52 +08:00
|
|
|
db.Close()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func wrappedMigrate(x *xorm.Engine) error {
|
|
|
|
currentEngine = x
|
|
|
|
return migrations.Migrate(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doMigrationTest(t *testing.T, version string) {
|
2019-11-26 07:21:37 +08:00
|
|
|
defer integrations.PrintCurrentTest(t)()
|
2019-08-24 17:24:45 +08:00
|
|
|
integrations.Printf("Performing migration test for %s version: %s\n", setting.Database.Type, version)
|
2019-01-29 00:18:52 +08:00
|
|
|
if !restoreOldDB(t, version) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setting.NewXORMLogService(false)
|
|
|
|
err := models.SetEngine()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-12-15 17:51:28 +08:00
|
|
|
err = models.NewEngine(context.Background(), wrappedMigrate)
|
2019-01-29 00:18:52 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
currentEngine.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrations(t *testing.T) {
|
2019-11-26 07:21:37 +08:00
|
|
|
defer initMigrationTest(t)()
|
2019-01-29 00:18:52 +08:00
|
|
|
|
2019-08-24 17:24:45 +08:00
|
|
|
dialect := setting.Database.Type
|
2019-01-29 00:18:52 +08:00
|
|
|
versions, err := availableVersions()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
if len(versions) == 0 {
|
2019-04-07 08:25:14 +08:00
|
|
|
integrations.Printf("No old database versions available to migration test for %s\n", dialect)
|
2019-01-29 00:18:52 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-07 08:25:14 +08:00
|
|
|
integrations.Printf("Preparing to test %d migrations for %s\n", len(versions), dialect)
|
2019-01-29 00:18:52 +08:00
|
|
|
for _, version := range versions {
|
2019-04-07 08:25:14 +08:00
|
|
|
t.Run(fmt.Sprintf("Migrate-%s-%s", dialect, version), func(t *testing.T) {
|
|
|
|
doMigrationTest(t, version)
|
|
|
|
})
|
|
|
|
|
2019-01-29 00:18:52 +08:00
|
|
|
}
|
|
|
|
}
|