-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathschema_desc.go
307 lines (263 loc) · 9.56 KB
/
schema_desc.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright 2020 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 schemadesc
import (
"fmt"
"strings"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catconstants"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
)
var _ catalog.SchemaDescriptor = (*immutable)(nil)
var _ catalog.SchemaDescriptor = (*Mutable)(nil)
var _ catalog.MutableDescriptor = (*Mutable)(nil)
// immutable wraps a Schema descriptor and provides methods on it.
type immutable struct {
descpb.SchemaDescriptor
// isUncommittedVersion is set to true if this descriptor was created from
// a copy of a Mutable with an uncommitted version.
isUncommittedVersion bool
}
func (desc *immutable) SchemaKind() catalog.ResolvedSchemaKind {
return catalog.SchemaUserDefined
}
// SafeMessage makes immutable a SafeMessager.
func (desc *immutable) SafeMessage() string {
return formatSafeMessage("schemadesc.immutable", desc)
}
// SafeMessage makes Mutable a SafeMessager.
func (desc *Mutable) SafeMessage() string {
return formatSafeMessage("schemadesc.Mutable", desc)
}
func formatSafeMessage(typeName string, desc catalog.SchemaDescriptor) string {
var buf redact.StringBuilder
buf.Printf(typeName + ": {")
catalog.FormatSafeDescriptorProperties(&buf, desc)
buf.Printf("}")
return buf.String()
}
// Mutable is a mutable reference to a SchemaDescriptor.
//
// Note: Today this isn't actually ever mutated but rather exists for a future
// where we anticipate having a mutable copy of Schema descriptors. There's a
// large amount of space to question this `Mutable|ImmutableCopy` version of each
// descriptor type. Maybe it makes no sense but we're running with it for the
// moment. This is an intermediate state on the road to descriptors being
// handled outside of the catalog entirely as interfaces.
type Mutable struct {
immutable
ClusterVersion *immutable
// changed represents whether or not the descriptor was changed
// after RunPostDeserializationChanges.
changed bool
}
var _ redact.SafeMessager = (*immutable)(nil)
// SetDrainingNames implements the MutableDescriptor interface.
func (desc *Mutable) SetDrainingNames(names []descpb.NameInfo) {
desc.DrainingNames = names
}
// GetParentSchemaID implements the Descriptor interface.
func (desc *immutable) GetParentSchemaID() descpb.ID {
return keys.RootNamespaceID
}
// IsUncommittedVersion implements the Descriptor interface.
func (desc *immutable) IsUncommittedVersion() bool {
return desc.isUncommittedVersion
}
// GetAuditMode implements the DescriptorProto interface.
func (desc *immutable) GetAuditMode() descpb.TableDescriptor_AuditMode {
return descpb.TableDescriptor_DISABLED
}
// DescriptorType implements the DescriptorProto interface.
func (desc *immutable) DescriptorType() catalog.DescriptorType {
return catalog.Schema
}
// SchemaDesc implements the Descriptor interface.
func (desc *immutable) SchemaDesc() *descpb.SchemaDescriptor {
return &desc.SchemaDescriptor
}
// Public implements the Descriptor interface.
func (desc *immutable) Public() bool {
return desc.State == descpb.DescriptorState_PUBLIC
}
// Adding implements the Descriptor interface.
func (desc *immutable) Adding() bool {
return false
}
// Offline implements the Descriptor interface.
func (desc *immutable) Offline() bool {
return desc.State == descpb.DescriptorState_OFFLINE
}
// Dropped implements the Descriptor interface.
func (desc *immutable) Dropped() bool {
return desc.State == descpb.DescriptorState_DROP
}
// DescriptorProto wraps a SchemaDescriptor in a Descriptor.
func (desc *immutable) DescriptorProto() *descpb.Descriptor {
return &descpb.Descriptor{
Union: &descpb.Descriptor_Schema{
Schema: &desc.SchemaDescriptor,
},
}
}
// ValidateSelf implements the catalog.Descriptor interface.
func (desc *immutable) ValidateSelf(vea catalog.ValidationErrorAccumulator) {
// Validate local properties of the descriptor.
vea.Report(catalog.ValidateName(desc.GetName(), "descriptor"))
if desc.GetID() == descpb.InvalidID {
vea.Report(fmt.Errorf("invalid schema ID %d", desc.GetID()))
}
// Validate the privilege descriptor.
vea.Report(catprivilege.Validate(*desc.Privileges, desc, privilege.Schema))
}
// GetReferencedDescIDs returns the IDs of all descriptors referenced by
// this descriptor, including itself.
func (desc *immutable) GetReferencedDescIDs() (catalog.DescriptorIDSet, error) {
return catalog.MakeDescriptorIDSet(desc.GetID(), desc.GetParentID()), nil
}
// ValidateCrossReferences implements the catalog.Descriptor interface.
func (desc *immutable) ValidateCrossReferences(
vea catalog.ValidationErrorAccumulator, vdg catalog.ValidationDescGetter,
) {
// Check schema parent reference.
db, err := vdg.GetDatabaseDescriptor(desc.GetParentID())
if err != nil {
vea.Report(err)
return
}
// Check that parent has correct entry in schemas mapping.
isInDBSchemas := false
_ = db.ForEachSchemaInfo(func(id descpb.ID, name string, isDropped bool) error {
if id == desc.GetID() {
if isDropped {
if name == desc.GetName() {
vea.Report(errors.AssertionFailedf("present in parent database [%d] schemas mapping but marked as dropped",
desc.GetParentID()))
}
return nil
}
if name != desc.GetName() {
vea.Report(errors.AssertionFailedf("present in parent database [%d] schemas mapping but under name %q",
desc.GetParentID(), errors.Safe(name)))
return nil
}
isInDBSchemas = true
return nil
}
if name == desc.GetName() && !isDropped {
vea.Report(errors.AssertionFailedf("present in parent database [%d] schemas mapping but name maps to other schema [%d]",
desc.GetParentID(), id))
}
return nil
})
if !isInDBSchemas {
vea.Report(errors.AssertionFailedf("not present in parent database [%d] schemas mapping",
desc.GetParentID()))
}
}
// ValidateTxnCommit implements the catalog.Descriptor interface.
func (desc *immutable) ValidateTxnCommit(
_ catalog.ValidationErrorAccumulator, _ catalog.ValidationDescGetter,
) {
// No-op.
}
// MaybeIncrementVersion implements the MutableDescriptor interface.
func (desc *Mutable) MaybeIncrementVersion() {
// Already incremented, no-op.
if desc.ClusterVersion == nil || desc.Version == desc.ClusterVersion.Version+1 {
return
}
desc.Version++
desc.ModificationTime = hlc.Timestamp{}
}
// OriginalName implements the MutableDescriptor interface.
func (desc *Mutable) OriginalName() string {
if desc.ClusterVersion == nil {
return ""
}
return desc.ClusterVersion.Name
}
// OriginalID implements the MutableDescriptor interface.
func (desc *Mutable) OriginalID() descpb.ID {
if desc.ClusterVersion == nil {
return descpb.InvalidID
}
return desc.ClusterVersion.ID
}
// OriginalVersion implements the MutableDescriptor interface.
func (desc *Mutable) OriginalVersion() descpb.DescriptorVersion {
if desc.ClusterVersion == nil {
return 0
}
return desc.ClusterVersion.Version
}
// ImmutableCopy implements the MutableDescriptor interface.
func (desc *Mutable) ImmutableCopy() catalog.Descriptor {
imm := NewBuilder(desc.SchemaDesc()).BuildImmutable()
imm.(*immutable).isUncommittedVersion = desc.IsUncommittedVersion()
return imm
}
// IsNew implements the MutableDescriptor interface.
func (desc *Mutable) IsNew() bool {
return desc.ClusterVersion == nil
}
// SetPublic implements the MutableDescriptor interface.
func (desc *Mutable) SetPublic() {
desc.State = descpb.DescriptorState_PUBLIC
desc.OfflineReason = ""
}
// SetDropped implements the MutableDescriptor interface.
func (desc *Mutable) SetDropped() {
desc.State = descpb.DescriptorState_DROP
desc.OfflineReason = ""
}
// SetOffline implements the MutableDescriptor interface.
func (desc *Mutable) SetOffline(reason string) {
desc.State = descpb.DescriptorState_OFFLINE
desc.OfflineReason = reason
}
// SetName sets the name of the schema. It handles installing a draining name
// for the old name of the descriptor.
func (desc *Mutable) SetName(name string) {
desc.DrainingNames = append(desc.DrainingNames, descpb.NameInfo{
ParentID: desc.ParentID,
ParentSchemaID: keys.RootNamespaceID,
Name: desc.Name,
})
desc.Name = name
}
// IsUncommittedVersion implements the Descriptor interface.
func (desc *Mutable) IsUncommittedVersion() bool {
return desc.IsNew() || desc.GetVersion() != desc.ClusterVersion.GetVersion()
}
// HasPostDeserializationChanges returns if the MutableDescriptor was changed after running
// RunPostDeserializationChanges.
func (desc *Mutable) HasPostDeserializationChanges() bool {
return desc.changed
}
// IsSchemaNameValid returns whether the input name is valid for a user defined
// schema.
func IsSchemaNameValid(name string) error {
// Schemas starting with "pg_" are not allowed.
if strings.HasPrefix(name, catconstants.PgSchemaPrefix) {
err := pgerror.Newf(pgcode.ReservedName, "unacceptable schema name %q", name)
err = errors.WithDetail(err, `The prefix "pg_" is reserved for system schemas.`)
return err
}
return nil
}