-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
helpers_test.go
198 lines (185 loc) · 5.87 KB
/
helpers_test.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package upgrades
import (
"context"
gosql "database/sql"
"testing"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
HasColumn = hasColumn
HasIndex = hasIndex
DoesNotHaveIndex = doesNotHaveIndex
HasColumnFamily = hasColumnFamily
CreateSystemTable = createSystemTable
)
type Schema struct {
// Schema name.
Name string
// Function that validates the schema.
ValidationFn func(catalog.TableDescriptor, catalog.TableDescriptor, string) (bool, error)
}
// Upgrade runs cluster upgrade by changing the 'version' cluster setting.
func Upgrade(
t *testing.T, sqlDB *gosql.DB, key clusterversion.Key, done chan struct{}, expectError bool,
) {
UpgradeToVersion(t, sqlDB, clusterversion.ByKey(key), done, expectError)
}
func UpgradeToVersion(
t *testing.T, sqlDB *gosql.DB, v roachpb.Version, done chan struct{}, expectError bool,
) {
defer func() {
if done != nil {
done <- struct{}{}
}
}()
_, err := sqlDB.Exec(`SET CLUSTER SETTING version = $1`,
v.String())
if expectError {
assert.Error(t, err)
return
}
assert.NoError(t, err)
}
// InjectLegacyTable overwrites the existing table descriptor with the previous table descriptor.
func InjectLegacyTable(
ctx context.Context,
t *testing.T,
s serverutils.TestServerInterface,
table catalog.TableDescriptor,
getDeprecatedDescriptor func() *descpb.TableDescriptor,
) {
err := s.InternalDB().(descs.DB).DescsTxn(ctx, func(
ctx context.Context, txn descs.Txn,
) error {
deprecatedDesc := getDeprecatedDescriptor()
var tab *tabledesc.Mutable
switch id := table.GetID(); id {
// If the table descriptor does not have a valid ID, it must be a system
// table with a dynamically-allocated ID.
case descpb.InvalidID:
var err error
tab, err = txn.Descriptors().MutableByName(txn.KV()).Table(ctx,
systemschema.SystemDB, schemadesc.GetPublicSchema(), table.GetName())
if err != nil {
return err
}
deprecatedDesc.ID = tab.GetID()
default:
var err error
tab, err = txn.Descriptors().MutableByID(txn.KV()).Table(ctx, id)
if err != nil {
return err
}
}
builder := tabledesc.NewBuilder(deprecatedDesc)
if err := builder.RunPostDeserializationChanges(); err != nil {
return err
}
tab.TableDescriptor = builder.BuildCreatedMutableTable().TableDescriptor
tab.Version = tab.ClusterVersion().Version + 1
return txn.Descriptors().WriteDesc(ctx, false /* kvTrace */, tab, txn.KV())
})
require.NoError(t, err)
}
// ValidateSchemaExists validates whether the schema changes of the system table exist or not.
func ValidateSchemaExists(
ctx context.Context,
t *testing.T,
s serverutils.TestServerInterface,
sqlDB *gosql.DB,
storedTableID descpb.ID,
expectedTable catalog.TableDescriptor,
stmts []string,
schemas []Schema,
expectExists bool,
) {
// First validate by reading the columns and the index.
for _, stmt := range stmts {
_, err := sqlDB.Exec(stmt)
if expectExists {
require.NoErrorf(
t, err, "expected schema to exist, but unable to query it, using statement: %s", stmt,
)
} else {
require.Errorf(
t, err, "expected schema to not exist, but queried it successfully, using statement: %s", stmt,
)
}
}
// Manually verify the table descriptor.
storedTable := GetTable(ctx, t, s, storedTableID)
str := "not have"
if expectExists {
str = "have"
}
for _, schema := range schemas {
updated, err := schema.ValidationFn(storedTable, expectedTable, schema.Name)
require.NoError(t, err)
require.Equal(t, expectExists, updated,
"expected table to %s %s (name=%s)", str, schema, schema.Name)
}
}
// GetTable returns the system table descriptor, reading it from storage.
func GetTable(
ctx context.Context, t *testing.T, s serverutils.TestServerInterface, tableID descpb.ID,
) catalog.TableDescriptor {
var table catalog.TableDescriptor
// Retrieve the table.
err := s.InternalDB().(descs.DB).DescsTxn(ctx, func(
ctx context.Context, txn descs.Txn,
) (err error) {
table, err = txn.Descriptors().ByID(txn.KV()).WithoutNonPublic().Get().Table(ctx, tableID)
return err
})
require.NoError(t, err)
return table
}
// WaitForJobStatement is exported so that it can be detected by a testing knob.
const WaitForJobStatement = waitForJobStatement
// ExecForCountInTxns allows statements to be repeatedly run on a database
// in transactions of a specified size.
func ExecForCountInTxns(
ctx context.Context,
t *testing.T,
db *gosql.DB,
count int,
txCount int,
fn func(txRunner *sqlutils.SQLRunner, i int),
) {
tx, err := db.BeginTx(ctx, nil /* opts */)
require.NoError(t, err)
txRunner := sqlutils.MakeSQLRunner(tx)
// Group statements into transactions of txCount runs to speed up creation.
for i := 0; i < count; i++ {
if i != 0 && i%txCount == 0 {
err := tx.Commit()
require.NoError(t, err)
tx, err = db.BeginTx(ctx, nil /* opts */)
require.NoError(t, err)
txRunner = sqlutils.MakeSQLRunner(tx)
}
fn(txRunner, i)
}
err = tx.Commit()
require.NoError(t, err)
}