Skip to content

Commit

Permalink
Merge #91756
Browse files Browse the repository at this point in the history
91756: upgrades: fix idempotence issue in system table role id migrations r=ajwerner,rafiss a=andyyang890

This patch fixes an idempotence issue in the role id migrations for
the system.users and system.role_options tables.

Fixes #91449

Release note: None

Co-authored-by: Andy Yang <[email protected]>
  • Loading branch information
craig[bot] and andyyang890 committed Nov 16, 2022
2 parents aa73d5e + b8ca64f commit 6594b56
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 32 deletions.
21 changes: 5 additions & 16 deletions pkg/upgrade/upgrades/role_options_table_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ package upgrades
import (
"context"
"fmt"
"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"
Expand Down Expand Up @@ -46,7 +44,7 @@ func alterSystemRoleOptionsAddUserIDColumnWithIndex(
name: "add-system-role-options-user-id-column",
schemaList: []string{"user_id"},
query: addUserIDColumnToRoleOptions,
schemaExistsFn: hasColumn,
schemaExistsFn: columnExists,
},
{
name: "alter-system-role-options-add-index",
Expand Down Expand Up @@ -101,19 +99,10 @@ func setSystemRoleOptionsUserIDColumnNotNull(
ctx context.Context, cs clusterversion.ClusterVersion, d upgrade.TenantDeps, _ *jobs.Job,
) error {
op := operation{
name: "alter-system-role-options-user-id-column-not-null",
schemaList: []string{"user_id"},
query: updateUserIDColumnRoleOptionsSetNotNull,
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
},
name: "alter-system-role-options-user-id-column-not-null",
schemaList: []string{"user_id"},
query: updateUserIDColumnRoleOptionsSetNotNull,
schemaExistsFn: columnExistsAndIsNotNull,
}
return migrateTable(ctx, cs, d, op, keys.RoleOptionsTableID, systemschema.RoleOptionsTable)
}
21 changes: 5 additions & 16 deletions pkg/upgrade/upgrades/system_users_role_id_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ package upgrades
import (
"context"
"fmt"
"strings"

"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descidgen"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
Expand Down Expand Up @@ -61,7 +59,7 @@ func alterSystemUsersAddUserIDColumnWithIndex(
name: "add-system-users-user-id-column",
schemaList: []string{"user_id"},
query: addUserIDColumn,
schemaExistsFn: hasColumn,
schemaExistsFn: columnExists,
},
{
name: "alter-system-users-add-index",
Expand Down Expand Up @@ -156,19 +154,10 @@ func setUserIDNotNull(
ctx context.Context, cs clusterversion.ClusterVersion, d upgrade.TenantDeps, _ *jobs.Job,
) error {
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
},
name: "alter-system-users-user-id-column-not-null",
schemaList: []string{"user_id"},
query: updateUserIDColumnSetNotNull,
schemaExistsFn: columnExistsAndIsNotNull,
}
return migrateTable(ctx, cs, d, op, keys.UsersTableID, systemschema.UsersTable)
}

0 comments on commit 6594b56

Please sign in to comment.