-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
system_users_role_id_migration.go
128 lines (111 loc) · 4.07 KB
/
system_users_role_id_migration.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
// 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"
"strings"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/upgrade"
)
// The migration is broken down into four steps.
// 1. Adding the "user_id" column as NULL.
// 2. Update the column to use a default expression that uses a sequence.
// 3. Manually backfill the id column.
// 4. Set the column to not null.
// We need to do this because we cannot add a column with a nextval call as a
// default expression.
// It results in: unimplemented: cannot evaluate scalar expressions
// containing sequence operations in this context.
const addUserIDColumn = `
ALTER TABLE system.users
ADD COLUMN IF NOT EXISTS "user_id" OID CREATE FAMILY "fam_4_user_id"
`
const updateUserIDColumnDefaultExpr = `
ALTER TABLE system.users ALTER COLUMN "user_id" SET DEFAULT oid(nextval(48:::OID))
`
const updateUserIDColumnSetNotNull = `
ALTER TABLE system.users ALTER COLUMN "user_id" SET NOT NULL
`
func alterSystemUsersAddUserIDColumn(
ctx context.Context, cs clusterversion.ClusterVersion, d upgrade.TenantDeps, _ *jobs.Job,
) error {
for _, op := range []operation{
{
name: "add-system-users-user-id-column",
schemaList: []string{"user_id"},
query: addUserIDColumn,
schemaExistsFn: hasColumn,
},
{
name: "alter-system-users-user-id-default-expression",
schemaList: []string{"user_id"},
query: updateUserIDColumnDefaultExpr,
schemaExistsFn: func(storedTable, _ catalog.TableDescriptor, colName string) (bool, error) {
storedCol, err := storedTable.FindColumnWithName(tree.Name(colName))
if err != nil {
if strings.Contains(err.Error(), "does not exist") {
return false, nil
}
return false, err
}
return storedCol.HasDefault(), nil
},
},
} {
if err := migrateTable(ctx, cs, d, op, keys.UsersTableID, systemschema.UsersTable); err != nil {
return err
}
}
const upsertRootStmt = `
UPSERT INTO system.users (username, "hashedPassword", "isRole", "user_id") VALUES ('root', '', false, 1)
`
const upsertAdminStmt = `
UPSERT INTO system.users (username, "hashedPassword", "isRole", "user_id") VALUES ('admin', '', true, 2)
`
const updateSequenceValues = `
UPDATE system.users SET user_id = nextval(48:::OID) WHERE user_id IS NULL`
_, err := d.InternalExecutor.ExecEx(ctx, "upsert-root-user-in-role-id-migration", nil,
sessiondata.NodeUserSessionDataOverride, upsertRootStmt)
if err != nil {
return err
}
_, err = d.InternalExecutor.ExecEx(ctx, "upsert-admin-role-in-role-id-migration", nil,
sessiondata.NodeUserSessionDataOverride, upsertAdminStmt)
if err != nil {
return err
}
_, err = d.InternalExecutor.ExecEx(ctx, "update user ids", nil,
sessiondata.NodeUserSessionDataOverride, updateSequenceValues)
if err != nil {
return err
}
op := operation{
name: "alter-system-users-user-id-column-not-null",
schemaList: []string{"user_id"},
query: updateUserIDColumnSetNotNull,
schemaExistsFn: func(storedTable, _ catalog.TableDescriptor, colName string) (bool, error) {
storedCol, err := storedTable.FindColumnWithName(tree.Name(colName))
if err != nil {
if strings.Contains(err.Error(), "does not exist") {
return false, nil
}
return false, err
}
return !storedCol.IsNullable(), nil
},
}
return migrateTable(ctx, cs, d, op, keys.UsersTableID, systemschema.UsersTable)
}