2022-06-15 09:02:00 +02:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-06-15 09:02:00 +02:00
|
|
|
|
|
|
|
package db_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2023-08-18 04:39:23 +02:00
|
|
|
"time"
|
2022-06-15 09:02:00 +02:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2023-08-18 04:39:23 +02:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-06-15 09:02:00 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2023-08-18 04:39:23 +02:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2022-06-15 09:02:00 +02:00
|
|
|
|
2022-12-23 12:35:43 +01:00
|
|
|
_ "code.gitea.io/gitea/cmd" // for TestPrimaryKeys
|
|
|
|
|
2022-06-15 09:02:00 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:42:06 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-08-18 04:39:23 +02:00
|
|
|
"xorm.io/xorm"
|
2022-06-15 09:02:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDumpDatabase(t *testing.T) {
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-15 09:02:00 +02:00
|
|
|
|
2022-09-04 17:14:53 +02:00
|
|
|
dir := t.TempDir()
|
2022-06-15 09:02:00 +02:00
|
|
|
|
|
|
|
type Version struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
Version int64
|
|
|
|
}
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, db.GetEngine(db.DefaultContext).Sync(new(Version)))
|
2022-06-15 09:02:00 +02:00
|
|
|
|
|
|
|
for _, dbType := range setting.SupportedDatabaseTypes {
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, db.DumpDatabase(filepath.Join(dir, dbType+".sql"), dbType))
|
2022-06-15 09:02:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteOrphanedObjects(t *testing.T) {
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-15 09:02:00 +02:00
|
|
|
|
|
|
|
countBefore, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-15 09:02:00 +02:00
|
|
|
|
|
|
|
_, err = db.GetEngine(db.DefaultContext).Insert(&issues_model.PullRequest{IssueID: 1000}, &issues_model.PullRequest{IssueID: 1001}, &issues_model.PullRequest{IssueID: 1003})
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-15 09:02:00 +02:00
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
orphaned, err := db.CountOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-15 09:02:00 +02:00
|
|
|
assert.EqualValues(t, 3, orphaned)
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
err = db.DeleteOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-15 09:02:00 +02:00
|
|
|
|
|
|
|
countAfter, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})
|
2024-07-30 21:42:06 +02:00
|
|
|
require.NoError(t, err)
|
2022-06-15 09:02:00 +02:00
|
|
|
assert.EqualValues(t, countBefore, countAfter)
|
|
|
|
}
|
2022-12-23 12:35:43 +01:00
|
|
|
|
|
|
|
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",
|
2023-08-08 23:55:25 +02:00
|
|
|
"forgejo_sem_ver": "seriously dude",
|
2022-12-23 12:35:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-18 04:39:23 +02:00
|
|
|
|
|
|
|
func TestSlowQuery(t *testing.T) {
|
2024-01-25 11:35:29 +01:00
|
|
|
lc, cleanup := test.NewLogChecker("slow-query", log.INFO)
|
2023-08-18 04:39:23 +02:00
|
|
|
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)
|
|
|
|
}
|
2024-01-14 00:01:39 +01:00
|
|
|
|
|
|
|
func TestErrorQuery(t *testing.T) {
|
2024-01-25 11:35:29 +01:00
|
|
|
lc, cleanup := test.NewLogChecker("error-query", log.INFO)
|
2024-01-14 00:01:39 +01:00
|
|
|
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)
|
|
|
|
}
|