-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
alter_database.go
66 lines (54 loc) · 1.92 KB
/
alter_database.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
// 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 sql
import (
"context"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc"
"github.com/cockroachdb/cockroach/pkg/sql/roleoption"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
)
type alterDatabaseOwnerNode struct {
n *tree.AlterDatabaseOwner
desc *dbdesc.Mutable
}
// AlterDatabaseOwner transforms a tree.AlterDatabaseOwner into a plan node.
func (p *planner) AlterDatabaseOwner(
ctx context.Context, n *tree.AlterDatabaseOwner,
) (planNode, error) {
dbDesc, err := p.ResolveMutableDatabaseDescriptor(ctx, n.Name.String(), true)
if err != nil {
return nil, err
}
newOwner := string(n.Owner)
if err := p.checkCanAlterToNewOwner(ctx, dbDesc, newOwner); err != nil {
return nil, err
}
// To alter the owner, the user also has to have CREATEDB privilege.
if err := p.CheckRoleOption(ctx, roleoption.CREATEDB); err != nil {
return nil, err
}
return &alterDatabaseOwnerNode{n: n, desc: dbDesc}, nil
}
func (n *alterDatabaseOwnerNode) startExec(params runParams) error {
privs := n.desc.GetPrivileges()
// If the owner we want to set to is the current owner, do a no-op.
if string(n.n.Owner) == privs.Owner {
return nil
}
n.desc.GetPrivileges().SetOwner(string(n.n.Owner))
return params.p.writeNonDropDatabaseChange(
params.ctx,
n.desc,
tree.AsStringWithFQNames(n.n, params.Ann()),
)
}
func (n *alterDatabaseOwnerNode) Next(runParams) (bool, error) { return false, nil }
func (n *alterDatabaseOwnerNode) Values() tree.Datums { return tree.Datums{} }
func (n *alterDatabaseOwnerNode) Close(context.Context) {}