-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathinitialization.go
159 lines (133 loc) · 4.87 KB
/
initialization.go
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
155
156
157
158
159
// Copyright 2022 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package enginetest
import (
"fmt"
"strings"
"sync/atomic"
"testing"
"github.com/stretchr/testify/require"
sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/enginetest/scriptgen/setup"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/analyzer"
"github.com/dolthub/go-mysql-server/sql/information_schema"
)
func NewContext(harness Harness) *sql.Context {
return newContextSetup(harness.NewContext())
}
func NewContextWithClient(harness ClientHarness, client sql.Client) *sql.Context {
return newContextSetup(harness.NewContextWithClient(client))
}
var pid uint64
func newContextSetup(ctx *sql.Context) *sql.Context {
// Select a current database if there isn't one yet
if ctx.GetCurrentDatabase() == "" {
ctx.SetCurrentDatabase("mydb")
}
ctx.ApplyOpts(sql.WithPid(atomic.AddUint64(&pid, 1)))
// We don't want to show any external procedures in our engine tests, so we exclude them
_ = ctx.SetSessionVariable(ctx, "show_external_procedures", false)
return ctx
}
func NewSession(harness Harness) *sql.Context {
th, ok := harness.(TransactionHarness)
if !ok {
panic("Cannot use NewSession except on a TransactionHarness")
}
ctx := th.NewSession()
currentDB := ctx.GetCurrentDatabase()
if currentDB == "" {
currentDB = "mydb"
ctx.SetCurrentDatabase(currentDB)
}
ctx.ApplyOpts(sql.WithPid(atomic.AddUint64(&pid, 1)))
return ctx
}
// NewBaseSession returns a new BaseSession compatible with these tests. Most tests will work with any session
// implementation, but for full compatibility use a session based on this one.
func NewBaseSession() *sql.BaseSession {
return sql.NewBaseSessionWithClientServer("address", sql.Client{Address: "localhost", User: "root"}, 1)
}
// NewEngineWithProvider returns a new engine with the specified provider
func NewEngineWithProvider(_ *testing.T, harness Harness, provider sql.DatabaseProvider) *sqle.Engine {
analyzer := analyzer.NewDefault(provider)
// All tests will run with all privileges on the built-in root account
analyzer.Catalog.MySQLDb.AddRootAccount()
// Almost no tests require an information schema that can be updated, but test setup makes it difficult to not
// provide everywhere
analyzer.Catalog.InfoSchema = information_schema.NewInformationSchemaDatabase()
engine := sqle.New(analyzer, new(sqle.Config))
if idh, ok := harness.(IndexDriverHarness); ok {
idh.InitializeIndexDriver(engine.Analyzer.Catalog.AllDatabases(NewContext(harness)))
}
analyzer.Runner = engine
return engine
}
// NewEngine creates an engine and sets it up for testing using harness, provider, and setup data given.
func NewEngine(t *testing.T, harness Harness, dbProvider sql.DatabaseProvider, setupData []setup.SetupScript, statsProvider sql.StatsProvider) (*sqle.Engine, error) {
e := NewEngineWithProvider(t, harness, dbProvider)
e.Analyzer.Catalog.StatsProvider = statsProvider
ctx := NewContext(harness)
var supportsIndexes bool
if ih, ok := harness.(IndexHarness); ok && ih.SupportsNativeIndexCreation() {
supportsIndexes = true
}
// TODO: remove ths, make it explicit everywhere
if len(setupData) == 0 {
setupData = setup.MydbData
}
return RunSetupScripts(ctx, e, setupData, supportsIndexes)
}
// RunSetupScripts runs the given setup scripts on the given engine, returning any error
func RunSetupScripts(ctx *sql.Context, e *sqle.Engine, scripts []setup.SetupScript, createIndexes bool) (*sqle.Engine, error) {
for i := range scripts {
for _, s := range scripts[i] {
if !createIndexes {
if strings.Contains("create index", s) {
continue
}
}
// ctx.GetLogger().Warnf("running query %s\n", s)
ctx := ctx.WithQuery(s)
_, iter, _, err := e.Query(ctx, s)
if err != nil {
return nil, err
}
_, err = sql.RowIterToRows(ctx, iter)
if err != nil {
return nil, err
}
}
}
return e, nil
}
func MustQuery(ctx *sql.Context, e QueryEngine, q string) (sql.Schema, []sql.Row) {
sch, iter, _, err := e.Query(ctx, q)
if err != nil {
panic(fmt.Sprintf("err running query %s: %s", q, err))
}
rows, err := sql.RowIterToRows(ctx, iter)
if err != nil {
panic(fmt.Sprintf("err running query %s: %s", q, err))
}
return sch, rows
}
func mustNewEngine(t *testing.T, h Harness) QueryEngine {
e, err := h.NewEngine(t)
if err != nil {
require.NoError(t, err)
}
return e
}