Skip to content

Commit

Permalink
upgrades: fix idempotence issue in system table role id migrations
Browse files Browse the repository at this point in the history
This patch fixes an idempotence issue in the role id migrations for
the system.users and system.role_options tables.

Release note: None
  • Loading branch information
andyyang890 committed Nov 15, 2022
1 parent 03f6062 commit b8ca64f
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 b8ca64f

Please sign in to comment.