d1cb590c78
- When the database returns an error about the SQL query, the error is logged but not the SQL query and arguments, which is just as valuable as the vague deeply hidden documented error that the database returns. It's possible to log the SQL query by logging **all** SQL queries. For bigger instances such as Codeberg, this is not a viable option. - Adds a new hook, enabled by default, to log SQL queries with their arguments and the error returned by the database when the database returns an error. - This likely needs some fine tuning in the future to decide when to enable this, as the error is already logged and if people have the `[database].LOG_SQL` option enabled, the SQL would be logged twice. But given that it's an rare occurence for SQL queries to error, it's fine to leave that as-is. - Ref: https://codeberg.org/forgejo/forgejo/issues/1998 (cherry picked from commit866229bc32
) (cherry picked from commit96dd3e87cf
) (cherry picked from commite165510317
) (cherry picked from commit1638e2b3f5
)
153 lines
4.1 KiB
Go
153 lines
4.1 KiB
Go
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package db_test
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/test"
|
|
|
|
_ "code.gitea.io/gitea/cmd" // for TestPrimaryKeys
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
func TestDumpDatabase(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
dir := t.TempDir()
|
|
|
|
type Version struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
Version int64
|
|
}
|
|
assert.NoError(t, db.GetEngine(db.DefaultContext).Sync(new(Version)))
|
|
|
|
for _, dbType := range setting.SupportedDatabaseTypes {
|
|
assert.NoError(t, db.DumpDatabase(filepath.Join(dir, dbType+".sql"), dbType))
|
|
}
|
|
}
|
|
|
|
func TestDeleteOrphanedObjects(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
countBefore, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})
|
|
assert.NoError(t, err)
|
|
|
|
_, err = db.GetEngine(db.DefaultContext).Insert(&issues_model.PullRequest{IssueID: 1000}, &issues_model.PullRequest{IssueID: 1001}, &issues_model.PullRequest{IssueID: 1003})
|
|
assert.NoError(t, err)
|
|
|
|
orphaned, err := db.CountOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 3, orphaned)
|
|
|
|
err = db.DeleteOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
|
|
assert.NoError(t, err)
|
|
|
|
countAfter, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, countBefore, countAfter)
|
|
}
|
|
|
|
func TestPrimaryKeys(t *testing.T) {
|
|
// Some dbs require that all tables have primary keys, see
|
|
// https://github.com/go-gitea/gitea/issues/21086
|
|
// https://github.com/go-gitea/gitea/issues/16802
|
|
// To avoid creating tables without primary key again, this test will check them.
|
|
// Import "code.gitea.io/gitea/cmd" to make sure each db.RegisterModel in init functions has been called.
|
|
|
|
beans, err := db.NamesToBean()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
whitelist := map[string]string{
|
|
"the_table_name_to_skip_checking": "Write a note here to explain why",
|
|
"forgejo_sem_ver": "seriously dude",
|
|
}
|
|
|
|
for _, bean := range beans {
|
|
table, err := db.TableInfo(bean)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if why, ok := whitelist[table.Name]; ok {
|
|
t.Logf("ignore %q because %q", table.Name, why)
|
|
continue
|
|
}
|
|
if len(table.PrimaryKeys) == 0 {
|
|
t.Errorf("table %q has no primary key", table.Name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSlowQuery(t *testing.T) {
|
|
lc, cleanup := test.NewLogChecker("slow-query")
|
|
lc.StopMark("[Slow SQL Query]")
|
|
defer cleanup()
|
|
|
|
e := db.GetEngine(db.DefaultContext)
|
|
engine, ok := e.(*xorm.Engine)
|
|
assert.True(t, ok)
|
|
|
|
// It's not possible to clean this up with XORM, but it's luckily not harmful
|
|
// to leave around.
|
|
engine.AddHook(&db.SlowQueryHook{
|
|
Treshold: time.Second * 10,
|
|
Logger: log.GetLogger("slow-query"),
|
|
})
|
|
|
|
// NOOP query.
|
|
e.Exec("SELECT 1 WHERE false;")
|
|
|
|
_, stopped := lc.Check(100 * time.Millisecond)
|
|
assert.False(t, stopped)
|
|
|
|
engine.AddHook(&db.SlowQueryHook{
|
|
Treshold: 0, // Every query should be logged.
|
|
Logger: log.GetLogger("slow-query"),
|
|
})
|
|
|
|
// NOOP query.
|
|
e.Exec("SELECT 1 WHERE false;")
|
|
|
|
_, stopped = lc.Check(100 * time.Millisecond)
|
|
assert.True(t, stopped)
|
|
}
|
|
|
|
func TestErrorQuery(t *testing.T) {
|
|
lc, cleanup := test.NewLogChecker("error-query")
|
|
lc.StopMark("[Error SQL Query]")
|
|
defer cleanup()
|
|
|
|
e := db.GetEngine(db.DefaultContext)
|
|
engine, ok := e.(*xorm.Engine)
|
|
assert.True(t, ok)
|
|
|
|
// It's not possible to clean this up with XORM, but it's luckily not harmful
|
|
// to leave around.
|
|
engine.AddHook(&db.ErrorQueryHook{
|
|
Logger: log.GetLogger("error-query"),
|
|
})
|
|
|
|
// Valid query.
|
|
e.Exec("SELECT 1 WHERE false;")
|
|
|
|
_, stopped := lc.Check(100 * time.Millisecond)
|
|
assert.False(t, stopped)
|
|
|
|
// Table doesn't exist.
|
|
e.Exec("SELECT column FROM table;")
|
|
|
|
_, stopped = lc.Check(100 * time.Millisecond)
|
|
assert.True(t, stopped)
|
|
}
|