summaryrefslogtreecommitdiffstats
path: root/models/db/engine_test.go
blob: 230ee3f2b184dc8257e37f82d3c5854fe0684840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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"
	"github.com/stretchr/testify/require"
	"xorm.io/xorm"
)

func TestDumpDatabase(t *testing.T) {
	require.NoError(t, unittest.PrepareTestDatabase())

	dir := t.TempDir()

	type Version struct {
		ID      int64 `xorm:"pk autoincr"`
		Version int64
	}
	require.NoError(t, db.GetEngine(db.DefaultContext).Sync(new(Version)))

	for _, dbType := range setting.SupportedDatabaseTypes {
		require.NoError(t, db.DumpDatabase(filepath.Join(dir, dbType+".sql"), dbType))
	}
}

func TestDeleteOrphanedObjects(t *testing.T) {
	require.NoError(t, unittest.PrepareTestDatabase())

	countBefore, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})
	require.NoError(t, err)

	_, err = db.GetEngine(db.DefaultContext).Insert(&issues_model.PullRequest{IssueID: 1000}, &issues_model.PullRequest{IssueID: 1001}, &issues_model.PullRequest{IssueID: 1003})
	require.NoError(t, err)

	orphaned, err := db.CountOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
	require.NoError(t, err)
	assert.EqualValues(t, 3, orphaned)

	err = db.DeleteOrphanedObjects(db.DefaultContext, "pull_request", "issue", "pull_request.issue_id=issue.id")
	require.NoError(t, err)

	countAfter, err := db.GetEngine(db.DefaultContext).Count(&issues_model.PullRequest{})
	require.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", log.INFO)
	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", log.INFO)
	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)
}