2019-01-28 09:18:52 -07:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-01-28 09:18:52 -07:00
|
|
|
|
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/gzip"
|
2019-12-15 02:51:28 -07:00
|
|
|
"context"
|
2019-01-28 09:18:52 -07:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2021-09-21 23:38:34 -06:00
|
|
|
"io"
|
2019-01-28 09:18:52 -07:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-04-14 07:53:34 -06:00
|
|
|
"path/filepath"
|
2019-01-28 09:18:52 -07:00
|
|
|
"regexp"
|
|
|
|
"sort"
|
2019-05-05 17:42:29 -06:00
|
|
|
"strings"
|
2019-01-28 09:18:52 -07:00
|
|
|
"testing"
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2019-01-28 09:18:52 -07:00
|
|
|
"code.gitea.io/gitea/models/migrations"
|
2022-11-02 02:54:36 -06:00
|
|
|
migrate_base "code.gitea.io/gitea/models/migrations/base"
|
2022-04-01 10:34:57 -06:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2019-04-17 10:06:35 -06:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2019-08-15 06:07:28 -06:00
|
|
|
"code.gitea.io/gitea/modules/charset"
|
2021-06-26 05:28:55 -06:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-21 16:35:11 -06:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-01-28 09:18:52 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-21 16:35:11 -06:00
|
|
|
"code.gitea.io/gitea/modules/testlogger"
|
2020-08-11 14:05:34 -06:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-09-02 13:18:23 -06:00
|
|
|
"code.gitea.io/gitea/tests"
|
2019-01-28 09:18:52 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-10-17 03:26:49 -06:00
|
|
|
"xorm.io/xorm"
|
2019-01-28 09:18:52 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var currentEngine *xorm.Engine
|
|
|
|
|
2019-11-25 16:21:37 -07:00
|
|
|
func initMigrationTest(t *testing.T) func() {
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-21 16:35:11 -06:00
|
|
|
log.RegisterEventWriter("test", testlogger.NewTestLoggerWriter)
|
|
|
|
|
2022-09-02 13:18:23 -06:00
|
|
|
deferFn := tests.PrintCurrentTest(t, 2)
|
2019-04-17 10:06:35 -06:00
|
|
|
giteaRoot := base.SetupGiteaRoot()
|
2019-01-28 09:18:52 -07:00
|
|
|
if giteaRoot == "" {
|
2022-09-02 13:18:23 -06:00
|
|
|
tests.Printf("Environment variable $GITEA_ROOT not set\n")
|
2019-01-28 09:18:52 -07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
setting.AppPath = path.Join(giteaRoot, "gitea")
|
|
|
|
if _, err := os.Stat(setting.AppPath); err != nil {
|
2022-09-02 13:18:23 -06:00
|
|
|
tests.Printf("Could not find gitea binary at %s\n", setting.AppPath)
|
2019-01-28 09:18:52 -07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
giteaConf := os.Getenv("GITEA_CONF")
|
|
|
|
if giteaConf == "" {
|
2022-09-02 13:18:23 -06:00
|
|
|
tests.Printf("Environment variable $GITEA_CONF not set\n")
|
2019-01-28 09:18:52 -07:00
|
|
|
os.Exit(1)
|
|
|
|
} else if !path.IsAbs(giteaConf) {
|
|
|
|
setting.CustomConf = path.Join(giteaRoot, giteaConf)
|
|
|
|
} else {
|
|
|
|
setting.CustomConf = giteaConf
|
|
|
|
}
|
|
|
|
|
2023-05-03 21:55:35 -06:00
|
|
|
unittest.InitSettings()
|
2020-04-14 07:53:34 -06:00
|
|
|
|
|
|
|
assert.True(t, len(setting.RepoRootPath) != 0)
|
2024-11-14 12:28:46 -07:00
|
|
|
assert.NoError(t, unittest.SyncDirs(path.Join(filepath.Dir(setting.AppPath), "tests/gitea-repositories-meta"), setting.RepoRootPath))
|
2022-08-08 21:22:24 -06:00
|
|
|
assert.NoError(t, git.InitFull(context.Background()))
|
2023-02-19 09:12:01 -07:00
|
|
|
setting.LoadDBSetting()
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-21 16:35:11 -06:00
|
|
|
setting.InitLoggersForTest()
|
2019-11-25 16:21:37 -07:00
|
|
|
return deferFn
|
2019-01-28 09:18:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func availableVersions() ([]string, error) {
|
2022-09-02 13:18:23 -06:00
|
|
|
migrationsDir, err := os.Open("tests/integration/migration-test")
|
2019-01-28 09:18:52 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer migrationsDir.Close()
|
2023-03-07 03:51:06 -07:00
|
|
|
versionRE, err := regexp.Compile("gitea-v(?P<version>.+)\\." + regexp.QuoteMeta(setting.Database.Type.String()) + "\\.sql.gz")
|
2019-01-28 09:18:52 -07: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 13:18:23 -06:00
|
|
|
filename := fmt.Sprintf("tests/integration/migration-test/gitea-v%s.%s.sql.gz", version, setting.Database.Type)
|
2019-01-28 09:18:52 -07: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-21 23:38:34 -06:00
|
|
|
bytes, err := io.ReadAll(gr)
|
2019-01-28 09:18:52 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-01-27 11:02:51 -07:00
|
|
|
return string(charset.MaybeRemoveBOM(bytes, charset.ConvertOpts{})), nil
|
2019-01-28 09:18:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func restoreOldDB(t *testing.T, version string) bool {
|
|
|
|
data, err := readSQLFromFile(version)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if len(data) == 0 {
|
2022-09-02 13:18:23 -06:00
|
|
|
tests.Printf("No db found to restore for %s version: %s\n", setting.Database.Type, version)
|
2019-01-28 09:18:52 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2023-03-07 03:51:06 -07:00
|
|
|
case setting.Database.Type.IsSQLite3():
|
2020-08-11 14:05:34 -06:00
|
|
|
util.Remove(setting.Database.Path)
|
2019-08-24 03:24:45 -06:00
|
|
|
err := os.MkdirAll(path.Dir(setting.Database.Path), os.ModePerm)
|
2019-01-28 09:18:52 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-02-26 16:51:37 -07: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 09:18:52 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2023-03-07 03:51:06 -07:00
|
|
|
case setting.Database.Type.IsMySQL():
|
2019-01-28 09:18:52 -07:00
|
|
|
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/",
|
2019-08-24 03:24:45 -06:00
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host))
|
2019-01-28 09:18:52 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
2019-08-24 03:24:45 -06:00
|
|
|
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", setting.Database.Name))
|
2019-01-28 09:18:52 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-08-24 03:24:45 -06:00
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", setting.Database.Name))
|
2019-01-28 09:18:52 -07:00
|
|
|
assert.NoError(t, err)
|
2020-09-17 19:36:14 -06:00
|
|
|
db.Close()
|
2019-01-28 09:18:52 -07:00
|
|
|
|
|
|
|
db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?multiStatements=true",
|
2019-08-24 03:24:45 -06:00
|
|
|
setting.Database.User, setting.Database.Passwd, setting.Database.Host, setting.Database.Name))
|
2019-01-28 09:18:52 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2023-03-07 03:51:06 -07:00
|
|
|
case setting.Database.Type.IsPostgreSQL():
|
2022-06-03 00:14:09 -06: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 09:18:52 -07:00
|
|
|
defer db.Close()
|
|
|
|
|
2019-08-24 03:24:45 -06:00
|
|
|
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", setting.Database.Name))
|
2019-01-28 09:18:52 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-08-24 03:24:45 -06:00
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", setting.Database.Name))
|
2019-01-28 09:18:52 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2020-01-20 08:45:14 -07:00
|
|
|
// Check if we need to setup a specific schema
|
|
|
|
if len(setting.Database.Schema) != 0 {
|
2022-06-03 00:14:09 -06: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 08:45:14 -07:00
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return false
|
|
|
|
}
|
2020-09-17 19:36:14 -06:00
|
|
|
defer db.Close()
|
|
|
|
|
2020-01-20 08:45:14 -07: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 00:14:09 -06: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 09:18:52 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
_, err = db.Exec(data)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
db.Close()
|
|
|
|
|
2023-03-07 03:51:06 -07:00
|
|
|
case setting.Database.Type.IsMSSQL():
|
2019-08-24 03:24:45 -06:00
|
|
|
host, port := setting.ParseMSSQLHostPort(setting.Database.Host)
|
2019-01-28 09:18:52 -07:00
|
|
|
db, err := sql.Open("mssql", fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;",
|
2019-08-24 03:24:45 -06:00
|
|
|
host, port, "master", setting.Database.User, setting.Database.Passwd))
|
2019-01-28 09:18:52 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
2019-07-25 22:10:20 -06:00
|
|
|
_, err = db.Exec("DROP DATABASE IF EXISTS [gitea]")
|
2019-01-28 09:18:52 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-05-05 17:42:29 -06:00
|
|
|
statements := strings.Split(data, "\nGO\n")
|
|
|
|
for _, statement := range statements {
|
2019-07-25 22:10:20 -06: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 03:24:45 -06:00
|
|
|
host, port, dbname, setting.Database.User, setting.Database.Passwd))
|
2019-07-25 22:10:20 -06:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
}
|
2019-05-05 17:42:29 -06:00
|
|
|
_, err = db.Exec(statement)
|
|
|
|
assert.NoError(t, err, "Failure whilst running: %s\nError: %v", statement, err)
|
|
|
|
}
|
2019-01-28 09:18:52 -07: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 13:18:23 -06:00
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
tests.Printf("Performing migration test for %s version: %s\n", setting.Database.Type, version)
|
2019-01-28 09:18:52 -07:00
|
|
|
if !restoreOldDB(t, version) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-21 16:35:11 -06:00
|
|
|
setting.InitSQLLoggersForCli(log.INFO)
|
2019-01-28 09:18:52 -07:00
|
|
|
|
2021-10-30 08:32:11 -06:00
|
|
|
err := db.InitEngineWithMigration(context.Background(), wrappedMigrate)
|
2019-01-28 09:18:52 -07:00
|
|
|
assert.NoError(t, err)
|
|
|
|
currentEngine.Close()
|
2020-09-06 15:52:01 -06:00
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
beans, _ := db.NamesToBean()
|
2020-09-06 15:52:01 -06:00
|
|
|
|
2021-10-30 08:32:11 -06:00
|
|
|
err = db.InitEngineWithMigration(context.Background(), func(x *xorm.Engine) error {
|
2020-09-06 15:52:01 -06:00
|
|
|
currentEngine = x
|
2022-11-02 02:54:36 -06:00
|
|
|
return migrate_base.RecreateTables(beans...)(x)
|
2020-09-06 15:52:01 -06: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 08:32:11 -06:00
|
|
|
err = db.InitEngineWithMigration(context.Background(), func(x *xorm.Engine) error {
|
2020-09-06 15:52:01 -06:00
|
|
|
currentEngine = x
|
2022-11-02 02:54:36 -06:00
|
|
|
return migrate_base.RecreateTables(beans...)(x)
|
2020-09-06 15:52:01 -06:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
currentEngine.Close()
|
2019-01-28 09:18:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMigrations(t *testing.T) {
|
2019-11-25 16:21:37 -07:00
|
|
|
defer initMigrationTest(t)()
|
2019-01-28 09:18:52 -07:00
|
|
|
|
2019-08-24 03:24:45 -06:00
|
|
|
dialect := setting.Database.Type
|
2019-01-28 09:18:52 -07:00
|
|
|
versions, err := availableVersions()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
if len(versions) == 0 {
|
2022-09-02 13:18:23 -06:00
|
|
|
tests.Printf("No old database versions available to migration test for %s\n", dialect)
|
2019-01-28 09:18:52 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 13:18:23 -06:00
|
|
|
tests.Printf("Preparing to test %d migrations for %s\n", len(versions), dialect)
|
2019-01-28 09:18:52 -07:00
|
|
|
for _, version := range versions {
|
2019-04-06 18:25:14 -06:00
|
|
|
t.Run(fmt.Sprintf("Migrate-%s-%s", dialect, version), func(t *testing.T) {
|
|
|
|
doMigrationTest(t, version)
|
|
|
|
})
|
2019-01-28 09:18:52 -07:00
|
|
|
}
|
|
|
|
}
|