Skip to content

Commit

Permalink
Merge pull request #91973 from andyyang890/backport22.2-91756
Browse files Browse the repository at this point in the history
release-22.2: upgrades: fix idempotence issue in system table role id migrations
  • Loading branch information
andyyang890 authored Nov 16, 2022
2 parents 8acc409 + 99204f7 commit b49f392
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 b49f392

Please sign in to comment.