From 23391d0ca3c86a81d9c3180e2f4755da54c38580 Mon Sep 17 00:00:00 2001 From: Andrew Werner Date: Thu, 27 Jan 2022 02:15:48 -0500 Subject: [PATCH 1/5] catpb,descpb,scpb,*: move privileges to catpb, break scpb dep on descpb This is a mechanical change to break the dependency on descpb from scpb. It requires moving all of the privilege symbols down to catpb. There's another minor tweak here to use `gogoproto.equal_all` at the file level in `scpb/elements.proto`. Release note: None --- pkg/ccl/backupccl/restore_job.go | 11 +- pkg/ccl/importccl/BUILD.bazel | 1 + pkg/ccl/importccl/import_table_creation.go | 4 +- pkg/ccl/importccl/read_import_mysql.go | 3 +- pkg/ccl/importccl/read_import_pgdump.go | 3 +- pkg/ccl/importccl/testutils_test.go | 3 +- pkg/ccl/streamingccl/streamclient/BUILD.bazel | 1 + .../streamclient/random_stream_client.go | 3 +- pkg/migration/migrations/BUILD.bazel | 1 + ...ter_statement_diagnostics_requests_test.go | 3 +- ..._table_protected_timestamp_records_test.go | 3 +- .../alter_table_statistics_avg_size_test.go | 3 +- .../migrations/descriptor_utils_test.go | 3 +- pkg/sql/alter_default_privileges.go | 22 +-- pkg/sql/catalog/catpb/BUILD.bazel | 37 +++- .../{descpb => catpb}/default_privilege.go | 2 +- .../catalog/{descpb => catpb}/privilege.go | 15 +- .../catalog/{descpb => catpb}/privilege.proto | 2 +- .../{descpb => catpb}/privilege_test.go | 165 +++++++++--------- .../privilegedescversion_string.go | 2 +- pkg/sql/catalog/catprivilege/BUILD.bazel | 2 + .../catalog/catprivilege/default_privilege.go | 81 ++++----- .../catprivilege/default_privilege_test.go | 131 +++++++------- pkg/sql/catalog/catprivilege/fix.go | 17 +- pkg/sql/catalog/catprivilege/fix_test.go | 33 ++-- pkg/sql/catalog/catprivilege/validate.go | 10 +- pkg/sql/catalog/dbdesc/BUILD.bazel | 1 + pkg/sql/catalog/dbdesc/database_desc.go | 6 +- .../catalog/dbdesc/database_desc_builder.go | 16 +- pkg/sql/catalog/dbdesc/database_test.go | 41 +++-- pkg/sql/catalog/descidgen/BUILD.bazel | 24 --- pkg/sql/catalog/descpb/BUILD.bazel | 25 +-- pkg/sql/catalog/descpb/structured.go | 2 +- pkg/sql/catalog/descpb/structured.proto | 2 +- pkg/sql/catalog/descriptor.go | 8 +- pkg/sql/catalog/descs/BUILD.bazel | 1 + pkg/sql/catalog/descs/collection_test.go | 3 +- pkg/sql/catalog/internal/validate/BUILD.bazel | 14 -- pkg/sql/catalog/schemadesc/BUILD.bazel | 2 + .../catalog/schemadesc/public_schema_desc.go | 5 +- pkg/sql/catalog/schemadesc/schema_desc.go | 7 +- .../catalog/schemadesc/schema_desc_test.go | 3 +- .../schemadesc/synthetic_schema_desc.go | 5 +- .../schemadesc/temporary_schema_desc.go | 5 +- .../catalog/schemadesc/virtual_schema_desc.go | 5 +- pkg/sql/catalog/systemschema/system.go | 8 +- pkg/sql/catalog/tabledesc/safe_format_test.go | 2 +- pkg/sql/catalog/tabledesc/structured_test.go | 17 +- pkg/sql/catalog/tabledesc/table.go | 2 +- pkg/sql/catalog/tabledesc/validate_test.go | 18 +- pkg/sql/catalog/typedesc/BUILD.bazel | 1 + pkg/sql/catalog/typedesc/safe_format_test.go | 5 +- .../typedesc/table_implicit_record_type.go | 8 +- pkg/sql/catalog/typedesc/type_desc_test.go | 7 +- pkg/sql/colexec/colexecspan/BUILD.bazel | 1 + .../colexecspan/span_assembler_test.go | 3 +- pkg/sql/crdb_internal.go | 11 +- pkg/sql/create_schema.go | 5 +- pkg/sql/create_sequence.go | 3 +- pkg/sql/create_table.go | 7 +- pkg/sql/create_view.go | 3 +- pkg/sql/descriptor.go | 2 +- pkg/sql/doctor/BUILD.bazel | 1 + pkg/sql/doctor/doctor_test.go | 3 +- pkg/sql/drop_role.go | 10 +- pkg/sql/execinfrapb/BUILD.bazel | 1 + .../execinfrapb/flow_diagram_external_test.go | 3 +- pkg/sql/grant_revoke.go | 7 +- pkg/sql/pg_catalog.go | 2 +- pkg/sql/repair.go | 9 +- pkg/sql/reparent_database.go | 3 +- pkg/sql/schemachanger/scpb/BUILD.bazel | 3 +- pkg/sql/schemachanger/scpb/elements.proto | 56 ++---- pkg/sql/schemachanger/scpb/scpb.proto | 2 + pkg/sql/sem/catid/ids.go | 3 + pkg/sql/table_test.go | 9 +- pkg/sql/tests/BUILD.bazel | 1 + pkg/sql/tests/system_table_test.go | 3 +- pkg/sql/testutils.go | 6 +- pkg/sql/virtual_schema.go | 14 +- pkg/sql/zone_config_test.go | 3 +- 81 files changed, 493 insertions(+), 485 deletions(-) rename pkg/sql/catalog/{descpb => catpb}/default_privilege.go (99%) rename pkg/sql/catalog/{descpb => catpb}/privilege.go (98%) rename pkg/sql/catalog/{descpb => catpb}/privilege.proto (99%) rename pkg/sql/catalog/{descpb => catpb}/privilege_test.go (75%) rename pkg/sql/catalog/{descpb => catpb}/privilegedescversion_string.go (98%) diff --git a/pkg/ccl/backupccl/restore_job.go b/pkg/ccl/backupccl/restore_job.go index 1bdece8f0444..1dd8f508e631 100644 --- a/pkg/ccl/backupccl/restore_job.go +++ b/pkg/ccl/backupccl/restore_job.go @@ -31,6 +31,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descidgen" @@ -1323,7 +1324,7 @@ func remapPublicSchemas( db.AddSchemaToDatabase(tree.PublicSchema, descpb.DatabaseDescriptor_SchemaInfo{ID: id}) // Every database must be initialized with the public schema. // Create the SchemaDescriptor. - publicSchemaPrivileges := descpb.NewPublicSchemaPrivilegeDescriptor() + publicSchemaPrivileges := catpb.NewPublicSchemaPrivilegeDescriptor() publicSchemaDesc := schemadesc.NewBuilder(&descpb.SchemaDescriptor{ ParentID: db.GetID(), Name: tree.PublicSchema, @@ -2409,7 +2410,7 @@ func getRestoringPrivileges( user security.SQLUsername, wroteDBs map[descpb.ID]catalog.DatabaseDescriptor, descCoverage tree.DescriptorCoverage, -) (updatedPrivileges *descpb.PrivilegeDescriptor, err error) { +) (updatedPrivileges *catpb.PrivilegeDescriptor, err error) { switch desc := desc.(type) { case catalog.TableDescriptor: return getRestorePrivilegesForTableOrSchema( @@ -2438,14 +2439,14 @@ func getRestoringPrivileges( // the restoring cluster match the ones that were on the cluster that was // backed up. So we wipe the privileges on the type. if descCoverage == tree.RequestedDescriptors { - updatedPrivileges = descpb.NewBasePrivilegeDescriptor(user) + updatedPrivileges = catpb.NewBasePrivilegeDescriptor(user) } case catalog.DatabaseDescriptor: // If the restore is not a cluster restore we cannot know that the users on // the restoring cluster match the ones that were on the cluster that was // backed up. So we wipe the privileges on the database. if descCoverage == tree.RequestedDescriptors { - updatedPrivileges = descpb.NewBaseDatabasePrivilegeDescriptor(user) + updatedPrivileges = catpb.NewBaseDatabasePrivilegeDescriptor(user) } } return updatedPrivileges, nil @@ -2460,7 +2461,7 @@ func getRestorePrivilegesForTableOrSchema( wroteDBs map[descpb.ID]catalog.DatabaseDescriptor, descCoverage tree.DescriptorCoverage, privilegeType privilege.ObjectType, -) (updatedPrivileges *descpb.PrivilegeDescriptor, err error) { +) (updatedPrivileges *catpb.PrivilegeDescriptor, err error) { if wrote, ok := wroteDBs[desc.GetParentID()]; ok { // If we're creating a new database in this restore, the privileges of the // table and schema should be that of the parent DB. diff --git a/pkg/ccl/importccl/BUILD.bazel b/pkg/ccl/importccl/BUILD.bazel index b3dbac9e041e..88daecd9e5b9 100644 --- a/pkg/ccl/importccl/BUILD.bazel +++ b/pkg/ccl/importccl/BUILD.bazel @@ -175,6 +175,7 @@ go_test( "//pkg/sql/catalog", "//pkg/sql/catalog/bootstrap", "//pkg/sql/catalog/catformat", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/colinfo", "//pkg/sql/catalog/dbdesc", "//pkg/sql/catalog/descpb", diff --git a/pkg/ccl/importccl/import_table_creation.go b/pkg/ccl/importccl/import_table_creation.go index fc6a240c23a3..f38622ef0bd2 100644 --- a/pkg/ccl/importccl/import_table_creation.go +++ b/pkg/ccl/importccl/import_table_creation.go @@ -75,7 +75,7 @@ func MakeTestingSimpleTableDescriptor( ID: parentSchemaID, Version: 1, ParentID: parentID, - Privileges: descpb.NewPrivilegeDescriptor( + Privileges: catpb.NewPrivilegeDescriptor( security.PublicRoleName(), privilege.SchemaPrivileges, privilege.List{}, @@ -185,7 +185,7 @@ func MakeSimpleTableDescriptor( tableID, nil, /* regionConfig */ hlc.Timestamp{WallTime: walltime}, - descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), + catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), affected, semaCtx, &evalCtx, diff --git a/pkg/ccl/importccl/read_import_mysql.go b/pkg/ccl/importccl/read_import_mysql.go index 9d5039264703..a880b08c7fb5 100644 --- a/pkg/ccl/importccl/read_import_mysql.go +++ b/pkg/ccl/importccl/read_import_mysql.go @@ -22,6 +22,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" @@ -427,7 +428,7 @@ func mysqlTableToCockroach( seqVals[id] = startingValue } var err error - privilegeDesc := descpb.NewBasePrivilegeDescriptor(owner) + privilegeDesc := catpb.NewBasePrivilegeDescriptor(owner) seqDesc, err = sql.NewSequenceTableDesc( ctx, nil, /* planner */ diff --git a/pkg/ccl/importccl/read_import_pgdump.go b/pkg/ccl/importccl/read_import_pgdump.go index 1d72135ef650..6ba21ede9fea 100644 --- a/pkg/ccl/importccl/read_import_pgdump.go +++ b/pkg/ccl/importccl/read_import_pgdump.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descidgen" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" @@ -329,7 +330,7 @@ func createPostgresSequences( schema.GetID(), id, hlc.Timestamp{WallTime: walltime}, - descpb.NewBasePrivilegeDescriptor(owner), + catpb.NewBasePrivilegeDescriptor(owner), tree.PersistencePermanent, // If this is multi-region, this will get added by WriteDescriptors. false, /* isMultiRegion */ diff --git a/pkg/ccl/importccl/testutils_test.go b/pkg/ccl/importccl/testutils_test.go index da37310a8c95..69beb047803b 100644 --- a/pkg/ccl/importccl/testutils_test.go +++ b/pkg/ccl/importccl/testutils_test.go @@ -24,6 +24,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" "github.com/cockroachdb/cockroach/pkg/sql/parser" @@ -57,7 +58,7 @@ func descForTable( name := parsed[0].AST.(*tree.CreateSequence).Name.String() ts := hlc.Timestamp{WallTime: nanos} - priv := descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) + priv := catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) desc, err := sql.NewSequenceTableDesc( ctx, nil, /* planner */ diff --git a/pkg/ccl/streamingccl/streamclient/BUILD.bazel b/pkg/ccl/streamingccl/streamclient/BUILD.bazel index a0a346401726..a7c5a8ae1688 100644 --- a/pkg/ccl/streamingccl/streamclient/BUILD.bazel +++ b/pkg/ccl/streamingccl/streamclient/BUILD.bazel @@ -19,6 +19,7 @@ go_library( "//pkg/sql", "//pkg/sql/catalog", "//pkg/sql/catalog/catalogkeys", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/tabledesc", "//pkg/sql/rowenc", diff --git a/pkg/ccl/streamingccl/streamclient/random_stream_client.go b/pkg/ccl/streamingccl/streamclient/random_stream_client.go index 6568dc17d055..92e16d5690d6 100644 --- a/pkg/ccl/streamingccl/streamclient/random_stream_client.go +++ b/pkg/ccl/streamingccl/streamclient/random_stream_client.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" "github.com/cockroachdb/cockroach/pkg/sql/rowenc" @@ -273,7 +274,7 @@ func (m *randomStreamClient) getDescriptorAndNamespaceKVForTableID( IngestionDatabaseID, tableID, fmt.Sprintf(RandomStreamSchemaPlaceholder, tableName), - descpb.NewBasePrivilegeDescriptor(security.RootUserName()), + catpb.NewBasePrivilegeDescriptor(security.RootUserName()), ) if err != nil { return nil, nil, err diff --git a/pkg/migration/migrations/BUILD.bazel b/pkg/migration/migrations/BUILD.bazel index abf49abcec5b..1dd734a21fbc 100644 --- a/pkg/migration/migrations/BUILD.bazel +++ b/pkg/migration/migrations/BUILD.bazel @@ -99,6 +99,7 @@ go_test( "//pkg/sql", "//pkg/sql/catalog", "//pkg/sql/catalog/catalogkeys", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/descs", "//pkg/sql/catalog/desctestutils", diff --git a/pkg/migration/migrations/alter_statement_diagnostics_requests_test.go b/pkg/migration/migrations/alter_statement_diagnostics_requests_test.go index e6cee939c4bc..efb8f43a8c68 100644 --- a/pkg/migration/migrations/alter_statement_diagnostics_requests_test.go +++ b/pkg/migration/migrations/alter_statement_diagnostics_requests_test.go @@ -20,6 +20,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/migration/migrations" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/server" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" @@ -146,7 +147,7 @@ func getDeprecatedStmtDiagReqsDescriptor() *descpb.TableDescriptor { }, }, NextIndexID: 3, - Privileges: descpb.NewCustomSuperuserPrivilegeDescriptor(privilege.ReadWriteData, security.NodeUserName()), + Privileges: catpb.NewCustomSuperuserPrivilegeDescriptor(privilege.ReadWriteData, security.NodeUserName()), NextMutationID: 1, FormatVersion: 3, } diff --git a/pkg/migration/migrations/alter_table_protected_timestamp_records_test.go b/pkg/migration/migrations/alter_table_protected_timestamp_records_test.go index 01293f38da01..5a04b20af8f1 100644 --- a/pkg/migration/migrations/alter_table_protected_timestamp_records_test.go +++ b/pkg/migration/migrations/alter_table_protected_timestamp_records_test.go @@ -20,6 +20,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/migration/migrations" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/server" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" "github.com/cockroachdb/cockroach/pkg/sql/privilege" @@ -137,7 +138,7 @@ func getDeprecatedProtectedTimestampRecordsDescriptor() *descpb.TableDescriptor }, }, NextIndexID: 2, - Privileges: descpb.NewCustomSuperuserPrivilegeDescriptor(privilege.ReadWriteData, security.NodeUserName()), + Privileges: catpb.NewCustomSuperuserPrivilegeDescriptor(privilege.ReadWriteData, security.NodeUserName()), NextMutationID: 1, FormatVersion: 3, } diff --git a/pkg/migration/migrations/alter_table_statistics_avg_size_test.go b/pkg/migration/migrations/alter_table_statistics_avg_size_test.go index d879b8f6cbc1..89c88881a7b6 100644 --- a/pkg/migration/migrations/alter_table_statistics_avg_size_test.go +++ b/pkg/migration/migrations/alter_table_statistics_avg_size_test.go @@ -20,6 +20,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/migration/migrations" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/server" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" "github.com/cockroachdb/cockroach/pkg/sql/privilege" @@ -149,7 +150,7 @@ func getDeprecatedTableStatisticsDescriptor() *descpb.TableDescriptor { KeyColumnIDs: []descpb.ColumnID{1, 2}, }, NextIndexID: 2, - Privileges: descpb.NewCustomSuperuserPrivilegeDescriptor(privilege.ReadWriteData, security.NodeUserName()), + Privileges: catpb.NewCustomSuperuserPrivilegeDescriptor(privilege.ReadWriteData, security.NodeUserName()), NextMutationID: 1, FormatVersion: 3, } diff --git a/pkg/migration/migrations/descriptor_utils_test.go b/pkg/migration/migrations/descriptor_utils_test.go index 14ba90a12cc5..38b24f9e5715 100644 --- a/pkg/migration/migrations/descriptor_utils_test.go +++ b/pkg/migration/migrations/descriptor_utils_test.go @@ -19,6 +19,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/migration/migrations" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" "github.com/cockroachdb/cockroach/pkg/sql/privilege" @@ -66,7 +67,7 @@ func TestCreateSystemTable(t *testing.T) { KeyColumnIDs: []descpb.ColumnID{1}, }, NextIndexID: 2, - Privileges: descpb.NewCustomSuperuserPrivilegeDescriptor( + Privileges: catpb.NewCustomSuperuserPrivilegeDescriptor( privilege.ReadData, security.NodeUserName(), ), diff --git a/pkg/sql/alter_default_privileges.go b/pkg/sql/alter_default_privileges.go index e703d875fa48..59d3c9e485fa 100644 --- a/pkg/sql/alter_default_privileges.go +++ b/pkg/sql/alter_default_privileges.go @@ -15,9 +15,9 @@ import ( "github.com/cockroachdb/cockroach/pkg/clusterversion" "github.com/cockroachdb/cockroach/pkg/security" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" @@ -175,20 +175,20 @@ func (n *alterDefaultPrivilegesNode) alterDefaultPrivilegesForSchemas( var events []eventLogEntry for _, schemaDesc := range n.schemaDescs { if schemaDesc.GetDefaultPrivileges() == nil { - schemaDesc.SetDefaultPrivilegeDescriptor(catprivilege.MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_SCHEMA)) + schemaDesc.SetDefaultPrivilegeDescriptor(catprivilege.MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_SCHEMA)) } defaultPrivs := schemaDesc.GetMutableDefaultPrivilegeDescriptor() - var roles []descpb.DefaultPrivilegesRole + var roles []catpb.DefaultPrivilegesRole if n.n.ForAllRoles { - roles = append(roles, descpb.DefaultPrivilegesRole{ + roles = append(roles, catpb.DefaultPrivilegesRole{ ForAllRoles: true, }) } else { - roles = make([]descpb.DefaultPrivilegesRole, len(targetRoles)) + roles = make([]catpb.DefaultPrivilegesRole, len(targetRoles)) for i, role := range targetRoles { - roles[i] = descpb.DefaultPrivilegesRole{ + roles[i] = catpb.DefaultPrivilegesRole{ Role: role, } } @@ -276,20 +276,20 @@ func (n *alterDefaultPrivilegesNode) alterDefaultPrivilegesForDatabase( grantOption bool, ) error { if n.dbDesc.GetDefaultPrivileges() == nil { - n.dbDesc.SetDefaultPrivilegeDescriptor(catprivilege.MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE)) + n.dbDesc.SetDefaultPrivilegeDescriptor(catprivilege.MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_DATABASE)) } defaultPrivs := n.dbDesc.GetMutableDefaultPrivilegeDescriptor() - var roles []descpb.DefaultPrivilegesRole + var roles []catpb.DefaultPrivilegesRole if n.n.ForAllRoles { - roles = append(roles, descpb.DefaultPrivilegesRole{ + roles = append(roles, catpb.DefaultPrivilegesRole{ ForAllRoles: true, }) } else { - roles = make([]descpb.DefaultPrivilegesRole, len(targetRoles)) + roles = make([]catpb.DefaultPrivilegesRole, len(targetRoles)) for i, role := range targetRoles { - roles[i] = descpb.DefaultPrivilegesRole{ + roles[i] = catpb.DefaultPrivilegesRole{ Role: role, } } diff --git a/pkg/sql/catalog/catpb/BUILD.bazel b/pkg/sql/catalog/catpb/BUILD.bazel index dd9910b71a21..de55ed1192ca 100644 --- a/pkg/sql/catalog/catpb/BUILD.bazel +++ b/pkg/sql/catalog/catpb/BUILD.bazel @@ -1,10 +1,14 @@ load("@rules_proto//proto:defs.bzl", "proto_library") -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") +load("//build:STRINGER.bzl", "stringer") proto_library( name = "catpb_proto", - srcs = ["catalog.proto"], + srcs = [ + "catalog.proto", + "privilege.proto", + ], strip_import_prefix = "/pkg", visibility = ["//visibility:public"], deps = ["@com_github_gogo_protobuf//gogoproto:gogo_proto"], @@ -23,15 +27,44 @@ go_library( name = "catpb", srcs = [ "constraint.go", + "default_privilege.go", "doc.go", "multiregion.go", + "privilege.go", + ":gen-privilegedescversion-stringer", # keep ], embed = [":catpb_go_proto"], importpath = "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb", visibility = ["//visibility:public"], deps = [ + "//pkg/keys", + "//pkg/security", + "//pkg/sql/catalog/catconstants", + "//pkg/sql/privilege", + "//pkg/sql/sem/catid", "//pkg/sql/sem/tree", "@com_github_cockroachdb_errors//:errors", "@com_github_cockroachdb_redact//:redact", ], ) + +go_test( + name = "catpb_test", + srcs = ["privilege_test.go"], + deps = [ + ":catpb", + "//pkg/keys", + "//pkg/security", + "//pkg/sql/catalog/bootstrap", + "//pkg/sql/privilege", + "//pkg/sql/sem/catid", + "//pkg/testutils", + "//pkg/util/leaktest", + ], +) + +stringer( + name = "gen-privilegedescversion-stringer", + src = "privilege.go", + typ = "PrivilegeDescVersion", +) diff --git a/pkg/sql/catalog/descpb/default_privilege.go b/pkg/sql/catalog/catpb/default_privilege.go similarity index 99% rename from pkg/sql/catalog/descpb/default_privilege.go rename to pkg/sql/catalog/catpb/default_privilege.go index c3a309f8a885..7bdf6777afba 100644 --- a/pkg/sql/catalog/descpb/default_privilege.go +++ b/pkg/sql/catalog/catpb/default_privilege.go @@ -8,7 +8,7 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. -package descpb +package catpb import ( "sort" diff --git a/pkg/sql/catalog/descpb/privilege.go b/pkg/sql/catalog/catpb/privilege.go similarity index 98% rename from pkg/sql/catalog/descpb/privilege.go rename to pkg/sql/catalog/catpb/privilege.go index 3a7ba46df883..f98ea95d2818 100644 --- a/pkg/sql/catalog/descpb/privilege.go +++ b/pkg/sql/catalog/catpb/privilege.go @@ -8,7 +8,7 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. -package descpb +package catpb import ( "fmt" @@ -19,6 +19,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catconstants" "github.com/cockroachdb/cockroach/pkg/sql/privilege" + "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" "github.com/cockroachdb/errors" ) @@ -363,12 +364,12 @@ func (p *PrivilegeDescriptor) GrantPrivilegeToGrantOptions( // It requires the objectType to determine the superset of privileges allowed // for regular users. func (p PrivilegeDescriptor) ValidateSuperuserPrivileges( - parentID ID, + parentID catid.DescID, objectType privilege.ObjectType, objectName string, allowedSuperuserPrivileges privilege.List, ) error { - if parentID == InvalidID && objectType != privilege.Database { + if parentID == catid.InvalidDescID && objectType != privilege.Database { // Special case for virtual objects. return nil } @@ -402,7 +403,7 @@ func (p PrivilegeDescriptor) ValidateSuperuserPrivileges( // Validate returns an assertion error if the privilege descriptor is invalid. func (p PrivilegeDescriptor) Validate( - parentID ID, + parentID catid.DescID, objectType privilege.ObjectType, objectName string, allowedSuperuserPrivileges privilege.List, @@ -556,9 +557,11 @@ func (p *PrivilegeDescriptor) SetVersion(version PrivilegeDescVersion) { } // privilegeObject is a helper function for privilege errors. -func privilegeObject(parentID ID, objectType privilege.ObjectType, objectName string) string { +func privilegeObject( + parentID catid.DescID, objectType privilege.ObjectType, objectName string, +) string { if parentID == keys.SystemDatabaseID || - (parentID == InvalidID && objectName == catconstants.SystemDatabaseName) { + (parentID == catid.InvalidDescID && objectName == catconstants.SystemDatabaseName) { return fmt.Sprintf("system %s %q", objectType, objectName) } return fmt.Sprintf("%s %q", objectType, objectName) diff --git a/pkg/sql/catalog/descpb/privilege.proto b/pkg/sql/catalog/catpb/privilege.proto similarity index 99% rename from pkg/sql/catalog/descpb/privilege.proto rename to pkg/sql/catalog/catpb/privilege.proto index b9ece6a4686b..04315c1f1ffd 100644 --- a/pkg/sql/catalog/descpb/privilege.proto +++ b/pkg/sql/catalog/catpb/privilege.proto @@ -10,7 +10,7 @@ syntax = "proto2"; package cockroach.sql.sqlbase; -option go_package = "descpb"; +option go_package = "catpb"; import "gogoproto/gogo.proto"; diff --git a/pkg/sql/catalog/descpb/privilege_test.go b/pkg/sql/catalog/catpb/privilege_test.go similarity index 75% rename from pkg/sql/catalog/descpb/privilege_test.go rename to pkg/sql/catalog/catpb/privilege_test.go index dfb20cb2c780..a52f9d4e5df0 100644 --- a/pkg/sql/catalog/descpb/privilege_test.go +++ b/pkg/sql/catalog/catpb/privilege_test.go @@ -8,7 +8,7 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. -package descpb_test +package catpb_test import ( "reflect" @@ -17,15 +17,16 @@ import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog/bootstrap" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/privilege" + "github.com/cockroachdb/cockroach/pkg/sql/sem/catid" "github.com/cockroachdb/cockroach/pkg/testutils" "github.com/cockroachdb/cockroach/pkg/util/leaktest" ) func TestPrivilege(t *testing.T) { defer leaktest.AfterTest(t)() - descriptor := descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) + descriptor := catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) testUser := security.TestUserName() barUser := security.MakeSQLUsernameFromPreNormalizedString("bar") @@ -33,32 +34,32 @@ func TestPrivilege(t *testing.T) { testCases := []struct { grantee security.SQLUsername // User to grant/revoke privileges on. grant, revoke privilege.List - show []descpb.UserPrivilege + show []catpb.UserPrivilege objectType privilege.ObjectType }{ {security.SQLUsername{}, nil, nil, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {security.RootUserName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, }, privilege.Table, }, {security.RootUserName(), privilege.List{privilege.ALL}, nil, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {security.RootUserName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, }, privilege.Table, }, {security.RootUserName(), privilege.List{privilege.INSERT, privilege.DROP}, nil, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {security.RootUserName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, }, privilege.Table, }, {testUser, privilege.List{privilege.INSERT, privilege.DROP}, nil, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {security.RootUserName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {testUser, []privilege.Privilege{{Kind: privilege.DROP}, {Kind: privilege.INSERT}}}, @@ -66,7 +67,7 @@ func TestPrivilege(t *testing.T) { privilege.Table, }, {barUser, nil, privilege.List{privilege.INSERT, privilege.ALL}, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {security.RootUserName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {testUser, []privilege.Privilege{{Kind: privilege.DROP}, {Kind: privilege.INSERT}}}, @@ -74,7 +75,7 @@ func TestPrivilege(t *testing.T) { privilege.Table, }, {testUser, privilege.List{privilege.ALL}, nil, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {security.RootUserName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {testUser, []privilege.Privilege{{Kind: privilege.ALL}}}, @@ -82,7 +83,7 @@ func TestPrivilege(t *testing.T) { privilege.Table, }, {testUser, nil, privilege.List{privilege.SELECT, privilege.INSERT}, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {security.RootUserName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {testUser, []privilege.Privilege{ @@ -97,7 +98,7 @@ func TestPrivilege(t *testing.T) { privilege.Table, }, {testUser, nil, privilege.List{privilege.ALL}, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {security.RootUserName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, }, @@ -105,7 +106,7 @@ func TestPrivilege(t *testing.T) { }, // Validate checks that root still has ALL privileges, but we do not call it here. {security.RootUserName(), nil, privilege.List{privilege.ALL}, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, }, privilege.Table, @@ -113,7 +114,7 @@ func TestPrivilege(t *testing.T) { // Ensure revoking USAGE from a user with ALL privilege on a type // leaves the user with only GRANT privilege. {testUser, privilege.List{privilege.ALL}, privilege.List{privilege.USAGE}, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, {testUser, []privilege.Privilege{{Kind: privilege.GRANT}}}, }, @@ -123,7 +124,7 @@ func TestPrivilege(t *testing.T) { // leaves the user with no privileges. {testUser, privilege.List{privilege.ALL}, privilege.List{privilege.USAGE, privilege.GRANT}, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, }, privilege.Type, @@ -134,7 +135,7 @@ func TestPrivilege(t *testing.T) { privilege.List{privilege.ALL}, privilege.List{privilege.CREATE, privilege.DROP, privilege.GRANT, privilege.SELECT, privilege.INSERT, privilege.DELETE, privilege.UPDATE, privilege.ZONECONFIG}, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, }, privilege.Table, @@ -145,7 +146,7 @@ func TestPrivilege(t *testing.T) { privilege.List{privilege.ALL}, privilege.List{privilege.CONNECT, privilege.CREATE, privilege.DROP, privilege.GRANT, privilege.SELECT, privilege.INSERT, privilege.DELETE, privilege.UPDATE, privilege.ZONECONFIG}, - []descpb.UserPrivilege{ + []catpb.UserPrivilege{ {security.AdminRoleName(), []privilege.Privilege{{Kind: privilege.ALL, GrantOption: true}}}, }, privilege.Database, @@ -182,36 +183,36 @@ func TestCheckPrivilege(t *testing.T) { barUser := security.MakeSQLUsernameFromPreNormalizedString("bar") testCases := []struct { - pd *descpb.PrivilegeDescriptor + pd *catpb.PrivilegeDescriptor user security.SQLUsername priv privilege.Kind exp bool }{ - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), testUser, privilege.CREATE, true}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), barUser, privilege.CREATE, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), barUser, privilege.DROP, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), testUser, privilege.DROP, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{}, security.AdminRoleName()), testUser, privilege.CREATE, true}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), testUser, privilege.ALL, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{}, security.AdminRoleName()), testUser, privilege.ALL, true}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{}, privilege.List{}, security.AdminRoleName()), testUser, privilege.ALL, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{}, privilege.List{}, security.AdminRoleName()), testUser, privilege.CREATE, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.DROP}, privilege.List{}, + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.DROP}, privilege.List{}, security.AdminRoleName()), testUser, privilege.UPDATE, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.DROP}, privilege.List{}, + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.DROP}, privilege.List{}, security.AdminRoleName()), testUser, privilege.DROP, true}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.ALL}, privilege.List{}, + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.ALL}, privilege.List{}, security.AdminRoleName()), testUser, privilege.DROP, true}, } @@ -231,22 +232,22 @@ func TestAnyPrivilege(t *testing.T) { barUser := security.MakeSQLUsernameFromPreNormalizedString("bar") testCases := []struct { - pd *descpb.PrivilegeDescriptor + pd *catpb.PrivilegeDescriptor user security.SQLUsername exp bool }{ - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), testUser, true}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), barUser, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{}, security.AdminRoleName()), testUser, true}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{}, privilege.List{}, security.AdminRoleName()), testUser, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.DROP}, privilege.List{}, + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.DROP}, privilege.List{}, security.AdminRoleName()), testUser, true}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.DROP}, privilege.List{}, + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.DROP}, privilege.List{}, security.AdminRoleName()), barUser, false}, } @@ -265,10 +266,10 @@ func TestPrivilegeValidate(t *testing.T) { testUser := security.TestUserName() - descriptor := descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) + descriptor := catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) validate := func() error { - id := descpb.ID(bootstrap.TestingMinUserDescID()) - return descriptor.Validate(id, privilege.Table, "whatever", descpb.DefaultSuperuserPrivileges) + id := catid.DescID(bootstrap.TestingMinUserDescID()) + return descriptor.Validate(id, privilege.Table, "whatever", catpb.DefaultSuperuserPrivileges) } if err := validate(); err != nil { @@ -300,7 +301,7 @@ func TestPrivilegeValidate(t *testing.T) { func TestValidPrivilegesForObjects(t *testing.T) { defer leaktest.AfterTest(t)() - id := descpb.ID(bootstrap.TestingMinUserDescID()) + id := catid.DescID(bootstrap.TestingMinUserDescID()) testUser := security.TestUserName() @@ -316,9 +317,9 @@ func TestValidPrivilegesForObjects(t *testing.T) { for _, tc := range testCases { for _, priv := range tc.validPrivileges { - privDesc := descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) + privDesc := catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) privDesc.Grant(testUser, privilege.List{priv}, false) - err := privDesc.Validate(id, tc.objectType, "whatever", descpb.DefaultSuperuserPrivileges) + err := privDesc.Validate(id, tc.objectType, "whatever", catpb.DefaultSuperuserPrivileges) if err != nil { t.Fatal(err) } @@ -333,9 +334,9 @@ func TestValidPrivilegesForObjects(t *testing.T) { } for _, priv := range invalidPrivileges { - privDesc := descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) + privDesc := catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) privDesc.Grant(testUser, privilege.List{priv}, false) - err := privDesc.Validate(id, tc.objectType, "whatever", descpb.DefaultSuperuserPrivileges) + err := privDesc.Validate(id, tc.objectType, "whatever", catpb.DefaultSuperuserPrivileges) if err == nil { t.Fatalf("unexpected success, %s should not be a valid privilege for a %s", priv, tc.objectType) @@ -352,7 +353,7 @@ func TestSystemPrivilegeValidate(t *testing.T) { testUser := security.TestUserName() - validate := func(descriptor *descpb.PrivilegeDescriptor) error { + validate := func(descriptor *catpb.PrivilegeDescriptor) error { return descriptor.Validate(keys.SystemDatabaseID, privilege.Table, "whatever", privilege.ReadData) } @@ -363,7 +364,7 @@ func TestSystemPrivilegeValidate(t *testing.T) { { // Valid: root user has one of the allowable privilege sets. - descriptor := descpb.NewCustomSuperuserPrivilegeDescriptor( + descriptor := catpb.NewCustomSuperuserPrivilegeDescriptor( privilege.List{privilege.SELECT, privilege.GRANT}, security.AdminRoleName(), ) @@ -386,7 +387,7 @@ func TestSystemPrivilegeValidate(t *testing.T) { { // Valid: root has exactly the allowed privileges. - descriptor := descpb.NewCustomSuperuserPrivilegeDescriptor( + descriptor := catpb.NewCustomSuperuserPrivilegeDescriptor( privilege.List{privilege.SELECT, privilege.GRANT}, security.AdminRoleName(), ) @@ -413,7 +414,7 @@ func TestSystemPrivilegeValidate(t *testing.T) { { // Invalid: root has a non-allowable privilege set. - descriptor := descpb.NewCustomSuperuserPrivilegeDescriptor(privilege.List{privilege.UPDATE}, + descriptor := catpb.NewCustomSuperuserPrivilegeDescriptor(privilege.List{privilege.UPDATE}, security.AdminRoleName()) if err := validate(descriptor); !testutils.IsError(err, rootWrongPrivilegesErr) { t.Fatalf("expected err=%s, got err=%v", rootWrongPrivilegesErr, err) @@ -446,22 +447,22 @@ func TestValidateOwnership(t *testing.T) { defer leaktest.AfterTest(t)() // Use a non-system id. - id := descpb.ID(bootstrap.TestingMinUserDescID()) - validate := func(privs descpb.PrivilegeDescriptor) error { - return privs.Validate(id, privilege.Table, "whatever", descpb.DefaultSuperuserPrivileges) + id := catid.DescID(bootstrap.TestingMinUserDescID()) + validate := func(privs catpb.PrivilegeDescriptor) error { + return privs.Validate(id, privilege.Table, "whatever", catpb.DefaultSuperuserPrivileges) } // A privilege descriptor with a version before OwnerVersion can have // no owner. - privs := descpb.PrivilegeDescriptor{ - Users: []descpb.UserPrivileges{ + privs := catpb.PrivilegeDescriptor{ + Users: []catpb.UserPrivileges{ { UserProto: security.AdminRoleName().EncodeProto(), - Privileges: descpb.DefaultSuperuserPrivileges.ToBitField(), + Privileges: catpb.DefaultSuperuserPrivileges.ToBitField(), }, { UserProto: security.RootUserName().EncodeProto(), - Privileges: descpb.DefaultSuperuserPrivileges.ToBitField(), + Privileges: catpb.DefaultSuperuserPrivileges.ToBitField(), }, }} err := validate(privs) @@ -471,18 +472,18 @@ func TestValidateOwnership(t *testing.T) { // A privilege descriptor with version OwnerVersion and onwards should // have an owner. - privs = descpb.PrivilegeDescriptor{ - Users: []descpb.UserPrivileges{ + privs = catpb.PrivilegeDescriptor{ + Users: []catpb.UserPrivileges{ { UserProto: security.AdminRoleName().EncodeProto(), - Privileges: descpb.DefaultSuperuserPrivileges.ToBitField(), + Privileges: catpb.DefaultSuperuserPrivileges.ToBitField(), }, { UserProto: security.RootUserName().EncodeProto(), - Privileges: descpb.DefaultSuperuserPrivileges.ToBitField(), + Privileges: catpb.DefaultSuperuserPrivileges.ToBitField(), }, }, - Version: descpb.OwnerVersion, + Version: catpb.OwnerVersion, } err = validate(privs) @@ -490,19 +491,19 @@ func TestValidateOwnership(t *testing.T) { t.Fatal("unexpected success") } - privs = descpb.PrivilegeDescriptor{ + privs = catpb.PrivilegeDescriptor{ OwnerProto: security.RootUserName().EncodeProto(), - Users: []descpb.UserPrivileges{ + Users: []catpb.UserPrivileges{ { UserProto: security.AdminRoleName().EncodeProto(), - Privileges: descpb.DefaultSuperuserPrivileges.ToBitField(), + Privileges: catpb.DefaultSuperuserPrivileges.ToBitField(), }, { UserProto: security.RootUserName().EncodeProto(), - Privileges: descpb.DefaultSuperuserPrivileges.ToBitField(), + Privileges: catpb.DefaultSuperuserPrivileges.ToBitField(), }, }, - Version: descpb.OwnerVersion, + Version: catpb.OwnerVersion, } err = validate(privs) @@ -519,39 +520,39 @@ func TestGrantWithGrantOption(t *testing.T) { testUser := security.TestUserName() testCases := []struct { - pd *descpb.PrivilegeDescriptor + pd *catpb.PrivilegeDescriptor user security.SQLUsername objectType privilege.ObjectType grantPrivileges privilege.List expectedPrivileges privilege.List expectedGrantOption privilege.List }{ - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{}, privilege.List{}, security.AdminRoleName()), testUser, privilege.Table, privilege.List{privilege.SELECT, privilege.INSERT}, privilege.List{privilege.SELECT, privilege.INSERT}, privilege.List{privilege.SELECT, privilege.INSERT}}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{}, security.AdminRoleName()), testUser, privilege.Table, privilege.List{privilege.SELECT, privilege.INSERT}, privilege.List{privilege.CREATE, privilege.SELECT, privilege.INSERT}, privilege.List{privilege.SELECT, privilege.INSERT}}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{}, security.AdminRoleName()), testUser, privilege.Table, privilege.List{privilege.SELECT, privilege.INSERT}, privilege.List{privilege.ALL, privilege.SELECT, privilege.INSERT}, privilege.List{privilege.SELECT, privilege.INSERT}}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.INSERT}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.INSERT}, privilege.List{}, security.AdminRoleName()), testUser, privilege.Table, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, security.AdminRoleName()), testUser, privilege.Schema, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{privilege.CREATE}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{privilege.CREATE}, security.AdminRoleName()), testUser, privilege.Schema, privilege.List{privilege.ALL, privilege.CREATE}, privilege.List{privilege.ALL}, @@ -579,7 +580,7 @@ func TestRevokeWithGrantOption(t *testing.T) { testUser := security.TestUserName() testCases := []struct { - pd *descpb.PrivilegeDescriptor + pd *catpb.PrivilegeDescriptor user security.SQLUsername objectType privilege.ObjectType grantOptionFor bool @@ -588,56 +589,56 @@ func TestRevokeWithGrantOption(t *testing.T) { expectedGrantOption privilege.List shouldBeEmpty bool }{ - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{}, security.AdminRoleName()), testUser, privilege.Table, true, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, privilege.List{}, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, security.AdminRoleName()), testUser, privilege.Table, true, privilege.List{privilege.CREATE, privilege.GRANT}, privilege.List{privilege.ALL}, privilege.List{privilege.DROP, privilege.SELECT, privilege.INSERT, privilege.DELETE, privilege.UPDATE, privilege.ZONECONFIG}, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, security.AdminRoleName()), testUser, privilege.Table, true, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, privilege.List{}, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, security.AdminRoleName()), testUser, privilege.Table, true, privilege.List{privilege.ALL, privilege.CREATE, privilege.SELECT}, privilege.List{privilege.ALL}, privilege.List{}, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.SELECT, privilege.INSERT}, privilege.List{privilege.SELECT}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.SELECT, privilege.INSERT}, privilege.List{privilege.SELECT}, security.AdminRoleName()), testUser, privilege.Table, true, privilege.List{privilege.CREATE, privilege.DROP, privilege.SELECT}, privilege.List{privilege.CREATE, privilege.SELECT, privilege.INSERT}, privilege.List{}, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.SELECT, privilege.INSERT}, privilege.List{privilege.CREATE, privilege.SELECT, privilege.INSERT}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.SELECT, privilege.INSERT}, privilege.List{privilege.CREATE, privilege.SELECT, privilege.INSERT}, security.AdminRoleName()), testUser, privilege.Table, false, privilege.List{privilege.SELECT, privilege.INSERT}, privilege.List{privilege.CREATE}, privilege.List{privilege.CREATE}, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, security.AdminRoleName()), testUser, privilege.Table, false, privilege.List{privilege.CREATE, privilege.GRANT}, privilege.List{privilege.DROP, privilege.SELECT, privilege.INSERT, privilege.DELETE, privilege.UPDATE, privilege.ZONECONFIG}, privilege.List{privilege.DROP, privilege.SELECT, privilege.INSERT, privilege.DELETE, privilege.UPDATE, privilege.ZONECONFIG}, false}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.SELECT, privilege.INSERT}, privilege.List{privilege.INSERT}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.SELECT, privilege.INSERT}, privilege.List{privilege.INSERT}, security.AdminRoleName()), testUser, privilege.Table, false, privilege.List{privilege.ALL}, diff --git a/pkg/sql/catalog/descpb/privilegedescversion_string.go b/pkg/sql/catalog/catpb/privilegedescversion_string.go similarity index 98% rename from pkg/sql/catalog/descpb/privilegedescversion_string.go rename to pkg/sql/catalog/catpb/privilegedescversion_string.go index 295607e8c4c3..2cbf7620c4e3 100644 --- a/pkg/sql/catalog/descpb/privilegedescversion_string.go +++ b/pkg/sql/catalog/catpb/privilegedescversion_string.go @@ -1,6 +1,6 @@ // Code generated by "stringer"; DO NOT EDIT. -package descpb +package catpb import "strconv" diff --git a/pkg/sql/catalog/catprivilege/BUILD.bazel b/pkg/sql/catalog/catprivilege/BUILD.bazel index 476468945967..10e209210cf5 100644 --- a/pkg/sql/catalog/catprivilege/BUILD.bazel +++ b/pkg/sql/catalog/catprivilege/BUILD.bazel @@ -15,6 +15,7 @@ go_library( "//pkg/security", "//pkg/sql/catalog", "//pkg/sql/catalog/catconstants", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/descpb", "//pkg/sql/privilege", "//pkg/sql/sem/tree", @@ -32,6 +33,7 @@ go_test( "//pkg/keys", "//pkg/security", "//pkg/sql/catalog/bootstrap", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/descpb", "//pkg/sql/privilege", "//pkg/sql/sem/tree", diff --git a/pkg/sql/catalog/catprivilege/default_privilege.go b/pkg/sql/catalog/catprivilege/default_privilege.go index 140562d00b6c..4967f29655a9 100644 --- a/pkg/sql/catalog/catprivilege/default_privilege.go +++ b/pkg/sql/catalog/catprivilege/default_privilege.go @@ -16,6 +16,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" @@ -27,7 +28,7 @@ var _ catalog.DefaultPrivilegeDescriptor = &Mutable{} // immutable is a wrapper for a DefaultPrivilegeDescriptor // that only exposes getters. type immutable struct { - defaultPrivilegeDescriptor *descpb.DefaultPrivilegeDescriptor + defaultPrivilegeDescriptor *catpb.DefaultPrivilegeDescriptor } // Mutable is a wrapper for a DefaultPrivilegeDescriptor @@ -39,10 +40,10 @@ type Mutable struct { // MakeDefaultPrivilegeDescriptor returns a new DefaultPrivilegeDescriptor for // the specified DefaultPrivilegeDescriptorType. func MakeDefaultPrivilegeDescriptor( - typ descpb.DefaultPrivilegeDescriptor_DefaultPrivilegeDescriptorType, -) *descpb.DefaultPrivilegeDescriptor { - var defaultPrivilegesForRole []descpb.DefaultPrivilegesForRole - return &descpb.DefaultPrivilegeDescriptor{ + typ catpb.DefaultPrivilegeDescriptor_DefaultPrivilegeDescriptorType, +) *catpb.DefaultPrivilegeDescriptor { + var defaultPrivilegesForRole []catpb.DefaultPrivilegesForRole + return &catpb.DefaultPrivilegeDescriptor{ DefaultPrivilegesPerRole: defaultPrivilegesForRole, Type: typ, } @@ -51,7 +52,7 @@ func MakeDefaultPrivilegeDescriptor( // MakeDefaultPrivileges returns an immutable // given a defaultPrivilegeDescriptor. func MakeDefaultPrivileges( - defaultPrivilegeDescriptor *descpb.DefaultPrivilegeDescriptor, + defaultPrivilegeDescriptor *catpb.DefaultPrivilegeDescriptor, ) catalog.DefaultPrivilegeDescriptor { return &immutable{ defaultPrivilegeDescriptor: defaultPrivilegeDescriptor, @@ -61,7 +62,7 @@ func MakeDefaultPrivileges( // NewMutableDefaultPrivileges returns a Mutable // given a defaultPrivilegeDescriptor. func NewMutableDefaultPrivileges( - defaultPrivilegeDescriptor *descpb.DefaultPrivilegeDescriptor, + defaultPrivilegeDescriptor *catpb.DefaultPrivilegeDescriptor, ) *Mutable { return &Mutable{ immutable{ @@ -75,8 +76,8 @@ func NewMutableDefaultPrivileges( // If the DefaultPrivilegeDescriptor is for a schema we simply // call grant/revoke. func (d *immutable) grantOrRevokeDefaultPrivilegesHelper( - defaultPrivilegesForRole *descpb.DefaultPrivilegesForRole, - role descpb.DefaultPrivilegesRole, + defaultPrivilegesForRole *catpb.DefaultPrivilegesForRole, + role catpb.DefaultPrivilegesRole, targetObject tree.AlterDefaultPrivilegesTargetObject, grantee security.SQLUsername, privList privilege.List, @@ -111,7 +112,7 @@ func (d *immutable) grantOrRevokeDefaultPrivilegesHelper( // GrantDefaultPrivileges grants privileges for the specified users. func (d *Mutable) GrantDefaultPrivileges( - role descpb.DefaultPrivilegesRole, + role catpb.DefaultPrivilegesRole, privileges privilege.List, grantees []security.SQLUsername, targetObject tree.AlterDefaultPrivilegesTargetObject, @@ -126,7 +127,7 @@ func (d *Mutable) GrantDefaultPrivileges( // RevokeDefaultPrivileges revokes privileges for the specified users. func (d *Mutable) RevokeDefaultPrivileges( - role descpb.DefaultPrivilegesRole, + role catpb.DefaultPrivilegesRole, privileges privilege.List, grantees []security.SQLUsername, targetObject tree.AlterDefaultPrivilegesTargetObject, @@ -174,12 +175,12 @@ func CreatePrivilegesFromDefaultPrivileges( dbID descpb.ID, user security.SQLUsername, targetObject tree.AlterDefaultPrivilegesTargetObject, - databasePrivileges *descpb.PrivilegeDescriptor, -) *descpb.PrivilegeDescriptor { + databasePrivileges *catpb.PrivilegeDescriptor, +) *catpb.PrivilegeDescriptor { // If a new system table is being created (which should only be doable by // an internal user account), make sure it gets the correct privileges. if dbID == keys.SystemDatabaseID { - return descpb.NewBasePrivilegeDescriptor(security.NodeUserName()) + return catpb.NewBasePrivilegeDescriptor(security.NodeUserName()) } defaultPrivilegeDescriptors := []catalog.DefaultPrivilegeDescriptor{ @@ -190,13 +191,13 @@ func CreatePrivilegesFromDefaultPrivileges( defaultPrivilegeDescriptors = append(defaultPrivilegeDescriptors, schemaDefaultPrivilegeDescriptor) } - newPrivs := descpb.NewBasePrivilegeDescriptor(user) - role := descpb.DefaultPrivilegesRole{Role: user} + newPrivs := catpb.NewBasePrivilegeDescriptor(user) + role := catpb.DefaultPrivilegesRole{Role: user} for _, d := range defaultPrivilegeDescriptors { if defaultPrivilegesForRole, found := d.GetDefaultPrivilegesForRole(role); !found { // If default privileges are not defined for the creator role, we handle // it as the case where the user has all privileges. - defaultPrivilegesForCreatorRole := descpb.InitDefaultPrivilegesForRole(role, d.GetDefaultPrivilegeDescriptorType()) + defaultPrivilegesForCreatorRole := catpb.InitDefaultPrivilegesForRole(role, d.GetDefaultPrivilegeDescriptorType()) for _, user := range GetUserPrivilegesForObject(defaultPrivilegesForCreatorRole, targetObject) { applyDefaultPrivileges( newPrivs, @@ -221,7 +222,7 @@ func CreatePrivilegesFromDefaultPrivileges( // The privileges for the object are the union of the default privileges // defined for the object for the object creator and the default privileges // defined for all roles. - defaultPrivilegesForAllRoles, found := d.GetDefaultPrivilegesForRole(descpb.DefaultPrivilegesRole{ForAllRoles: true}) + defaultPrivilegesForAllRoles, found := d.GetDefaultPrivilegesForRole(catpb.DefaultPrivilegesRole{ForAllRoles: true}) if found { for _, user := range GetUserPrivilegesForObject(*defaultPrivilegesForAllRoles, targetObject) { applyDefaultPrivileges( @@ -234,7 +235,7 @@ func CreatePrivilegesFromDefaultPrivileges( } } - newPrivs.Version = descpb.Version21_2 + newPrivs.Version = catpb.Version21_2 // TODO(richardjcai): Remove this depending on how we handle the migration. // For backwards compatibility, also "inherit" privileges from the dbDesc. @@ -266,7 +267,7 @@ func CreatePrivilegesFromDefaultPrivileges( // ForEachDefaultPrivilegeForRole loops through the DefaultPrivilegeDescriptior's // DefaultPrivilegePerRole entry and calls f on it. func (d *immutable) ForEachDefaultPrivilegeForRole( - f func(defaultPrivilegesForRole descpb.DefaultPrivilegesForRole) error, + f func(defaultPrivilegesForRole catpb.DefaultPrivilegesForRole) error, ) error { if d.defaultPrivilegeDescriptor == nil { return nil @@ -284,8 +285,8 @@ func (d *immutable) ForEachDefaultPrivilegeForRole( // GetDefaultPrivilegesForRole looks for a specific user in the list. // Returns (nil, false) if not found, or (ptr, true) if found. func (d *immutable) GetDefaultPrivilegesForRole( - role descpb.DefaultPrivilegesRole, -) (*descpb.DefaultPrivilegesForRole, bool) { + role catpb.DefaultPrivilegesRole, +) (*catpb.DefaultPrivilegesForRole, bool) { idx := d.defaultPrivilegeDescriptor.FindUserIndex(role) if idx == -1 { return nil, false @@ -294,14 +295,14 @@ func (d *immutable) GetDefaultPrivilegesForRole( } // GetDefaultPrivilegeDescriptorType the Type of the default privilege descriptor. -func (d *immutable) GetDefaultPrivilegeDescriptorType() descpb.DefaultPrivilegeDescriptor_DefaultPrivilegeDescriptorType { +func (d *immutable) GetDefaultPrivilegeDescriptorType() catpb.DefaultPrivilegeDescriptor_DefaultPrivilegeDescriptorType { return d.defaultPrivilegeDescriptor.Type } // IsDatabaseDefaultPrivilege returns whether the Type of the default privilege // descriptor is for databases. func (d *immutable) IsDatabaseDefaultPrivilege() bool { - return d.defaultPrivilegeDescriptor.Type == descpb.DefaultPrivilegeDescriptor_DATABASE + return d.defaultPrivilegeDescriptor.Type == catpb.DefaultPrivilegeDescriptor_DATABASE } // foldPrivileges folds ALL privileges for role and USAGE on public into @@ -312,9 +313,9 @@ func (d *immutable) IsDatabaseDefaultPrivilege() bool { // This is necessary as role having ALL privileges on tables is the default state // and should not prevent the role from being dropped if it has ALL privileges. func foldPrivileges( - defaultPrivilegesForRole *descpb.DefaultPrivilegesForRole, - role descpb.DefaultPrivilegesRole, - privileges *descpb.PrivilegeDescriptor, + defaultPrivilegesForRole *catpb.DefaultPrivilegesForRole, + role catpb.DefaultPrivilegesRole, + privileges *catpb.PrivilegeDescriptor, targetObject tree.AlterDefaultPrivilegesTargetObject, ) { if targetObject == tree.Types && @@ -351,9 +352,9 @@ func foldPrivileges( // For example - if RoleHasAllPrivilegesOnTables is true, ALL privilege is added // into the UserPrivileges array for the Role. func expandPrivileges( - defaultPrivilegesForRole *descpb.DefaultPrivilegesForRole, - role descpb.DefaultPrivilegesRole, - privileges *descpb.PrivilegeDescriptor, + defaultPrivilegesForRole *catpb.DefaultPrivilegesForRole, + role catpb.DefaultPrivilegesRole, + privileges *catpb.PrivilegeDescriptor, targetObject tree.AlterDefaultPrivilegesTargetObject, ) { if targetObject == tree.Types && GetPublicHasUsageOnTypes(defaultPrivilegesForRole) { @@ -373,14 +374,14 @@ func expandPrivileges( // GetUserPrivilegesForObject returns the set of []UserPrivileges constructed // from the DefaultPrivilegesForRole. func GetUserPrivilegesForObject( - p descpb.DefaultPrivilegesForRole, targetObject tree.AlterDefaultPrivilegesTargetObject, -) []descpb.UserPrivileges { - var userPrivileges []descpb.UserPrivileges + p catpb.DefaultPrivilegesForRole, targetObject tree.AlterDefaultPrivilegesTargetObject, +) []catpb.UserPrivileges { + var userPrivileges []catpb.UserPrivileges if privileges, ok := p.DefaultPrivilegesPerObject[targetObject]; ok { userPrivileges = privileges.Users } if GetPublicHasUsageOnTypes(&p) && targetObject == tree.Types { - userPrivileges = append(userPrivileges, descpb.UserPrivileges{ + userPrivileges = append(userPrivileges, catpb.UserPrivileges{ UserProto: security.PublicRoleName().EncodeProto(), Privileges: privilege.USAGE.Mask(), }) @@ -393,7 +394,7 @@ func GetUserPrivilegesForObject( } userProto := p.GetExplicitRole().UserProto if GetRoleHasAllPrivilegesOnTargetObject(&p, targetObject) { - return append(userPrivileges, descpb.UserPrivileges{ + return append(userPrivileges, catpb.UserPrivileges{ UserProto: userProto, Privileges: privilege.ALL.Mask(), }) @@ -402,7 +403,7 @@ func GetUserPrivilegesForObject( } // GetPublicHasUsageOnTypes returns whether Public has Usage privilege on types. -func GetPublicHasUsageOnTypes(defaultPrivilegesForRole *descpb.DefaultPrivilegesForRole) bool { +func GetPublicHasUsageOnTypes(defaultPrivilegesForRole *catpb.DefaultPrivilegesForRole) bool { if defaultPrivilegesForRole.IsExplicitRole() { return defaultPrivilegesForRole.GetExplicitRole().PublicHasUsageOnTypes } @@ -412,7 +413,7 @@ func GetPublicHasUsageOnTypes(defaultPrivilegesForRole *descpb.DefaultPrivileges // GetRoleHasAllPrivilegesOnTargetObject returns whether the creator role // has all privileges on the default privileges target object. func GetRoleHasAllPrivilegesOnTargetObject( - defaultPrivilegesForRole *descpb.DefaultPrivilegesForRole, + defaultPrivilegesForRole *catpb.DefaultPrivilegesForRole, targetObject tree.AlterDefaultPrivilegesTargetObject, ) bool { if !defaultPrivilegesForRole.IsExplicitRole() { @@ -435,7 +436,7 @@ func GetRoleHasAllPrivilegesOnTargetObject( // setPublicHasUsageOnTypes sets PublicHasUsageOnTypes to publicHasUsageOnTypes. func setPublicHasUsageOnTypes( - defaultPrivilegesForRole *descpb.DefaultPrivilegesForRole, publicHasUsageOnTypes bool, + defaultPrivilegesForRole *catpb.DefaultPrivilegesForRole, publicHasUsageOnTypes bool, ) { if defaultPrivilegesForRole.IsExplicitRole() { defaultPrivilegesForRole.GetExplicitRole().PublicHasUsageOnTypes = publicHasUsageOnTypes @@ -448,7 +449,7 @@ func setPublicHasUsageOnTypes( // could be different from the privileges. Unlike the normal grant, the privileges // and the grant options being granted could be different func applyDefaultPrivileges( - p *descpb.PrivilegeDescriptor, + p *catpb.PrivilegeDescriptor, user security.SQLUsername, privList privilege.List, grantOptionList privilege.List, @@ -491,7 +492,7 @@ func applyDefaultPrivileges( } func setRoleHasAllOnTargetObject( - defaultPrivilegesForRole *descpb.DefaultPrivilegesForRole, + defaultPrivilegesForRole *catpb.DefaultPrivilegesForRole, roleHasAll bool, targetObject tree.AlterDefaultPrivilegesTargetObject, ) { diff --git a/pkg/sql/catalog/catprivilege/default_privilege_test.go b/pkg/sql/catalog/catprivilege/default_privilege_test.go index 3c9cae8c1b24..e6ad0c271eac 100644 --- a/pkg/sql/catalog/catprivilege/default_privilege_test.go +++ b/pkg/sql/catalog/catprivilege/default_privilege_test.go @@ -15,6 +15,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/security" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/privilege" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" @@ -33,70 +34,70 @@ func TestGrantDefaultPrivileges(t *testing.T) { creatorUser := security.MakeSQLUsernameFromPreNormalizedString("creator") testCases := []struct { - defaultPrivilegesRole descpb.DefaultPrivilegesRole + defaultPrivilegesRole catpb.DefaultPrivilegesRole privileges privilege.List grantees []security.SQLUsername targetObject tree.AlterDefaultPrivilegesTargetObject objectCreator security.SQLUsername }{ { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, privileges: privilege.List{privilege.ALL}, grantees: []security.SQLUsername{fooUser}, targetObject: tree.Tables, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, privileges: privilege.List{privilege.ALL}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Tables, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, privileges: privilege.List{privilege.ALL}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Sequences, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, privileges: privilege.List{privilege.ALL}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Types, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, privileges: privilege.List{privilege.ALL}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Schemas, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, privileges: privilege.List{privilege.SELECT, privilege.DELETE}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Tables, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, privileges: privilege.List{privilege.SELECT, privilege.DELETE}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Sequences, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, privileges: privilege.List{privilege.USAGE}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Types, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, privileges: privilege.List{privilege.USAGE}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Schemas, @@ -104,56 +105,56 @@ func TestGrantDefaultPrivileges(t *testing.T) { }, /* Test cases for ForAllRoles */ { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{ForAllRoles: true}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{ForAllRoles: true}, privileges: privilege.List{privilege.ALL}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Tables, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{ForAllRoles: true}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{ForAllRoles: true}, privileges: privilege.List{privilege.ALL}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Sequences, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{ForAllRoles: true}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{ForAllRoles: true}, privileges: privilege.List{privilege.ALL}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Types, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{ForAllRoles: true}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{ForAllRoles: true}, privileges: privilege.List{privilege.ALL}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Schemas, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{ForAllRoles: true}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{ForAllRoles: true}, privileges: privilege.List{privilege.SELECT, privilege.DELETE}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Tables, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{ForAllRoles: true}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{ForAllRoles: true}, privileges: privilege.List{privilege.SELECT, privilege.DELETE}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Sequences, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{ForAllRoles: true}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{ForAllRoles: true}, privileges: privilege.List{privilege.USAGE}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Types, objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{ForAllRoles: true}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{ForAllRoles: true}, privileges: privilege.List{privilege.USAGE}, grantees: []security.SQLUsername{fooUser, barUser, bazUser}, targetObject: tree.Schemas, @@ -162,14 +163,14 @@ func TestGrantDefaultPrivileges(t *testing.T) { } for _, tc := range testCases { - defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE) + defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_DATABASE) defaultPrivileges := NewMutableDefaultPrivileges(defaultPrivilegeDescriptor) defaultPrivileges.GrantDefaultPrivileges(tc.defaultPrivilegesRole, tc.privileges, tc.grantees, tc.targetObject, false /* withGrantOption */, false /*deprecateGrant*/) newPrivileges := CreatePrivilegesFromDefaultPrivileges( defaultPrivileges, nil, /* schemaDefaultPrivilegeDescriptor */ - nonSystemDatabaseID, tc.objectCreator, tc.targetObject, &descpb.PrivilegeDescriptor{}, + nonSystemDatabaseID, tc.objectCreator, tc.targetObject, &catpb.PrivilegeDescriptor{}, ) for _, grantee := range tc.grantees { @@ -191,14 +192,14 @@ func TestRevokeDefaultPrivileges(t *testing.T) { creatorUser := security.MakeSQLUsernameFromPreNormalizedString("creator") testCases := []struct { - defaultPrivilegesRole descpb.DefaultPrivilegesRole + defaultPrivilegesRole catpb.DefaultPrivilegesRole grantPrivileges, revokePrivileges, expectedPrivileges privilege.List grantees []security.SQLUsername targetObject tree.AlterDefaultPrivilegesTargetObject objectCreator security.SQLUsername }{ { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, grantPrivileges: privilege.List{privilege.ALL}, revokePrivileges: privilege.List{privilege.SELECT}, expectedPrivileges: privilege.List{ @@ -210,7 +211,7 @@ func TestRevokeDefaultPrivileges(t *testing.T) { objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, grantPrivileges: privilege.List{privilege.ALL}, revokePrivileges: privilege.List{privilege.SELECT}, expectedPrivileges: privilege.List{ @@ -222,7 +223,7 @@ func TestRevokeDefaultPrivileges(t *testing.T) { objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, grantPrivileges: privilege.List{privilege.ALL}, revokePrivileges: privilege.List{privilege.USAGE}, expectedPrivileges: privilege.List{ @@ -233,7 +234,7 @@ func TestRevokeDefaultPrivileges(t *testing.T) { objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{Role: creatorUser}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{Role: creatorUser}, grantPrivileges: privilege.List{privilege.ALL}, revokePrivileges: privilege.List{privilege.USAGE}, expectedPrivileges: privilege.List{ @@ -245,7 +246,7 @@ func TestRevokeDefaultPrivileges(t *testing.T) { }, /* Test cases for ForAllRoles */ { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{ForAllRoles: true}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{ForAllRoles: true}, grantPrivileges: privilege.List{privilege.ALL}, revokePrivileges: privilege.List{privilege.SELECT}, expectedPrivileges: privilege.List{ @@ -257,7 +258,7 @@ func TestRevokeDefaultPrivileges(t *testing.T) { objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{ForAllRoles: true}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{ForAllRoles: true}, grantPrivileges: privilege.List{privilege.ALL}, revokePrivileges: privilege.List{privilege.USAGE}, expectedPrivileges: privilege.List{ @@ -268,7 +269,7 @@ func TestRevokeDefaultPrivileges(t *testing.T) { objectCreator: creatorUser, }, { - defaultPrivilegesRole: descpb.DefaultPrivilegesRole{ForAllRoles: true}, + defaultPrivilegesRole: catpb.DefaultPrivilegesRole{ForAllRoles: true}, grantPrivileges: privilege.List{privilege.ALL}, revokePrivileges: privilege.List{privilege.USAGE}, expectedPrivileges: privilege.List{ @@ -281,7 +282,7 @@ func TestRevokeDefaultPrivileges(t *testing.T) { } for _, tc := range testCases { - defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE) + defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_DATABASE) defaultPrivileges := NewMutableDefaultPrivileges(defaultPrivilegeDescriptor) defaultPrivileges.GrantDefaultPrivileges(tc.defaultPrivilegesRole, tc.grantPrivileges, tc.grantees, tc.targetObject, false /* withGrantOption */, false /*deprecateGrant*/) @@ -289,7 +290,7 @@ func TestRevokeDefaultPrivileges(t *testing.T) { newPrivileges := CreatePrivilegesFromDefaultPrivileges( defaultPrivileges, nil, /* schemaDefaultPrivilegeDescriptor */ - nonSystemDatabaseID, tc.objectCreator, tc.targetObject, &descpb.PrivilegeDescriptor{}, + nonSystemDatabaseID, tc.objectCreator, tc.targetObject, &catpb.PrivilegeDescriptor{}, ) for _, grantee := range tc.grantees { @@ -305,17 +306,17 @@ func TestRevokeDefaultPrivileges(t *testing.T) { func TestRevokeDefaultPrivilegesFromEmptyList(t *testing.T) { defer leaktest.AfterTest(t)() - defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE) + defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_DATABASE) defaultPrivileges := NewMutableDefaultPrivileges(defaultPrivilegeDescriptor) creatorUser := security.MakeSQLUsernameFromPreNormalizedString("creator") fooUser := security.MakeSQLUsernameFromPreNormalizedString("foo") - defaultPrivileges.RevokeDefaultPrivileges(descpb.DefaultPrivilegesRole{ + defaultPrivileges.RevokeDefaultPrivileges(catpb.DefaultPrivilegesRole{ Role: creatorUser, }, privilege.List{privilege.ALL}, []security.SQLUsername{fooUser}, tree.Tables, false /* grantOptionFor */, false /*deprecateGrant*/) newPrivileges := CreatePrivilegesFromDefaultPrivileges( defaultPrivileges, nil, /* schemaDefaultPrivilegeDescriptor */ - nonSystemDatabaseID, creatorUser, tree.Tables, &descpb.PrivilegeDescriptor{}, + nonSystemDatabaseID, creatorUser, tree.Tables, &catpb.PrivilegeDescriptor{}, ) if newPrivileges.AnyPrivilege(fooUser) { @@ -326,12 +327,12 @@ func TestRevokeDefaultPrivilegesFromEmptyList(t *testing.T) { func TestCreatePrivilegesFromDefaultPrivilegesForSystemDatabase(t *testing.T) { defer leaktest.AfterTest(t)() - defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE) + defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_DATABASE) defaultPrivileges := NewMutableDefaultPrivileges(defaultPrivilegeDescriptor) creatorUser := security.MakeSQLUsernameFromPreNormalizedString("creator") newPrivileges := CreatePrivilegesFromDefaultPrivileges( defaultPrivileges, nil, /* schemaDefaultPrivilegeDescriptor */ - keys.SystemDatabaseID, creatorUser, tree.Tables, &descpb.PrivilegeDescriptor{}, + keys.SystemDatabaseID, creatorUser, tree.Tables, &catpb.PrivilegeDescriptor{}, ) if !newPrivileges.Owner().IsNodeUser() { @@ -342,7 +343,7 @@ func TestCreatePrivilegesFromDefaultPrivilegesForSystemDatabase(t *testing.T) { func TestPresetDefaultPrivileges(t *testing.T) { defer leaktest.AfterTest(t)() - defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE) + defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_DATABASE) defaultPrivileges := NewMutableDefaultPrivileges(defaultPrivilegeDescriptor) creatorUser := security.MakeSQLUsernameFromPreNormalizedString("creator") @@ -350,7 +351,7 @@ func TestPresetDefaultPrivileges(t *testing.T) { for _, targetObject := range targetObjectTypes { newPrivileges := CreatePrivilegesFromDefaultPrivileges( defaultPrivileges, nil, /* schemaDefaultPrivilegeDescriptor */ - nonSystemDatabaseID, creatorUser, targetObject, &descpb.PrivilegeDescriptor{}, + nonSystemDatabaseID, creatorUser, targetObject, &catpb.PrivilegeDescriptor{}, ) if !newPrivileges.CheckPrivilege(creatorUser, privilege.ALL) { @@ -368,7 +369,7 @@ func TestPresetDefaultPrivileges(t *testing.T) { func TestPresetDefaultPrivilegesInSchema(t *testing.T) { defer leaktest.AfterTest(t)() - defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_SCHEMA) + defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_SCHEMA) defaultPrivileges := NewMutableDefaultPrivileges(defaultPrivilegeDescriptor) creatorUser := security.MakeSQLUsernameFromPreNormalizedString("creator") @@ -376,7 +377,7 @@ func TestPresetDefaultPrivilegesInSchema(t *testing.T) { for _, targetObject := range targetObjectTypes { newPrivileges := CreatePrivilegesFromDefaultPrivileges( defaultPrivileges, nil, /* schemaDefaultPrivilegeDescriptor */ - nonSystemDatabaseID, creatorUser, targetObject, &descpb.PrivilegeDescriptor{}, + nonSystemDatabaseID, creatorUser, targetObject, &catpb.PrivilegeDescriptor{}, ) // There are no preset privileges on a default privilege descriptor defined @@ -661,15 +662,15 @@ func TestDefaultPrivileges(t *testing.T) { }, } for _, tc := range testCases { - defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE) + defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_DATABASE) defaultPrivileges := NewMutableDefaultPrivileges(defaultPrivilegeDescriptor) - schemaPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_SCHEMA) + schemaPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_SCHEMA) schemaDefaultPrivileges := NewMutableDefaultPrivileges(schemaPrivilegeDescriptor) for _, userAndGrant := range tc.userAndGrants { defaultPrivileges.GrantDefaultPrivileges( - descpb.DefaultPrivilegesRole{Role: tc.defaultPrivilegesRole}, + catpb.DefaultPrivilegesRole{Role: tc.defaultPrivilegesRole}, userAndGrant.grants, []security.SQLUsername{userAndGrant.user}, tc.targetObject, false, /* withGrantOption */ @@ -679,7 +680,7 @@ func TestDefaultPrivileges(t *testing.T) { for _, userAndGrant := range tc.userAndGrantsInSchema { schemaDefaultPrivileges.GrantDefaultPrivileges( - descpb.DefaultPrivilegesRole{Role: tc.defaultPrivilegesRole}, + catpb.DefaultPrivilegesRole{Role: tc.defaultPrivilegesRole}, userAndGrant.grants, []security.SQLUsername{userAndGrant.user}, tc.targetObject, @@ -694,7 +695,7 @@ func TestDefaultPrivileges(t *testing.T) { tc.dbID, tc.objectCreator, tc.targetObject, - &descpb.PrivilegeDescriptor{}, + &catpb.PrivilegeDescriptor{}, ) for _, userAndGrant := range tc.expectedGrantsOnObject { @@ -733,17 +734,17 @@ func TestModifyDefaultDefaultPrivileges(t *testing.T) { } for _, tc := range testCases { - defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE) + defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_DATABASE) defaultPrivileges := NewMutableDefaultPrivileges(defaultPrivilegeDescriptor) creatorUser := security.MakeSQLUsernameFromPreNormalizedString("creator") defaultPrivilegesForCreator := defaultPrivileges.defaultPrivilegeDescriptor. - FindOrCreateUser(descpb.DefaultPrivilegesRole{ + FindOrCreateUser(catpb.DefaultPrivilegesRole{ Role: creatorUser, }) defaultPrivileges.RevokeDefaultPrivileges( - descpb.DefaultPrivilegesRole{Role: creatorUser}, + catpb.DefaultPrivilegesRole{Role: creatorUser}, tc.revokeAndGrantPrivileges, []security.SQLUsername{creatorUser}, tc.targetObject, false, /* grantOptionFor */ @@ -753,7 +754,7 @@ func TestModifyDefaultDefaultPrivileges(t *testing.T) { t.Errorf("expected role to not have ALL privileges on %s", tc.targetObject) } defaultPrivileges.GrantDefaultPrivileges( - descpb.DefaultPrivilegesRole{Role: creatorUser}, + catpb.DefaultPrivilegesRole{Role: creatorUser}, tc.revokeAndGrantPrivileges, []security.SQLUsername{creatorUser}, tc.targetObject, false, /* withGrantOption */ @@ -768,17 +769,17 @@ func TestModifyDefaultDefaultPrivileges(t *testing.T) { func TestModifyDefaultDefaultPrivilegesForPublic(t *testing.T) { defer leaktest.AfterTest(t)() - defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE) + defaultPrivilegeDescriptor := MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_DATABASE) defaultPrivileges := NewMutableDefaultPrivileges(defaultPrivilegeDescriptor) creatorUser := security.MakeSQLUsernameFromPreNormalizedString("creator") defaultPrivilegesForCreator := defaultPrivileges.defaultPrivilegeDescriptor. - FindOrCreateUser(descpb.DefaultPrivilegesRole{ + FindOrCreateUser(catpb.DefaultPrivilegesRole{ Role: creatorUser, }) defaultPrivileges.RevokeDefaultPrivileges( - descpb.DefaultPrivilegesRole{Role: creatorUser}, + catpb.DefaultPrivilegesRole{Role: creatorUser}, privilege.List{privilege.USAGE}, []security.SQLUsername{security.PublicRoleName()}, tree.Types, false, /* grantOptionFor */ @@ -788,7 +789,7 @@ func TestModifyDefaultDefaultPrivilegesForPublic(t *testing.T) { t.Errorf("expected public to not have USAGE privilege on types") } defaultPrivileges.GrantDefaultPrivileges( - descpb.DefaultPrivilegesRole{Role: creatorUser}, + catpb.DefaultPrivilegesRole{Role: creatorUser}, privilege.List{privilege.USAGE}, []security.SQLUsername{security.PublicRoleName()}, tree.Types, false, /* withGrantOption */ @@ -800,7 +801,7 @@ func TestModifyDefaultDefaultPrivilegesForPublic(t *testing.T) { // Test granting when withGrantOption is true. defaultPrivileges.GrantDefaultPrivileges( - descpb.DefaultPrivilegesRole{Role: creatorUser}, + catpb.DefaultPrivilegesRole{Role: creatorUser}, privilege.List{privilege.USAGE}, []security.SQLUsername{security.PublicRoleName()}, tree.Types, true, /* withGrantOption */ @@ -823,7 +824,7 @@ func TestModifyDefaultDefaultPrivilegesForPublic(t *testing.T) { // Test revoking when grantOptionFor is true. defaultPrivileges.RevokeDefaultPrivileges( - descpb.DefaultPrivilegesRole{Role: creatorUser}, + catpb.DefaultPrivilegesRole{Role: creatorUser}, privilege.List{privilege.USAGE}, []security.SQLUsername{security.PublicRoleName()}, tree.Types, true, /* grantOptionFor */ @@ -842,7 +843,7 @@ func TestModifyDefaultDefaultPrivilegesForPublic(t *testing.T) { // Test a complete revoke afterwards. defaultPrivileges.RevokeDefaultPrivileges( - descpb.DefaultPrivilegesRole{Role: creatorUser}, + catpb.DefaultPrivilegesRole{Role: creatorUser}, privilege.List{privilege.USAGE}, []security.SQLUsername{security.PublicRoleName()}, tree.Types, false, /* grantOptionFor */ @@ -861,7 +862,7 @@ func TestApplyDefaultPrivileges(t *testing.T) { testUser := security.TestUserName() testCases := []struct { - pd *descpb.PrivilegeDescriptor + pd *catpb.PrivilegeDescriptor user security.SQLUsername objectType privilege.ObjectType grantPrivileges privilege.List @@ -869,37 +870,37 @@ func TestApplyDefaultPrivileges(t *testing.T) { expectedPrivileges privilege.List expectedGrantOption privilege.List }{ - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{}, privilege.List{}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{}, privilege.List{}, security.AdminRoleName()), testUser, privilege.Table, privilege.List{privilege.SELECT, privilege.INSERT}, privilege.List{privilege.SELECT, privilege.INSERT}, privilege.List{privilege.SELECT, privilege.INSERT}, privilege.List{privilege.SELECT, privilege.INSERT}}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.INSERT}, privilege.List{privilege.INSERT}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.INSERT}, privilege.List{privilege.INSERT}, security.AdminRoleName()), testUser, privilege.Table, privilege.List{privilege.ALL}, privilege.List{privilege.CREATE, privilege.SELECT}, privilege.List{privilege.ALL}, privilege.List{privilege.CREATE, privilege.SELECT, privilege.INSERT}}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.INSERT}, privilege.List{privilege.CREATE}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE, privilege.INSERT}, privilege.List{privilege.CREATE}, security.AdminRoleName()), testUser, privilege.Table, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{privilege.CREATE}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{privilege.CREATE}, security.AdminRoleName()), testUser, privilege.Table, privilege.List{privilege.CREATE, privilege.SELECT, privilege.INSERT, privilege.UPDATE}, privilege.List{privilege.SELECT}, privilege.List{privilege.CREATE, privilege.SELECT, privilege.INSERT, privilege.UPDATE}, privilege.List{privilege.CREATE, privilege.SELECT}}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{privilege.CREATE}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{privilege.CREATE}, security.AdminRoleName()), testUser, privilege.Table, privilege.List{privilege.ALL, privilege.CREATE, privilege.SELECT, privilege.INSERT, privilege.UPDATE}, privilege.List{privilege.ALL, privilege.SELECT}, privilege.List{privilege.ALL}, privilege.List{privilege.ALL}}, - {descpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{privilege.CREATE}, security.AdminRoleName()), + {catpb.NewPrivilegeDescriptor(testUser, privilege.List{privilege.CREATE}, privilege.List{privilege.CREATE}, security.AdminRoleName()), testUser, privilege.Table, privilege.List{privilege.SELECT}, privilege.List{privilege.SELECT, privilege.INSERT}, @@ -921,9 +922,9 @@ func TestApplyDefaultPrivileges(t *testing.T) { } func TestUnsetDefaultPrivilegeDescriptorType(t *testing.T) { - emptyDefaultPrivilegeDescriptor := descpb.DefaultPrivilegeDescriptor{} + emptyDefaultPrivilegeDescriptor := catpb.DefaultPrivilegeDescriptor{} // If Type is not set, it should resolve to - // descpb.DefaultPrivilegeDescriptor_DATABASE by default. - require.Equal(t, emptyDefaultPrivilegeDescriptor.Type, descpb.DefaultPrivilegeDescriptor_DATABASE) + // catpb.DefaultPrivilegeDescriptor_DATABASE by default. + require.Equal(t, emptyDefaultPrivilegeDescriptor.Type, catpb.DefaultPrivilegeDescriptor_DATABASE) } diff --git a/pkg/sql/catalog/catprivilege/fix.go b/pkg/sql/catalog/catprivilege/fix.go index fd9737e54e1b..3f64e57c1522 100644 --- a/pkg/sql/catalog/catprivilege/fix.go +++ b/pkg/sql/catalog/catprivilege/fix.go @@ -12,6 +12,7 @@ package catprivilege import ( "github.com/cockroachdb/cockroach/pkg/security" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/privilege" ) @@ -23,13 +24,13 @@ import ( // instead of ZONECONFIG. // Fortunately ZONECONFIG was only valid on TABLES/DB while USAGE is not valid // on either so we know if the descriptor was corrupted. -func MaybeFixUsagePrivForTablesAndDBs(ptr **descpb.PrivilegeDescriptor) bool { +func MaybeFixUsagePrivForTablesAndDBs(ptr **catpb.PrivilegeDescriptor) bool { if *ptr == nil { - *ptr = &descpb.PrivilegeDescriptor{} + *ptr = &catpb.PrivilegeDescriptor{} } p := *ptr - if p.Version > descpb.InitialVersion { + if p.Version > catpb.InitialVersion { // InitialVersion is for descriptors that were created in versions 20.1 and // earlier. If the privilege descriptor was created after 20.1, then we // do not have to fix it. Furthermore privilege descriptor versions are @@ -66,13 +67,13 @@ func MaybeFixUsagePrivForTablesAndDBs(ptr **descpb.PrivilegeDescriptor) bool { // * updating version field to Version21_2. // MaybeFixPrivileges can be removed after v21.2. func MaybeFixPrivileges( - ptr **descpb.PrivilegeDescriptor, + ptr **catpb.PrivilegeDescriptor, parentID, parentSchemaID descpb.ID, objectType privilege.ObjectType, objectName string, ) bool { if *ptr == nil { - *ptr = &descpb.PrivilegeDescriptor{} + *ptr = &catpb.PrivilegeDescriptor{} } p := *ptr allowedPrivilegesBits := privilege.GetValidPrivilegesForObject(objectType).ToBitField() @@ -132,8 +133,8 @@ func MaybeFixPrivileges( changed = true } - if p.Version < descpb.Version21_2 { - p.SetVersion(descpb.Version21_2) + if p.Version < catpb.Version21_2 { + p.SetVersion(catpb.Version21_2) changed = true } return changed @@ -142,7 +143,7 @@ func MaybeFixPrivileges( // MaybeUpdateGrantOptions iterates over the users of the descriptor and checks // if they have the GRANT privilege - if so, then set the user's grant option // bits equal to the privilege bits. -func MaybeUpdateGrantOptions(p *descpb.PrivilegeDescriptor) bool { +func MaybeUpdateGrantOptions(p *catpb.PrivilegeDescriptor) bool { // If admin has grant option bits set, then we know the descriptor was // created by a new binary, so all the other grant options are already // correct. Note that admin always has SELECT on *every* table including diff --git a/pkg/sql/catalog/catprivilege/fix_test.go b/pkg/sql/catalog/catprivilege/fix_test.go index a0ad688dce45..5d4e2e9dcae7 100644 --- a/pkg/sql/catalog/catprivilege/fix_test.go +++ b/pkg/sql/catalog/catprivilege/fix_test.go @@ -15,6 +15,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog/bootstrap" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/privilege" @@ -137,7 +138,7 @@ func TestFixPrivileges(t *testing.T) { } for num, testCase := range testCases { - desc := &descpb.PrivilegeDescriptor{} + desc := &catpb.PrivilegeDescriptor{} for u, p := range testCase.input { desc.Grant(u, p, false /* withGrantOption */) } @@ -184,7 +185,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { modified bool output userPrivileges objectType privilege.ObjectType - privDescVersion descpb.PrivilegeDescVersion + privDescVersion catpb.PrivilegeDescVersion description string isValid bool }{ @@ -198,7 +199,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { fooUser: privilege.List{privilege.ZONECONFIG}, }, privilege.Table, - descpb.InitialVersion, + catpb.InitialVersion, "A privilege descriptor from a table created in v20.1 or prior " + "(InitialVersion) with USAGE should have the privilege converted to ZONECONFIG.", true, @@ -212,7 +213,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { fooUser: privilege.List{privilege.ZONECONFIG}, }, privilege.Database, - descpb.InitialVersion, + catpb.InitialVersion, "A privilege descriptor from a database created in v20.1 or prior " + "(InitialVersion) with USAGE should have the privilege converted to ZONECONFIG.", true, @@ -226,7 +227,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { fooUser: privilege.List{privilege.ALL}, }, privilege.Table, - descpb.InitialVersion, + catpb.InitialVersion, "ALL should stay as ALL", true, }, @@ -239,7 +240,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { fooUser: privilege.List{privilege.USAGE}, }, privilege.Table, - descpb.OwnerVersion, + catpb.OwnerVersion, "A privilege descriptor from a table created in v20.2 onwards " + "(OwnerVersion) should not be modified.", false, @@ -253,7 +254,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { fooUser: privilege.List{privilege.USAGE}, }, privilege.Database, - descpb.OwnerVersion, + catpb.OwnerVersion, "A privilege descriptor from a Database created in v20.2 onwards " + "(OwnerVersion) should not be modified.", false, @@ -267,7 +268,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { fooUser: privilege.List{privilege.ZONECONFIG}, }, privilege.Table, - descpb.OwnerVersion, + catpb.OwnerVersion, "A privilege descriptor from a table created in v20.2 onwards " + "(OwnerVersion) should not be modified.", true, @@ -281,7 +282,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { fooUser: privilege.List{privilege.ZONECONFIG}, }, privilege.Database, - descpb.OwnerVersion, + catpb.OwnerVersion, "A privilege descriptor from a Database created in v20.2 onwards " + "(OwnerVersion) should not be modified.", true, @@ -298,7 +299,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { barUser: privilege.List{privilege.ZONECONFIG, privilege.CREATE, privilege.SELECT}, }, privilege.Table, - descpb.InitialVersion, + catpb.InitialVersion, "A privilege descriptor from a table created in v20.1 or prior " + "(InitialVersion) with USAGE should have the privilege converted to ZONECONFIG.", true, @@ -314,7 +315,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { barUser: privilege.List{privilege.ZONECONFIG, privilege.CREATE}, }, privilege.Database, - descpb.InitialVersion, + catpb.InitialVersion, "A privilege descriptor from a table created in v20.1 or prior " + "(InitialVersion) with USAGE should have the privilege converted to ZONECONFIG.", true, @@ -330,7 +331,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { barUser: privilege.List{privilege.ZONECONFIG, privilege.CREATE, privilege.GRANT}, }, privilege.Database, - descpb.InitialVersion, + catpb.InitialVersion, "A privilege descriptor from a database created in v20.1 or prior " + "(InitialVersion) with USAGE should have the privilege converted to ZONECONFIG.", true, @@ -348,7 +349,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { bazUser: privilege.List{privilege.ALL}, }, privilege.Database, - descpb.InitialVersion, + catpb.InitialVersion, "A privilege descriptor from a table created in v20.1 or prior " + "(InitialVersion) with USAGE should have the privilege converted to ZONECONFIG.", true, @@ -363,7 +364,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { fooUser: privilege.List{privilege.ZONECONFIG}, }, privilege.Table, - descpb.InitialVersion, + catpb.InitialVersion, "If the descriptor has USAGE and ZONECONFIG, it should become just " + "ZONECONFIG", true, @@ -371,7 +372,7 @@ func TestMaybeFixUsageAndZoneConfigPrivilege(t *testing.T) { } for num, tc := range testCases { - desc := &descpb.PrivilegeDescriptor{Version: tc.privDescVersion} + desc := &catpb.PrivilegeDescriptor{Version: tc.privDescVersion} for u, p := range tc.input { desc.Grant(u, p, false /* withGrantOption */) } @@ -478,7 +479,7 @@ func TestMaybeFixSchemaPrivileges(t *testing.T) { } for num, tc := range testCases { - desc := &descpb.PrivilegeDescriptor{} + desc := &catpb.PrivilegeDescriptor{} for u, p := range tc.input { desc.Grant(u, p, false /* withGrantOption */) } diff --git a/pkg/sql/catalog/catprivilege/validate.go b/pkg/sql/catalog/catprivilege/validate.go index d577acd5817e..af40333a4709 100644 --- a/pkg/sql/catalog/catprivilege/validate.go +++ b/pkg/sql/catalog/catprivilege/validate.go @@ -12,13 +12,13 @@ package catprivilege import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/privilege" ) // Validate validates a privilege descriptor. func Validate( - p descpb.PrivilegeDescriptor, objectNameKey catalog.NameKey, objectType privilege.ObjectType, + p catpb.PrivilegeDescriptor, objectNameKey catalog.NameKey, objectType privilege.ObjectType, ) error { return p.Validate( objectNameKey.GetParentID(), @@ -30,7 +30,7 @@ func Validate( // ValidateSuperuserPrivileges validates superuser privileges. func ValidateSuperuserPrivileges( - p descpb.PrivilegeDescriptor, objectNameKey catalog.NameKey, objectType privilege.ObjectType, + p catpb.PrivilegeDescriptor, objectNameKey catalog.NameKey, objectType privilege.ObjectType, ) error { return p.ValidateSuperuserPrivileges( objectNameKey.GetParentID(), @@ -41,7 +41,7 @@ func ValidateSuperuserPrivileges( } // ValidateDefaultPrivileges validates default privileges. -func ValidateDefaultPrivileges(p descpb.DefaultPrivilegeDescriptor) error { +func ValidateDefaultPrivileges(p catpb.DefaultPrivilegeDescriptor) error { return p.Validate() } @@ -50,5 +50,5 @@ func allowedSuperuserPrivileges(objectNameKey catalog.NameKey) privilege.List { if privs != nil { return privs } - return descpb.DefaultSuperuserPrivileges + return catpb.DefaultSuperuserPrivileges } diff --git a/pkg/sql/catalog/dbdesc/BUILD.bazel b/pkg/sql/catalog/dbdesc/BUILD.bazel index 0b88aafa9873..9efd0b0abd04 100644 --- a/pkg/sql/catalog/dbdesc/BUILD.bazel +++ b/pkg/sql/catalog/dbdesc/BUILD.bazel @@ -35,6 +35,7 @@ go_test( "//pkg/clusterversion", "//pkg/security", "//pkg/sql/catalog", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/internal/validate", "//pkg/sql/catalog/nstree", diff --git a/pkg/sql/catalog/dbdesc/database_desc.go b/pkg/sql/catalog/dbdesc/database_desc.go index 90443f02e100..3fe6c8d29b23 100644 --- a/pkg/sql/catalog/dbdesc/database_desc.go +++ b/pkg/sql/catalog/dbdesc/database_desc.go @@ -467,7 +467,7 @@ func (desc *Mutable) HasPostDeserializationChanges() bool { func (desc *immutable) GetDefaultPrivilegeDescriptor() catalog.DefaultPrivilegeDescriptor { defaultPrivilegeDescriptor := desc.GetDefaultPrivileges() if defaultPrivilegeDescriptor == nil { - defaultPrivilegeDescriptor = catprivilege.MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE) + defaultPrivilegeDescriptor = catprivilege.MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_DATABASE) } return catprivilege.MakeDefaultPrivileges(defaultPrivilegeDescriptor) } @@ -476,7 +476,7 @@ func (desc *immutable) GetDefaultPrivilegeDescriptor() catalog.DefaultPrivilegeD func (desc *Mutable) GetMutableDefaultPrivilegeDescriptor() *catprivilege.Mutable { defaultPrivilegeDescriptor := desc.GetDefaultPrivileges() if defaultPrivilegeDescriptor == nil { - defaultPrivilegeDescriptor = catprivilege.MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE) + defaultPrivilegeDescriptor = catprivilege.MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_DATABASE) } return catprivilege.NewMutableDefaultPrivileges(defaultPrivilegeDescriptor) } @@ -484,7 +484,7 @@ func (desc *Mutable) GetMutableDefaultPrivilegeDescriptor() *catprivilege.Mutabl // SetDefaultPrivilegeDescriptor sets the default privilege descriptor // for the database. func (desc *Mutable) SetDefaultPrivilegeDescriptor( - defaultPrivilegeDescriptor *descpb.DefaultPrivilegeDescriptor, + defaultPrivilegeDescriptor *catpb.DefaultPrivilegeDescriptor, ) { desc.DefaultPrivileges = defaultPrivilegeDescriptor } diff --git a/pkg/sql/catalog/dbdesc/database_desc_builder.go b/pkg/sql/catalog/dbdesc/database_desc_builder.go index 86d0230d9bcc..45b3811614a0 100644 --- a/pkg/sql/catalog/dbdesc/database_desc_builder.go +++ b/pkg/sql/catalog/dbdesc/database_desc_builder.go @@ -14,6 +14,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/multiregion" @@ -65,7 +66,8 @@ func (ddb *databaseDescriptorBuilder) RunPostDeserializationChanges() { // want any default privileges on the system database. if ddb.original.GetID() != keys.SystemDatabaseID { if ddb.maybeModified.DefaultPrivileges == nil { - ddb.maybeModified.DefaultPrivileges = catprivilege.MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE) + ddb.maybeModified.DefaultPrivileges = catprivilege.MakeDefaultPrivilegeDescriptor( + catpb.DefaultPrivilegeDescriptor_DATABASE) createdDefaultPrivileges = true } @@ -94,7 +96,7 @@ func (ddb *databaseDescriptorBuilder) RunRestoreChanges( } func maybeConvertIncompatibleDBPrivilegesToDefaultPrivileges( - privileges *descpb.PrivilegeDescriptor, defaultPrivileges *descpb.DefaultPrivilegeDescriptor, + privileges *catpb.PrivilegeDescriptor, defaultPrivileges *catpb.DefaultPrivilegeDescriptor, ) (hasChanged bool) { // If privileges are nil, there is nothing to convert. // This case can happen during restore where privileges are not yet created. @@ -121,7 +123,7 @@ func maybeConvertIncompatibleDBPrivilegesToDefaultPrivileges( privileges.Users[i] = user // Convert the incompatible privileges to default privileges. - role := defaultPrivileges.FindOrCreateUser(descpb.DefaultPrivilegesRole{ForAllRoles: true}) + role := defaultPrivileges.FindOrCreateUser(catpb.DefaultPrivilegesRole{ForAllRoles: true}) tableDefaultPrivilegesForAllRoles := role.DefaultPrivilegesPerObject[tree.Tables] defaultPrivilegesForUser := tableDefaultPrivilegesForAllRoles.FindOrCreateUser(user.User()) @@ -229,8 +231,8 @@ func NewInitial( return newInitialWithPrivileges( id, name, - descpb.NewBaseDatabasePrivilegeDescriptor(owner), - catprivilege.MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_DATABASE), + catpb.NewBaseDatabasePrivilegeDescriptor(owner), + catprivilege.MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_DATABASE), options..., ) } @@ -240,8 +242,8 @@ func NewInitial( func newInitialWithPrivileges( id descpb.ID, name string, - privileges *descpb.PrivilegeDescriptor, - defaultPrivileges *descpb.DefaultPrivilegeDescriptor, + privileges *catpb.PrivilegeDescriptor, + defaultPrivileges *catpb.DefaultPrivilegeDescriptor, options ...NewInitialOption, ) *Mutable { ret := descpb.DatabaseDescriptor{ diff --git a/pkg/sql/catalog/dbdesc/database_test.go b/pkg/sql/catalog/dbdesc/database_test.go index 1aa5cc0d00f1..3b87b3d3c165 100644 --- a/pkg/sql/catalog/dbdesc/database_test.go +++ b/pkg/sql/catalog/dbdesc/database_test.go @@ -18,6 +18,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/clusterversion" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/internal/validate" "github.com/cockroachdb/cockroach/pkg/sql/catalog/nstree" @@ -100,7 +101,7 @@ func TestValidateDatabaseDesc(t *testing.T) { descpb.DatabaseDescriptor{ Name: "db", ID: 0, - Privileges: descpb.NewBaseDatabasePrivilegeDescriptor(security.RootUserName()), + Privileges: catpb.NewBaseDatabasePrivilegeDescriptor(security.RootUserName()), }, }, { @@ -109,7 +110,7 @@ func TestValidateDatabaseDesc(t *testing.T) { Name: "multi-region-db", ID: 200, RegionConfig: &descpb.DatabaseDescriptor_RegionConfig{}, - Privileges: descpb.NewBaseDatabasePrivilegeDescriptor(security.RootUserName()), + Privileges: catpb.NewBaseDatabasePrivilegeDescriptor(security.RootUserName()), }, }, } @@ -272,7 +273,7 @@ func TestValidateCrossDatabaseReferences(t *testing.T) { } for i, test := range tests { - privilege := descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) + privilege := catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) var cb nstree.MutableCatalog test.desc.Privileges = privilege desc := NewBuilder(&test.desc).BuildImmutable() @@ -316,7 +317,7 @@ func TestFixDroppedSchemaName(t *testing.T) { Schemas: map[string]descpb.DatabaseDescriptor_SchemaInfo{ dbName: {ID: dbID, Dropped: true}, }, - Privileges: descpb.NewBasePrivilegeDescriptor(security.RootUserName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.RootUserName()), } b := NewBuilder(&dbDesc) b.RunPostDeserializationChanges() @@ -328,16 +329,16 @@ func TestFixDroppedSchemaName(t *testing.T) { func TestMaybeConvertIncompatibleDBPrivilegesToDefaultPrivileges(t *testing.T) { tests := []struct { - privilegeDesc descpb.PrivilegeDescriptor - defaultPrivilegeDesc descpb.DefaultPrivilegeDescriptor + privilegeDesc catpb.PrivilegeDescriptor + defaultPrivilegeDesc catpb.DefaultPrivilegeDescriptor privileges privilege.List incompatiblePrivileges privilege.List shouldChange bool users []security.SQLUsername }{ { // 0 - privilegeDesc: descpb.PrivilegeDescriptor{}, - defaultPrivilegeDesc: descpb.DefaultPrivilegeDescriptor{}, + privilegeDesc: catpb.PrivilegeDescriptor{}, + defaultPrivilegeDesc: catpb.DefaultPrivilegeDescriptor{}, privileges: privilege.List{privilege.SELECT}, incompatiblePrivileges: privilege.List{privilege.SELECT}, shouldChange: true, @@ -346,8 +347,8 @@ func TestMaybeConvertIncompatibleDBPrivilegesToDefaultPrivileges(t *testing.T) { }, }, { // 1 - privilegeDesc: descpb.PrivilegeDescriptor{}, - defaultPrivilegeDesc: descpb.DefaultPrivilegeDescriptor{}, + privilegeDesc: catpb.PrivilegeDescriptor{}, + defaultPrivilegeDesc: catpb.DefaultPrivilegeDescriptor{}, privileges: privilege.List{ privilege.CONNECT, privilege.CREATE, privilege.DROP, privilege.GRANT, privilege.ZONECONFIG, }, @@ -358,8 +359,8 @@ func TestMaybeConvertIncompatibleDBPrivilegesToDefaultPrivileges(t *testing.T) { }, }, { // 2 - privilegeDesc: descpb.PrivilegeDescriptor{}, - defaultPrivilegeDesc: descpb.DefaultPrivilegeDescriptor{}, + privilegeDesc: catpb.PrivilegeDescriptor{}, + defaultPrivilegeDesc: catpb.DefaultPrivilegeDescriptor{}, privileges: privilege.List{privilege.SELECT, privilege.INSERT, privilege.UPDATE, privilege.DELETE}, incompatiblePrivileges: privilege.List{privilege.SELECT, privilege.INSERT, privilege.UPDATE, privilege.DELETE}, shouldChange: true, @@ -368,8 +369,8 @@ func TestMaybeConvertIncompatibleDBPrivilegesToDefaultPrivileges(t *testing.T) { }, }, { // 3 - privilegeDesc: descpb.PrivilegeDescriptor{}, - defaultPrivilegeDesc: descpb.DefaultPrivilegeDescriptor{}, + privilegeDesc: catpb.PrivilegeDescriptor{}, + defaultPrivilegeDesc: catpb.DefaultPrivilegeDescriptor{}, privileges: privilege.List{privilege.SELECT}, incompatiblePrivileges: privilege.List{privilege.SELECT}, shouldChange: true, @@ -379,8 +380,8 @@ func TestMaybeConvertIncompatibleDBPrivilegesToDefaultPrivileges(t *testing.T) { }, }, { // 4 - privilegeDesc: descpb.PrivilegeDescriptor{}, - defaultPrivilegeDesc: descpb.DefaultPrivilegeDescriptor{}, + privilegeDesc: catpb.PrivilegeDescriptor{}, + defaultPrivilegeDesc: catpb.DefaultPrivilegeDescriptor{}, privileges: privilege.List{ privilege.CONNECT, privilege.CREATE, privilege.DROP, privilege.GRANT, privilege.ZONECONFIG, }, @@ -409,11 +410,15 @@ func TestMaybeConvertIncompatibleDBPrivilegesToDefaultPrivileges(t *testing.T) { t.Errorf("found incompatible privilege %s", privilege.String()) } - forAllRoles := test.defaultPrivilegeDesc.FindOrCreateUser(descpb.DefaultPrivilegesRole{ForAllRoles: true}) + forAllRoles := test.defaultPrivilegeDesc. + FindOrCreateUser(catpb.DefaultPrivilegesRole{ForAllRoles: true}) // Check that the incompatible privileges have been converted to the // equivalent default privileges. if !forAllRoles.DefaultPrivilegesPerObject[tree.Tables].CheckPrivilege(testUser, privilege) { - t.Errorf("expected incompatible privilege %s to be converted to a default privilege", privilege.String()) + t.Errorf( + "expected incompatible privilege %s to be converted to a default privilege", + privilege.String(), + ) } } } diff --git a/pkg/sql/catalog/descidgen/BUILD.bazel b/pkg/sql/catalog/descidgen/BUILD.bazel index 9faac0ab61e1..013213bb6489 100644 --- a/pkg/sql/catalog/descidgen/BUILD.bazel +++ b/pkg/sql/catalog/descidgen/BUILD.bazel @@ -1,28 +1,4 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") -load("//build:STRINGER.bzl", "stringer") - -go_library( - name = "catalogkv", - srcs = ["catalogkv.go"], - importpath = "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkv", - visibility = ["//visibility:public"], - deps = [ - "//pkg/clusterversion", - "//pkg/keys", - "//pkg/kv", - "//pkg/sql/catalog", - "//pkg/sql/catalog/catalogkeys", - "//pkg/sql/catalog/descpb", - "//pkg/sql/sem/tree", - "//pkg/util/log", - ], -) - -stringer( - name = "gen-descriptorkind-stringer", - src = "catalogkv.go", - typ = "DescriptorKind", -) go_library( name = "descidgen", diff --git a/pkg/sql/catalog/descpb/BUILD.bazel b/pkg/sql/catalog/descpb/BUILD.bazel index 25515b7a0c88..5ea27b4ace83 100644 --- a/pkg/sql/catalog/descpb/BUILD.bazel +++ b/pkg/sql/catalog/descpb/BUILD.bazel @@ -8,29 +8,24 @@ go_library( srcs = [ "column.go", "constraint.go", - "default_privilege.go", "descriptor.go", "index.go", "index_fetch.go", "join_type.go", "locking.go", - "privilege.go", "structured.go", ":gen-formatversion-stringer", # keep - ":gen-privilegedescversion-stringer", # keep ], embed = [":descpb_go_proto"], importpath = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb", visibility = ["//visibility:public"], deps = [ "//pkg/keys", - "//pkg/security", "//pkg/sql/catalog/catconstants", "//pkg/sql/catalog/catpb", "//pkg/sql/parser", "//pkg/sql/pgwire/pgcode", "//pkg/sql/pgwire/pgerror", - "//pkg/sql/privilege", "//pkg/sql/protoreflect", "//pkg/sql/sem/catid", "//pkg/sql/sem/tree", @@ -47,19 +42,8 @@ go_library( go_test( name = "descpb_test", size = "small", - srcs = [ - "privilege_test.go", - "structured_test.go", - ], + srcs = ["structured_test.go"], embed = [":descpb"], - deps = [ - "//pkg/keys", - "//pkg/security", - "//pkg/sql/catalog/bootstrap", - "//pkg/sql/privilege", - "//pkg/testutils", - "//pkg/util/leaktest", - ], ) proto_library( @@ -69,7 +53,6 @@ proto_library( "index_fetch.proto", "join_type.proto", "locking.proto", - "privilege.proto", "structured.proto", "tenant.proto", ], @@ -101,12 +84,6 @@ go_proto_library( ], ) -stringer( - name = "gen-privilegedescversion-stringer", - src = "privilege.go", - typ = "PrivilegeDescVersion", -) - stringer( name = "gen-formatversion-stringer", src = "structured.go", diff --git a/pkg/sql/catalog/descpb/structured.go b/pkg/sql/catalog/descpb/structured.go index b5a61ed108f3..431b3669876a 100644 --- a/pkg/sql/catalog/descpb/structured.go +++ b/pkg/sql/catalog/descpb/structured.go @@ -41,7 +41,7 @@ func (dir IndexDescriptor_Direction) ToEncodingDirection() (encoding.Direction, type ID = catid.DescID // InvalidID is the uninitialised descriptor id. -const InvalidID ID = 0 +const InvalidID = catid.InvalidDescID // IDs is a sortable list of IDs. type IDs []ID diff --git a/pkg/sql/catalog/descpb/structured.proto b/pkg/sql/catalog/descpb/structured.proto index 7aafc58f5032..258d91f7e99b 100644 --- a/pkg/sql/catalog/descpb/structured.proto +++ b/pkg/sql/catalog/descpb/structured.proto @@ -15,7 +15,7 @@ option go_package = "descpb"; import "util/hlc/timestamp.proto"; import "sql/catalog/catpb/catalog.proto"; -import "sql/catalog/descpb/privilege.proto"; +import "sql/catalog/catpb/privilege.proto"; import "sql/types/types.proto"; import "geo/geoindex/config.proto"; import "gogoproto/gogo.proto"; diff --git a/pkg/sql/catalog/descriptor.go b/pkg/sql/catalog/descriptor.go index 79a9a8d4e580..511eb60ae392 100644 --- a/pkg/sql/catalog/descriptor.go +++ b/pkg/sql/catalog/descriptor.go @@ -161,7 +161,7 @@ type Descriptor interface { // describes the set of privileges that users have to use, modify, or delete // the object represented by this descriptor. Each descriptor type may have a // different set of capabilities represented by the PrivilegeDescriptor. - GetPrivileges() *descpb.PrivilegeDescriptor + GetPrivileges() *catpb.PrivilegeDescriptor // DescriptorType returns the type of this descriptor (like relation, type, // schema, database). DescriptorType() DescriptorType @@ -739,9 +739,9 @@ type TypeDescriptorResolver interface { // DefaultPrivilegeDescriptor protos are not accessed and interacted // with directly. type DefaultPrivilegeDescriptor interface { - GetDefaultPrivilegesForRole(descpb.DefaultPrivilegesRole) (*descpb.DefaultPrivilegesForRole, bool) - ForEachDefaultPrivilegeForRole(func(descpb.DefaultPrivilegesForRole) error) error - GetDefaultPrivilegeDescriptorType() descpb.DefaultPrivilegeDescriptor_DefaultPrivilegeDescriptorType + GetDefaultPrivilegesForRole(catpb.DefaultPrivilegesRole) (*catpb.DefaultPrivilegesForRole, bool) + ForEachDefaultPrivilegeForRole(func(catpb.DefaultPrivilegesForRole) error) error + GetDefaultPrivilegeDescriptorType() catpb.DefaultPrivilegeDescriptor_DefaultPrivilegeDescriptorType } // FilterDescriptorState inspects the state of a given descriptor and returns an diff --git a/pkg/sql/catalog/descs/BUILD.bazel b/pkg/sql/catalog/descs/BUILD.bazel index 7f53c69d11d5..56827805591d 100644 --- a/pkg/sql/catalog/descs/BUILD.bazel +++ b/pkg/sql/catalog/descs/BUILD.bazel @@ -90,6 +90,7 @@ go_test( "//pkg/sql", "//pkg/sql/catalog", "//pkg/sql/catalog/catalogkeys", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/lease", "//pkg/sql/catalog/tabledesc", diff --git a/pkg/sql/catalog/descs/collection_test.go b/pkg/sql/catalog/descs/collection_test.go index 2dec131b4034..28e9dd943efd 100644 --- a/pkg/sql/catalog/descs/collection_test.go +++ b/pkg/sql/catalog/descs/collection_test.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" "github.com/cockroachdb/cockroach/pkg/sql/catalog/lease" @@ -100,7 +101,7 @@ func TestCollectionWriteDescToBatch(t *testing.T) { Version: descpb.PrimaryIndexWithStoredColumnsVersion, ConstraintID: 1, }, - Privileges: descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), NextColumnID: 2, NextConstraintID: 2, NextFamilyID: 1, diff --git a/pkg/sql/catalog/internal/validate/BUILD.bazel b/pkg/sql/catalog/internal/validate/BUILD.bazel index 9bd6fe51b74a..4ef5093f30f3 100644 --- a/pkg/sql/catalog/internal/validate/BUILD.bazel +++ b/pkg/sql/catalog/internal/validate/BUILD.bazel @@ -14,17 +14,3 @@ go_library( "@com_github_cockroachdb_errors//:errors", ], ) - -go_library( - name = "catval", - srcs = ["validate.go"], - importpath = "github.com/cockroachdb/cockroach/pkg/sql/catalog/internal/catval", - visibility = ["//pkg/sql/catalog:__subpackages__"], - deps = [ - "//pkg/keys", - "//pkg/server/telemetry", - "//pkg/sql/catalog", - "//pkg/sql/catalog/descpb", - "@com_github_cockroachdb_errors//:errors", - ], -) diff --git a/pkg/sql/catalog/schemadesc/BUILD.bazel b/pkg/sql/catalog/schemadesc/BUILD.bazel index 46c8b796d1bd..09eae6fea9a9 100644 --- a/pkg/sql/catalog/schemadesc/BUILD.bazel +++ b/pkg/sql/catalog/schemadesc/BUILD.bazel @@ -16,6 +16,7 @@ go_library( "//pkg/keys", "//pkg/sql/catalog", "//pkg/sql/catalog/catconstants", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/catprivilege", "//pkg/sql/catalog/descpb", "//pkg/sql/pgwire/pgcode", @@ -39,6 +40,7 @@ go_test( "//pkg/clusterversion", "//pkg/security", "//pkg/sql/catalog", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/dbdesc", "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/nstree", diff --git a/pkg/sql/catalog/schemadesc/public_schema_desc.go b/pkg/sql/catalog/schemadesc/public_schema_desc.go index 221deb7b5bc2..090041f41fa7 100644 --- a/pkg/sql/catalog/schemadesc/public_schema_desc.go +++ b/pkg/sql/catalog/schemadesc/public_schema_desc.go @@ -13,6 +13,7 @@ package schemadesc import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" ) @@ -42,8 +43,8 @@ var _ catalog.SchemaDescriptor = public{} func (p public) GetParentID() descpb.ID { return descpb.InvalidID } func (p public) GetID() descpb.ID { return keys.PublicSchemaID } func (p public) GetName() string { return tree.PublicSchema } -func (p public) GetPrivileges() *descpb.PrivilegeDescriptor { - return descpb.NewPublicSchemaPrivilegeDescriptor() +func (p public) GetPrivileges() *catpb.PrivilegeDescriptor { + return catpb.NewPublicSchemaPrivilegeDescriptor() } type publicBase struct{} diff --git a/pkg/sql/catalog/schemadesc/schema_desc.go b/pkg/sql/catalog/schemadesc/schema_desc.go index dc5d89756d51..30ee62396a56 100644 --- a/pkg/sql/catalog/schemadesc/schema_desc.go +++ b/pkg/sql/catalog/schemadesc/schema_desc.go @@ -17,6 +17,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catconstants" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" @@ -240,7 +241,7 @@ func (desc *immutable) ValidateTxnCommit( func (desc *immutable) GetDefaultPrivilegeDescriptor() catalog.DefaultPrivilegeDescriptor { defaultPrivilegeDescriptor := desc.GetDefaultPrivileges() if defaultPrivilegeDescriptor == nil { - defaultPrivilegeDescriptor = catprivilege.MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_SCHEMA) + defaultPrivilegeDescriptor = catprivilege.MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_SCHEMA) } return catprivilege.MakeDefaultPrivileges(defaultPrivilegeDescriptor) } @@ -329,7 +330,7 @@ func (desc *Mutable) HasPostDeserializationChanges() bool { func (desc *Mutable) GetMutableDefaultPrivilegeDescriptor() *catprivilege.Mutable { defaultPrivilegeDescriptor := desc.GetDefaultPrivileges() if defaultPrivilegeDescriptor == nil { - defaultPrivilegeDescriptor = catprivilege.MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_SCHEMA) + defaultPrivilegeDescriptor = catprivilege.MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_SCHEMA) } return catprivilege.NewMutableDefaultPrivileges(defaultPrivilegeDescriptor) } @@ -337,7 +338,7 @@ func (desc *Mutable) GetMutableDefaultPrivilegeDescriptor() *catprivilege.Mutabl // SetDefaultPrivilegeDescriptor sets the default privilege descriptor // for the database. func (desc *Mutable) SetDefaultPrivilegeDescriptor( - defaultPrivilegeDescriptor *descpb.DefaultPrivilegeDescriptor, + defaultPrivilegeDescriptor *catpb.DefaultPrivilegeDescriptor, ) { desc.DefaultPrivileges = defaultPrivilegeDescriptor } diff --git a/pkg/sql/catalog/schemadesc/schema_desc_test.go b/pkg/sql/catalog/schemadesc/schema_desc_test.go index 63a4104bb03e..b7177ffa36d8 100644 --- a/pkg/sql/catalog/schemadesc/schema_desc_test.go +++ b/pkg/sql/catalog/schemadesc/schema_desc_test.go @@ -18,6 +18,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/clusterversion" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/nstree" @@ -165,7 +166,7 @@ func TestValidateCrossSchemaReferences(t *testing.T) { } for i, test := range tests { - privilege := descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) + privilege := catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) var cb nstree.MutableCatalog test.desc.Privileges = privilege desc := schemadesc.NewBuilder(&test.desc).BuildImmutable() diff --git a/pkg/sql/catalog/schemadesc/synthetic_schema_desc.go b/pkg/sql/catalog/schemadesc/synthetic_schema_desc.go index 52b30c813ac2..aff242cc854a 100644 --- a/pkg/sql/catalog/schemadesc/synthetic_schema_desc.go +++ b/pkg/sql/catalog/schemadesc/synthetic_schema_desc.go @@ -14,6 +14,7 @@ import ( "context" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/util/hlc" @@ -51,7 +52,7 @@ func (p synthetic) GetModificationTime() hlc.Timestamp { func (p synthetic) GetDrainingNames() []descpb.NameInfo { return nil } -func (p synthetic) GetPrivileges() *descpb.PrivilegeDescriptor { +func (p synthetic) GetPrivileges() *catpb.PrivilegeDescriptor { log.Fatalf(context.TODO(), "cannot access privileges on a %s descriptor", p.kindName()) return nil } @@ -110,5 +111,5 @@ func (p synthetic) SchemaDesc() *descpb.SchemaDescriptor { // GetDefaultPrivilegeDescriptor returns a DefaultPrivilegeDescriptor. func (p synthetic) GetDefaultPrivilegeDescriptor() catalog.DefaultPrivilegeDescriptor { - return catprivilege.MakeDefaultPrivileges(catprivilege.MakeDefaultPrivilegeDescriptor(descpb.DefaultPrivilegeDescriptor_SCHEMA)) + return catprivilege.MakeDefaultPrivileges(catprivilege.MakeDefaultPrivilegeDescriptor(catpb.DefaultPrivilegeDescriptor_SCHEMA)) } diff --git a/pkg/sql/catalog/schemadesc/temporary_schema_desc.go b/pkg/sql/catalog/schemadesc/temporary_schema_desc.go index 5cf174a61394..9fe89a4f6300 100644 --- a/pkg/sql/catalog/schemadesc/temporary_schema_desc.go +++ b/pkg/sql/catalog/schemadesc/temporary_schema_desc.go @@ -12,6 +12,7 @@ package schemadesc import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" ) @@ -45,8 +46,8 @@ var _ catalog.SchemaDescriptor = temporary{} func (p temporary) GetID() descpb.ID { return p.id } func (p temporary) GetName() string { return p.name } func (p temporary) GetParentID() descpb.ID { return p.parentID } -func (p temporary) GetPrivileges() *descpb.PrivilegeDescriptor { - return descpb.NewTemporarySchemaPrivilegeDescriptor() +func (p temporary) GetPrivileges() *catpb.PrivilegeDescriptor { + return catpb.NewTemporarySchemaPrivilegeDescriptor() } type temporaryBase struct{} diff --git a/pkg/sql/catalog/schemadesc/virtual_schema_desc.go b/pkg/sql/catalog/schemadesc/virtual_schema_desc.go index 559d91adee34..92360f544883 100644 --- a/pkg/sql/catalog/schemadesc/virtual_schema_desc.go +++ b/pkg/sql/catalog/schemadesc/virtual_schema_desc.go @@ -13,6 +13,7 @@ package schemadesc import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catconstants" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" ) @@ -55,8 +56,8 @@ var _ catalog.SchemaDescriptor = virtual{} func (p virtual) GetID() descpb.ID { return p.id } func (p virtual) GetName() string { return p.name } func (p virtual) GetParentID() descpb.ID { return descpb.InvalidID } -func (p virtual) GetPrivileges() *descpb.PrivilegeDescriptor { - return descpb.NewVirtualSchemaPrivilegeDescriptor() +func (p virtual) GetPrivileges() *catpb.PrivilegeDescriptor { + return catpb.NewVirtualSchemaPrivilegeDescriptor() } type virtualBase struct{} diff --git a/pkg/sql/catalog/systemschema/system.go b/pkg/sql/catalog/systemschema/system.go index 731a6bdab95c..c49658522639 100644 --- a/pkg/sql/catalog/systemschema/system.go +++ b/pkg/sql/catalog/systemschema/system.go @@ -721,7 +721,7 @@ func MakeSystemDatabaseDesc() catalog.DatabaseDescriptor { ID: keys.SystemDatabaseID, Version: 1, // Assign max privileges to root user. - Privileges: descpb.NewCustomSuperuserPrivilegeDescriptor( + Privileges: catpb.NewCustomSuperuserPrivilegeDescriptor( priv, security.NodeUserName()), }).BuildImmutableDatabase() } @@ -789,7 +789,7 @@ func registerSystemTable( if privs == nil { log.Fatalf(ctx, "No superuser privileges found when building descriptor of system table %q", tbl.Name) } - tbl.Privileges = descpb.NewCustomSuperuserPrivilegeDescriptor(privs, security.NodeUserName()) + tbl.Privileges = catpb.NewCustomSuperuserPrivilegeDescriptor(privs, security.NodeUserName()) } for _, fn := range fns { fn(&tbl) @@ -1452,8 +1452,8 @@ var ( }, ), func(tbl *descpb.TableDescriptor) { - tbl.Privileges.Version = descpb.Version21_2 - tbl.Privileges.Users = append(tbl.Privileges.Users, descpb.UserPrivileges{ + tbl.Privileges.Version = catpb.Version21_2 + tbl.Privileges.Users = append(tbl.Privileges.Users, catpb.UserPrivileges{ UserProto: security.PublicRoleName().EncodeProto(), Privileges: privilege.List{privilege.SELECT}.ToBitField(), }) diff --git a/pkg/sql/catalog/tabledesc/safe_format_test.go b/pkg/sql/catalog/tabledesc/safe_format_test.go index 16c2f32903ac..b27e693083e5 100644 --- a/pkg/sql/catalog/tabledesc/safe_format_test.go +++ b/pkg/sql/catalog/tabledesc/safe_format_test.go @@ -273,7 +273,7 @@ func TestSafeMessage(t *testing.T) { tc.parentID, tc.id, tc.schema, - descpb.NewBasePrivilegeDescriptor(security.RootUserName()), + catpb.NewBasePrivilegeDescriptor(security.RootUserName()), ) require.NoError(t, err) var td catalog.TableDescriptor diff --git a/pkg/sql/catalog/tabledesc/structured_test.go b/pkg/sql/catalog/tabledesc/structured_test.go index 36328f3611ec..7a0bafa74f02 100644 --- a/pkg/sql/catalog/tabledesc/structured_test.go +++ b/pkg/sql/catalog/tabledesc/structured_test.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/bootstrap" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/desctestutils" "github.com/cockroachdb/cockroach/pkg/sql/catalog/internal/validate" @@ -81,7 +82,7 @@ func TestAllocateIDs(t *testing.T) { return idx }(), }, - Privileges: descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), FormatVersion: descpb.InterleavedFormatVersion, }).BuildCreatedMutableTable() if err := desc.AllocateIDs(ctx, clusterversion.TestingClusterVersion); err != nil { @@ -131,7 +132,7 @@ func TestAllocateIDs(t *testing.T) { EncodingType: descpb.PrimaryIndexEncoding, Version: descpb.LatestNonPrimaryIndexDescriptorVersion}, }, - Privileges: descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), NextColumnID: 4, NextFamilyID: 1, NextIndexID: 5, @@ -282,7 +283,7 @@ func TestMaybeUpgradeFormatVersion(t *testing.T) { Columns: []descpb.ColumnDescriptor{ {ID: 1, Name: "foo"}, }, - Privileges: descpb.NewBasePrivilegeDescriptor(security.RootUserName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.RootUserName()), }, expUpgrade: true, verify: func(i int, desc catalog.TableDescriptor) { @@ -298,7 +299,7 @@ func TestMaybeUpgradeFormatVersion(t *testing.T) { Columns: []descpb.ColumnDescriptor{ {ID: 1, Name: "foo"}, }, - Privileges: descpb.NewBasePrivilegeDescriptor(security.RootUserName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.RootUserName()), }, expUpgrade: false, verify: nil, @@ -380,7 +381,7 @@ func TestMaybeUpgradeIndexFormatVersion(t *testing.T) { Version: descpb.LatestPrimaryIndexDescriptorVersion, ConstraintID: 1, }, - Privileges: descpb.NewBasePrivilegeDescriptor(security.RootUserName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.RootUserName()), }, }, { // 2 @@ -427,7 +428,7 @@ func TestMaybeUpgradeIndexFormatVersion(t *testing.T) { Version: descpb.SecondaryIndexFamilyFormatVersion, }, }, - Privileges: descpb.NewBasePrivilegeDescriptor(security.RootUserName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.RootUserName()), }, upgraded: nil, }, @@ -563,7 +564,7 @@ func TestMaybeUpgradeIndexFormatVersion(t *testing.T) { Version: descpb.LatestNonPrimaryIndexDescriptorVersion, }, }, - Privileges: descpb.NewBasePrivilegeDescriptor(security.RootUserName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.RootUserName()), }, }, } @@ -617,7 +618,7 @@ func TestUnvalidateConstraints(t *testing.T) { {Name: "c", Type: types.Int}}, FormatVersion: descpb.InterleavedFormatVersion, Indexes: []descpb.IndexDescriptor{makeIndexDescriptor("d", []string{"b", "a"})}, - Privileges: descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), OutboundFKs: []descpb.ForeignKeyConstraint{ { Name: "fk", diff --git a/pkg/sql/catalog/tabledesc/table.go b/pkg/sql/catalog/tabledesc/table.go index a90724d15b6d..ec8acb9551c7 100644 --- a/pkg/sql/catalog/tabledesc/table.go +++ b/pkg/sql/catalog/tabledesc/table.go @@ -502,7 +502,7 @@ func InitTableDescriptor( id, parentID, parentSchemaID descpb.ID, name string, creationTime hlc.Timestamp, - privileges *descpb.PrivilegeDescriptor, + privileges *catpb.PrivilegeDescriptor, persistence tree.Persistence, ) Mutable { return Mutable{ diff --git a/pkg/sql/catalog/tabledesc/validate_test.go b/pkg/sql/catalog/tabledesc/validate_test.go index f83eca7b0f3a..76101b539ddd 100644 --- a/pkg/sql/catalog/tabledesc/validate_test.go +++ b/pkg/sql/catalog/tabledesc/validate_test.go @@ -1286,7 +1286,7 @@ func TestValidateTableDesc(t *testing.T) { NextFamilyID: 1, NextIndexID: 5, NextConstraintID: 2, - Privileges: descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), }}, {`index "sec" cannot store virtual column "c3"`, descpb.TableDescriptor{ @@ -1367,7 +1367,7 @@ func TestValidateTableDesc(t *testing.T) { NextColumnID: 4, NextFamilyID: 1, NextIndexID: 5, - Privileges: descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), }}, {`index "new_sec" cannot store virtual column "c3"`, descpb.TableDescriptor{ @@ -1448,7 +1448,7 @@ func TestValidateTableDesc(t *testing.T) { NextColumnID: 4, NextFamilyID: 1, NextIndexID: 5, - Privileges: descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), }}, {`index "sec" cannot store virtual column "v"`, descpb.TableDescriptor{ @@ -1868,7 +1868,7 @@ func TestValidateTableDesc(t *testing.T) { } for i, d := range testData { t.Run(d.err, func(t *testing.T) { - d.desc.Privileges = descpb.NewBasePrivilegeDescriptor(security.RootUserName()) + d.desc.Privileges = catpb.NewBasePrivilegeDescriptor(security.RootUserName()) desc := NewBuilder(&d.desc).BuildImmutableTable() expectedErr := fmt.Sprintf("%s %q (%d): %s", desc.DescriptorType(), desc.GetName(), desc.GetID(), d.err) err := validate.Self(clusterversion.TestingClusterVersion, desc) @@ -2241,7 +2241,7 @@ func TestValidateCrossTableReferences(t *testing.T) { var cb nstree.MutableCatalog cb.UpsertDescriptorEntry(dbdesc.NewBuilder(&descpb.DatabaseDescriptor{ID: 1}).BuildImmutable()) for _, otherDesc := range test.otherDescs { - otherDesc.Privileges = descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) + otherDesc.Privileges = catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()) cb.UpsertDescriptorEntry(NewBuilder(&otherDesc).BuildImmutable()) } desc := NewBuilder(&test.desc).BuildImmutable() @@ -2492,7 +2492,7 @@ func TestValidateConstraintID(t *testing.T) { KeyColumnDirections: []descpb.IndexDescriptor_Direction{descpb.IndexDescriptor_ASC}}, NextColumnID: 2, NextFamilyID: 1, - Privileges: descpb.NewPrivilegeDescriptor( + Privileges: catpb.NewPrivilegeDescriptor( security.PublicRoleName(), privilege.SchemaPrivileges, privilege.List{}, @@ -2519,7 +2519,7 @@ func TestValidateConstraintID(t *testing.T) { }, NextColumnID: 2, NextFamilyID: 1, - Privileges: descpb.NewPrivilegeDescriptor( + Privileges: catpb.NewPrivilegeDescriptor( security.PublicRoleName(), privilege.SchemaPrivileges, privilege.List{}, @@ -2542,7 +2542,7 @@ func TestValidateConstraintID(t *testing.T) { }, NextColumnID: 2, NextFamilyID: 1, - Privileges: descpb.NewPrivilegeDescriptor( + Privileges: catpb.NewPrivilegeDescriptor( security.PublicRoleName(), privilege.SchemaPrivileges, privilege.List{}, @@ -2565,7 +2565,7 @@ func TestValidateConstraintID(t *testing.T) { }, NextColumnID: 2, NextFamilyID: 1, - Privileges: descpb.NewPrivilegeDescriptor( + Privileges: catpb.NewPrivilegeDescriptor( security.PublicRoleName(), privilege.SchemaPrivileges, privilege.List{}, diff --git a/pkg/sql/catalog/typedesc/BUILD.bazel b/pkg/sql/catalog/typedesc/BUILD.bazel index a3470bd3b9f4..be44469fef9e 100644 --- a/pkg/sql/catalog/typedesc/BUILD.bazel +++ b/pkg/sql/catalog/typedesc/BUILD.bazel @@ -48,6 +48,7 @@ go_test( "//pkg/server", "//pkg/sql/catalog", "//pkg/sql/catalog/bootstrap", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/dbdesc", "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/internal/validate", diff --git a/pkg/sql/catalog/typedesc/safe_format_test.go b/pkg/sql/catalog/typedesc/safe_format_test.go index 14899cd8021a..993d3627a6aa 100644 --- a/pkg/sql/catalog/typedesc/safe_format_test.go +++ b/pkg/sql/catalog/typedesc/safe_format_test.go @@ -15,6 +15,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc" "github.com/cockroachdb/redact" @@ -32,7 +33,7 @@ func TestSafeMessage(t *testing.T) { Name: "foo", ID: 21, Version: 3, - Privileges: descpb.NewBasePrivilegeDescriptor(security.RootUserName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.RootUserName()), ParentID: 2, ParentSchemaID: 29, ArrayTypeID: 117, @@ -49,7 +50,7 @@ func TestSafeMessage(t *testing.T) { Name: "foo", ID: 21, Version: 3, - Privileges: descpb.NewBasePrivilegeDescriptor(security.RootUserName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.RootUserName()), ParentID: 2, ParentSchemaID: 29, ArrayTypeID: 117, diff --git a/pkg/sql/catalog/typedesc/table_implicit_record_type.go b/pkg/sql/catalog/typedesc/table_implicit_record_type.go index 750b31d5cf0f..7df267a05402 100644 --- a/pkg/sql/catalog/typedesc/table_implicit_record_type.go +++ b/pkg/sql/catalog/typedesc/table_implicit_record_type.go @@ -39,7 +39,7 @@ type TableImplicitRecordType struct { // by examining the privileges for the table that the record type corresponds // to, and providing the USAGE privilege if the table had the SELECT // privilege. - privs *descpb.PrivilegeDescriptor + privs *catpb.PrivilegeDescriptor } var _ catalog.TypeDescriptor = (*TableImplicitRecordType)(nil) @@ -82,7 +82,7 @@ func CreateImplicitRecordTypeFromTableDesc( } tablePrivs := descriptor.GetPrivileges() - newPrivs := make([]descpb.UserPrivileges, len(tablePrivs.Users)) + newPrivs := make([]catpb.UserPrivileges, len(tablePrivs.Users)) for i := range tablePrivs.Users { newPrivs[i].UserProto = tablePrivs.Users[i].UserProto // A table's record type has USAGE privs if a user has SELECT on the table. @@ -94,7 +94,7 @@ func CreateImplicitRecordTypeFromTableDesc( return &TableImplicitRecordType{ desc: descriptor, typ: typ, - privs: &descpb.PrivilegeDescriptor{ + privs: &catpb.PrivilegeDescriptor{ Users: newPrivs, OwnerProto: tablePrivs.OwnerProto, Version: tablePrivs.Version, @@ -132,7 +132,7 @@ func (v TableImplicitRecordType) GetDrainingNames() []descpb.NameInfo { } // GetPrivileges implements the Descriptor interface. -func (v TableImplicitRecordType) GetPrivileges() *descpb.PrivilegeDescriptor { +func (v TableImplicitRecordType) GetPrivileges() *catpb.PrivilegeDescriptor { return v.privs } diff --git a/pkg/sql/catalog/typedesc/type_desc_test.go b/pkg/sql/catalog/typedesc/type_desc_test.go index fff9373c7083..32f2528ba28d 100644 --- a/pkg/sql/catalog/typedesc/type_desc_test.go +++ b/pkg/sql/catalog/typedesc/type_desc_test.go @@ -21,6 +21,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/bootstrap" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/internal/validate" @@ -384,8 +385,8 @@ func TestValidateTypeDesc(t *testing.T) { }, }).BuildImmutable()) - defaultPrivileges := descpb.NewBasePrivilegeDescriptor(security.RootUserName()) - invalidPrivileges := descpb.NewBasePrivilegeDescriptor(security.RootUserName()) + defaultPrivileges := catpb.NewBasePrivilegeDescriptor(security.RootUserName()) + invalidPrivileges := catpb.NewBasePrivilegeDescriptor(security.RootUserName()) // Make the PrivilegeDescriptor invalid by granting SELECT to a type. invalidPrivileges.Grant(security.TestUserName(), privilege.List{privilege.SELECT}, false) typeDescID := descpb.ID(bootstrap.TestingUserDescID(0)) @@ -832,7 +833,7 @@ func TestTableImplicitTypeDescCannotBeSerializedOrValidated(t *testing.T) { ParentID: 1, ParentSchemaID: 1, Kind: descpb.TypeDescriptor_TABLE_IMPLICIT_RECORD_TYPE, - Privileges: descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), } desc := typedesc.NewBuilder(td).BuildImmutable() diff --git a/pkg/sql/colexec/colexecspan/BUILD.bazel b/pkg/sql/colexec/colexecspan/BUILD.bazel index 2cc139ae6466..27afb50c63c4 100644 --- a/pkg/sql/colexec/colexecspan/BUILD.bazel +++ b/pkg/sql/colexec/colexecspan/BUILD.bazel @@ -50,6 +50,7 @@ go_test( "//pkg/settings/cluster", "//pkg/sql/catalog", "//pkg/sql/catalog/bootstrap", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/tabledesc", "//pkg/sql/colconv", diff --git a/pkg/sql/colexec/colexecspan/span_assembler_test.go b/pkg/sql/colexec/colexecspan/span_assembler_test.go index c80bdf7ae37f..33398e1c2829 100644 --- a/pkg/sql/colexec/colexecspan/span_assembler_test.go +++ b/pkg/sql/colexec/colexecspan/span_assembler_test.go @@ -25,6 +25,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/bootstrap" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" "github.com/cockroachdb/cockroach/pkg/sql/colconv" @@ -193,7 +194,7 @@ func makeTable(useColFamilies bool) catalog.TableDescriptor { var testTableDesc = descpb.TableDescriptor{ Name: "abcd", ID: descpb.ID(tableID), - Privileges: descpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), + Privileges: catpb.NewBasePrivilegeDescriptor(security.AdminRoleName()), Version: 1, Columns: []descpb.ColumnDescriptor{ {Name: "a", ID: 1, Type: types.Int}, diff --git a/pkg/sql/crdb_internal.go b/pkg/sql/crdb_internal.go index 462996a28a82..e976c8e4f381 100644 --- a/pkg/sql/crdb_internal.go +++ b/pkg/sql/crdb_internal.go @@ -41,6 +41,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/settings" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catconstants" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" @@ -4918,7 +4919,7 @@ CREATE TABLE crdb_internal.default_privileges ( populate: func(ctx context.Context, p *planner, dbContext catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error { return forEachDatabaseDesc(ctx, p, nil /* all databases */, true, /* requiresPrivileges */ func(descriptor catalog.DatabaseDescriptor) error { - addRowHelper := func(defaultPrivilegesForRole descpb.DefaultPrivilegesForRole) error { + addRowHelper := func(defaultPrivilegesForRole catpb.DefaultPrivilegesForRole) error { role := tree.DNull forAllRoles := tree.DBoolTrue if defaultPrivilegesForRole.IsExplicitRole() { @@ -4980,13 +4981,13 @@ CREATE TABLE crdb_internal.default_privileges ( } return nil } - addRowForRole := func(role descpb.DefaultPrivilegesRole) error { + addRowForRole := func(role catpb.DefaultPrivilegesRole) error { defaultPrivilegeDescriptor := dbContext.GetDefaultPrivilegeDescriptor() defaultPrivilegesForRole, found := defaultPrivilegeDescriptor.GetDefaultPrivilegesForRole(role) if !found { // If an entry is not found for the role, the role still has // the default set of default privileges. - newDefaultPrivilegesForRole := descpb.InitDefaultPrivilegesForRole(role, defaultPrivilegeDescriptor.GetDefaultPrivilegeDescriptorType()) + newDefaultPrivilegesForRole := catpb.InitDefaultPrivilegesForRole(role, defaultPrivilegeDescriptor.GetDefaultPrivilegeDescriptorType()) defaultPrivilegesForRole = &newDefaultPrivilegesForRole } if err := addRowHelper(*defaultPrivilegesForRole); err != nil { @@ -4995,7 +4996,7 @@ CREATE TABLE crdb_internal.default_privileges ( return nil } if err := forEachRole(ctx, p, func(username security.SQLUsername, isRole bool, options roleOptions, settings tree.Datum) error { - role := descpb.DefaultPrivilegesRole{ + role := catpb.DefaultPrivilegesRole{ Role: username, } return addRowForRole(role) @@ -5004,7 +5005,7 @@ CREATE TABLE crdb_internal.default_privileges ( } // Handle ForAllRoles outside of forEachRole since it is a pseudo role. - role := descpb.DefaultPrivilegesRole{ + role := catpb.DefaultPrivilegesRole{ ForAllRoles: true, } return addRowForRole(role) diff --git a/pkg/sql/create_schema.go b/pkg/sql/create_schema.go index 12f13c7031a6..a6c4208817b4 100644 --- a/pkg/sql/create_schema.go +++ b/pkg/sql/create_schema.go @@ -19,6 +19,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descidgen" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" @@ -52,7 +53,7 @@ func CreateUserDefinedSchemaDescriptor( execCfg *ExecutorConfig, db catalog.DatabaseDescriptor, allocateID bool, -) (*schemadesc.Mutable, *descpb.PrivilegeDescriptor, error) { +) (*schemadesc.Mutable, *catpb.PrivilegeDescriptor, error) { authRole, err := n.AuthRole.ToSQLUsername(sessionData, security.UsernameValidation) if err != nil { return nil, nil, err @@ -133,7 +134,7 @@ func CreateSchemaDescriptorWithPrivileges( schemaName string, user, owner security.SQLUsername, allocateID bool, -) (*schemadesc.Mutable, *descpb.PrivilegeDescriptor, error) { +) (*schemadesc.Mutable, *catpb.PrivilegeDescriptor, error) { // Create the ID. var id descpb.ID var err error diff --git a/pkg/sql/create_sequence.go b/pkg/sql/create_sequence.go index 83e23144f035..da9337d73105 100644 --- a/pkg/sql/create_sequence.go +++ b/pkg/sql/create_sequence.go @@ -20,6 +20,7 @@ import ( clustersettings "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descbuilder" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descidgen" @@ -248,7 +249,7 @@ func NewSequenceTableDesc( schemaID descpb.ID, id descpb.ID, creationTime hlc.Timestamp, - privileges *descpb.PrivilegeDescriptor, + privileges *catpb.PrivilegeDescriptor, persistence tree.Persistence, isMultiRegion bool, ) (*tabledesc.Mutable, error) { diff --git a/pkg/sql/create_table.go b/pkg/sql/create_table.go index e84a5e75becb..4332d09bc82c 100644 --- a/pkg/sql/create_table.go +++ b/pkg/sql/create_table.go @@ -26,6 +26,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descidgen" @@ -1145,7 +1146,7 @@ func newTableDescIfAs( id descpb.ID, creationTime hlc.Timestamp, resultColumns []colinfo.ResultColumn, - privileges *descpb.PrivilegeDescriptor, + privileges *catpb.PrivilegeDescriptor, evalContext *tree.EvalContext, ) (desc *tabledesc.Mutable, err error) { if err := validateUniqueConstraintParamsForCreateTableAs(p); err != nil { @@ -1254,7 +1255,7 @@ func NewTableDesc( id descpb.ID, regionConfig *multiregion.RegionConfig, creationTime hlc.Timestamp, - privileges *descpb.PrivilegeDescriptor, + privileges *catpb.PrivilegeDescriptor, affected map[descpb.ID]*tabledesc.Mutable, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, @@ -2251,7 +2252,7 @@ func newTableDesc( sc catalog.SchemaDescriptor, id descpb.ID, creationTime hlc.Timestamp, - privileges *descpb.PrivilegeDescriptor, + privileges *catpb.PrivilegeDescriptor, affected map[descpb.ID]*tabledesc.Mutable, ) (ret *tabledesc.Mutable, err error) { if err := validateUniqueConstraintParamsForCreateTable(n); err != nil { diff --git a/pkg/sql/create_view.go b/pkg/sql/create_view.go index 491c080f1551..f6eb454dfd2c 100644 --- a/pkg/sql/create_view.go +++ b/pkg/sql/create_view.go @@ -20,6 +20,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descidgen" @@ -368,7 +369,7 @@ func makeViewTableDesc( id descpb.ID, resultColumns []colinfo.ResultColumn, creationTime hlc.Timestamp, - privileges *descpb.PrivilegeDescriptor, + privileges *catpb.PrivilegeDescriptor, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, st *cluster.Settings, diff --git a/pkg/sql/descriptor.go b/pkg/sql/descriptor.go index ad2badb03a43..8cd96227dcda 100644 --- a/pkg/sql/descriptor.go +++ b/pkg/sql/descriptor.go @@ -176,7 +176,7 @@ func (p *planner) maybeCreatePublicSchemaWithDescriptor( // Every database must be initialized with the public schema. // Create the SchemaDescriptor. - publicSchemaPrivileges := descpb.NewPublicSchemaPrivilegeDescriptor() + publicSchemaPrivileges := catpb.NewPublicSchemaPrivilegeDescriptor() publicSchemaDesc := schemadesc.NewBuilder(&descpb.SchemaDescriptor{ ParentID: dbID, Name: tree.PublicSchema, diff --git a/pkg/sql/doctor/BUILD.bazel b/pkg/sql/doctor/BUILD.bazel index edcb81f46c12..a05ec125886d 100644 --- a/pkg/sql/doctor/BUILD.bazel +++ b/pkg/sql/doctor/BUILD.bazel @@ -35,6 +35,7 @@ go_test( "//pkg/jobs/jobspb", "//pkg/keys", "//pkg/security", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/catprivilege", "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/tabledesc", diff --git a/pkg/sql/doctor/doctor_test.go b/pkg/sql/doctor/doctor_test.go index 4fe917ccc158..c601a22e5c2c 100644 --- a/pkg/sql/doctor/doctor_test.go +++ b/pkg/sql/doctor/doctor_test.go @@ -22,6 +22,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/security" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" @@ -60,7 +61,7 @@ var validTableDesc = &descpb.Descriptor{ ConstraintID: 1, }, NextIndexID: 2, - Privileges: descpb.NewCustomSuperuserPrivilegeDescriptor( + Privileges: catpb.NewCustomSuperuserPrivilegeDescriptor( privilege.ReadWriteData, security.NodeUserName()), FormatVersion: descpb.InterleavedFormatVersion, NextMutationID: 1, diff --git a/pkg/sql/drop_role.go b/pkg/sql/drop_role.go index 4a819fcbe984..7ac965c9e13e 100644 --- a/pkg/sql/drop_role.go +++ b/pkg/sql/drop_role.go @@ -17,7 +17,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" - "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/roleoption" @@ -426,8 +426,8 @@ func accumulateDependentDefaultPrivileges( // The func we pass into ForEachDefaultPrivilegeForRole will never // err so no err will be returned. return defaultPrivilegeDescriptor.ForEachDefaultPrivilegeForRole(func( - defaultPrivilegesForRole descpb.DefaultPrivilegesForRole) error { - role := descpb.DefaultPrivilegesRole{} + defaultPrivilegesForRole catpb.DefaultPrivilegesForRole) error { + role := catpb.DefaultPrivilegesRole{} if defaultPrivilegesForRole.IsExplicitRole() { role.Role = defaultPrivilegesForRole.GetExplicitRole().UserProto.Decode() } else { @@ -442,8 +442,8 @@ func accumulateDependentDefaultPrivileges( func addDependentPrivileges( object tree.AlterDefaultPrivilegesTargetObject, - defaultPrivs descpb.PrivilegeDescriptor, - role descpb.DefaultPrivilegesRole, + defaultPrivs catpb.PrivilegeDescriptor, + role catpb.DefaultPrivilegesRole, userNames map[security.SQLUsername][]objectAndType, ) { var objectType string diff --git a/pkg/sql/execinfrapb/BUILD.bazel b/pkg/sql/execinfrapb/BUILD.bazel index 4f19fdb41f18..b70bdd59568c 100644 --- a/pkg/sql/execinfrapb/BUILD.bazel +++ b/pkg/sql/execinfrapb/BUILD.bazel @@ -76,6 +76,7 @@ go_test( "//pkg/security", "//pkg/settings/cluster", "//pkg/sql", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/descpb", "//pkg/sql/sem/tree", "//pkg/sql/types", diff --git a/pkg/sql/execinfrapb/flow_diagram_external_test.go b/pkg/sql/execinfrapb/flow_diagram_external_test.go index 188be364223b..8aecf8820a77 100644 --- a/pkg/sql/execinfrapb/flow_diagram_external_test.go +++ b/pkg/sql/execinfrapb/flow_diagram_external_test.go @@ -17,6 +17,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/base" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/execinfrapb" "github.com/cockroachdb/cockroach/pkg/util/leaktest" @@ -50,7 +51,7 @@ func TestPlanDiagramTableReaderWrapColumns(t *testing.T) { this_is_a_super_duper_long_name_that_is_longer_than_any_reasonable_wrapping_limit_and_must_appear_on_its_own_line INT, z INT )`, - descpb.NewBasePrivilegeDescriptor(security.NodeUserName()), + catpb.NewBasePrivilegeDescriptor(security.NodeUserName()), ) if err != nil { t.Fatal(err) diff --git a/pkg/sql/grant_revoke.go b/pkg/sql/grant_revoke.go index 5c3c43644ea1..7cad00e1227c 100644 --- a/pkg/sql/grant_revoke.go +++ b/pkg/sql/grant_revoke.go @@ -17,6 +17,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/clusterversion" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" @@ -57,7 +58,7 @@ func (p *planner) Grant(ctx context.Context, n *tree.Grant) (planNode, error) { targets: n.Targets, grantees: grantees, desiredprivs: n.Privileges, - changePrivilege: func(privDesc *descpb.PrivilegeDescriptor, privileges privilege.List, grantee security.SQLUsername) { + changePrivilege: func(privDesc *catpb.PrivilegeDescriptor, privileges privilege.List, grantee security.SQLUsername) { privDesc.Grant(grantee, privileges, n.WithGrantOption) }, grantOn: grantOn, @@ -88,7 +89,7 @@ func (p *planner) Revoke(ctx context.Context, n *tree.Revoke) (planNode, error) targets: n.Targets, grantees: grantees, desiredprivs: n.Privileges, - changePrivilege: func(privDesc *descpb.PrivilegeDescriptor, privileges privilege.List, grantee security.SQLUsername) { + changePrivilege: func(privDesc *catpb.PrivilegeDescriptor, privileges privilege.List, grantee security.SQLUsername) { privDesc.Revoke(grantee, privileges, grantOn, n.GrantOptionFor) }, grantOn: grantOn, @@ -102,7 +103,7 @@ type changePrivilegesNode struct { targets tree.TargetList grantees []security.SQLUsername desiredprivs privilege.List - changePrivilege func(*descpb.PrivilegeDescriptor, privilege.List, security.SQLUsername) + changePrivilege func(*catpb.PrivilegeDescriptor, privilege.List, security.SQLUsername) grantOn privilege.ObjectType // granteesNameList is used for creating an AST node for alter default diff --git a/pkg/sql/pg_catalog.go b/pkg/sql/pg_catalog.go index e2cbf7902047..c5b4fb8d1629 100644 --- a/pkg/sql/pg_catalog.go +++ b/pkg/sql/pg_catalog.go @@ -1196,7 +1196,7 @@ https://www.postgresql.org/docs/13/catalog-pg-default-acl.html`, schema: vtable.PGCatalogDefaultACL, populate: func(ctx context.Context, p *planner, dbContext catalog.DatabaseDescriptor, addRow func(...tree.Datum) error) error { h := makeOidHasher() - f := func(defaultPrivilegesForRole descpb.DefaultPrivilegesForRole) error { + f := func(defaultPrivilegesForRole catpb.DefaultPrivilegesForRole) error { objectTypes := tree.GetAlterDefaultPrivilegesTargetObjects() for _, objectType := range objectTypes { privs, ok := defaultPrivilegesForRole.DefaultPrivilegesPerObject[objectType] diff --git a/pkg/sql/repair.go b/pkg/sql/repair.go index 20e3502dcd11..8a51e8d39720 100644 --- a/pkg/sql/repair.go +++ b/pkg/sql/repair.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descbuilder" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" @@ -105,7 +106,7 @@ func (p *planner) UnsafeUpsertDescriptor( var existingVersion descpb.DescriptorVersion var existingModTime hlc.Timestamp var previousOwner string - var previousUserPrivileges []descpb.UserPrivileges + var previousUserPrivileges []catpb.UserPrivileges if mut != nil { if mut.IsUncommittedVersion() { return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState, @@ -283,10 +284,10 @@ func comparePrivileges( ctx context.Context, p *planner, existing catalog.MutableDescriptor, - prevUserPrivileges []descpb.UserPrivileges, + prevUserPrivileges []catpb.UserPrivileges, objectType privilege.ObjectType, ) error { - computePrivilegeChanges := func(prev, cur *descpb.UserPrivileges) (granted, revoked []string) { + computePrivilegeChanges := func(prev, cur *catpb.UserPrivileges) (granted, revoked []string) { // User has no privileges anymore after upsert, all privileges revoked. if cur == nil { revoked = privilege.ListFromBitField(prev.Privileges, objectType).SortedNames() @@ -326,7 +327,7 @@ func comparePrivileges( } curUserPrivileges := existing.GetPrivileges().Users - curUserMap := make(map[string]*descpb.UserPrivileges) + curUserMap := make(map[string]*catpb.UserPrivileges) for i := range curUserPrivileges { curUser := &curUserPrivileges[i] curUserMap[curUser.User().Normalized()] = curUser diff --git a/pkg/sql/reparent_database.go b/pkg/sql/reparent_database.go index 5c4d47e9c500..e25e58171681 100644 --- a/pkg/sql/reparent_database.go +++ b/pkg/sql/reparent_database.go @@ -15,6 +15,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/clusterversion" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descidgen" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" @@ -126,7 +127,7 @@ func (n *reparentDatabaseNode) startExec(params runParams) error { ParentID: n.newParent.ID, Name: n.db.Name, ID: id, - Privileges: protoutil.Clone(n.db.Privileges).(*descpb.PrivilegeDescriptor), + Privileges: protoutil.Clone(n.db.Privileges).(*catpb.PrivilegeDescriptor), Version: 1, }).BuildCreatedMutable() // Add the new schema to the parent database's name map. diff --git a/pkg/sql/schemachanger/scpb/BUILD.bazel b/pkg/sql/schemachanger/scpb/BUILD.bazel index b213581d521c..ba96f2ee5552 100644 --- a/pkg/sql/schemachanger/scpb/BUILD.bazel +++ b/pkg/sql/schemachanger/scpb/BUILD.bazel @@ -13,6 +13,7 @@ go_library( importpath = "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb", visibility = ["//visibility:public"], deps = [ + "//pkg/sql/sem/catid", # keep "//pkg/util/protoutil", "@com_github_cockroachdb_errors//:errors", ], @@ -26,7 +27,6 @@ go_proto_library( visibility = ["//visibility:public"], deps = [ "//pkg/sql/catalog/catpb", - "//pkg/sql/catalog/descpb", "//pkg/sql/sem/catid", # keep "//pkg/sql/types", "@com_github_gogo_protobuf//gogoproto", @@ -43,7 +43,6 @@ proto_library( visibility = ["//visibility:public"], deps = [ "//pkg/sql/catalog/catpb:catpb_proto", - "//pkg/sql/catalog/descpb:descpb_proto", "//pkg/sql/types:types_proto", "@com_github_gogo_protobuf//gogoproto:gogo_proto", ], diff --git a/pkg/sql/schemachanger/scpb/elements.proto b/pkg/sql/schemachanger/scpb/elements.proto index 7edab78774be..94ab36233efa 100644 --- a/pkg/sql/schemachanger/scpb/elements.proto +++ b/pkg/sql/schemachanger/scpb/elements.proto @@ -13,10 +13,12 @@ package cockroach.sql.schemachanger.scpb; option go_package = "scpb"; import "sql/catalog/catpb/catalog.proto"; -import "sql/catalog/descpb/privilege.proto"; +import "sql/catalog/catpb/privilege.proto"; import "sql/types/types.proto"; import "gogoproto/gogo.proto"; +option (gogoproto.equal_all) = true; + message ElementProto { option (gogoproto.onlyone) = true; Column column = 1 [(gogoproto.moretags) = "parent:\"Table\""]; @@ -61,7 +63,6 @@ message ElementProto { } message Column { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 column_id = 3 [(gogoproto.customname) = "ColumnID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.ColumnID"]; uint32 family_id = 4 [(gogoproto.customname) = "FamilyID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.FamilyID"]; @@ -87,7 +88,6 @@ message PrimaryIndex { ASC = 0; DESC = 1; } - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 index_id = 2 [(gogoproto.customname) = "IndexID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.IndexID"]; bool unique = 3; @@ -111,7 +111,6 @@ message SecondaryIndex { ASC = 0; DESC = 1; } - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 index_id = 2 [(gogoproto.customname) = "IndexID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.IndexID"]; bool unique = 3; @@ -130,7 +129,6 @@ message SecondaryIndex { } message SequenceDependency { - option (gogoproto.equal) = true; enum Type { UNKNOWN = 0; @@ -145,7 +143,6 @@ message SequenceDependency { } message UniqueConstraint { - option (gogoproto.equal) = true; ConstraintType constraint_type = 1; uint32 constraint_ordinal = 2; uint32 table_id = 3 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; @@ -154,7 +151,6 @@ message UniqueConstraint { } message CheckConstraint { - option (gogoproto.equal) = true; ConstraintType constraint_type = 1; uint32 constraint_ordinal = 2; uint32 table_id = 3 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; @@ -165,12 +161,10 @@ message CheckConstraint { } message Sequence { - option (gogoproto.equal) = true; uint32 sequence_id = 1 [(gogoproto.customname) = "SequenceID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; } message DefaultExpression { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 column_id = 2 [(gogoproto.customname) = "ColumnID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.ColumnID"]; repeated uint32 uses_sequence_ids = 3 [(gogoproto.customname) = "UsesSequenceIDs", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; @@ -178,58 +172,49 @@ message DefaultExpression { } message View { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; } message Table { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; } message OnUpdateExprTypeReference { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 column_id = 2 [(gogoproto.customname) = "ColumnID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.ColumnID"]; uint32 type_id = 3 [(gogoproto.customname) = "TypeID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; } message ComputedExprTypeReference { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 column_id = 2 [(gogoproto.customname) = "ColumnID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.ColumnID"]; uint32 type_id = 3 [(gogoproto.customname) = "TypeID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; } message DefaultExprTypeReference { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 column_id = 2 [(gogoproto.customname) = "ColumnID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.ColumnID"]; uint32 type_id = 3 [(gogoproto.customname) = "TypeID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; } message ColumnTypeReference { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 column_id = 2 [(gogoproto.customname) = "ColumnID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.ColumnID"]; uint32 type_id = 3 [(gogoproto.customname) = "TypeID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; } message CheckConstraintTypeReference { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 constraint_ordinal = 2; uint32 type_id = 3 [(gogoproto.customname) = "TypeID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; } message ViewDependsOnType { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 type_id = 3 [(gogoproto.customname) = "TypeID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; } message ForeignKey { - option (gogoproto.equal) = true; uint32 origin_id = 1 [(gogoproto.customname) = "OriginID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; repeated uint32 origin_columns = 3 [(gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.ColumnID"]; uint32 reference_id = 4 [(gogoproto.customname) = "ReferenceID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; @@ -240,7 +225,6 @@ message ForeignKey { } message ForeignKeyBackReference { - option (gogoproto.equal) = true; uint32 origin_id = 1 [(gogoproto.customname) = "OriginID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; repeated uint32 origin_columns = 3 [(gogoproto.customname) = "OriginColumns", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.ColumnID"]; uint32 reference_id = 4 [(gogoproto.customname) = "ReferenceID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; @@ -256,7 +240,6 @@ message SequenceOwnedBy { } message RelationDependedOnBy { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 depended_on = 2 [(gogoproto.customname) = "DependedOnBy", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 column_id = 3 [(gogoproto.customname) = "ColumnID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.ColumnID"]; @@ -277,20 +260,17 @@ message Database { } message ListPartition { - option (gogoproto.equal) = true; string name = 1; repeated string expr = 2; } message RangePartitions { - option (gogoproto.equal) = true; string name = 1; repeated string to = 2; repeated string from = 3; } message Partitioning { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 index_id = 2 [(gogoproto.customname) = "IndexID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.IndexID"]; repeated string fields = 3; @@ -322,14 +302,12 @@ message Locality { } message ColumnName { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 column_id = 2 [(gogoproto.customname) = "ColumnID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.ColumnID"]; string name = 3; } message IndexName { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; uint32 index_id = 2 [(gogoproto.customname) = "IndexID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.IndexID"]; string name = 3; @@ -344,14 +322,12 @@ enum ConstraintType { } message ConstraintName { - option (gogoproto.equal) = true; uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; ConstraintType constraint_type = 2; uint32 constraint_ordinal = 3; string name = 4; } - message DefaultPrivilege { uint32 descriptor_id = 1 [(gogoproto.customname) = "DescriptorID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; message DefaultObjectPrivilege{ @@ -370,45 +346,39 @@ message DatabaseSchemaEntry { } message TableComment { - option (gogoproto.equal) = true; - uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb.ID"]; + uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; string comment = 2; } message DatabaseComment { - option (gogoproto.equal) = true; - uint32 database_id = 1 [(gogoproto.customname) = "DatabaseID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb.ID"]; + uint32 database_id = 1 [(gogoproto.customname) = "DatabaseID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; string comment = 2; } message SchemaComment { - option (gogoproto.equal) = true; - uint32 schema_id = 1 [(gogoproto.customname) = "SchemaID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb.ID"]; + uint32 schema_id = 1 [(gogoproto.customname) = "SchemaID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; string comment = 2; } message IndexComment { - option (gogoproto.equal) = true; - uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb.ID"]; - uint32 index_id = 2 [(gogoproto.customname) = "IndexID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb.IndexID"]; + uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; + uint32 index_id = 2 [(gogoproto.customname) = "IndexID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.IndexID"]; string comment = 3; } message ColumnComment { - option (gogoproto.equal) = true; - uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb.ID"]; - uint32 column_id = 2 [(gogoproto.customname) = "ColumnID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb.ColumnID"]; + uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; + uint32 column_id = 2 [(gogoproto.customname) = "ColumnID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.ColumnID"]; string comment = 3; } message ConstraintComment { - option (gogoproto.equal) = true; - uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb.ID"]; - uint32 constraint_id = 2 [(gogoproto.customname) = "ConstraintID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb.ConstraintID"]; + uint32 table_id = 1 [(gogoproto.customname) = "TableID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; + uint32 constraint_id = 2 [(gogoproto.customname) = "ConstraintID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.ConstraintID"]; string comment = 3; } message DatabaseRoleSetting { option (gogoproto.equal) = true; - uint32 database_id = 1 [(gogoproto.customname) = "DatabaseID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb.ID"]; + uint32 database_id = 1 [(gogoproto.customname) = "DatabaseID", (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/sem/catid.DescID"]; string role_name = 2; } diff --git a/pkg/sql/schemachanger/scpb/scpb.proto b/pkg/sql/schemachanger/scpb/scpb.proto index 4aa525660fcd..9f72eec165d8 100644 --- a/pkg/sql/schemachanger/scpb/scpb.proto +++ b/pkg/sql/schemachanger/scpb/scpb.proto @@ -16,6 +16,7 @@ import "sql/schemachanger/scpb/elements.proto"; import "gogoproto/gogo.proto"; message Target { + option (gogoproto.equal) = true; ElementProto element_proto = 1 [(gogoproto.embed) = true, (gogoproto.nullable) = false]; TargetMetadata metadata = 2 [(gogoproto.nullable) = false]; Status target_status = 3; @@ -37,6 +38,7 @@ enum Status { // some fields like statement_id are indexes into the the full declarative // schema changer State. message TargetMetadata { + option (gogoproto.equal) = true; // SubWorkID identifies the individual portions of a statement that this // target belongs too. For example, if multiple objects such as tables or // schemas are dropped in a single statement (i.e. such as DROP TABLE A, B) diff --git a/pkg/sql/sem/catid/ids.go b/pkg/sql/sem/catid/ids.go index 337d6a1ad1e0..7847a6139806 100644 --- a/pkg/sql/sem/catid/ids.go +++ b/pkg/sql/sem/catid/ids.go @@ -14,6 +14,9 @@ package catid // DescID is a custom type for {Database,Table}Descriptor IDs. type DescID uint32 +// InvalidDescID is the uninitialised descriptor id. +const InvalidDescID DescID = 0 + // SafeValue implements the redact.SafeValue interface. func (DescID) SafeValue() {} diff --git a/pkg/sql/table_test.go b/pkg/sql/table_test.go index ef757e84a123..19a6fa0211de 100644 --- a/pkg/sql/table_test.go +++ b/pkg/sql/table_test.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/kv" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descbuilder" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descs" @@ -187,7 +188,7 @@ func TestMakeTableDescColumns(t *testing.T) { for i, d := range testData { s := "CREATE TABLE foo.test (a " + d.sqlType + " PRIMARY KEY, b " + d.sqlType + ")" schema, err := CreateTestTableDescriptor(context.Background(), 1, 100, s, - descpb.NewBasePrivilegeDescriptor(security.AdminRoleName())) + catpb.NewBasePrivilegeDescriptor(security.AdminRoleName())) if err != nil { t.Fatalf("%d: %v", i, err) } @@ -317,7 +318,7 @@ func TestMakeTableDescIndexes(t *testing.T) { for i, d := range testData { s := "CREATE TABLE foo.test (" + d.sql + ")" schema, err := CreateTestTableDescriptor(context.Background(), 1, 100, s, - descpb.NewBasePrivilegeDescriptor(security.AdminRoleName())) + catpb.NewBasePrivilegeDescriptor(security.AdminRoleName())) if err != nil { t.Fatalf("%d (%s): %v", i, d.sql, err) } @@ -391,7 +392,7 @@ func TestMakeTableDescUniqueConstraints(t *testing.T) { for i, d := range testData { s := "CREATE TABLE foo.test (" + d.sql + ")" schema, err := CreateTestTableDescriptor(context.Background(), 1, 100, s, - descpb.NewBasePrivilegeDescriptor(security.AdminRoleName())) + catpb.NewBasePrivilegeDescriptor(security.AdminRoleName())) if err != nil { t.Fatalf("%d (%s): %v", i, d.sql, err) } @@ -410,7 +411,7 @@ func TestPrimaryKeyUnspecified(t *testing.T) { s := "CREATE TABLE foo.test (a INT, b INT, CONSTRAINT c UNIQUE (b))" ctx := context.Background() desc, err := CreateTestTableDescriptor(ctx, 1, 100, s, - descpb.NewBasePrivilegeDescriptor(security.AdminRoleName())) + catpb.NewBasePrivilegeDescriptor(security.AdminRoleName())) if err != nil { t.Fatal(err) } diff --git a/pkg/sql/tests/BUILD.bazel b/pkg/sql/tests/BUILD.bazel index d7c16bb187e9..0d81b0cb2c28 100644 --- a/pkg/sql/tests/BUILD.bazel +++ b/pkg/sql/tests/BUILD.bazel @@ -73,6 +73,7 @@ go_test( "//pkg/sql", "//pkg/sql/catalog", "//pkg/sql/catalog/bootstrap", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/descbuilder", "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/descs", diff --git a/pkg/sql/tests/system_table_test.go b/pkg/sql/tests/system_table_test.go index 9c48e32bb6c3..30836846c97b 100644 --- a/pkg/sql/tests/system_table_test.go +++ b/pkg/sql/tests/system_table_test.go @@ -25,6 +25,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/bootstrap" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descbuilder" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" @@ -66,7 +67,7 @@ func TestInitialKeys(t *testing.T) { keys.SystemDatabaseID, descpb.ID(1000 /* suitably large descriptor ID */), "CREATE TABLE system.x (val INTEGER PRIMARY KEY)", - descpb.NewBasePrivilegeDescriptor(security.NodeUserName()), + catpb.NewBasePrivilegeDescriptor(security.NodeUserName()), ) if err != nil { t.Fatal(err) diff --git a/pkg/sql/testutils.go b/pkg/sql/testutils.go index 012206d5d827..142d17c23753 100644 --- a/pkg/sql/testutils.go +++ b/pkg/sql/testutils.go @@ -16,6 +16,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/keys" "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/settings/cluster" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc" @@ -32,10 +33,7 @@ import ( // Will fail on complex tables where that operation requires e.g. looking up // other tables. func CreateTestTableDescriptor( - ctx context.Context, - parentID, id descpb.ID, - schema string, - privileges *descpb.PrivilegeDescriptor, + ctx context.Context, parentID, id descpb.ID, schema string, privileges *catpb.PrivilegeDescriptor, ) (*tabledesc.Mutable, error) { st := cluster.MakeTestingClusterSettings() stmt, err := parser.ParseOne(schema) diff --git a/pkg/sql/virtual_schema.go b/pkg/sql/virtual_schema.go index 9780b4e8f261..45f68dcc3520 100644 --- a/pkg/sql/virtual_schema.go +++ b/pkg/sql/virtual_schema.go @@ -20,6 +20,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/settings/cluster" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catconstants" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descbuilder" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" @@ -178,7 +179,7 @@ func (t virtualSchemaTable) initVirtualTableDesc( id, nil, /* regionConfig */ startTime, /* creationTime */ - descpb.NewVirtualTablePrivilegeDescriptor(), + catpb.NewVirtualTablePrivilegeDescriptor(), nil, /* affected */ nil, /* semaCtx */ nil, /* evalCtx */ @@ -255,13 +256,14 @@ func (v virtualSchemaView) initVirtualTableDesc( id, columns, startTime, - descpb.NewVirtualTablePrivilegeDescriptor(), - nil, - nil, + catpb.NewVirtualTablePrivilegeDescriptor(), + nil, // semaCtx + nil, // evalCtx st, tree.PersistencePermanent, - false, - nil) + false, // isMultiRegion + nil, // sc + ) return mutDesc.TableDescriptor, err } diff --git a/pkg/sql/zone_config_test.go b/pkg/sql/zone_config_test.go index b908307eaf72..f97a5eab334d 100644 --- a/pkg/sql/zone_config_test.go +++ b/pkg/sql/zone_config_test.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql" "github.com/cockroachdb/cockroach/pkg/sql/catalog/bootstrap" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema" "github.com/cockroachdb/cockroach/pkg/sql/tests" @@ -50,7 +51,7 @@ func forceNewConfig(t testing.TB, s *server.TestServer) *config.SystemConfig { Database: &descpb.DatabaseDescriptor{ Name: "sentinel", ID: configID, - Privileges: &descpb.PrivilegeDescriptor{}, + Privileges: &catpb.PrivilegeDescriptor{}, }, }, } From a204e69edb0817959a8b121a2329e7515f8efde6 Mon Sep 17 00:00:00 2001 From: Andrew Werner Date: Thu, 27 Jan 2022 04:20:37 -0500 Subject: [PATCH 2/5] jobs,catpb,descpb: move the definition of JobID down to catpb, forward up This allows the JobID to be used in descriptors. Adds a little bit of type safety here and there. Release note: None --- docs/generated/redact_safe.md | 2 +- pkg/BUILD.bazel | 1 + .../restore_schema_change_creation.go | 2 +- pkg/jobs/jobspb/BUILD.bazel | 1 + pkg/jobs/jobspb/wrap.go | 8 +++---- pkg/jobs/validate.go | 2 +- pkg/sql/backfill.go | 2 +- pkg/sql/catalog/catpb/BUILD.bazel | 1 + pkg/sql/catalog/catpb/job_id.go | 22 ++++++++++++++++++ pkg/sql/catalog/descpb/structured.proto | 23 +++++++++++++------ pkg/sql/crdb_internal.go | 2 +- pkg/sql/drop_table.go | 3 +-- pkg/sql/indexbackfiller_test.go | 2 +- pkg/sql/rowexec/backfiller.go | 2 +- pkg/sql/rowexec/backfiller_test.go | 2 +- pkg/sql/schema_changer.go | 2 +- .../scexec/scmutationexec/scmutationexec.go | 4 ++-- pkg/sql/table.go | 4 ++-- 18 files changed, 58 insertions(+), 27 deletions(-) create mode 100644 pkg/sql/catalog/catpb/job_id.go diff --git a/docs/generated/redact_safe.md b/docs/generated/redact_safe.md index bbb781069e10..d91bd73b2b09 100644 --- a/docs/generated/redact_safe.md +++ b/docs/generated/redact_safe.md @@ -6,7 +6,6 @@ pkg/base/node_id.go | `*NodeIDContainer` pkg/base/node_id.go | `*SQLIDContainer` pkg/base/node_id.go | `*StoreIDContainer` pkg/cli/exit/exit.go | `Code` -pkg/jobs/jobspb/wrap.go | `JobID` pkg/jobs/jobspb/wrap.go | `Type` pkg/kv/kvserver/closedts/ctpb/service.go | `LAI` pkg/kv/kvserver/closedts/ctpb/service.go | `SeqNum` @@ -25,6 +24,7 @@ pkg/roachpb/method.go | `Method` pkg/roachpb/tenant.go | `TenantID` pkg/rpc/connection_class.go | `ConnectionClass` pkg/sql/catalog/catpb/constraint.go | `ForeignKeyAction` +pkg/sql/catalog/catpb/job_id.go | `JobID` pkg/sql/catalog/descpb/structured.go | `ConstraintType` pkg/sql/catalog/descpb/structured.go | `ConstraintValidity` pkg/sql/catalog/descpb/structured.go | `DescriptorMutation_Direction` diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index bd03f36c9dd3..95d22b293a83 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel @@ -203,6 +203,7 @@ ALL_TESTS = [ "//pkg/spanconfig/spanconfigtestutils:spanconfigtestutils_test", "//pkg/sql/catalog/catalogkeys:catalogkeys_test", "//pkg/sql/catalog/catformat:catformat_test", + "//pkg/sql/catalog/catpb:catpb_test", "//pkg/sql/catalog/catprivilege:catprivilege_test", "//pkg/sql/catalog/colinfo:colinfo_test", "//pkg/sql/catalog/dbdesc:dbdesc_test", diff --git a/pkg/ccl/backupccl/restore_schema_change_creation.go b/pkg/ccl/backupccl/restore_schema_change_creation.go index b5e441ae5029..3815b79d4415 100644 --- a/pkg/ccl/backupccl/restore_schema_change_creation.go +++ b/pkg/ccl/backupccl/restore_schema_change_creation.go @@ -207,7 +207,7 @@ func createSchemaChangeJobsFromMutations( } newMutationJob := descpb.TableDescriptor_MutationJob{ MutationID: mutationID, - JobID: int64(jobID), + JobID: jobID, } mutationJobs = append(mutationJobs, newMutationJob) diff --git a/pkg/jobs/jobspb/BUILD.bazel b/pkg/jobs/jobspb/BUILD.bazel index c6983e1a7465..e76bbef111c2 100644 --- a/pkg/jobs/jobspb/BUILD.bazel +++ b/pkg/jobs/jobspb/BUILD.bazel @@ -11,6 +11,7 @@ go_library( deps = [ "//pkg/base", "//pkg/cloud", + "//pkg/sql/catalog/catpb", "//pkg/sql/catalog/descpb", "//pkg/sql/protoreflect", "@com_github_cockroachdb_errors//:errors", diff --git a/pkg/jobs/jobspb/wrap.go b/pkg/jobs/jobspb/wrap.go index a73fa7c8760c..2ccb2dc20826 100644 --- a/pkg/jobs/jobspb/wrap.go +++ b/pkg/jobs/jobspb/wrap.go @@ -17,6 +17,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/base" "github.com/cockroachdb/cockroach/pkg/cloud" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/protoreflect" "github.com/cockroachdb/errors" @@ -24,13 +25,10 @@ import ( ) // JobID is the ID of a job. -type JobID int64 +type JobID = catpb.JobID // InvalidJobID is the zero value for JobID corresponding to no job. -const InvalidJobID JobID = 0 - -// SafeValue implements the redact.SafeValue interface. -func (j JobID) SafeValue() {} +const InvalidJobID = catpb.InvalidJobID // Details is a marker interface for job details proto structs. type Details interface{} diff --git a/pkg/jobs/validate.go b/pkg/jobs/validate.go index 8b251ba0586a..59ca91ad03c0 100644 --- a/pkg/jobs/validate.go +++ b/pkg/jobs/validate.go @@ -36,7 +36,7 @@ func ValidateJobReferencesInDescriptor( } for _, m := range tbl.GetMutationJobs() { - j, err := jmg.GetJobMetadata(jobspb.JobID(m.JobID)) + j, err := jmg.GetJobMetadata(m.JobID) if err != nil { errorAccFn(errors.WithAssertionFailure(errors.Wrapf(err, "mutation job %d", m.JobID))) continue diff --git a/pkg/sql/backfill.go b/pkg/sql/backfill.go index f0b5e45765d5..d9751567f9e1 100644 --- a/pkg/sql/backfill.go +++ b/pkg/sql/backfill.go @@ -789,7 +789,7 @@ func getJobIDForMutationWithDescriptor( ) (jobspb.JobID, error) { for _, job := range tableDesc.GetMutationJobs() { if job.MutationID == mutationID { - return jobspb.JobID(job.JobID), nil + return job.JobID, nil } } diff --git a/pkg/sql/catalog/catpb/BUILD.bazel b/pkg/sql/catalog/catpb/BUILD.bazel index de55ed1192ca..5e4f22587ae2 100644 --- a/pkg/sql/catalog/catpb/BUILD.bazel +++ b/pkg/sql/catalog/catpb/BUILD.bazel @@ -29,6 +29,7 @@ go_library( "constraint.go", "default_privilege.go", "doc.go", + "job_id.go", "multiregion.go", "privilege.go", ":gen-privilegedescversion-stringer", # keep diff --git a/pkg/sql/catalog/catpb/job_id.go b/pkg/sql/catalog/catpb/job_id.go new file mode 100644 index 000000000000..69b3234dd116 --- /dev/null +++ b/pkg/sql/catalog/catpb/job_id.go @@ -0,0 +1,22 @@ +// Copyright 2022 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 catpb + +// JobID is the ID of a job. It is defined here and imported in jobspb because +// jobs are referenced in descriptors and also jobs reference parts of +// descriptors. This avoids any dependency cycles. +type JobID int64 + +// InvalidJobID is the zero value for JobID corresponding to no job. +const InvalidJobID JobID = 0 + +// SafeValue implements the redact.SafeValue interface. +func (j JobID) SafeValue() {} diff --git a/pkg/sql/catalog/descpb/structured.proto b/pkg/sql/catalog/descpb/structured.proto index 258d91f7e99b..e92ad3aabfda 100644 --- a/pkg/sql/catalog/descpb/structured.proto +++ b/pkg/sql/catalog/descpb/structured.proto @@ -1068,8 +1068,10 @@ message TableDescriptor { // The job id for a mutation job is the id in the system.jobs table of the // schema change job executing the mutation referenced by mutation_id. // This is not a jobspb.JobID to avoid a dependency cycle. - optional int64 job_id = 2 [(gogoproto.nullable) = false, - (gogoproto.customname) = "JobID"]; + optional int64 job_id = 2 [ + (gogoproto.nullable) = false, + (gogoproto.customname) = "JobID", + (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb.JobID"]; } // Mutation jobs queued for execution in a FIFO order. Remains synchronized @@ -1079,8 +1081,10 @@ message TableDescriptor { // The job associated with a schema change job run in the new schema changer // (in sql/schemachanger), if one exists. Only one such job can exist at a // time. - optional int64 new_schema_change_job_id = 46 [(gogoproto.nullable) = false, - (gogoproto.customname) = "NewSchemaChangeJobID"]; + optional int64 new_schema_change_job_id = 46 [ + (gogoproto.nullable) = false, + (gogoproto.customname) = "NewSchemaChangeJobID", + (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb.JobID"]; message SequenceOpts { option (gogoproto.equal) = true; @@ -1156,7 +1160,10 @@ message TableDescriptor { // The job id for a drop job is the id in the system.jobs table of the // dropping of this table. - optional int64 drop_job_id = 32 [(gogoproto.nullable) = false, (gogoproto.customname) = "DropJobID"]; + optional int64 drop_job_id = 32 [ + (gogoproto.nullable) = false, + (gogoproto.customname) = "DropJobID", + (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb.JobID"]; message GCDescriptorMutation { option (gogoproto.equal) = true; @@ -1167,8 +1174,10 @@ message TableDescriptor { // The job id for a mutation job is the id in the system.jobs table of the // schema change job executing the mutation referenced by mutation_id. - optional int64 job_id = 3 [(gogoproto.nullable) = false, - (gogoproto.customname) = "JobID", deprecated = true]; + optional int64 job_id = 3 [ + (gogoproto.nullable) = false, + (gogoproto.customname) = "JobID", deprecated = true, + (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb.JobID"]; } // Before 22.1: diff --git a/pkg/sql/crdb_internal.go b/pkg/sql/crdb_internal.go index e976c8e4f381..19784e468ea4 100644 --- a/pkg/sql/crdb_internal.go +++ b/pkg/sql/crdb_internal.go @@ -4465,7 +4465,7 @@ func collectMarshaledJobMetadataMap( continue } for _, j := range tbl.GetMutationJobs() { - referencedJobIDs[jobspb.JobID(j.JobID)] = struct{}{} + referencedJobIDs[j.JobID] = struct{}{} } } if len(referencedJobIDs) == 0 { diff --git a/pkg/sql/drop_table.go b/pkg/sql/drop_table.go index 5966c5b53ffe..17ccc156105c 100644 --- a/pkg/sql/drop_table.go +++ b/pkg/sql/drop_table.go @@ -17,7 +17,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/clusterversion" "github.com/cockroachdb/cockroach/pkg/jobs" - "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" "github.com/cockroachdb/cockroach/pkg/kv" "github.com/cockroachdb/cockroach/pkg/kv/kvclient" "github.com/cockroachdb/cockroach/pkg/roachpb" @@ -460,7 +459,7 @@ func (p *planner) markTableMutationJobsSuccessful( ctx context.Context, tableDesc *tabledesc.Mutable, ) error { for _, mj := range tableDesc.MutationJobs { - jobID := jobspb.JobID(mj.JobID) + jobID := mj.JobID // Jobs are only added in the cache during the transaction and are created // in a batch only when the transaction commits. So, if a job's record exists // in the cache, we can simply delete that record from cache because the diff --git a/pkg/sql/indexbackfiller_test.go b/pkg/sql/indexbackfiller_test.go index 7d43cd3a4a35..3e85aab8c154 100644 --- a/pkg/sql/indexbackfiller_test.go +++ b/pkg/sql/indexbackfiller_test.go @@ -502,7 +502,7 @@ INSERT INTO foo VALUES (1), (10), (100); return err } mut.MutationJobs = append(mut.MutationJobs, descpb.TableDescriptor_MutationJob{ - JobID: int64(jobID), + JobID: jobID, MutationID: 1, }) jobToBlock.Store(jobID) diff --git a/pkg/sql/rowexec/backfiller.go b/pkg/sql/rowexec/backfiller.go index 7fdf583d53d6..f47b3ab86993 100644 --- a/pkg/sql/rowexec/backfiller.go +++ b/pkg/sql/rowexec/backfiller.go @@ -229,7 +229,7 @@ func GetResumeSpans( // know which job it's associated with. for _, job := range tableDesc.GetMutationJobs() { if job.MutationID == mutationID { - jobID = jobspb.JobID(job.JobID) + jobID = job.JobID break } } diff --git a/pkg/sql/rowexec/backfiller_test.go b/pkg/sql/rowexec/backfiller_test.go index 80c24492b471..a2bc02397532 100644 --- a/pkg/sql/rowexec/backfiller_test.go +++ b/pkg/sql/rowexec/backfiller_test.go @@ -122,7 +122,7 @@ func TestWriteResumeSpan(t *testing.T) { if len(tableDesc.MutationJobs) > 0 { for _, job := range tableDesc.MutationJobs { if job.MutationID == mutationID { - jobID = jobspb.JobID(job.JobID) + jobID = job.JobID break } } diff --git a/pkg/sql/schema_changer.go b/pkg/sql/schema_changer.go index ce0429d28e8c..4f27f5dd7792 100644 --- a/pkg/sql/schema_changer.go +++ b/pkg/sql/schema_changer.go @@ -2470,7 +2470,7 @@ func (sc *SchemaChanger) queueCleanupJob( log.Infof(ctx, "created job %d to drop previous columns and indexes", jobID) scDesc.MutationJobs = append(scDesc.MutationJobs, descpb.TableDescriptor_MutationJob{ MutationID: mutationID, - JobID: int64(jobID), + JobID: jobID, }) } return jobID, nil diff --git a/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go b/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go index 147600460cec..4a7839f2f26f 100644 --- a/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go +++ b/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go @@ -184,13 +184,13 @@ func (m *visitor) swapSchemaChangeJobID( if !ok { return nil } - if jobspb.JobID(mut.NewSchemaChangeJobID) != exp { + if mut.NewSchemaChangeJobID != exp { return errors.AssertionFailedf( "unexpected schema change job ID %d on table %d, expected %d", mut.NewSchemaChangeJobID, descID, exp, ) } - mut.NewSchemaChangeJobID = int64(to) + mut.NewSchemaChangeJobID = to return nil } diff --git a/pkg/sql/table.go b/pkg/sql/table.go index 02a893a01fc6..3171f0088d5f 100644 --- a/pkg/sql/table.go +++ b/pkg/sql/table.go @@ -171,7 +171,7 @@ func (p *planner) createOrUpdateSchemaChangeJob( // TODO (lucy): get rid of this when we get rid of MutationJobs. if mutationID != descpb.InvalidMutationID { tableDesc.MutationJobs = append(tableDesc.MutationJobs, descpb.TableDescriptor_MutationJob{ - MutationID: mutationID, JobID: int64(newRecord.JobID)}) + MutationID: mutationID, JobID: newRecord.JobID}) } log.Infof(ctx, "queued new schema-change job %d for table %d, mutation %d", newRecord.JobID, tableDesc.ID, mutationID) @@ -204,7 +204,7 @@ func (p *planner) createOrUpdateSchemaChangeJob( // Also add a MutationJob on the table descriptor. // TODO (lucy): get rid of this when we get rid of MutationJobs. tableDesc.MutationJobs = append(tableDesc.MutationJobs, descpb.TableDescriptor_MutationJob{ - MutationID: mutationID, JobID: int64(record.JobID)}) + MutationID: mutationID, JobID: record.JobID}) // For existing records, if a mutation ID ever gets assigned // at a later point then mark it as cancellable again. record.NonCancelable = false From 1305e7e7ea2632836fd18b92085202010c3d74ec Mon Sep 17 00:00:00 2001 From: Andrew Werner Date: Thu, 27 Jan 2022 05:14:27 -0500 Subject: [PATCH 3/5] descpb,scpb: add job ID and state to all descriptors This change sets us up to store the schema changer state in the descriptors. It also sets up the code to annotate each descriptor with a job ID. There's accompanying changes to the catalog interfaces. There's one minor other things which were added to get the tests to pass. We need to make sure that the descs.Collection is informed of deleted descriptors. Before this commit, we gave the collection the complete deleted descriptor. Now we just tell it about the ID. Also, we don't write updates to a descriptor which we're deleting in the same transaction. Release note: None --- pkg/BUILD.bazel | 1 + pkg/ccl/backupccl/restore_job.go | 8 +- pkg/ccl/importccl/import_job.go | 2 +- pkg/cli/testdata/doctor/test_recreate_zipdir | 12 +- pkg/sql/catalog/BUILD.bazel | 1 + pkg/sql/catalog/catalog.go | 5 + pkg/sql/catalog/dbdesc/BUILD.bazel | 1 + pkg/sql/catalog/dbdesc/database_desc.go | 13 + pkg/sql/catalog/descpb/BUILD.bazel | 2 + pkg/sql/catalog/descpb/structured.proto | 33 +- pkg/sql/catalog/descriptor.go | 8 + pkg/sql/catalog/descs/collection.go | 8 +- pkg/sql/catalog/descs/txn.go | 16 +- pkg/sql/catalog/internal/validate/BUILD.bazel | 31 +- .../catalog/internal/validate/helpers_test.go | 33 ++ .../internal/validate/schema_changer_state.go | 153 ++++++++ .../validate/schema_changer_state_test.go | 281 +++++++++++++++ pkg/sql/catalog/internal/validate/validate.go | 1 + pkg/sql/catalog/schemadesc/BUILD.bazel | 1 + pkg/sql/catalog/schemadesc/schema_desc.go | 13 + .../schemadesc/synthetic_schema_desc.go | 4 + pkg/sql/catalog/tabledesc/BUILD.bazel | 1 + pkg/sql/catalog/tabledesc/structured.go | 7 + pkg/sql/catalog/tabledesc/validate.go | 8 +- pkg/sql/catalog/tabledesc/validate_test.go | 78 +++-- pkg/sql/catalog/typedesc/BUILD.bazel | 1 + .../typedesc/table_implicit_record_type.go | 7 + pkg/sql/catalog/typedesc/type_desc.go | 7 + pkg/sql/drop_table.go | 2 +- .../testdata/show_trace_nonmetamorphic | 12 +- pkg/sql/schemachanger/scdeps/exec_deps.go | 1 + pkg/sql/schemachanger/scexec/exec_mutation.go | 3 + .../scexec/scmutationexec/scmutationexec.go | 63 ++-- pkg/sql/schemachanger/scpb/scpb.proto | 52 ++- pkg/sql/schemachanger/scpb/state.go | 8 + .../testdata/alter_table_add_column | 21 +- pkg/sql/schemachanger/testdata/drop | 331 ++++++++++++------ pkg/sql/schemachanger/testdata/index | 21 +- pkg/sql/table.go | 2 +- pkg/sql/truncate.go | 2 +- 40 files changed, 1025 insertions(+), 229 deletions(-) create mode 100644 pkg/sql/catalog/internal/validate/helpers_test.go create mode 100644 pkg/sql/catalog/internal/validate/schema_changer_state.go create mode 100644 pkg/sql/catalog/internal/validate/schema_changer_state_test.go diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index 95d22b293a83..3df6f6be0534 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel @@ -210,6 +210,7 @@ ALL_TESTS = [ "//pkg/sql/catalog/descpb:descpb_test", "//pkg/sql/catalog/descs:descs_test", "//pkg/sql/catalog/hydratedtables:hydratedtables_test", + "//pkg/sql/catalog/internal/validate:validate_test", "//pkg/sql/catalog/lease:lease_test", "//pkg/sql/catalog/multiregion:multiregion_test", "//pkg/sql/catalog/nstree:nstree_test", diff --git a/pkg/ccl/backupccl/restore_job.go b/pkg/ccl/backupccl/restore_job.go index 1dd8f508e631..7caa5753b4ab 100644 --- a/pkg/ccl/backupccl/restore_job.go +++ b/pkg/ccl/backupccl/restore_job.go @@ -2133,7 +2133,7 @@ func (r *restoreResumer) dropDescriptors( // and so we don't need to preserve MVCC semantics. tableToDrop.DropTime = dropTime b.Del(catalogkeys.EncodeNameKey(codec, tableToDrop)) - descsCol.AddDeletedDescriptor(tableToDrop) + descsCol.AddDeletedDescriptor(tableToDrop.GetID()) if err := descsCol.WriteDescToBatch(ctx, false /* kvTrace */, tableToDrop, b); err != nil { return errors.Wrap(err, "writing dropping table to batch") } @@ -2161,7 +2161,7 @@ func (r *restoreResumer) dropDescriptors( } // Remove the system.descriptor entry. b.Del(catalogkeys.MakeDescMetadataKey(codec, typDesc.ID)) - descsCol.AddDeletedDescriptor(mutType) + descsCol.AddDeletedDescriptor(mutType.GetID()) } // Queue a GC job. @@ -2233,7 +2233,7 @@ func (r *restoreResumer) dropDescriptors( b.Del(catalogkeys.EncodeNameKey(codec, mutSchema)) b.Del(catalogkeys.MakeDescMetadataKey(codec, mutSchema.GetID())) - descsCol.AddDeletedDescriptor(mutSchema) + descsCol.AddDeletedDescriptor(mutSchema.GetID()) dbsWithDeletedSchemas[mutSchema.GetParentID()] = append(dbsWithDeletedSchemas[mutSchema.GetParentID()], mutSchema) } @@ -2312,7 +2312,7 @@ func (r *restoreResumer) dropDescriptors( nameKey := catalogkeys.MakeDatabaseNameKey(codec, db.GetName()) b.Del(nameKey) - descsCol.AddDeletedDescriptor(db) + descsCol.AddDeletedDescriptor(db.GetID()) deletedDBs[db.GetID()] = struct{}{} } diff --git a/pkg/ccl/importccl/import_job.go b/pkg/ccl/importccl/import_job.go index 9a93b1bbd4e4..32ae5cc66f53 100644 --- a/pkg/ccl/importccl/import_job.go +++ b/pkg/ccl/importccl/import_job.go @@ -1462,7 +1462,7 @@ func (r *importResumer) dropTables( newTableDesc.DropTime = dropTime b.Del(catalogkeys.EncodeNameKey(execCfg.Codec, newTableDesc)) tablesToGC = append(tablesToGC, newTableDesc.ID) - descsCol.AddDeletedDescriptor(newTableDesc) + descsCol.AddDeletedDescriptor(newTableDesc.GetID()) } else { // IMPORT did not create this table, so we should not drop it. newTableDesc.SetPublic() diff --git a/pkg/cli/testdata/doctor/test_recreate_zipdir b/pkg/cli/testdata/doctor/test_recreate_zipdir index 1a80d4f5dc0f..d7d0a2403ee9 100644 --- a/pkg/cli/testdata/doctor/test_recreate_zipdir +++ b/pkg/cli/testdata/doctor/test_recreate_zipdir @@ -12,16 +12,16 @@ SELECT crdb_internal.unsafe_upsert_descriptor(50, decode('12380a0964656661756c74 SELECT crdb_internal.unsafe_upsert_namespace_entry(0, 0, 'defaultdb', 50, true); SELECT crdb_internal.unsafe_upsert_descriptor(51, decode('12370a08706f73746772657310331a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f7418012200280140004a00', 'hex'), true); SELECT crdb_internal.unsafe_upsert_namespace_entry(0, 0, 'postgres', 51, true); -SELECT crdb_internal.unsafe_upsert_descriptor(53, decode('0afd040a0575736572731835203428013a0042280a02696410011a0d080e100018003000508617600020003000680070007800800100880100980100422a0a046369747910021a0d0807100018003007509308600020003000680070007800800100880100980100422a0a046e616d6510031a0d0807100018003007509308600020013000680070007800800100880100980100422d0a076164647265737310041a0d080710001800300750930860002001300068007000780080010088010098010042310a0b6372656469745f6361726410051a0d0807100018003007509308600020013000680070007800800100880100980100480652600a077072696d617279100118012204636974792202696430023001400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d0010060026a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f741801800101880103980100b2013d0a077072696d61727910001a0269641a04636974791a046e616d651a07616464726573731a0b6372656469745f63617264200120022003200420052800b80101c20100e80100f2010408001200f801008002009202009a0200aa02290836100210041802180120352a11666b5f636974795f7265665f757365727330023800400048007000aa02290837100210041802180120352a11666b5f636974795f7265665f757365727330023800400048007000aa0229083a100110021802180120352a11666b5f636974795f7265665f757365727330023800400048007000b20200b80200c0021dc80200e00200f00200800300880300', 'hex'), true); +SELECT crdb_internal.unsafe_upsert_descriptor(53, decode('0afa040a0575736572731835203428013a0042280a02696410011a0d080e100018003000508617600020003000680070007800800100880100980100422a0a046369747910021a0d0807100018003007509308600020003000680070007800800100880100980100422a0a046e616d6510031a0d0807100018003007509308600020013000680070007800800100880100980100422d0a076164647265737310041a0d080710001800300750930860002001300068007000780080010088010098010042310a0b6372656469745f6361726410051a0d0807100018003007509308600020013000680070007800800100880100980100480652600a077072696d617279100118012204636974792202696430023001400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d0010060026a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f741801800101880103980100b2013d0a077072696d61727910001a0269641a04636974791a046e616d651a07616464726573731a0b6372656469745f63617264200120022003200420052800b80101c20100e80100f2010408001200f801008002009202009a0200aa02290836100210041802180120352a11666b5f636974795f7265665f757365727330023800400048007000aa02290837100210041802180120352a11666b5f636974795f7265665f757365727330023800400048007000aa0229083a100110021802180120352a11666b5f636974795f7265665f757365727330023800400048007000b20200b80200c0021dc80200e00200800300880300', 'hex'), true); SELECT crdb_internal.unsafe_upsert_namespace_entry(52, 29, 'users', 53, true); -SELECT crdb_internal.unsafe_upsert_descriptor(54, decode('0aa4070a0876656869636c65731836203428013a0042280a02696410011a0d080e100018003000508617600020003000680070007800800100880100980100422a0a046369747910021a0d0807100018003007509308600020003000680070007800800100880100980100422a0a047479706510031a0d0807100018003007509308600020013000680070007800800100880100980100422e0a086f776e65725f696410041a0d080e10001800300050861760002001300068007000780080010088010098010042330a0d6372656174696f6e5f74696d6510051a0d080510001800300050da08600020013000680070007800800100880100980100422c0a0673746174757310061a0d080710001800300750930860002001300068007000780080010088010098010042360a1063757272656e745f6c6f636174696f6e10071a0d080710001800300750930860002001300068007000780080010088010098010042290a0365787410081a0d081210001800300050da1d600020013000680070007800800100880100980100480952600a077072696d617279100118012204636974792202696430023001400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d001005a86010a2576656869636c65735f6175746f5f696e6465785f666b5f636974795f7265665f75736572731002180022046369747922086f776e65725f6964300230043801400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d0010060036a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f741801800102880103980100b201650a077072696d61727910001a0269641a04636974791a04747970651a086f776e65725f69641a0d6372656174696f6e5f74696d651a067374617475731a1063757272656e745f6c6f636174696f6e1a03657874200120022003200420052006200720082800b80101c20100e80100f2010408001200f801008002009202009a0200a202290836100210041802180120352a11666b5f636974795f7265665f757365727330003800400048007000aa02340837100310051802180120362a1c666b5f76656869636c655f636974795f7265665f76656869636c657330023800400048007000b20200b80200c0021dc80200e00200f00200800300880300', 'hex'), true); +SELECT crdb_internal.unsafe_upsert_descriptor(54, decode('0aa1070a0876656869636c65731836203428013a0042280a02696410011a0d080e100018003000508617600020003000680070007800800100880100980100422a0a046369747910021a0d0807100018003007509308600020003000680070007800800100880100980100422a0a047479706510031a0d0807100018003007509308600020013000680070007800800100880100980100422e0a086f776e65725f696410041a0d080e10001800300050861760002001300068007000780080010088010098010042330a0d6372656174696f6e5f74696d6510051a0d080510001800300050da08600020013000680070007800800100880100980100422c0a0673746174757310061a0d080710001800300750930860002001300068007000780080010088010098010042360a1063757272656e745f6c6f636174696f6e10071a0d080710001800300750930860002001300068007000780080010088010098010042290a0365787410081a0d081210001800300050da1d600020013000680070007800800100880100980100480952600a077072696d617279100118012204636974792202696430023001400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d001005a86010a2576656869636c65735f6175746f5f696e6465785f666b5f636974795f7265665f75736572731002180022046369747922086f776e65725f6964300230043801400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d0010060036a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f741801800102880103980100b201650a077072696d61727910001a0269641a04636974791a04747970651a086f776e65725f69641a0d6372656174696f6e5f74696d651a067374617475731a1063757272656e745f6c6f636174696f6e1a03657874200120022003200420052006200720082800b80101c20100e80100f2010408001200f801008002009202009a0200a202290836100210041802180120352a11666b5f636974795f7265665f757365727330003800400048007000aa02340837100310051802180120362a1c666b5f76656869636c655f636974795f7265665f76656869636c657330023800400048007000b20200b80200c0021dc80200e00200800300880300', 'hex'), true); SELECT crdb_internal.unsafe_upsert_namespace_entry(52, 29, 'vehicles', 54, true); -SELECT crdb_internal.unsafe_upsert_descriptor(55, decode('0ab70a0a0572696465731837203428013a0042280a02696410011a0d080e100018003000508617600020003000680070007800800100880100980100422a0a046369747910021a0d080710001800300750930860002000300068007000780080010088010098010042320a0c76656869636c655f6369747910031a0d0807100018003007509308600020013000680070007800800100880100980100422e0a0872696465725f696410041a0d080e10001800300050861760002001300068007000780080010088010098010042300a0a76656869636c655f696410051a0d080e10001800300050861760002001300068007000780080010088010098010042330a0d73746172745f6164647265737310061a0d080710001800300750930860002001300068007000780080010088010098010042310a0b656e645f6164647265737310071a0d080710001800300750930860002001300068007000780080010088010098010042300a0a73746172745f74696d6510081a0d080510001800300050da08600020013000680070007800800100880100980100422e0a08656e645f74696d6510091a0d080510001800300050da08600020013000680070007800800100880100980100422d0a07726576656e7565100a1a0d08031002180a300050a40d600020013000680070007800800100880100980100480b52600a077072696d617279100118012204636974792202696430023001400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d001005a83010a2272696465735f6175746f5f696e6465785f666b5f636974795f7265665f757365727310021800220463697479220872696465725f6964300230043801400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d001005a9a010a2d72696465735f6175746f5f696e6465785f666b5f76656869636c655f636974795f7265665f76656869636c657310031800220c76656869636c655f63697479220a76656869636c655f69643003300538023801400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d0010060046a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f741801800103880103980100a2013a0a1376656869636c655f63697479203d20636974791217636865636b5f76656869636c655f636974795f63697479180028022803300038004000b2018a010a077072696d61727910001a0269641a04636974791a0c76656869636c655f636974791a0872696465725f69641a0a76656869636c655f69641a0d73746172745f616464726573731a0b656e645f616464726573731a0a73746172745f74696d651a08656e645f74696d651a07726576656e7565200120022003200420052006200720082009200a2800b80101c20100e80100f2010408001200f801008002009202009a0200a202290837100210041802180120352a11666b5f636974795f7265665f757365727330003800400048007000a202340837100310051802180120362a1c666b5f76656869636c655f636974795f7265665f76656869636c657330003800400048007000aa02290838100110021802180120372a11666b5f636974795f7265665f726964657330023800400048007000b20200b80200c0021dc80200e00200f00200800300880300', 'hex'), true); +SELECT crdb_internal.unsafe_upsert_descriptor(55, decode('0ab40a0a0572696465731837203428013a0042280a02696410011a0d080e100018003000508617600020003000680070007800800100880100980100422a0a046369747910021a0d080710001800300750930860002000300068007000780080010088010098010042320a0c76656869636c655f6369747910031a0d0807100018003007509308600020013000680070007800800100880100980100422e0a0872696465725f696410041a0d080e10001800300050861760002001300068007000780080010088010098010042300a0a76656869636c655f696410051a0d080e10001800300050861760002001300068007000780080010088010098010042330a0d73746172745f6164647265737310061a0d080710001800300750930860002001300068007000780080010088010098010042310a0b656e645f6164647265737310071a0d080710001800300750930860002001300068007000780080010088010098010042300a0a73746172745f74696d6510081a0d080510001800300050da08600020013000680070007800800100880100980100422e0a08656e645f74696d6510091a0d080510001800300050da08600020013000680070007800800100880100980100422d0a07726576656e7565100a1a0d08031002180a300050a40d600020013000680070007800800100880100980100480b52600a077072696d617279100118012204636974792202696430023001400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d001005a83010a2272696465735f6175746f5f696e6465785f666b5f636974795f7265665f757365727310021800220463697479220872696465725f6964300230043801400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d001005a9a010a2d72696465735f6175746f5f696e6465785f666b5f76656869636c655f636974795f7265665f76656869636c657310031800220c76656869636c655f63697479220a76656869636c655f69643003300538023801400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d0010060046a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f741801800103880103980100a2013a0a1376656869636c655f63697479203d20636974791217636865636b5f76656869636c655f636974795f63697479180028022803300038004000b2018a010a077072696d61727910001a0269641a04636974791a0c76656869636c655f636974791a0872696465725f69641a0a76656869636c655f69641a0d73746172745f616464726573731a0b656e645f616464726573731a0a73746172745f74696d651a08656e645f74696d651a07726576656e7565200120022003200420052006200720082009200a2800b80101c20100e80100f2010408001200f801008002009202009a0200a202290837100210041802180120352a11666b5f636974795f7265665f757365727330003800400048007000a202340837100310051802180120362a1c666b5f76656869636c655f636974795f7265665f76656869636c657330003800400048007000aa02290838100110021802180120372a11666b5f636974795f7265665f726964657330023800400048007000b20200b80200c0021dc80200e00200800300880300', 'hex'), true); SELECT crdb_internal.unsafe_upsert_namespace_entry(52, 29, 'rides', 55, true); -SELECT crdb_internal.unsafe_upsert_descriptor(56, decode('0acc040a1a76656869636c655f6c6f636174696f6e5f686973746f726965731838203428013a00422a0a046369747910011a0d0807100018003007509308600020003000680070007800800100880100980100422d0a07726964655f696410021a0d080e100018003000508617600020003000680070007800800100880100980100422f0a0974696d657374616d7010031a0d080510001800300050da0860002000300068007000780080010088010098010042290a036c617410041a0d080210401800300050bd05600020013000680070007800800100880100980100422a0a046c6f6e6710051a0d080210401800300050bd05600020013000680070007800800100880100980100480652740a077072696d617279100118012204636974792207726964655f6964220974696d657374616d703001300230034000400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d0010060026a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f741801800102880103980100b2013c0a077072696d61727910001a04636974791a07726964655f69641a0974696d657374616d701a036c61741a046c6f6e67200120022003200420052800b80101c20100e80100f2010408001200f801008002009202009a0200a202290838100110021802180120372a11666b5f636974795f7265665f726964657330003800400048007000b20200b80200c0021dc80200e00200f00200800300880300', 'hex'), true); +SELECT crdb_internal.unsafe_upsert_descriptor(56, decode('0ac9040a1a76656869636c655f6c6f636174696f6e5f686973746f726965731838203428013a00422a0a046369747910011a0d0807100018003007509308600020003000680070007800800100880100980100422d0a07726964655f696410021a0d080e100018003000508617600020003000680070007800800100880100980100422f0a0974696d657374616d7010031a0d080510001800300050da0860002000300068007000780080010088010098010042290a036c617410041a0d080210401800300050bd05600020013000680070007800800100880100980100422a0a046c6f6e6710051a0d080210401800300050bd05600020013000680070007800800100880100980100480652740a077072696d617279100118012204636974792207726964655f6964220974696d657374616d703001300230034000400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d0010060026a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f741801800102880103980100b2013c0a077072696d61727910001a04636974791a07726964655f69641a0974696d657374616d701a036c61741a046c6f6e67200120022003200420052800b80101c20100e80100f2010408001200f801008002009202009a0200a202290838100110021802180120372a11666b5f636974795f7265665f726964657330003800400048007000b20200b80200c0021dc80200e00200800300880300', 'hex'), true); SELECT crdb_internal.unsafe_upsert_namespace_entry(52, 29, 'vehicle_location_histories', 56, true); -SELECT crdb_internal.unsafe_upsert_descriptor(57, decode('0a9f040a0b70726f6d6f5f636f6465731839203428013a00422a0a04636f646510011a0d080710001800300750930860002000300068007000780080010088010098010042310a0b6465736372697074696f6e10021a0d080710001800300750930860002001300068007000780080010088010098010042330a0d6372656174696f6e5f74696d6510031a0d080510001800300050da0860002001300068007000780080010088010098010042350a0f65787069726174696f6e5f74696d6510041a0d080510001800300050da08600020013000680070007800800100880100980100422b0a0572756c657310051a0d081210001800300050da1d600020013000680070007800800100880100980100480652580a077072696d617279100118012204636f6465300140004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d0010060026a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f741801800101880103980100b201510a077072696d61727910001a04636f64651a0b6465736372697074696f6e1a0d6372656174696f6e5f74696d651a0f65787069726174696f6e5f74696d651a0572756c6573200120022003200420052800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200f00200800300880300', 'hex'), true); +SELECT crdb_internal.unsafe_upsert_descriptor(57, decode('0a9c040a0b70726f6d6f5f636f6465731839203428013a00422a0a04636f646510011a0d080710001800300750930860002000300068007000780080010088010098010042310a0b6465736372697074696f6e10021a0d080710001800300750930860002001300068007000780080010088010098010042330a0d6372656174696f6e5f74696d6510031a0d080510001800300050da0860002001300068007000780080010088010098010042350a0f65787069726174696f6e5f74696d6510041a0d080510001800300050da08600020013000680070007800800100880100980100422b0a0572756c657310051a0d081210001800300050da1d600020013000680070007800800100880100980100480652580a077072696d617279100118012204636f6465300140004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d0010060026a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f741801800101880103980100b201510a077072696d61727910001a04636f64651a0b6465736372697074696f6e1a0d6372656174696f6e5f74696d651a0f65787069726174696f6e5f74696d651a0572756c6573200120022003200420052800b80101c20100e80100f2010408001200f801008002009202009a0200b20200b80200c0021dc80200e00200800300880300', 'hex'), true); SELECT crdb_internal.unsafe_upsert_namespace_entry(52, 29, 'promo_codes', 57, true); -SELECT crdb_internal.unsafe_upsert_descriptor(58, decode('0acc040a10757365725f70726f6d6f5f636f646573183a203428013a00422a0a046369747910011a0d0807100018003007509308600020003000680070007800800100880100980100422d0a07757365725f696410021a0d080e100018003000508617600020003000680070007800800100880100980100422a0a04636f646510031a0d0807100018003007509308600020003000680070007800800100880100980100422f0a0974696d657374616d7010041a0d080510001800300050da0860002001300068007000780080010088010098010042300a0b75736167655f636f756e7410051a0c080110401800300050146000200130006800700078008001008801009801004806526f0a077072696d617279100118012204636974792207757365725f69642204636f64653001300230034000400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d0010060026a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f741801800102880103980100b201440a077072696d61727910001a04636974791a07757365725f69641a04636f64651a0974696d657374616d701a0b75736167655f636f756e74200120022003200420052800b80101c20100e80100f2010408001200f801008002009202009a0200a20229083a100110021802180120352a11666b5f636974795f7265665f757365727330003800400048007000b20200b80200c0021dc80200e00200f00200800300880300', 'hex'), true); +SELECT crdb_internal.unsafe_upsert_descriptor(58, decode('0ac9040a10757365725f70726f6d6f5f636f646573183a203428013a00422a0a046369747910011a0d0807100018003007509308600020003000680070007800800100880100980100422d0a07757365725f696410021a0d080e100018003000508617600020003000680070007800800100880100980100422a0a04636f646510031a0d0807100018003007509308600020003000680070007800800100880100980100422f0a0974696d657374616d7010041a0d080510001800300050da0860002001300068007000780080010088010098010042300a0b75736167655f636f756e7410051a0c080110401800300050146000200130006800700078008001008801009801004806526f0a077072696d617279100118012204636974792207757365725f69642204636f64653001300230034000400040004a10080010001a00200028003000380040005a007a0408002000800100880100900101980100a20106080012001800a80100b20100ba0100c00100c80100d0010060026a210a0b0a0561646d696e100218000a0a0a04726f6f74100218001204726f6f741801800102880103980100b201440a077072696d61727910001a04636974791a07757365725f69641a04636f64651a0974696d657374616d701a0b75736167655f636f756e74200120022003200420052800b80101c20100e80100f2010408001200f801008002009202009a0200a20229083a100110021802180120352a11666b5f636974795f7265665f757365727330003800400048007000b20200b80200c0021dc80200e00200800300880300', 'hex'), true); SELECT crdb_internal.unsafe_upsert_namespace_entry(52, 29, 'user_promo_codes', 58, true); COMMIT; diff --git a/pkg/sql/catalog/BUILD.bazel b/pkg/sql/catalog/BUILD.bazel index 19bd57705f8b..9fe419c59cbe 100644 --- a/pkg/sql/catalog/BUILD.bazel +++ b/pkg/sql/catalog/BUILD.bazel @@ -27,6 +27,7 @@ go_library( "//pkg/sql/catalog/descpb", "//pkg/sql/pgwire/pgcode", "//pkg/sql/pgwire/pgerror", + "//pkg/sql/schemachanger/scpb", "//pkg/sql/sem/tree", "//pkg/sql/types", "//pkg/util", diff --git a/pkg/sql/catalog/catalog.go b/pkg/sql/catalog/catalog.go index 20ca9643e528..60dd47699015 100644 --- a/pkg/sql/catalog/catalog.go +++ b/pkg/sql/catalog/catalog.go @@ -14,6 +14,7 @@ import ( "math" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" ) @@ -56,6 +57,10 @@ type MutableDescriptor interface { // HasPostDeserializationChanges returns if the MutableDescriptor was changed after running // RunPostDeserializationChanges. HasPostDeserializationChanges() bool + + // SetDeclarativeSchemaChangerState sets the state of the declarative + // schema change currently operating on this descriptor. + SetDeclarativeSchemaChangerState(*scpb.DescriptorState) } // VirtualSchemas is a collection of VirtualSchemas. diff --git a/pkg/sql/catalog/dbdesc/BUILD.bazel b/pkg/sql/catalog/dbdesc/BUILD.bazel index 9efd0b0abd04..af6800902d02 100644 --- a/pkg/sql/catalog/dbdesc/BUILD.bazel +++ b/pkg/sql/catalog/dbdesc/BUILD.bazel @@ -17,6 +17,7 @@ go_library( "//pkg/sql/catalog/descpb", "//pkg/sql/catalog/multiregion", "//pkg/sql/privilege", + "//pkg/sql/schemachanger/scpb", "//pkg/sql/sem/tree", "//pkg/util/hlc", "//pkg/util/iterutil", diff --git a/pkg/sql/catalog/dbdesc/database_desc.go b/pkg/sql/catalog/dbdesc/database_desc.go index 3fe6c8d29b23..d75703b11dff 100644 --- a/pkg/sql/catalog/dbdesc/database_desc.go +++ b/pkg/sql/catalog/dbdesc/database_desc.go @@ -22,6 +22,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/multiregion" "github.com/cockroachdb/cockroach/pkg/sql/privilege" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/iterutil" @@ -503,6 +504,18 @@ func (desc *Mutable) AddSchemaToDatabase( desc.Schemas[schemaName] = schemaInfo } +// GetDeclarativeSchemaChangerState is part of the catalog.MutableDescriptor +// interface. +func (desc *immutable) GetDeclarativeSchemaChangerState() *scpb.DescriptorState { + return desc.DeclarativeSchemaChangerState.Clone() +} + +// SetDeclarativeSchemaChangerState is part of the catalog.MutableDescriptor +// interface. +func (desc *Mutable) SetDeclarativeSchemaChangerState(state *scpb.DescriptorState) { + desc.DeclarativeSchemaChangerState = state +} + // maybeRemoveDroppedSelfEntryFromSchemas removes an entry in the Schemas map corresponding to the // database itself which was added due to a bug in prior versions when dropping any user-defined schema. // The bug inserted an entry for the database rather than the schema being dropped. This function fixes the diff --git a/pkg/sql/catalog/descpb/BUILD.bazel b/pkg/sql/catalog/descpb/BUILD.bazel index 5ea27b4ace83..3b051ca0232b 100644 --- a/pkg/sql/catalog/descpb/BUILD.bazel +++ b/pkg/sql/catalog/descpb/BUILD.bazel @@ -62,6 +62,7 @@ proto_library( "//pkg/geo/geoindex:geoindex_proto", "//pkg/roachpb:roachpb_proto", "//pkg/sql/catalog/catpb:catpb_proto", + "//pkg/sql/schemachanger/scpb:scpb_proto", "//pkg/sql/types:types_proto", "//pkg/util/hlc:hlc_proto", "@com_github_gogo_protobuf//gogoproto:gogo_proto", @@ -78,6 +79,7 @@ go_proto_library( "//pkg/geo/geoindex", "//pkg/roachpb", # keep "//pkg/sql/catalog/catpb", + "//pkg/sql/schemachanger/scpb", "//pkg/sql/types", "//pkg/util/hlc", "@com_github_gogo_protobuf//gogoproto", diff --git a/pkg/sql/catalog/descpb/structured.proto b/pkg/sql/catalog/descpb/structured.proto index e92ad3aabfda..8b6be40a2f3b 100644 --- a/pkg/sql/catalog/descpb/structured.proto +++ b/pkg/sql/catalog/descpb/structured.proto @@ -16,6 +16,7 @@ option go_package = "descpb"; import "util/hlc/timestamp.proto"; import "sql/catalog/catpb/catalog.proto"; import "sql/catalog/catpb/privilege.proto"; +import "sql/schemachanger/scpb/scpb.proto"; import "sql/types/types.proto"; import "geo/geoindex/config.proto"; import "gogoproto/gogo.proto"; @@ -1078,13 +1079,11 @@ message TableDescriptor { // with the mutations list. repeated MutationJob mutationJobs = 27 [(gogoproto.nullable) = false]; - // The job associated with a schema change job run in the new schema changer - // (in sql/schemachanger), if one exists. Only one such job can exist at a - // time. - optional int64 new_schema_change_job_id = 46 [ - (gogoproto.nullable) = false, - (gogoproto.customname) = "NewSchemaChangeJobID", - (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb.JobID"]; + reserved 46; + + // DeclarativeSchemaChangerState contains the state corresponding to the + // descriptor being changed as part of a declarative schema change. + optional cockroach.sql.schemachanger.scpb.DescriptorState declarative_schema_changer_state = 50; message SequenceOpts { option (gogoproto.equal) = true; @@ -1252,6 +1251,8 @@ message TableDescriptor { // Constraint ID for the next constraint. optional uint32 next_constraint_id = 49 [(gogoproto.nullable) = false, (gogoproto.customname) = "NextConstraintID", (gogoproto.casttype) = "ConstraintID"]; + + // Next ID: 51 } // SurvivalGoal is the survival goal for a database. @@ -1335,6 +1336,12 @@ message DatabaseDescriptor { // DefaultPrivileges contains the default privileges for the database. optional DefaultPrivilegeDescriptor default_privileges = 11; + + // DeclarativeSchemaChangerState contains the state corresponding to the + // descriptor being changed as part of a declarative schema change. + optional cockroach.sql.schemachanger.scpb.DescriptorState declarative_schema_changer_state = 12; + + // Next field is 13. } // TypeDescriptor represents a user defined type and is stored in a structured @@ -1456,6 +1463,12 @@ message TypeDescriptor { } optional RegionConfig region_config = 16; + + // DeclarativeSchemaChangerState contains the state corresponding to the + // descriptor being changed as part of a declarative schema change. + optional cockroach.sql.schemachanger.scpb.DescriptorState declarative_schema_changer_state = 17; + + // Next field is 18. } // SchemaDescriptor represents a physical schema and is stored in a structured @@ -1493,6 +1506,12 @@ message SchemaDescriptor { // DefaultPrivileges contains the default privileges for the database. optional DefaultPrivilegeDescriptor default_privileges = 10; + + // DeclarativeSchemaChangerState contains the state corresponding to the + // descriptor being changed as part of a declarative schema change. + optional cockroach.sql.schemachanger.scpb.DescriptorState declarative_schema_changer_state = 11; + + // Next field is 12. } // Descriptor is a union type for descriptors for tables, schemas, databases, diff --git a/pkg/sql/catalog/descriptor.go b/pkg/sql/catalog/descriptor.go index 511eb60ae392..80468c6ec1dd 100644 --- a/pkg/sql/catalog/descriptor.go +++ b/pkg/sql/catalog/descriptor.go @@ -19,6 +19,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/hlc" @@ -203,6 +204,13 @@ type Descriptor interface { // ValidateTxnCommit performs pre-commit checks. ValidateTxnCommit(vea ValidationErrorAccumulator, vdg ValidationDescGetter) + + // GetDeclarativeSchemaChangerState returns the state of the declarative + // schema change currently operating on this descriptor. It will be nil if + // there is no declarative schema change job. Note that it is a clone of + // the value on the descriptor. Changes will need to be written back to + // the descriptor using SetDeclarativeSchemaChangeState. + GetDeclarativeSchemaChangerState() *scpb.DescriptorState } // DatabaseDescriptor encapsulates the concept of a database. diff --git a/pkg/sql/catalog/descs/collection.go b/pkg/sql/catalog/descs/collection.go index 915f93ec6493..48f7ef59c22e 100644 --- a/pkg/sql/catalog/descs/collection.go +++ b/pkg/sql/catalog/descs/collection.go @@ -115,7 +115,7 @@ type Collection struct { // droppedDescriptors that will not need to wait for new // lease versions. - deletedDescs []catalog.Descriptor + deletedDescs catalog.DescriptorIDSet // maxTimestampBoundDeadlineHolder contains the maximum timestamp to read // schemas at. This is only set during the retries of bounded_staleness when @@ -178,7 +178,7 @@ func (tc *Collection) ReleaseAll(ctx context.Context) { tc.uncommitted.reset() tc.kv.reset() tc.synthetic.reset() - tc.deletedDescs = nil + tc.deletedDescs = catalog.DescriptorIDSet{} tc.skipValidationOnWrite = false } @@ -426,8 +426,8 @@ func (tc *Collection) codec() keys.SQLCodec { // be inside storage. // Note: that this happens, at time of writing, only when reverting an // IMPORT or RESTORE. -func (tc *Collection) AddDeletedDescriptor(desc catalog.Descriptor) { - tc.deletedDescs = append(tc.deletedDescs, desc) +func (tc *Collection) AddDeletedDescriptor(id descpb.ID) { + tc.deletedDescs.Add(id) } // SetSession sets the sqlliveness.Session for the transaction. This diff --git a/pkg/sql/catalog/descs/txn.go b/pkg/sql/catalog/descs/txn.go index 269f69fa2416..08661b70ac1f 100644 --- a/pkg/sql/catalog/descs/txn.go +++ b/pkg/sql/catalog/descs/txn.go @@ -60,17 +60,11 @@ func (cf *CollectionFactory) Txn( ) error { // Waits for descriptors that were modified, skipping // over ones that had their descriptor wiped. - waitForDescriptors := func(modifiedDescriptors []lease.IDVersion, deletedDescs []catalog.Descriptor) error { + waitForDescriptors := func(modifiedDescriptors []lease.IDVersion, deletedDescs catalog.DescriptorIDSet) error { // Wait for a single version on leased descriptors. for _, ld := range modifiedDescriptors { - waitForNoVersion := false + waitForNoVersion := deletedDescs.Contains(ld.ID) // Detect unpublished ones. - for _, deletedDesc := range deletedDescs { - if deletedDesc.GetID() == ld.ID { - waitForNoVersion = true - break - } - } if waitForNoVersion { err := cf.leaseMgr.WaitForNoVersion(ctx, ld.ID, retry.Options{}) if err != nil { @@ -87,12 +81,12 @@ func (cf *CollectionFactory) Txn( } for { var modifiedDescriptors []lease.IDVersion - var deletedDescs []catalog.Descriptor + var deletedDescs catalog.DescriptorIDSet var descsCol Collection if err := db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error { modifiedDescriptors = nil - deletedDescs = nil - descsCol = cf.MakeCollection(ctx, nil) + deletedDescs = catalog.DescriptorIDSet{} + descsCol = cf.MakeCollection(ctx, nil /* temporarySchemaProvider */) defer descsCol.ReleaseAll(ctx) if !UnsafeSkipSystemConfigTrigger.Get(&cf.settings.SV) { if err := txn.SetSystemConfigTrigger(cf.leaseMgr.Codec().ForSystemTenant()); err != nil { diff --git a/pkg/sql/catalog/internal/validate/BUILD.bazel b/pkg/sql/catalog/internal/validate/BUILD.bazel index 4ef5093f30f3..1ae987251e39 100644 --- a/pkg/sql/catalog/internal/validate/BUILD.bazel +++ b/pkg/sql/catalog/internal/validate/BUILD.bazel @@ -1,8 +1,11 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "validate", - srcs = ["validate.go"], + srcs = [ + "schema_changer_state.go", + "validate.go", + ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/catalog/internal/validate", visibility = ["//pkg/sql/catalog:__subpackages__"], deps = [ @@ -11,6 +14,30 @@ go_library( "//pkg/server/telemetry", "//pkg/sql/catalog", "//pkg/sql/catalog/descpb", + "//pkg/sql/schemachanger/scpb", + "//pkg/sql/schemachanger/screl", + "//pkg/util", "@com_github_cockroachdb_errors//:errors", + "@com_github_cockroachdb_redact//:redact", + ], +) + +go_test( + name = "validate_test", + srcs = [ + "helpers_test.go", + "schema_changer_state_test.go", + ], + embed = [":validate"], + deps = [ + "//pkg/clusterversion", + "//pkg/sql/catalog", + "//pkg/sql/catalog/dbdesc", + "//pkg/sql/catalog/descpb", + "//pkg/sql/catalog/schemadesc", + "//pkg/sql/catalog/tabledesc", + "//pkg/sql/catalog/typedesc", + "//pkg/sql/schemachanger/scpb", + "@com_github_stretchr_testify//require", ], ) diff --git a/pkg/sql/catalog/internal/validate/helpers_test.go b/pkg/sql/catalog/internal/validate/helpers_test.go new file mode 100644 index 000000000000..7e6e3956d311 --- /dev/null +++ b/pkg/sql/catalog/internal/validate/helpers_test.go @@ -0,0 +1,33 @@ +// Copyright 2022 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 validate + +import ( + "context" + + "github.com/cockroachdb/cockroach/pkg/clusterversion" + "github.com/cockroachdb/cockroach/pkg/sql/catalog" +) + +const InvalidSchemaChangerStatePrefix = invalidSchemaChangerStatePrefix + ":" + +func TestingSchemaChangerState( + ctx context.Context, desc catalog.Descriptor, +) catalog.ValidationErrors { + vea := validationErrorAccumulator{ + targetLevel: catalog.ValidationLevelSelfOnly, + activeVersion: clusterversion.TestingClusterVersion, + currentState: validatingDescriptor, + currentDescriptor: desc, + } + validateSchemaChangerState(desc, &vea) + return vea.errors +} diff --git a/pkg/sql/catalog/internal/validate/schema_changer_state.go b/pkg/sql/catalog/internal/validate/schema_changer_state.go new file mode 100644 index 000000000000..88b8d4b2a0ad --- /dev/null +++ b/pkg/sql/catalog/internal/validate/schema_changer_state.go @@ -0,0 +1,153 @@ +// Copyright 2022 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 validate + +import ( + "fmt" + "sort" + "strings" + + "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/screl" + "github.com/cockroachdb/cockroach/pkg/util" + "github.com/cockroachdb/errors" + "github.com/cockroachdb/redact" +) + +const invalidSchemaChangerStatePrefix = "invalid schema changer state" + +func validateSchemaChangerState(d catalog.Descriptor, vea catalog.ValidationErrorAccumulator) { + scs := d.GetDeclarativeSchemaChangerState() + if scs == nil { + return + } + report := func(err error) { + vea.Report(errors.WrapWithDepth( + 1, err, invalidSchemaChangerStatePrefix, + )) + } + + // TODO(ajwerner): Add job validation to ensure that the job exists and is + // in a non-terminal state. This would probably not be part of ValidateSelf. + if scs.JobID == 0 { + report(errors.New("empty job ID")) + } + + if len(scs.Targets) == 0 { + report(errors.New("no targets")) + } + + // Validate that the targets correspond to this descriptor. + for i, t := range scs.Targets { + if got := screl.GetDescID(t.Element()); got != d.GetID() { + report(errors.Errorf("target %d corresponds to descriptor %d != %d", + i, got, d.GetID())) + } + } + + // Validate that the various parallel fields are sound. + { + var haveProblem bool + if nt, ntr := len(scs.Targets), len(scs.TargetRanks); nt != ntr { + haveProblem = true + report(errors.Errorf("number mismatch between Targets and TargetRanks: %d != %d", + nt, ntr)) + } + if nt, ns := len(scs.Targets), len(scs.CurrentStatuses); nt != ns { + haveProblem = true + report(errors.Errorf("number mismatch between Targets and CurentStatuses: %d != %d", + nt, ns)) + } + // If there's a mismatch, the validation below will not be sane. + if haveProblem { + return + } + } + + // Validate that the target ranks are unique. + ranksToTarget := map[uint32]*scpb.Target{} + { + var duplicates util.FastIntSet + for i, r := range scs.TargetRanks { + if _, exists := ranksToTarget[r]; exists { + duplicates.Add(int(r)) + } else { + ranksToTarget[r] = &scs.Targets[i] + } + } + if duplicates.Len() > 0 { + report(errors.Errorf("TargetRanks contains duplicate entries %v", + redact.SafeString(fmt.Sprint(duplicates.Ordered())))) + } + } + + // Validate that the statements are sorted. + if !sort.SliceIsSorted(scs.RelevantStatements, func(i, j int) bool { + return scs.RelevantStatements[i].StatementRank < scs.RelevantStatements[j].StatementRank + }) { + report(errors.New("RelevantStatements are not sorted")) + } + + // Validate that the statements refer exclusively to targets in this + // descriptor. + statementsExpected := map[uint32]*util.FastIntSet{} + for i := range scs.Targets { + t := &scs.Targets[i] + exp, ok := statementsExpected[t.Metadata.StatementID] + if !ok { + exp = &util.FastIntSet{} + statementsExpected[t.Metadata.StatementID] = exp + } + exp.Add(int(scs.TargetRanks[i])) + } + var statementRanks util.FastIntSet + for _, s := range scs.RelevantStatements { + statementRanks.Add(int(s.StatementRank)) + if _, ok := statementsExpected[s.StatementRank]; !ok { + report(errors.Errorf("unexpected statement %d (%s)", + s.StatementRank, s.Statement.Statement)) + } + } + + // Validate that there are no duplicate statements. + if statementRanks.Len() != len(scs.RelevantStatements) { + report(errors.Errorf("duplicates exist in RelevantStatements")) + } + + // Validate that all targets have a corresponding statement. + { + var expected util.FastIntSet + stmts := statementRanks.Copy() + for rank := range statementsExpected { + expected.Add(int(rank)) + } + stmts.ForEach(func(i int) { expected.Remove(i) }) + + expected.ForEach(func(stmtRank int) { + expectedTargetRanks := statementsExpected[uint32(stmtRank)] + var ranks, elementStrs []string + expectedTargetRanks.ForEach(func(targetRank int) { + ranks = append(ranks, fmt.Sprint(targetRank)) + elementStrs = append(elementStrs, + screl.ElementString(ranksToTarget[uint32(targetRank)].Element())) + }) + report(errors.Errorf("missing statement for targets (%s) / (%s)", + redact.SafeString(strings.Join(ranks, ", ")), + strings.Join(elementStrs, ", "), + )) + }) + } + + // TODO(ajwerner): Consider asserting that the current statuses make sense + // for the target elements. + +} diff --git a/pkg/sql/catalog/internal/validate/schema_changer_state_test.go b/pkg/sql/catalog/internal/validate/schema_changer_state_test.go new file mode 100644 index 000000000000..119760e3e20d --- /dev/null +++ b/pkg/sql/catalog/internal/validate/schema_changer_state_test.go @@ -0,0 +1,281 @@ +// Copyright 2022 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 validate_test + +import ( + "context" + "testing" + + "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/dbdesc" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/internal/validate" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" + "github.com/stretchr/testify/require" +) + +func TestValidateSchemaChangerState(t *testing.T) { + // TODO(ajwerner): bitmask the cases for which descriptor types they + // should apply to when verifying that the elements make sense given + // the descriptor targets. + type ds = scpb.DescriptorState + type mkDesc = func(name string, id descpb.ID, s scpb.DescriptorState) catalog.Descriptor + type testCase struct { + name string + id descpb.ID + ds ds + expectedErrors []string + } + runTestCase := func(t *testing.T, c testCase, mk mkDesc) { + d := mk(c.name, c.id, c.ds) + errs := validate.TestingSchemaChangerState(context.Background(), d) + require.Equalf(t, len(errs), len(c.expectedErrors), + "expected %d errors, got %d: %q", len(c.expectedErrors), len(errs), errs) + for i, exp := range c.expectedErrors { + require.Regexp(t, exp, errs[i]) + } + } + const prefix = validate.InvalidSchemaChangerStatePrefix + testCases := []testCase{ + { + name: "no targets or job ID", + ds: ds{}, + expectedErrors: []string{ + prefix + " empty job ID", + prefix + " no targets", + }, + }, + { + name: "no targets", + ds: ds{JobID: 1}, + expectedErrors: []string{ + prefix + " no targets", + }, + }, + { + name: "target desc ID mismatch, and missing ranks and status", + ds: ds{ + JobID: 1, + Targets: []scpb.Target{ + scpb.MakeTarget(scpb.Status_PUBLIC, &scpb.Namespace{ + DatabaseID: 2, + SchemaID: 2, + DescriptorID: 3, + Name: "foo", + }, &scpb.TargetMetadata{ + SubWorkID: 1, + SourceElementID: 1, + StatementID: 2, + }), + }, + }, + expectedErrors: []string{ + prefix + " target 0 corresponds to descriptor 3 != 0", + prefix + " number mismatch between Targets and TargetRanks: 1 != 0", + prefix + " number mismatch between Targets and CurentStatuses: 1 != 0", + }, + }, + { + name: "statement has wrong target", + id: 3, + ds: ds{ + JobID: 1, + Targets: []scpb.Target{ + scpb.MakeTarget(scpb.Status_PUBLIC, &scpb.Namespace{ + DatabaseID: 2, + SchemaID: 2, + DescriptorID: 3, + Name: "foo", + }, &scpb.TargetMetadata{ + SubWorkID: 1, + SourceElementID: 1, + StatementID: 2, + }), + }, + CurrentStatuses: []scpb.Status{scpb.Status_ABSENT}, + TargetRanks: []uint32{0}, + RelevantStatements: []scpb.DescriptorState_Statement{ + { + Statement: scpb.Statement{ + Statement: "ALTER TABLE a RENAME TO b", + }, + StatementRank: 0, + }, + }, + }, + expectedErrors: []string{ + prefix + ` unexpected statement 0 \(ALTER TABLE a RENAME TO b\)`, + prefix + ` missing statement for targets \(0\) / \(Namespace:\{DescID: 3, Name: foo\}\)`, + }, + }, + { + name: "statements are out of order", + id: 3, + ds: ds{ + JobID: 1, + Targets: []scpb.Target{ + scpb.MakeTarget(scpb.Status_PUBLIC, &scpb.Namespace{ + DatabaseID: 2, + SchemaID: 2, + DescriptorID: 3, + Name: "foo", + }, &scpb.TargetMetadata{ + SubWorkID: 1, + SourceElementID: 1, + StatementID: 0, + }), + scpb.MakeTarget(scpb.Status_PUBLIC, &scpb.Namespace{ + DatabaseID: 2, + SchemaID: 2, + DescriptorID: 3, + Name: "foo", + }, &scpb.TargetMetadata{ + SubWorkID: 1, + SourceElementID: 1, + StatementID: 1, + }), + }, + CurrentStatuses: []scpb.Status{scpb.Status_ABSENT, scpb.Status_ABSENT}, + TargetRanks: []uint32{0, 1}, + RelevantStatements: []scpb.DescriptorState_Statement{ + { + Statement: scpb.Statement{ + Statement: "ALTER TABLE a RENAME TO b", + }, + StatementRank: 1, + }, + { + Statement: scpb.Statement{ + Statement: "ALTER TABLE b RENAME TO a", + }, + StatementRank: 0, + }, + }, + }, + expectedErrors: []string{ + prefix + ` RelevantStatements are not sorted`, + }, + }, + { + name: "duplicate target ranks", + id: 3, + ds: ds{ + JobID: 1, + Targets: []scpb.Target{ + scpb.MakeTarget(scpb.Status_PUBLIC, &scpb.Namespace{ + DatabaseID: 2, + SchemaID: 2, + DescriptorID: 3, + Name: "foo", + }, &scpb.TargetMetadata{ + SubWorkID: 1, + SourceElementID: 1, + StatementID: 1, + }), + scpb.MakeTarget(scpb.Status_PUBLIC, &scpb.Namespace{ + DatabaseID: 2, + SchemaID: 2, + DescriptorID: 3, + Name: "foo", + }, &scpb.TargetMetadata{ + SubWorkID: 1, + SourceElementID: 1, + StatementID: 1, + }), + }, + CurrentStatuses: []scpb.Status{scpb.Status_ABSENT, scpb.Status_ABSENT}, + TargetRanks: []uint32{1, 1}, + RelevantStatements: []scpb.DescriptorState_Statement{ + { + Statement: scpb.Statement{ + Statement: "ALTER TABLE a RENAME TO b", + }, + StatementRank: 1, + }, + }, + }, + expectedErrors: []string{ + prefix + ` TargetRanks contains duplicate entries \[1\]`, + }, + }, + } + + mkFuncs := []struct { + name string + mk mkDesc + }{ + { + name: "type", + mk: func(name string, id descpb.ID, s scpb.DescriptorState) catalog.Descriptor { + return typedesc.NewBuilder(&descpb.TypeDescriptor{ + Name: name, + ID: id, + Version: 1, + ParentID: 1, + ParentSchemaID: 29, + State: descpb.DescriptorState_PUBLIC, + DeclarativeSchemaChangerState: &s, + }).BuildCreatedMutable() + }, + }, + { + name: "database", + mk: func(name string, id descpb.ID, s scpb.DescriptorState) catalog.Descriptor { + return dbdesc.NewBuilder(&descpb.DatabaseDescriptor{ + Name: name, + ID: id, + Version: 1, + State: descpb.DescriptorState_PUBLIC, + DeclarativeSchemaChangerState: &s, + }).BuildCreatedMutable() + }, + }, + { + name: "schema", + mk: func(name string, id descpb.ID, s scpb.DescriptorState) catalog.Descriptor { + return schemadesc.NewBuilder(&descpb.SchemaDescriptor{ + Name: name, + ID: id, + ParentID: 1, + Version: 1, + State: descpb.DescriptorState_PUBLIC, + DeclarativeSchemaChangerState: &s, + }).BuildCreatedMutable() + }, + }, + { + name: "table", + mk: func(name string, id descpb.ID, s scpb.DescriptorState) catalog.Descriptor { + return tabledesc.NewBuilder(&descpb.TableDescriptor{ + Name: name, + ID: id, + ParentID: 1, + Version: 1, + State: descpb.DescriptorState_PUBLIC, + UnexposedParentSchemaID: 29, + DeclarativeSchemaChangerState: &s, + }).BuildCreatedMutable() + }, + }, + } + for _, c := range testCases { + t.Run(c.name, func(t *testing.T) { + for _, mk := range mkFuncs { + t.Run(mk.name, func(t *testing.T) { + runTestCase(t, c, mk.mk) + }) + } + }) + } +} diff --git a/pkg/sql/catalog/internal/validate/validate.go b/pkg/sql/catalog/internal/validate/validate.go index 20223c7e2061..634d699ca018 100644 --- a/pkg/sql/catalog/internal/validate/validate.go +++ b/pkg/sql/catalog/internal/validate/validate.go @@ -56,6 +56,7 @@ func Validate( descriptors, func(desc catalog.Descriptor) { desc.ValidateSelf(&vea) + validateSchemaChangerState(desc, &vea) }) { return vea.errors } diff --git a/pkg/sql/catalog/schemadesc/BUILD.bazel b/pkg/sql/catalog/schemadesc/BUILD.bazel index 09eae6fea9a9..5e28d7b11450 100644 --- a/pkg/sql/catalog/schemadesc/BUILD.bazel +++ b/pkg/sql/catalog/schemadesc/BUILD.bazel @@ -22,6 +22,7 @@ go_library( "//pkg/sql/pgwire/pgcode", "//pkg/sql/pgwire/pgerror", "//pkg/sql/privilege", + "//pkg/sql/schemachanger/scpb", "//pkg/sql/sem/tree", "//pkg/util/hlc", "//pkg/util/log", diff --git a/pkg/sql/catalog/schemadesc/schema_desc.go b/pkg/sql/catalog/schemadesc/schema_desc.go index 30ee62396a56..e89a9a705bba 100644 --- a/pkg/sql/catalog/schemadesc/schema_desc.go +++ b/pkg/sql/catalog/schemadesc/schema_desc.go @@ -23,6 +23,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/privilege" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/errors" "github.com/cockroachdb/redact" @@ -343,6 +344,18 @@ func (desc *Mutable) SetDefaultPrivilegeDescriptor( desc.DefaultPrivileges = defaultPrivilegeDescriptor } +// GetDeclarativeSchemaChangeState is part of the catalog.MutableDescriptor +// interface. +func (desc *immutable) GetDeclarativeSchemaChangeState() *scpb.DescriptorState { + return desc.DeclarativeSchemaChangerState.Clone() +} + +// SetDeclarativeSchemaChangerState is part of the catalog.MutableDescriptor +// interface. +func (desc *Mutable) SetDeclarativeSchemaChangerState(state *scpb.DescriptorState) { + desc.DeclarativeSchemaChangerState = state +} + // IsSchemaNameValid returns whether the input name is valid for a user defined // schema. func IsSchemaNameValid(name string) error { diff --git a/pkg/sql/catalog/schemadesc/synthetic_schema_desc.go b/pkg/sql/catalog/schemadesc/synthetic_schema_desc.go index aff242cc854a..248388b7067c 100644 --- a/pkg/sql/catalog/schemadesc/synthetic_schema_desc.go +++ b/pkg/sql/catalog/schemadesc/synthetic_schema_desc.go @@ -17,6 +17,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/catprivilege" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/log" ) @@ -108,6 +109,9 @@ func (p synthetic) SchemaDesc() *descpb.SchemaDescriptor { "synthetic %s cannot be encoded", p.kindName()) return nil // unreachable } +func (p synthetic) GetDeclarativeSchemaChangerState() *scpb.DescriptorState { + return nil +} // GetDefaultPrivilegeDescriptor returns a DefaultPrivilegeDescriptor. func (p synthetic) GetDefaultPrivilegeDescriptor() catalog.DefaultPrivilegeDescriptor { diff --git a/pkg/sql/catalog/tabledesc/BUILD.bazel b/pkg/sql/catalog/tabledesc/BUILD.bazel index bcbc9b271937..31839211b433 100644 --- a/pkg/sql/catalog/tabledesc/BUILD.bazel +++ b/pkg/sql/catalog/tabledesc/BUILD.bazel @@ -38,6 +38,7 @@ go_library( "//pkg/sql/pgwire/pgerror", "//pkg/sql/privilege", "//pkg/sql/rowenc", + "//pkg/sql/schemachanger/scpb", "//pkg/sql/sem/tree", "//pkg/sql/types", "//pkg/util", diff --git a/pkg/sql/catalog/tabledesc/structured.go b/pkg/sql/catalog/tabledesc/structured.go index a893c1d07dd4..2e1b96448117 100644 --- a/pkg/sql/catalog/tabledesc/structured.go +++ b/pkg/sql/catalog/tabledesc/structured.go @@ -31,6 +31,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/rowenc" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented" @@ -2518,6 +2519,12 @@ func (desc *Mutable) SetTableLocalityGlobal() { desc.LocalityConfig = &lc } +// SetDeclarativeSchemaChangerState is part of the catalog.MutableDescriptor +// interface. +func (desc *Mutable) SetDeclarativeSchemaChangerState(state *scpb.DescriptorState) { + desc.DeclarativeSchemaChangerState = state +} + // LocalityConfigGlobal returns a config for a GLOBAL table. func LocalityConfigGlobal() catpb.LocalityConfig { return catpb.LocalityConfig{ diff --git a/pkg/sql/catalog/tabledesc/validate.go b/pkg/sql/catalog/tabledesc/validate.go index e6e6a91a1966..64ac310ad2cf 100644 --- a/pkg/sql/catalog/tabledesc/validate.go +++ b/pkg/sql/catalog/tabledesc/validate.go @@ -47,7 +47,7 @@ func (desc *wrapper) ValidateTxnCommit( } // Check that the mutation ID values are appropriately set when a declarative // schema change is underway. - if n := len(desc.Mutations); n > 0 && desc.NewSchemaChangeJobID != 0 { + if n := len(desc.Mutations); n > 0 && desc.GetDeclarativeSchemaChangerState() != nil { lastMutationID := desc.Mutations[n-1].MutationID if lastMutationID != desc.NextMutationID { vea.Report(errors.AssertionFailedf( @@ -591,14 +591,14 @@ func (desc *wrapper) ValidateSelf(vea catalog.ValidationErrorAccumulator) { } // Validate that the presence of MutationJobs (from the old schema changer) - // and the presence of a NewSchemaChangeJobID are mutually exclusive. (Note + // and the presence of a DeclarativeSchemaChangeJobID are mutually exclusive. (Note // the jobs themselves can be running simultaneously, since a resumer can // still be running after the schema change is complete from the point of view // of the descriptor, in both the new and old schema change jobs.) - if len(desc.MutationJobs) > 0 && desc.NewSchemaChangeJobID != 0 { + if dscs := desc.DeclarativeSchemaChangerState; dscs != nil && len(desc.MutationJobs) > 0 { vea.Report(errors.AssertionFailedf( "invalid concurrent declarative schema change job %d and legacy schema change jobs %v", - desc.NewSchemaChangeJobID, desc.MutationJobs)) + dscs.JobID, desc.MutationJobs)) } // Check that all expression strings can be parsed. diff --git a/pkg/sql/catalog/tabledesc/validate_test.go b/pkg/sql/catalog/tabledesc/validate_test.go index 76101b539ddd..4c819054792d 100644 --- a/pkg/sql/catalog/tabledesc/validate_test.go +++ b/pkg/sql/catalog/tabledesc/validate_test.go @@ -130,6 +130,7 @@ var validationMap = []struct { "RowLevelTTL": {status: iSolemnlySwearThisFieldIsValidated}, "ExcludeDataFromBackup": {status: thisFieldReferencesNoObjects}, "NextConstraintID": {status: iSolemnlySwearThisFieldIsValidated}, + "DeclarativeSchemaChangerState": {status: iSolemnlySwearThisFieldIsValidated}, }, }, { @@ -236,53 +237,56 @@ var validationMap = []struct { { obj: descpb.TypeDescriptor{}, fieldMap: map[string]validationStatusInfo{ - "Name": {status: iSolemnlySwearThisFieldIsValidated}, - "ID": {status: iSolemnlySwearThisFieldIsValidated}, - "Version": {status: thisFieldReferencesNoObjects}, - "ModificationTime": {status: thisFieldReferencesNoObjects}, - "DrainingNames": {status: thisFieldReferencesNoObjects}, - "ParentID": {status: iSolemnlySwearThisFieldIsValidated}, - "ParentSchemaID": {status: iSolemnlySwearThisFieldIsValidated}, - "Kind": {status: thisFieldReferencesNoObjects}, - "ArrayTypeID": {status: iSolemnlySwearThisFieldIsValidated}, - "EnumMembers": {status: iSolemnlySwearThisFieldIsValidated}, - "Alias": {status: iSolemnlySwearThisFieldIsValidated}, - "State": {status: thisFieldReferencesNoObjects}, - "ReferencingDescriptorIDs": {status: iSolemnlySwearThisFieldIsValidated}, - "Privileges": {status: iSolemnlySwearThisFieldIsValidated}, - "OfflineReason": {status: thisFieldReferencesNoObjects}, - "RegionConfig": {status: iSolemnlySwearThisFieldIsValidated}, + "Name": {status: iSolemnlySwearThisFieldIsValidated}, + "ID": {status: iSolemnlySwearThisFieldIsValidated}, + "Version": {status: thisFieldReferencesNoObjects}, + "ModificationTime": {status: thisFieldReferencesNoObjects}, + "DrainingNames": {status: thisFieldReferencesNoObjects}, + "ParentID": {status: iSolemnlySwearThisFieldIsValidated}, + "ParentSchemaID": {status: iSolemnlySwearThisFieldIsValidated}, + "Kind": {status: thisFieldReferencesNoObjects}, + "ArrayTypeID": {status: iSolemnlySwearThisFieldIsValidated}, + "EnumMembers": {status: iSolemnlySwearThisFieldIsValidated}, + "Alias": {status: iSolemnlySwearThisFieldIsValidated}, + "State": {status: thisFieldReferencesNoObjects}, + "ReferencingDescriptorIDs": {status: iSolemnlySwearThisFieldIsValidated}, + "Privileges": {status: iSolemnlySwearThisFieldIsValidated}, + "OfflineReason": {status: thisFieldReferencesNoObjects}, + "RegionConfig": {status: iSolemnlySwearThisFieldIsValidated}, + "DeclarativeSchemaChangerState": {status: thisFieldReferencesNoObjects}, }, }, { obj: descpb.DatabaseDescriptor{}, fieldMap: map[string]validationStatusInfo{ - "Name": {status: iSolemnlySwearThisFieldIsValidated}, - "ID": {status: iSolemnlySwearThisFieldIsValidated}, - "Version": {status: thisFieldReferencesNoObjects}, - "ModificationTime": {status: thisFieldReferencesNoObjects}, - "DrainingNames": {status: thisFieldReferencesNoObjects}, - "Privileges": {status: iSolemnlySwearThisFieldIsValidated}, - "Schemas": {status: iSolemnlySwearThisFieldIsValidated}, - "State": {status: thisFieldReferencesNoObjects}, - "OfflineReason": {status: thisFieldReferencesNoObjects}, - "RegionConfig": {status: iSolemnlySwearThisFieldIsValidated}, - "DefaultPrivileges": {status: iSolemnlySwearThisFieldIsValidated}, + "Name": {status: iSolemnlySwearThisFieldIsValidated}, + "ID": {status: iSolemnlySwearThisFieldIsValidated}, + "Version": {status: thisFieldReferencesNoObjects}, + "ModificationTime": {status: thisFieldReferencesNoObjects}, + "DrainingNames": {status: thisFieldReferencesNoObjects}, + "Privileges": {status: iSolemnlySwearThisFieldIsValidated}, + "Schemas": {status: iSolemnlySwearThisFieldIsValidated}, + "State": {status: thisFieldReferencesNoObjects}, + "OfflineReason": {status: thisFieldReferencesNoObjects}, + "RegionConfig": {status: iSolemnlySwearThisFieldIsValidated}, + "DefaultPrivileges": {status: iSolemnlySwearThisFieldIsValidated}, + "DeclarativeSchemaChangerState": {status: thisFieldReferencesNoObjects}, }, }, { obj: descpb.SchemaDescriptor{}, fieldMap: map[string]validationStatusInfo{ - "Name": {status: iSolemnlySwearThisFieldIsValidated}, - "ID": {status: iSolemnlySwearThisFieldIsValidated}, - "State": {status: thisFieldReferencesNoObjects}, - "OfflineReason": {status: thisFieldReferencesNoObjects}, - "ModificationTime": {status: thisFieldReferencesNoObjects}, - "Version": {status: thisFieldReferencesNoObjects}, - "DrainingNames": {status: thisFieldReferencesNoObjects}, - "ParentID": {status: iSolemnlySwearThisFieldIsValidated}, - "Privileges": {status: iSolemnlySwearThisFieldIsValidated}, - "DefaultPrivileges": {status: iSolemnlySwearThisFieldIsValidated}, + "Name": {status: iSolemnlySwearThisFieldIsValidated}, + "ID": {status: iSolemnlySwearThisFieldIsValidated}, + "State": {status: thisFieldReferencesNoObjects}, + "OfflineReason": {status: thisFieldReferencesNoObjects}, + "ModificationTime": {status: thisFieldReferencesNoObjects}, + "Version": {status: thisFieldReferencesNoObjects}, + "DrainingNames": {status: thisFieldReferencesNoObjects}, + "ParentID": {status: iSolemnlySwearThisFieldIsValidated}, + "Privileges": {status: iSolemnlySwearThisFieldIsValidated}, + "DefaultPrivileges": {status: iSolemnlySwearThisFieldIsValidated}, + "DeclarativeSchemaChangerState": {status: thisFieldReferencesNoObjects}, }, }, } diff --git a/pkg/sql/catalog/typedesc/BUILD.bazel b/pkg/sql/catalog/typedesc/BUILD.bazel index be44469fef9e..a6859e48cfd4 100644 --- a/pkg/sql/catalog/typedesc/BUILD.bazel +++ b/pkg/sql/catalog/typedesc/BUILD.bazel @@ -21,6 +21,7 @@ go_library( "//pkg/sql/pgwire/pgcode", "//pkg/sql/pgwire/pgerror", "//pkg/sql/privilege", + "//pkg/sql/schemachanger/scpb", "//pkg/sql/sem/tree", "//pkg/sql/types", "//pkg/util/hlc", diff --git a/pkg/sql/catalog/typedesc/table_implicit_record_type.go b/pkg/sql/catalog/typedesc/table_implicit_record_type.go index 7df267a05402..af7aadc73edf 100644 --- a/pkg/sql/catalog/typedesc/table_implicit_record_type.go +++ b/pkg/sql/catalog/typedesc/table_implicit_record_type.go @@ -17,6 +17,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/privilege" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/hlc" @@ -330,3 +331,9 @@ func (v TableImplicitRecordType) GetReferencingDescriptorID(_ int) descpb.ID { r func (v TableImplicitRecordType) panicNotSupported(message string) { panic(errors.AssertionFailedf("implicit table record type for table %q: not supported: %s", v.GetName(), message)) } + +// GetDeclarativeSchemaChangerState implements the Descriptor interface. +func (v TableImplicitRecordType) GetDeclarativeSchemaChangerState() *scpb.DescriptorState { + v.panicNotSupported("GetDeclarativeSchemaChangeState") + return nil +} diff --git a/pkg/sql/catalog/typedesc/type_desc.go b/pkg/sql/catalog/typedesc/type_desc.go index 260fb9715aa4..6c8d0459f044 100644 --- a/pkg/sql/catalog/typedesc/type_desc.go +++ b/pkg/sql/catalog/typedesc/type_desc.go @@ -28,6 +28,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" "github.com/cockroachdb/cockroach/pkg/sql/privilege" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/sem/tree" "github.com/cockroachdb/cockroach/pkg/sql/types" "github.com/cockroachdb/cockroach/pkg/util/hlc" @@ -997,3 +998,9 @@ func GetTypeDescriptorClosure(typ *types.T) (map[descpb.ID]struct{}, error) { func (desc *Mutable) HasPostDeserializationChanges() bool { return desc.changed } + +// SetDeclarativeSchemaChangerState is part of the catalog.MutableDescriptor +// interface. +func (desc *Mutable) SetDeclarativeSchemaChangerState(state *scpb.DescriptorState) { + desc.DeclarativeSchemaChangerState = state +} diff --git a/pkg/sql/drop_table.go b/pkg/sql/drop_table.go index 17ccc156105c..7c933bc4738c 100644 --- a/pkg/sql/drop_table.go +++ b/pkg/sql/drop_table.go @@ -383,7 +383,7 @@ func (p *planner) initiateDropTable( // Exit early with an error if the table is undergoing a declarative schema // change, before we try to get job IDs and update job statuses later. See // createOrUpdateSchemaChangeJob. - if tableDesc.NewSchemaChangeJobID != 0 { + if tableDesc.GetDeclarativeSchemaChangerState() != nil { return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState, "cannot perform a schema change on table %q while it is undergoing a declarative schema change", tableDesc.GetName(), diff --git a/pkg/sql/opt/exec/execbuilder/testdata/show_trace_nonmetamorphic b/pkg/sql/opt/exec/execbuilder/testdata/show_trace_nonmetamorphic index 17b128e25f85..ba23eceb958c 100644 --- a/pkg/sql/opt/exec/execbuilder/testdata/show_trace_nonmetamorphic +++ b/pkg/sql/opt/exec/execbuilder/testdata/show_trace_nonmetamorphic @@ -60,7 +60,7 @@ query TT $trace_query ---- batch flow coordinator CPut /NamespaceTable/30/1/106/107/"kv"/4/1 -> 108 -batch flow coordinator CPut /Table/3/1/108/2/1 -> table: parent_id:106 unexposed_parent_schema_id:107 columns: nullable:false hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:3 families: next_family_id:1 primary_index: interleave:<> partitioning: type:FORWARD created_explicitly:false encoding_type:1 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 > next_index_id:2 privileges: users: users: owner_proto:"root" version:2 > next_mutation_id:1 format_version:3 state:PUBLIC offline_reason:"" view_query:"" is_materialized_view:false new_schema_change_job_id:0 drop_time:0 replacement_of: > audit_mode:DISABLED drop_job_id:0 create_query:"" create_as_of_time:<> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:2 > +batch flow coordinator CPut /Table/3/1/108/2/1 -> table: parent_id:106 unexposed_parent_schema_id:107 columns: nullable:false hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:3 families: next_family_id:1 primary_index: interleave:<> partitioning: type:FORWARD created_explicitly:false encoding_type:1 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 > next_index_id:2 privileges: users: users: owner_proto:"root" version:2 > next_mutation_id:1 format_version:3 state:PUBLIC offline_reason:"" view_query:"" is_materialized_view:false drop_time:0 replacement_of: > audit_mode:DISABLED drop_job_id:0 create_query:"" create_as_of_time:<> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:2 > sql query rows affected: 0 # We avoid using the full trace output, because that would make the @@ -75,7 +75,7 @@ SET tracing = on,kv,results; CREATE UNIQUE INDEX woo ON t.kv(v); SET tracing = o query TT $trace_query ---- -batch flow coordinator Put /Table/3/1/108/2/1 -> table: parent_id:106 unexposed_parent_schema_id:107 columns: nullable:false hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:3 families: next_family_id:1 primary_index: interleave:<> partitioning: type:FORWARD created_explicitly:false encoding_type:1 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 > next_index_id:3 privileges: users: users: owner_proto:"root" version:2 > mutations: interleave:<> partitioning: type:FORWARD created_explicitly:true encoding_type:0 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:2 > state:DELETE_ONLY direction:ADD mutation_id:1 rollback:false > next_mutation_id:2 format_version:3 state:PUBLIC offline_reason:"" view_query:"" is_materialized_view:false mutationJobs:<...> new_schema_change_job_id:0 drop_time:0 replacement_of: > audit_mode:DISABLED drop_job_id:0 create_query:"" create_as_of_time:<...> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:3 > +batch flow coordinator Put /Table/3/1/108/2/1 -> table: parent_id:106 unexposed_parent_schema_id:107 columns: nullable:false hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:3 families: next_family_id:1 primary_index: interleave:<> partitioning: type:FORWARD created_explicitly:false encoding_type:1 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 > next_index_id:3 privileges: users: users: owner_proto:"root" version:2 > mutations: interleave:<> partitioning: type:FORWARD created_explicitly:true encoding_type:0 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:2 > state:DELETE_ONLY direction:ADD mutation_id:1 rollback:false > next_mutation_id:2 format_version:3 state:PUBLIC offline_reason:"" view_query:"" is_materialized_view:false mutationJobs:<...> drop_time:0 replacement_of: > audit_mode:DISABLED drop_job_id:0 create_query:"" create_as_of_time:<...> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:3 > sql query rows affected: 0 statement ok @@ -119,7 +119,7 @@ query TT $trace_query ---- batch flow coordinator CPut /NamespaceTable/30/1/106/107/"kv2"/4/1 -> 109 -batch flow coordinator CPut /Table/3/1/109/2/1 -> table: parent_id:106 unexposed_parent_schema_id:107 columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:false default_expr:"unique_rowid()" hidden:true inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:4 families: next_family_id:1 primary_index: interleave:<> partitioning: type:FORWARD created_explicitly:false encoding_type:1 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 > next_index_id:2 privileges: users: users: owner_proto:"root" version:2 > next_mutation_id:1 format_version:3 state:ADD offline_reason:"" view_query:"" is_materialized_view:false new_schema_change_job_id:0 drop_time:0 replacement_of: > audit_mode:DISABLED drop_job_id:0 create_query:"TABLE t.public.kv" create_as_of_time:<> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:2 > +batch flow coordinator CPut /Table/3/1/109/2/1 -> table: parent_id:106 unexposed_parent_schema_id:107 columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:false default_expr:"unique_rowid()" hidden:true inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:4 families: next_family_id:1 primary_index: interleave:<> partitioning: type:FORWARD created_explicitly:false encoding_type:1 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 > next_index_id:2 privileges: users: users: owner_proto:"root" version:2 > next_mutation_id:1 format_version:3 state:ADD offline_reason:"" view_query:"" is_materialized_view:false drop_time:0 replacement_of: > audit_mode:DISABLED drop_job_id:0 create_query:"TABLE t.public.kv" create_as_of_time:<> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:2 > sql query rows affected: 0 statement ok @@ -151,7 +151,7 @@ query TT $trace_query ---- batch flow coordinator Del /NamespaceTable/30/1/106/107/"kv2"/4/1 -batch flow coordinator Put /Table/3/1/109/2/1 -> table: parent_id:106 unexposed_parent_schema_id:107 columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:false default_expr:"unique_rowid()" hidden:true inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:4 families: next_family_id:1 primary_index: interleave:<> partitioning: type:FORWARD created_explicitly:false encoding_type:1 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 > next_index_id:2 privileges: users: users: owner_proto:"root" version:2 > next_mutation_id:1 format_version:3 state:DROP offline_reason:"" view_query:"" is_materialized_view:false new_schema_change_job_id:0 drop_time:... replacement_of: > audit_mode:DISABLED drop_job_id:0 create_query:"TABLE t.public.kv" create_as_of_time:<...> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:2 > +batch flow coordinator Put /Table/3/1/109/2/1 -> table: parent_id:106 unexposed_parent_schema_id:107 columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:false default_expr:"unique_rowid()" hidden:true inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:4 families: next_family_id:1 primary_index: interleave:<> partitioning: type:FORWARD created_explicitly:false encoding_type:1 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 > next_index_id:2 privileges: users: users: owner_proto:"root" version:2 > next_mutation_id:1 format_version:3 state:DROP offline_reason:"" view_query:"" is_materialized_view:false drop_time:... replacement_of: > audit_mode:DISABLED drop_job_id:0 create_query:"TABLE t.public.kv" create_as_of_time:<...> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:2 > sql query rows affected: 0 statement ok @@ -173,7 +173,7 @@ SET tracing = on,kv,results; DROP INDEX t.kv@woo CASCADE; SET tracing = off query TT $trace_query ---- -batch flow coordinator Put /Table/3/1/108/2/1 -> table: parent_id:106 unexposed_parent_schema_id:107 columns: nullable:false hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:3 families: next_family_id:1 primary_index: interleave:<> partitioning: type:FORWARD created_explicitly:false encoding_type:1 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 > next_index_id:3 privileges: users: users: owner_proto:"root" version:2 > mutations: interleave:<> partitioning: type:FORWARD created_explicitly:true encoding_type:0 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:2 > state:DELETE_AND_WRITE_ONLY direction:DROP mutation_id:2 rollback:false > next_mutation_id:3 format_version:3 state:PUBLIC offline_reason:"" view_query:"" is_materialized_view:false mutationJobs:<...> new_schema_change_job_id:0 drop_time:0 replacement_of: > audit_mode:DISABLED drop_job_id:0 create_query:"" create_as_of_time:<...> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:3 > +batch flow coordinator Put /Table/3/1/108/2/1 -> table: parent_id:106 unexposed_parent_schema_id:107 columns: nullable:false hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:3 families: next_family_id:1 primary_index: interleave:<> partitioning: type:FORWARD created_explicitly:false encoding_type:1 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 > next_index_id:3 privileges: users: users: owner_proto:"root" version:2 > mutations: interleave:<> partitioning: type:FORWARD created_explicitly:true encoding_type:0 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:2 > state:DELETE_AND_WRITE_ONLY direction:DROP mutation_id:2 rollback:false > next_mutation_id:3 format_version:3 state:PUBLIC offline_reason:"" view_query:"" is_materialized_view:false mutationJobs:<...> drop_time:0 replacement_of: > audit_mode:DISABLED drop_job_id:0 create_query:"" create_as_of_time:<...> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:3 > sql query rows affected: 0 statement ok @@ -183,7 +183,7 @@ query TT $trace_query ---- batch flow coordinator Del /NamespaceTable/30/1/106/107/"kv"/4/1 -batch flow coordinator Put /Table/3/1/108/2/1 -> table: parent_id:106 unexposed_parent_schema_id:107 columns: nullable:false hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:3 families: next_family_id:1 primary_index: interleave:<> partitioning: type:FORWARD created_explicitly:false encoding_type:1 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 > next_index_id:3 privileges: users: users: owner_proto:"root" version:2 > next_mutation_id:3 format_version:3 state:DROP offline_reason:"" view_query:"" is_materialized_view:false new_schema_change_job_id:0 drop_time:... replacement_of: > audit_mode:DISABLED drop_job_id:0 create_query:"" create_as_of_time:<...> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:3 > +batch flow coordinator Put /Table/3/1/108/2/1 -> table: parent_id:106 unexposed_parent_schema_id:107 columns: nullable:false hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns: nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:3 families: next_family_id:1 primary_index: interleave:<> partitioning: type:FORWARD created_explicitly:false encoding_type:1 sharded: disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 > next_index_id:3 privileges: users: users: owner_proto:"root" version:2 > next_mutation_id:3 format_version:3 state:DROP offline_reason:"" view_query:"" is_materialized_view:false drop_time:... replacement_of: > audit_mode:DISABLED drop_job_id:0 create_query:"" create_as_of_time:<...> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:3 > sql query rows affected: 0 # Check that session tracing does not inhibit the fast path for inserts & diff --git a/pkg/sql/schemachanger/scdeps/exec_deps.go b/pkg/sql/schemachanger/scdeps/exec_deps.go index 067404b9f4b9..5cf75a713095 100644 --- a/pkg/sql/schemachanger/scdeps/exec_deps.go +++ b/pkg/sql/schemachanger/scdeps/exec_deps.go @@ -240,6 +240,7 @@ func (b *catalogChangeBatcher) DeleteName( func (b *catalogChangeBatcher) DeleteDescriptor(ctx context.Context, id descpb.ID) error { b.batch.Del(catalogkeys.MakeDescMetadataKey(b.codec, id)) b.deletedDescriptors.Add(id) + b.descsCollection.AddDeletedDescriptor(id) return nil } diff --git a/pkg/sql/schemachanger/scexec/exec_mutation.go b/pkg/sql/schemachanger/scexec/exec_mutation.go index 8381de829972..4e5ec791fb42 100644 --- a/pkg/sql/schemachanger/scexec/exec_mutation.go +++ b/pkg/sql/schemachanger/scexec/exec_mutation.go @@ -41,6 +41,9 @@ func executeDescriptorMutationOps(ctx context.Context, deps Dependencies, ops [] } } b := deps.Catalog().NewCatalogChangeBatcher() + mvs.descriptorsToDelete.ForEach(func(id descpb.ID) { + mvs.checkedOutDescriptors.Remove(id) + }) err := mvs.checkedOutDescriptors.IterateByID(func(entry catalog.NameEntry) error { return b.CreateOrUpdateDescriptor(ctx, entry.(catalog.MutableDescriptor)) }) diff --git a/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go b/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go index 4a7839f2f26f..ff9b5881e1bb 100644 --- a/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go +++ b/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go @@ -153,44 +153,49 @@ func (m *visitor) AddJobReference(ctx context.Context, reference scop.AddJobRefe func (m *visitor) swapSchemaChangeJobID( ctx context.Context, descID descpb.ID, exp, to jobspb.JobID, ) error { - // TODO(ajwerner): Support all of the descriptor types. We need to write this - // to avoid concurrency. - d, err := m.cr.MustReadImmutableDescriptor(ctx, descID) - - // If we're clearing the status, we might have already deleted the - // descriptor. Permit that by detecting the prior deletion and - // short-circuiting. - // - // TODO(ajwerner): Ideally we'd model the clearing of the job dependency as - // an operation which has to happen before deleting the descriptor. If that - // were the case, this error would become unexpected. - if errors.Is(err, catalog.ErrDescriptorNotFound) && to == 0 { - return nil - } - if err != nil { - return err - } - // Short-circuit writing an update if this isn't a table because we'd have - // no field to touch. - if _, isTable := d.(catalog.TableDescriptor); !isTable { - return nil + { + _, err := m.cr.MustReadImmutableDescriptor(ctx, descID) + + // If we're clearing the status, we might have already deleted the + // descriptor. Permit that by detecting the prior deletion and + // short-circuiting. + // + // TODO(ajwerner): Ideally we'd model the clearing of the job dependency as + // an operation which has to happen before deleting the descriptor. If that + // were the case, this error would become unexpected. + if errors.Is(err, catalog.ErrDescriptorNotFound) && to == 0 { + return nil + } + if err != nil { + return err + } } - tbl, err := m.s.CheckOutDescriptor(ctx, descID) + mut, err := m.s.CheckOutDescriptor(ctx, descID) if err != nil { return err } - mut, ok := tbl.(*tabledesc.Mutable) - if !ok { - return nil - } - if mut.NewSchemaChangeJobID != exp { + // TODO(ajwerner): We could leverage this spot to determine whether + // there's an outstanding concurrent schema change and bubble up the + // relevant error. It's probably better to detect the conflict at + // build time when we resolve the descriptor or perhaps immediately + // after building by taking a peek at the implied descriptor set so + // that this can remain an assertion. + scs := mut.GetDeclarativeSchemaChangerState() + if scs != nil && scs.JobID != exp || scs == nil && exp != 0 { return errors.AssertionFailedf( "unexpected schema change job ID %d on table %d, expected %d", - mut.NewSchemaChangeJobID, descID, exp, + scs.JobID, descID, exp, ) } - mut.NewSchemaChangeJobID = to + if to == 0 { + scs = nil + } else if scs == nil { + scs = &scpb.DescriptorState{JobID: to} + } else { + scs.JobID = to + } + mut.SetDeclarativeSchemaChangerState(scs) return nil } diff --git a/pkg/sql/schemachanger/scpb/scpb.proto b/pkg/sql/schemachanger/scpb/scpb.proto index 9f72eec165d8..0ba8a680ab87 100644 --- a/pkg/sql/schemachanger/scpb/scpb.proto +++ b/pkg/sql/schemachanger/scpb/scpb.proto @@ -15,8 +15,9 @@ option go_package = "scpb"; import "sql/schemachanger/scpb/elements.proto"; import "gogoproto/gogo.proto"; +option (gogoproto.equal_all) = true; + message Target { - option (gogoproto.equal) = true; ElementProto element_proto = 1 [(gogoproto.embed) = true, (gogoproto.nullable) = false]; TargetMetadata metadata = 2 [(gogoproto.nullable) = false]; Status target_status = 3; @@ -52,6 +53,10 @@ message TargetMetadata { uint32 source_element_id = 2 [(gogoproto.customname) = "SourceElementID", (gogoproto.casttype) = "SourceElementID"]; // StatementID refers to the statement that produced this element, where // the ID indexes into the State structure. + // + // TODO(ajwerner): Potentially multiple statements in a transaction affect + // the same target. We'll need to retain a set of ids to deal with that case. + // Fortunately making a field repeated is backwards compatible. uint32 statement_id = 3 [(gogoproto.customname) = "StatementID"]; } @@ -71,3 +76,48 @@ message Authorization { string user_name = 1; string app_name = 2; } + +// DescriptorState contains the portion of a schema change state +// corresponding to an individual descriptor. The combination of +// these messages for all descriptors involved in a schema change produces the +// current state of the entire schema change. +message DescriptorState { + + // JobID is the ID of the job responsible for this DescriptorState. + int64 job_id = 5 [ + (gogoproto.customname) = "JobID", + (gogoproto.casttype) = "github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb.JobID"]; + + // Targets is the set of targets in the schema change belonging to this + // descriptor. + repeated Target targets = 1 [(gogoproto.nullable) = false]; + + // CurrentStatuses is parallel to Targets and stores the current status for + // those targets. + repeated Status current_statuses = 4; + + // TargetRanks is parallel to Targets and stores the rank of the target in + // the complete schema change. These ranks are used to deterministically + // reconstruct the complete TargetState from a collection of DescriptorState + // messages. + repeated uint32 target_ranks = 6; + + // Statement is a statement which is associated with one or more targets + // in the current DescriptorState. + message Statement { + + // Statement is a statement that is relevant to one or more targets + // in this descriptor. + scpb.Statement statement = 1 [(gogoproto.nullable) = false]; + + // StatementRank is the rank of the statement in the transaction. + uint32 statement_rank = 2; + } + + // RelevantStatements is the set of statements which are relevant to any of + // the targets. It is sorted internally by StatementRank. + repeated Statement relevant_statements = 2 [(gogoproto.nullable) = false]; + + // Authorization is information about the creator of the schema change. + Authorization authorization = 3 [(gogoproto.nullable) = false]; +} diff --git a/pkg/sql/schemachanger/scpb/state.go b/pkg/sql/schemachanger/scpb/state.go index aa434d2a061d..095f65452c60 100644 --- a/pkg/sql/schemachanger/scpb/state.go +++ b/pkg/sql/schemachanger/scpb/state.go @@ -70,3 +70,11 @@ func MakeTarget(status Status, elem Element, metadata *TargetMetadata) Target { // This ID is dynamically allocated when any parent element is // created and has no relation to the descriptor ID. type SourceElementID uint32 + +// Clone will make a deep copy of the DescriptorState. +func (m *DescriptorState) Clone() *DescriptorState { + if m == nil { + return nil + } + return protoutil.Clone(m).(*DescriptorState) +} diff --git a/pkg/sql/schemachanger/testdata/alter_table_add_column b/pkg/sql/schemachanger/testdata/alter_table_add_column index 5481e66e5420..59ada409de77 100644 --- a/pkg/sql/schemachanger/testdata/alter_table_add_column +++ b/pkg/sql/schemachanger/testdata/alter_table_add_column @@ -21,6 +21,12 @@ create job #1: "schema change job" write *eventpb.AlterTable to event log for descriptor #106: ALTER TABLE ‹db›.‹public›.‹tbl› ADD COLUMN ‹j› INT8 NOT NULL DEFAULT ‹42› upsert descriptor #106 ... + createAsOfTime: + wallTime: "1" + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + families: - columnIds: - 1 + - 2 @@ -73,7 +79,6 @@ upsert descriptor #106 name: tbl - nextColumnId: 2 - nextConstraintId: 2 - + newSchemaChangeJobId: "1" + nextColumnId: 3 + nextConstraintId: 3 nextFamilyId: 1 @@ -106,7 +111,7 @@ upsert descriptor #106 - state: DELETE_ONLY + state: DELETE_AND_WRITE_ONLY name: tbl - newSchemaChangeJobId: "1" + nextColumnId: 3 ... time: {} unexposedParentSchemaId: 105 @@ -227,7 +232,7 @@ upsert descriptor #106 - state: DELETE_AND_WRITE_ONLY + state: DELETE_ONLY name: tbl - newSchemaChangeJobId: "1" + nextColumnId: 3 ... time: {} unexposedParentSchemaId: 105 @@ -240,6 +245,14 @@ create job #2: "GC for dropping table 106 index 1" descriptor IDs: [106] update progress of schema change job #1 upsert descriptor #106 + ... + createAsOfTime: + wallTime: "1" + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + families: + - columnIds: ... id: 106 modificationTime: {} @@ -268,9 +281,7 @@ upsert descriptor #106 - state: DELETE_ONLY + mutations: [] name: tbl - - newSchemaChangeJobId: "1" nextColumnId: 3 - nextConstraintId: 3 ... time: {} unexposedParentSchemaId: 105 diff --git a/pkg/sql/schemachanger/testdata/drop b/pkg/sql/schemachanger/testdata/drop index 3f4b2f0d2050..e6cd5e7b3f5c 100644 --- a/pkg/sql/schemachanger/testdata/drop +++ b/pkg/sql/schemachanger/testdata/drop @@ -21,6 +21,12 @@ delete comment for descriptor #106 of type SchemaCommentType create job #1: "schema change job" descriptor IDs: [104 106] upsert descriptor #104 + database: + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + id: 104 + modificationTime: {} ... public: id: 105 @@ -29,6 +35,12 @@ upsert descriptor #104 - version: "2" + version: "3" upsert descriptor #106 + schema: + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + id: 106 + modificationTime: {} ... withGrantOption: 2 version: 2 @@ -44,6 +56,18 @@ write *eventpb.DropSchema to event log for descriptor #106: DROP SCHEMA ‹db› update progress of schema change job #1 set schema change job #1 to non-cancellable delete schema namespace entry {104 0 sc} -> 106 +upsert descriptor #104 + database: + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + id: 104 + modificationTime: {} + ... + public: + id: 105 + - version: "3" + + version: "4" delete descriptor #106 commit transaction #2 # end PostCommitPhase @@ -79,11 +103,13 @@ create job #1: "schema change job" delete object namespace entry {104 107 t} -> 108 upsert descriptor #108 ... - modificationTime: {} - name: t - + newSchemaChangeJobId: "1" - nextColumnId: 4 - nextConstraintId: 2 + createAsOfTime: + wallTime: "1" + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + families: + - columnIds: ... replacementOf: time: {} @@ -103,11 +129,13 @@ update progress of schema change job #1 set schema change job #1 to non-cancellable upsert descriptor #108 ... - modificationTime: {} - name: t - - newSchemaChangeJobId: "1" - nextColumnId: 4 - nextConstraintId: 2 + createAsOfTime: + wallTime: "1" + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + families: + - columnIds: ... state: DROP unexposedParentSchemaId: 107 @@ -132,6 +160,12 @@ create job #1: "schema change job" delete object namespace entry {104 107 e} -> 109 delete object namespace entry {104 107 _e} -> 110 upsert descriptor #104 + database: + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + id: 104 + modificationTime: {} ... public: id: 105 @@ -140,6 +174,12 @@ upsert descriptor #104 - version: "4" + version: "5" upsert descriptor #107 + schema: + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + id: 107 + modificationTime: {} ... withGrantOption: 2 version: 2 @@ -147,6 +187,13 @@ upsert descriptor #107 + state: DROP + version: "2" upsert descriptor #109 + type: + arrayTypeId: 110 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + enumMembers: + - logicalRepresentation: a ... withGrantOption: 2 version: 2 @@ -154,6 +201,14 @@ upsert descriptor #109 + state: DROP + version: "2" upsert descriptor #110 + ... + family: ArrayFamily + oid: 100110 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + id: 110 + kind: ALIAS ... withGrantOption: 2 version: 2 @@ -170,6 +225,18 @@ write *eventpb.DropType to event log for descriptor #110: DROP SCHEMA ‹db›. update progress of schema change job #1 set schema change job #1 to non-cancellable delete schema namespace entry {104 0 sc} -> 107 +upsert descriptor #104 + database: + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + id: 104 + modificationTime: {} + ... + public: + id: 105 + - version: "5" + + version: "6" delete descriptor #107 delete descriptor #109 delete descriptor #110 @@ -192,6 +259,12 @@ delete role settings for database on #104 create job #1: "schema change job" descriptor IDs: [104 105] upsert descriptor #104 + database: + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + id: 104 + modificationTime: {} ... withGrantOption: 2 version: 2 @@ -203,6 +276,12 @@ upsert descriptor #104 + state: DROP + version: "6" upsert descriptor #105 + schema: + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + id: 105 + modificationTime: {} ... withGrantOption: 2 version: 2 @@ -222,6 +301,12 @@ set schema change job #1 to non-cancellable delete database namespace entry {0 0 db} -> 104 delete schema namespace entry {104 0 public} -> 105 upsert descriptor #104 + database: + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + id: 104 + modificationTime: {} ... version: 2 state: DROP @@ -308,6 +393,12 @@ delete object namespace entry {111 113 typ} -> 122 delete object namespace entry {111 113 _typ} -> 123 delete object namespace entry {111 113 v5} -> 124 upsert descriptor #111 + database: + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + id: 111 + modificationTime: {} ... withGrantOption: 2 version: 2 @@ -321,6 +412,12 @@ upsert descriptor #111 + state: DROP + version: "3" upsert descriptor #112 + schema: + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + id: 112 + modificationTime: {} ... withGrantOption: 2 version: 2 @@ -328,6 +425,12 @@ upsert descriptor #112 + state: DROP + version: "2" upsert descriptor #113 + schema: + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + id: 113 + modificationTime: {} ... withGrantOption: 2 version: 2 @@ -343,14 +446,11 @@ upsert descriptor #114 - columnIds: - - 3 - id: 117 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" families: - columnIds: - ... - modificationTime: {} - name: sq1 - + newSchemaChangeJobId: "1" - parentId: 111 - primaryIndex: ... sequenceOwner: {} start: "1" @@ -367,14 +467,11 @@ upsert descriptor #115 - columnIds: - - 3 - id: 116 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" families: - columnIds: - ... - modificationTime: {} - name: sq1 - + newSchemaChangeJobId: "1" - parentId: 111 - primaryIndex: ... sequenceOwner: {} start: "1" @@ -402,14 +499,11 @@ upsert descriptor #116 - - columnIds: - - 2 - id: 118 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" families: - columnIds: - ... - modificationTime: {} - name: t1 - + newSchemaChangeJobId: "1" - nextColumnId: 4 - nextConstraintId: 2 ... replacementOf: time: {} @@ -433,12 +527,11 @@ upsert descriptor #117 - - 114 createAsOfTime: wallTime: "1" - ... - modificationTime: {} - name: t1 - + newSchemaChangeJobId: "1" - nextColumnId: 4 - nextConstraintId: 2 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + families: + - columnIds: ... replacementOf: time: {} @@ -459,13 +552,11 @@ upsert descriptor #118 - id: 120 - dependsOn: - - 116 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" formatVersion: 3 id: 118 - modificationTime: {} - name: v1 - + newSchemaChangeJobId: "1" - nextColumnId: 2 - nextConstraintId: 1 ... replacementOf: time: {} @@ -489,13 +580,11 @@ upsert descriptor #119 - id: 121 - dependsOn: - - 118 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" formatVersion: 3 id: 119 - modificationTime: {} - name: v2 - + newSchemaChangeJobId: "1" - nextColumnId: 3 - nextConstraintId: 1 ... replacementOf: time: {} @@ -511,13 +600,11 @@ upsert descriptor #120 - dependsOn: - - 118 - - 119 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" formatVersion: 3 id: 120 - modificationTime: {} - name: v3 - + newSchemaChangeJobId: "1" - nextColumnId: 3 - nextConstraintId: 1 ... replacementOf: time: {} @@ -537,13 +624,11 @@ upsert descriptor #121 - id: 124 - dependsOn: - - 119 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" formatVersion: 3 id: 121 - modificationTime: {} - name: v4 - + newSchemaChangeJobId: "1" - nextColumnId: 3 - nextConstraintId: 1 ... replacementOf: time: {} @@ -553,6 +638,13 @@ upsert descriptor #121 + version: "3" viewQuery: (SELECT n2, n1 FROM db1.sc1.v2) upsert descriptor #122 + type: + arrayTypeId: 123 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + enumMembers: + - logicalRepresentation: a ... withGrantOption: 2 version: 2 @@ -563,6 +655,14 @@ upsert descriptor #122 + state: DROP + version: "3" upsert descriptor #123 + ... + family: ArrayFamily + oid: 100123 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + id: 123 + kind: ALIAS ... withGrantOption: 2 version: 2 @@ -578,14 +678,11 @@ upsert descriptor #124 wallTime: "1" - dependsOn: - - 121 + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" dependsOnTypes: - 122 - ... - modificationTime: {} - name: v5 - + newSchemaChangeJobId: "1" - nextColumnId: 4 - nextConstraintId: 1 ... replacementOf: time: {} @@ -609,6 +706,12 @@ delete database namespace entry {0 0 db1} -> 111 delete schema namespace entry {111 0 public} -> 112 delete schema namespace entry {111 0 sc1} -> 113 upsert descriptor #111 + database: + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + id: 111 + modificationTime: {} ... version: 2 state: DROP @@ -616,11 +719,13 @@ upsert descriptor #111 + version: "4" upsert descriptor #114 ... - modificationTime: {} - name: sq1 - - newSchemaChangeJobId: "1" - parentId: 111 - primaryIndex: + createAsOfTime: + wallTime: "1" + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + families: + - columnIds: ... state: DROP unexposedParentSchemaId: 112 @@ -628,11 +733,13 @@ upsert descriptor #114 + version: "4" upsert descriptor #115 ... - modificationTime: {} - name: sq1 - - newSchemaChangeJobId: "1" - parentId: 111 - primaryIndex: + createAsOfTime: + wallTime: "1" + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + families: + - columnIds: ... state: DROP unexposedParentSchemaId: 113 @@ -640,11 +747,13 @@ upsert descriptor #115 + version: "4" upsert descriptor #116 ... - modificationTime: {} - name: t1 - - newSchemaChangeJobId: "1" - nextColumnId: 4 - nextConstraintId: 2 + createAsOfTime: + wallTime: "1" + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + families: + - columnIds: ... state: DROP unexposedParentSchemaId: 113 @@ -652,11 +761,13 @@ upsert descriptor #116 + version: "4" upsert descriptor #117 ... - modificationTime: {} - name: t1 - - newSchemaChangeJobId: "1" - nextColumnId: 4 - nextConstraintId: 2 + createAsOfTime: + wallTime: "1" + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + families: + - columnIds: ... state: DROP unexposedParentSchemaId: 112 @@ -664,11 +775,13 @@ upsert descriptor #117 + version: "3" upsert descriptor #118 ... - modificationTime: {} - name: v1 - - newSchemaChangeJobId: "1" - nextColumnId: 2 - nextConstraintId: 1 + createAsOfTime: + wallTime: "1" + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + formatVersion: 3 + id: 118 ... state: DROP unexposedParentSchemaId: 113 @@ -677,11 +790,13 @@ upsert descriptor #118 viewQuery: (SELECT name FROM db1.sc1.t1) upsert descriptor #119 ... - modificationTime: {} - name: v2 - - newSchemaChangeJobId: "1" - nextColumnId: 3 - nextConstraintId: 1 + createAsOfTime: + wallTime: "1" + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + formatVersion: 3 + id: 119 ... state: DROP unexposedParentSchemaId: 113 @@ -690,11 +805,13 @@ upsert descriptor #119 viewQuery: (SELECT name AS n1, name AS n2 FROM db1.sc1.v1) upsert descriptor #120 ... - modificationTime: {} - name: v3 - - newSchemaChangeJobId: "1" - nextColumnId: 3 - nextConstraintId: 1 + createAsOfTime: + wallTime: "1" + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + formatVersion: 3 + id: 120 ... state: DROP unexposedParentSchemaId: 113 @@ -703,11 +820,13 @@ upsert descriptor #120 viewQuery: (SELECT name, n1 FROM db1.sc1.v1, db1.sc1.v2) upsert descriptor #121 ... - modificationTime: {} - name: v4 - - newSchemaChangeJobId: "1" - nextColumnId: 3 - nextConstraintId: 1 + createAsOfTime: + wallTime: "1" + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + formatVersion: 3 + id: 121 ... state: DROP unexposedParentSchemaId: 113 @@ -716,11 +835,13 @@ upsert descriptor #121 viewQuery: (SELECT n2, n1 FROM db1.sc1.v2) upsert descriptor #124 ... - modificationTime: {} - name: v5 - - newSchemaChangeJobId: "1" - nextColumnId: 4 - nextConstraintId: 1 + createAsOfTime: + wallTime: "1" + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + dependsOnTypes: + - 122 ... state: DROP unexposedParentSchemaId: 113 diff --git a/pkg/sql/schemachanger/testdata/index b/pkg/sql/schemachanger/testdata/index index 08935b6ba8b6..4c51bcd89586 100644 --- a/pkg/sql/schemachanger/testdata/index +++ b/pkg/sql/schemachanger/testdata/index @@ -16,6 +16,14 @@ begin transaction #1 create job #1: "schema change job" descriptor IDs: [104] upsert descriptor #104 + ... + createAsOfTime: + wallTime: "1" + + declarativeSchemaChangerState: + + authorization: {} + + jobId: "1" + families: + - columnIds: ... id: 104 modificationTime: {} @@ -45,7 +53,6 @@ upsert descriptor #104 + mutationId: 1 + state: DELETE_ONLY name: t - + newSchemaChangeJobId: "1" nextColumnId: 3 - nextConstraintId: 2 + nextConstraintId: 3 @@ -72,7 +79,7 @@ upsert descriptor #104 - state: DELETE_ONLY + state: DELETE_AND_WRITE_ONLY name: t - newSchemaChangeJobId: "1" + nextColumnId: 3 ... time: {} unexposedParentSchemaId: 101 @@ -91,6 +98,14 @@ begin transaction #5 ## PostCommitPhase stage 4 of 4 with 4 MutationType ops update progress of schema change job #1 upsert descriptor #104 + ... + createAsOfTime: + wallTime: "1" + - declarativeSchemaChangerState: + - authorization: {} + - jobId: "1" + families: + - columnIds: ... formatVersion: 3 id: 104 @@ -142,9 +157,7 @@ upsert descriptor #104 - mutationId: 1 - state: DELETE_AND_WRITE_ONLY name: t - - newSchemaChangeJobId: "1" nextColumnId: 3 - nextConstraintId: 3 ... time: {} unexposedParentSchemaId: 101 diff --git a/pkg/sql/table.go b/pkg/sql/table.go index 3171f0088d5f..d2094c887972 100644 --- a/pkg/sql/table.go +++ b/pkg/sql/table.go @@ -106,7 +106,7 @@ func (p *planner) createNonDropDatabaseChangeJob( func (p *planner) createOrUpdateSchemaChangeJob( ctx context.Context, tableDesc *tabledesc.Mutable, jobDesc string, mutationID descpb.MutationID, ) error { - if tableDesc.NewSchemaChangeJobID != 0 { + if tableDesc.GetDeclarativeSchemaChangerState() != nil { return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState, "cannot perform a schema change on table %q while it is undergoing a declarative schema change", // We use the cluster version because the table may have been renamed. diff --git a/pkg/sql/truncate.go b/pkg/sql/truncate.go index c31e4a19ffb5..c3035530888e 100644 --- a/pkg/sql/truncate.go +++ b/pkg/sql/truncate.go @@ -178,7 +178,7 @@ func (p *planner) truncateTable(ctx context.Context, id descpb.ID, jobDesc strin // Exit early with an error if the table is undergoing a declarative schema // change, before we try to get job IDs and update job statuses later. See // createOrUpdateSchemaChangeJob. - if tableDesc.NewSchemaChangeJobID != 0 { + if tableDesc.GetDeclarativeSchemaChangerState() != nil { return pgerror.Newf(pgcode.ObjectNotInPrerequisiteState, "cannot perform a schema change on table %q while it is undergoing a declarative schema change", tableDesc.GetName(), From ca64f826fef8779e1e0fc58df54c6d3b2928e01f Mon Sep 17 00:00:00 2001 From: Andrew Werner Date: Mon, 31 Jan 2022 10:22:43 -0500 Subject: [PATCH 4/5] sql/schemachanger: move schema changer state to descriptors This is in support of being able to restore such descriptors and synthesize a job. Release note: None --- pkg/jobs/jobspb/BUILD.bazel | 2 - pkg/jobs/jobspb/jobs.proto | 12 +- pkg/sql/schemachanger/end_to_end_test.go | 5 +- pkg/sql/schemachanger/scbuild/builder_test.go | 1 + .../scdeps/sctestutils/sctestutils.go | 17 +- pkg/sql/schemachanger/scexec/BUILD.bazel | 1 - pkg/sql/schemachanger/scexec/exec_mutation.go | 32 +- pkg/sql/schemachanger/scexec/executor.go | 1 - .../scexec/scmutationexec/scmutationexec.go | 85 +- pkg/sql/schemachanger/scjob/job.go | 6 +- pkg/sql/schemachanger/scop/mutation.go | 42 +- .../scop/mutation_visitor_generated.go | 22 +- .../scplan/internal/scstage/BUILD.bazel | 1 + .../scplan/internal/scstage/build.go | 162 +- pkg/sql/schemachanger/scplan/plan_test.go | 7 + .../schemachanger/scplan/testdata/alter_table | 216 +- .../scplan/testdata/create_index | 91 +- .../scplan/testdata/drop_database | 114 +- .../schemachanger/scplan/testdata/drop_schema | 93 +- .../scplan/testdata/drop_sequence | 60 +- .../schemachanger/scplan/testdata/drop_table | 58 +- .../schemachanger/scplan/testdata/drop_type | 30 +- .../schemachanger/scplan/testdata/drop_view | 88 +- pkg/sql/schemachanger/screl/scalars.go | 4 +- pkg/sql/schemachanger/scrun/BUILD.bazel | 4 +- pkg/sql/schemachanger/scrun/scrun.go | 123 +- .../testdata/alter_table_add_column | 252 +- pkg/sql/schemachanger/testdata/drop | 3657 ++++++++++++++++- pkg/sql/schemachanger/testdata/index | 99 +- 29 files changed, 4751 insertions(+), 534 deletions(-) diff --git a/pkg/jobs/jobspb/BUILD.bazel b/pkg/jobs/jobspb/BUILD.bazel index e76bbef111c2..d585fd29964c 100644 --- a/pkg/jobs/jobspb/BUILD.bazel +++ b/pkg/jobs/jobspb/BUILD.bazel @@ -31,7 +31,6 @@ proto_library( "//pkg/clusterversion:clusterversion_proto", "//pkg/roachpb:roachpb_proto", "//pkg/sql/catalog/descpb:descpb_proto", - "//pkg/sql/schemachanger/scpb:scpb_proto", "//pkg/util/hlc:hlc_proto", "@com_github_cockroachdb_errors//errorspb:errorspb_proto", "@com_github_gogo_protobuf//gogoproto:gogo_proto", @@ -52,7 +51,6 @@ go_proto_library( "//pkg/security", # keep "//pkg/sql/catalog/catpb", # keep "//pkg/sql/catalog/descpb", - "//pkg/sql/schemachanger/scpb", "//pkg/sql/sem/tree", # keep "//pkg/util/hlc", "//pkg/util/tracing/tracingpb", # keep diff --git a/pkg/jobs/jobspb/jobs.proto b/pkg/jobs/jobspb/jobs.proto index 9c7d440093dd..c3d666465ae8 100644 --- a/pkg/jobs/jobspb/jobs.proto +++ b/pkg/jobs/jobspb/jobs.proto @@ -20,7 +20,6 @@ import "roachpb/io-formats.proto"; import "sql/catalog/descpb/structured.proto"; import "sql/catalog/descpb/tenant.proto"; import "util/hlc/timestamp.proto"; -import "sql/schemachanger/scpb/scpb.proto"; import "clusterversion/cluster_version.proto"; import "google/protobuf/timestamp.proto"; @@ -470,14 +469,14 @@ message TypeSchemaChangeProgress { } -// TypeSchemaChangeDetails is the job detail information for the new schema change job. +// NewSchemaChangeDetails is the job detail information for the new schema change job. message NewSchemaChangeDetails { - cockroach.sql.schemachanger.scpb.TargetState target_state = 5 [(gogoproto.nullable) = false]; - // BackfillProgress stores the progress for index backfills which may // be ongoing. repeated BackfillProgress backfill_progress = 4 [(gogoproto.nullable) = false]; + + reserved 1, 2, 3, 5; } // BackfillProgress is used to track backfill progress in the declarative @@ -519,10 +518,7 @@ message BackfillProgress { // NewSchemaChangeProgress is the persisted progress for the new schema change job. message NewSchemaChangeProgress { - - // Current tracks the current state of elements in the same order as in the job - // details. - repeated cockroach.sql.schemachanger.scpb.Status current = 1; + reserved 1; } // AutoSpanConfigReconciliationDetails is the job detail information for the diff --git a/pkg/sql/schemachanger/end_to_end_test.go b/pkg/sql/schemachanger/end_to_end_test.go index 34e0c7dd85f3..5587e0aa04c3 100644 --- a/pkg/sql/schemachanger/end_to_end_test.go +++ b/pkg/sql/schemachanger/end_to_end_test.go @@ -99,6 +99,7 @@ func TestSchemaChangerSideEffects(t *testing.T) { // For setting up a builder inside tests we will ensure that the new schema // changer will allow non-fully implemented operations. sd.NewSchemaChangerMode = sessiondatapb.UseNewSchemaChangerUnsafe + sd.ApplicationName = "" })), sctestdeps.WithTestingKnobs(&scrun.TestingKnobs{ BeforeStage: func(p scplan.Plan, stageIdx int) error { @@ -155,11 +156,9 @@ func execStatementWithTestDeps( // Run post-commit phase in mock schema change job. deps.IncrementPhase() deps.LogSideEffectf("# begin %s", deps.Phase()) - details := job.Details.(jobspb.NewSchemaChangeDetails) - progress := job.Progress.(jobspb.NewSchemaChangeProgress) const rollback = false err = scrun.RunSchemaChangesInJob( - ctx, deps.TestingKnobs(), deps.ClusterSettings(), deps, jobID, details, progress, rollback, + ctx, deps.TestingKnobs(), deps.ClusterSettings(), deps, jobID, job.DescriptorIDs, rollback, ) require.NoError(t, err, "error in mock schema change job execution") deps.LogSideEffectf("# end %s", deps.Phase()) diff --git a/pkg/sql/schemachanger/scbuild/builder_test.go b/pkg/sql/schemachanger/scbuild/builder_test.go index 3d915caa1263..28e08b2babac 100644 --- a/pkg/sql/schemachanger/scbuild/builder_test.go +++ b/pkg/sql/schemachanger/scbuild/builder_test.go @@ -72,6 +72,7 @@ func TestBuildDataDriven(t *testing.T) { // For setting up a builder inside tests we will ensure that the new schema // changer will allow non-fully implemented operations. sd.NewSchemaChangerMode = sessiondatapb.UseNewSchemaChangerUnsafe + sd.ApplicationName = "" })))) }, }, diff --git a/pkg/sql/schemachanger/scdeps/sctestutils/sctestutils.go b/pkg/sql/schemachanger/scdeps/sctestutils/sctestutils.go index 5682b7e29a64..b2ab23251117 100644 --- a/pkg/sql/schemachanger/scdeps/sctestutils/sctestutils.go +++ b/pkg/sql/schemachanger/scdeps/sctestutils/sctestutils.go @@ -168,17 +168,18 @@ func MakePlan(t *testing.T, state scpb.CurrentState, phase scop.Phase) scplan.Pl SchemaChangerJobIDSupplier: func() jobspb.JobID { return 1 }, }) require.NoError(t, err) - // Remove really long ops details that aren't that important anyway. + return plan +} + +// TruncateJobOps truncates really long ops details that aren't that important anyway. +func TruncateJobOps(plan *scplan.Plan) { for _, s := range plan.Stages { for _, o := range s.ExtraOps { - if op, ok := o.(*scop.CreateDeclarativeSchemaChangerJob); ok { - op.TargetState.Targets = nil - op.Current = nil - } - if op, ok := o.(*scop.UpdateSchemaChangerJob); ok { - op.Current = nil + if op, ok := o.(*scop.SetJobStateOnDescriptor); ok { + op.State = scpb.DescriptorState{ + JobID: op.State.JobID, + } } } } - return plan } diff --git a/pkg/sql/schemachanger/scexec/BUILD.bazel b/pkg/sql/schemachanger/scexec/BUILD.bazel index 5d511fc1c1f5..0f5943d4e2c1 100644 --- a/pkg/sql/schemachanger/scexec/BUILD.bazel +++ b/pkg/sql/schemachanger/scexec/BUILD.bazel @@ -25,7 +25,6 @@ go_library( "//pkg/sql/schemachanger/scexec/scmutationexec", "//pkg/sql/schemachanger/scop", "//pkg/sql/schemachanger/scpb", - "//pkg/sql/schemachanger/screl", "//pkg/sql/sessiondata", "//pkg/util/hlc", "//pkg/util/log", diff --git a/pkg/sql/schemachanger/scexec/exec_mutation.go b/pkg/sql/schemachanger/scexec/exec_mutation.go index 4e5ec791fb42..8e4e2f4baa4b 100644 --- a/pkg/sql/schemachanger/scexec/exec_mutation.go +++ b/pkg/sql/schemachanger/scexec/exec_mutation.go @@ -26,7 +26,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec/scmutationexec" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" - "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/screl" "github.com/cockroachdb/cockroach/pkg/util/log/eventpb" "github.com/cockroachdb/cockroach/pkg/util/timeutil" "github.com/cockroachdb/errors" @@ -184,7 +183,6 @@ func executeDescriptorMutationOps(ctx context.Context, deps Dependencies, ops [] md jobs.JobMetadata, updateProgress func(*jobspb.Progress), setNonCancelable func(), ) error { progress := *md.Progress - progress.GetNewSchemaChange().Current = update.current updateProgress(&progress) if !md.Payload.Noncancelable && update.isNonCancelable { setNonCancelable() @@ -321,12 +319,11 @@ type eventPayload struct { } type schemaChangerJobUpdate struct { - current []scpb.Status isNonCancelable bool } func (mvs *mutationVisitorState) UpdateSchemaChangerJob( - jobID jobspb.JobID, current []scpb.Status, isNonCancelable bool, + jobID jobspb.JobID, isNonCancelable bool, ) error { if mvs.schemaChangerJobUpdates == nil { mvs.schemaChangerJobUpdates = make(map[jobspb.JobID]schemaChangerJobUpdate) @@ -334,7 +331,6 @@ func (mvs *mutationVisitorState) UpdateSchemaChangerJob( return errors.AssertionFailedf("cannot update job %d more than once", jobID) } mvs.schemaChangerJobUpdates[jobID] = schemaChangerJobUpdate{ - current: current, isNonCancelable: isNonCancelable, } return nil @@ -436,29 +432,31 @@ func (mvs *mutationVisitorState) AddNewGCJobForIndex( } func (mvs *mutationVisitorState) AddNewSchemaChangerJob( - jobID jobspb.JobID, targetState scpb.TargetState, current []scpb.Status, + jobID jobspb.JobID, stmts []scpb.Statement, auth scpb.Authorization, descriptorIDs descpb.IDs, ) error { if mvs.schemaChangerJob != nil { return errors.AssertionFailedf("cannot create more than one new schema change job") } - stmts := make([]string, len(targetState.Statements)) - for i, stmt := range targetState.Statements { - stmts[i] = stmt.Statement + stmtStrs := make([]string, len(stmts)) + for i, stmt := range stmts { + stmtStrs[i] = stmt.Statement } mvs.schemaChangerJob = &jobs.Record{ JobID: jobID, Description: "schema change job", // TODO(ajwerner): use const - Statements: stmts, - Username: security.MakeSQLUsernameFromPreNormalizedString(targetState.Authorization.UserName), + Statements: stmtStrs, + Username: security.MakeSQLUsernameFromPreNormalizedString(auth.UserName), // TODO(ajwerner): It may be better in the future to have the builder be - // responsible for determining this set of descriptors. As of the time of - // writing, the descriptors to be "locked," descriptors that need schema - // change jobs, and descriptors with schema change mutations all coincide. + // responsible for determining this set of descriptorIDs. As of the time of + // writing, the descriptorIDs to be "locked," descriptorIDs that need schema + // change jobs, and descriptorIDs with schema change mutations all coincide. // But there are future schema changes to be implemented in the new schema // changer (e.g., RENAME TABLE) for which this may no longer be true. - DescriptorIDs: screl.GetDescIDs(targetState), - Details: jobspb.NewSchemaChangeDetails{TargetState: targetState}, - Progress: jobspb.NewSchemaChangeProgress{Current: current}, + DescriptorIDs: descriptorIDs, + Details: jobspb.NewSchemaChangeDetails{}, + Progress: jobspb.NewSchemaChangeProgress{}, + + // TODO(ajwerner): It'd be good to populate the RunningStatus at all times. RunningStatus: "", NonCancelable: false, } diff --git a/pkg/sql/schemachanger/scexec/executor.go b/pkg/sql/schemachanger/scexec/executor.go index 6d1851da47a5..5daa925ed460 100644 --- a/pkg/sql/schemachanger/scexec/executor.go +++ b/pkg/sql/schemachanger/scexec/executor.go @@ -27,7 +27,6 @@ func ExecuteStage(ctx context.Context, deps Dependencies, ops []scop.Op) error { return nil } typ := ops[0].Type() - log.Infof(ctx, "executing %d ops of type %s", len(ops), typ) switch typ { case scop.MutationType: return executeDescriptorMutationOps(ctx, deps, ops) diff --git a/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go b/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go index ff9b5881e1bb..56a40264d79f 100644 --- a/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go +++ b/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go @@ -113,11 +113,11 @@ type MutationVisitorStateUpdater interface { AddNewGCJobForIndex(tbl catalog.TableDescriptor, index catalog.Index) // AddNewSchemaChangerJob adds a schema changer job. - AddNewSchemaChangerJob(jobID jobspb.JobID, targetState scpb.TargetState, current []scpb.Status) error + AddNewSchemaChangerJob(jobID jobspb.JobID, stmts []scpb.Statement, auth scpb.Authorization, descriptors descpb.IDs) error // UpdateSchemaChangerJob will update the progress and payload of the // schema changer job. - UpdateSchemaChangerJob(jobID jobspb.JobID, current []scpb.Status, isNonCancelable bool) error + UpdateSchemaChangerJob(jobID jobspb.JobID, isNonCancelable bool) error // EnqueueEvent will enqueue an event to be written to the event log. EnqueueEvent(id descpb.ID, metadata scpb.TargetMetadata, details eventpb.CommonSQLEventDetails, event eventpb.EventPayload) error @@ -142,19 +142,11 @@ type visitor struct { p Partitioner } -func (m *visitor) RemoveJobReference(ctx context.Context, reference scop.RemoveJobReference) error { - return m.swapSchemaChangeJobID(ctx, reference.DescriptorID, reference.JobID, 0) -} - -func (m *visitor) AddJobReference(ctx context.Context, reference scop.AddJobReference) error { - return m.swapSchemaChangeJobID(ctx, reference.DescriptorID, 0, reference.JobID) -} - -func (m *visitor) swapSchemaChangeJobID( - ctx context.Context, descID descpb.ID, exp, to jobspb.JobID, +func (m *visitor) RemoveJobStateFromDescriptor( + ctx context.Context, op scop.RemoveJobStateFromDescriptor, ) error { { - _, err := m.cr.MustReadImmutableDescriptor(ctx, descID) + _, err := m.cr.MustReadImmutableDescriptor(ctx, op.DescriptorID) // If we're clearing the status, we might have already deleted the // descriptor. Permit that by detecting the prior deletion and @@ -163,7 +155,7 @@ func (m *visitor) swapSchemaChangeJobID( // TODO(ajwerner): Ideally we'd model the clearing of the job dependency as // an operation which has to happen before deleting the descriptor. If that // were the case, this error would become unexpected. - if errors.Is(err, catalog.ErrDescriptorNotFound) && to == 0 { + if errors.Is(err, catalog.ErrDescriptorNotFound) { return nil } if err != nil { @@ -171,44 +163,65 @@ func (m *visitor) swapSchemaChangeJobID( } } - mut, err := m.s.CheckOutDescriptor(ctx, descID) + mut, err := m.s.CheckOutDescriptor(ctx, op.DescriptorID) if err != nil { return err } - // TODO(ajwerner): We could leverage this spot to determine whether - // there's an outstanding concurrent schema change and bubble up the - // relevant error. It's probably better to detect the conflict at - // build time when we resolve the descriptor or perhaps immediately - // after building by taking a peek at the implied descriptor set so - // that this can remain an assertion. - scs := mut.GetDeclarativeSchemaChangerState() - if scs != nil && scs.JobID != exp || scs == nil && exp != 0 { + existing := mut.GetDeclarativeSchemaChangerState() + if existing == nil { + return errors.AssertionFailedf( + "expected schema change state with job ID %d on table %d, found none", + op.JobID, op.DescriptorID, + ) + } else if existing.JobID != op.JobID { return errors.AssertionFailedf( "unexpected schema change job ID %d on table %d, expected %d", - scs.JobID, descID, exp, + existing.JobID, op.DescriptorID, op.JobID, ) } - if to == 0 { - scs = nil - } else if scs == nil { - scs = &scpb.DescriptorState{JobID: to} - } else { - scs.JobID = to + mut.SetDeclarativeSchemaChangerState(nil) + return nil +} + +func (m *visitor) SetJobStateOnDescriptor( + ctx context.Context, op scop.SetJobStateOnDescriptor, +) error { + mut, err := m.s.CheckOutDescriptor(ctx, op.DescriptorID) + if err != nil { + return err + } + + // TODO(ajwerner): This check here could be used as a check for a concurrent + // attempt to place a schema change job on a descriptor. It might deserve + // a special error that is not an assertion if we didn't choose to handle + // this at a higher level. + expected := op.State.JobID + if op.Initialize { + expected = jobspb.InvalidJobID + } + scs := mut.GetDeclarativeSchemaChangerState() + if scs != nil { + if op.Initialize || scs.JobID != expected { + return errors.AssertionFailedf( + "unexpected schema change job ID %d on table %d, expected %d", + scs.JobID, op.DescriptorID, expected, + ) + } } - mut.SetDeclarativeSchemaChangerState(scs) + mut.SetDeclarativeSchemaChangerState(op.State.Clone()) return nil } -func (m *visitor) CreateDeclarativeSchemaChangerJob( - ctx context.Context, job scop.CreateDeclarativeSchemaChangerJob, +func (m *visitor) CreateSchemaChangerJob( + ctx context.Context, job scop.CreateSchemaChangerJob, ) error { - return m.s.AddNewSchemaChangerJob(job.JobID, job.TargetState, job.Current) + return m.s.AddNewSchemaChangerJob(job.JobID, job.Statements, job.Authorization, job.DescriptorIDs) } func (m *visitor) UpdateSchemaChangerJob( - ctx context.Context, progress scop.UpdateSchemaChangerJob, + ctx context.Context, op scop.UpdateSchemaChangerJob, ) error { - return m.s.UpdateSchemaChangerJob(progress.JobID, progress.Current, progress.IsNonCancelable) + return m.s.UpdateSchemaChangerJob(op.JobID, op.IsNonCancelable) } func (m *visitor) checkOutTable(ctx context.Context, id descpb.ID) (*tabledesc.Mutable, error) { diff --git a/pkg/sql/schemachanger/scjob/job.go b/pkg/sql/schemachanger/scjob/job.go index dc2f327f6dc9..1b8ac0fc93b8 100644 --- a/pkg/sql/schemachanger/scjob/job.go +++ b/pkg/sql/schemachanger/scjob/job.go @@ -61,9 +61,6 @@ func (n *newSchemaChangeResumer) run(ctx context.Context, execCtxI interface{}) // avoid restarts. payload := n.job.Payload() - progress := n.job.Progress() - newSchemaChangeProgress := progress.GetNewSchemaChange() - newSchemaChangeDetails := payload.GetNewSchemaChange() deps := scdeps.NewJobRunDependencies( execCfg.CollectionFactory, execCfg.DB, @@ -91,8 +88,7 @@ func (n *newSchemaChangeResumer) run(ctx context.Context, execCtxI interface{}) execCfg.Settings, deps, n.job.ID(), - *newSchemaChangeDetails, - *newSchemaChangeProgress, + payload.DescriptorIDs, n.rollback, ) } diff --git a/pkg/sql/schemachanger/scop/mutation.go b/pkg/sql/schemachanger/scop/mutation.go index a1c5deb2b62b..08293838451a 100644 --- a/pkg/sql/schemachanger/scop/mutation.go +++ b/pkg/sql/schemachanger/scop/mutation.go @@ -338,36 +338,44 @@ type DeleteDatabaseSchemaEntry struct { SchemaID descpb.ID } -// RemoveJobReference removes the reference to a job from the descriptor. -type RemoveJobReference struct { +// RemoveJobStateFromDescriptor removes the reference to a job from the +// descriptor and clears the pending targets. +type RemoveJobStateFromDescriptor struct { mutationOp DescriptorID descpb.ID JobID jobspb.JobID } -// AddJobReference adds the reference to a job to the descriptor. -type AddJobReference struct { +// SetJobStateOnDescriptor adds the reference to a job to the descriptor. +type SetJobStateOnDescriptor struct { mutationOp DescriptorID descpb.ID - JobID jobspb.JobID + // Initialize indicates whether this op ought to be setting the JobID and + // state for the first time for this job or whether it's an update. In true + // case, the expectation is that the job and state are currently unset. In + // the false case, the expectation is that they are set and match the values + // in the op. + Initialize bool + State scpb.DescriptorState } -// CreateDeclarativeSchemaChangerJob constructs the job for the -// declarative schema changer post-commit phases. -type CreateDeclarativeSchemaChangerJob struct { +// UpdateSchemaChangerJob may update the job's cancelable status. +type UpdateSchemaChangerJob struct { mutationOp - JobID jobspb.JobID - TargetState scpb.TargetState - Current []scpb.Status + IsNonCancelable bool + JobID jobspb.JobID + + // TODO(ajwerner): Plumb and set RunningStatus. } -// UpdateSchemaChangerJob is used to update the progress and payload of the -// schema changer job. -type UpdateSchemaChangerJob struct { +// CreateSchemaChangerJob constructs the job for the +// declarative schema changer post-commit phases. +type CreateSchemaChangerJob struct { mutationOp - JobID jobspb.JobID - Current []scpb.Status - IsNonCancelable bool + JobID jobspb.JobID + Authorization scpb.Authorization + Statements []scpb.Statement + DescriptorIDs []descpb.ID } // RemoveTableComment is used to delete a comment associated with a table. diff --git a/pkg/sql/schemachanger/scop/mutation_visitor_generated.go b/pkg/sql/schemachanger/scop/mutation_visitor_generated.go index 0c417293cb34..7fb0f0eb655f 100644 --- a/pkg/sql/schemachanger/scop/mutation_visitor_generated.go +++ b/pkg/sql/schemachanger/scop/mutation_visitor_generated.go @@ -58,10 +58,10 @@ type MutationVisitor interface { SetIndexName(context.Context, SetIndexName) error DeleteDescriptor(context.Context, DeleteDescriptor) error DeleteDatabaseSchemaEntry(context.Context, DeleteDatabaseSchemaEntry) error - RemoveJobReference(context.Context, RemoveJobReference) error - AddJobReference(context.Context, AddJobReference) error - CreateDeclarativeSchemaChangerJob(context.Context, CreateDeclarativeSchemaChangerJob) error + RemoveJobStateFromDescriptor(context.Context, RemoveJobStateFromDescriptor) error + SetJobStateOnDescriptor(context.Context, SetJobStateOnDescriptor) error UpdateSchemaChangerJob(context.Context, UpdateSchemaChangerJob) error + CreateSchemaChangerJob(context.Context, CreateSchemaChangerJob) error RemoveTableComment(context.Context, RemoveTableComment) error RemoveDatabaseComment(context.Context, RemoveDatabaseComment) error RemoveSchemaComment(context.Context, RemoveSchemaComment) error @@ -252,23 +252,23 @@ func (op DeleteDatabaseSchemaEntry) Visit(ctx context.Context, v MutationVisitor } // Visit is part of the MutationOp interface. -func (op RemoveJobReference) Visit(ctx context.Context, v MutationVisitor) error { - return v.RemoveJobReference(ctx, op) +func (op RemoveJobStateFromDescriptor) Visit(ctx context.Context, v MutationVisitor) error { + return v.RemoveJobStateFromDescriptor(ctx, op) } // Visit is part of the MutationOp interface. -func (op AddJobReference) Visit(ctx context.Context, v MutationVisitor) error { - return v.AddJobReference(ctx, op) +func (op SetJobStateOnDescriptor) Visit(ctx context.Context, v MutationVisitor) error { + return v.SetJobStateOnDescriptor(ctx, op) } // Visit is part of the MutationOp interface. -func (op CreateDeclarativeSchemaChangerJob) Visit(ctx context.Context, v MutationVisitor) error { - return v.CreateDeclarativeSchemaChangerJob(ctx, op) +func (op UpdateSchemaChangerJob) Visit(ctx context.Context, v MutationVisitor) error { + return v.UpdateSchemaChangerJob(ctx, op) } // Visit is part of the MutationOp interface. -func (op UpdateSchemaChangerJob) Visit(ctx context.Context, v MutationVisitor) error { - return v.UpdateSchemaChangerJob(ctx, op) +func (op CreateSchemaChangerJob) Visit(ctx context.Context, v MutationVisitor) error { + return v.CreateSchemaChangerJob(ctx, op) } // Visit is part of the MutationOp interface. diff --git a/pkg/sql/schemachanger/scplan/internal/scstage/BUILD.bazel b/pkg/sql/schemachanger/scplan/internal/scstage/BUILD.bazel index ff35f6be451b..de73b3d7250d 100644 --- a/pkg/sql/schemachanger/scplan/internal/scstage/BUILD.bazel +++ b/pkg/sql/schemachanger/scplan/internal/scstage/BUILD.bazel @@ -10,6 +10,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//pkg/jobs/jobspb", + "//pkg/sql/catalog", "//pkg/sql/catalog/descpb", "//pkg/sql/schemachanger/scop", "//pkg/sql/schemachanger/scpb", diff --git a/pkg/sql/schemachanger/scplan/internal/scstage/build.go b/pkg/sql/schemachanger/scplan/internal/scstage/build.go index 8df3129e3405..376062013e2f 100644 --- a/pkg/sql/schemachanger/scplan/internal/scstage/build.go +++ b/pkg/sql/schemachanger/scplan/internal/scstage/build.go @@ -12,6 +12,7 @@ package scstage import ( "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" + "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" @@ -22,6 +23,8 @@ import ( ) // BuildStages builds the plan's stages for this and all subsequent phases. +// Note that the scJobIDSupplier function is idempotent, and must return the +// same value for all calls. func BuildStages( init scpb.CurrentState, phase scop.Phase, g *scgraph.Graph, scJobIDSupplier func() jobspb.JobID, ) []Stage { @@ -374,82 +377,83 @@ func (sb stageBuilder) build() Stage { } s.EdgeOps = append(s.EdgeOps, e.Op()...) } - // Decorate stage with job-related operations. - // TODO(ajwerner): Rather than adding this above the opgen layer, it'd be - // better to do it as part of graph generation. We could treat the job as - // and the job references as elements and track their relationships. The - // oddity here is that the job gets both added and removed. In practice, this - // may prove to be a somewhat common pattern in other cases: consider the - // intermediate index needed when adding and dropping columns as part of the - // same transaction. + jobOps := sb.computeExtraJobOps(s, after) + s.ExtraOps = jobOps + return s +} + +// Decorate stage with job-related operations. +// +// TODO(ajwerner): Rather than adding this above the opgen layer, it may be +// better to do it as part of graph generation. We could treat the job as +// and the job references as elements and track their relationships. The +// oddity here is that the job gets both added and removed. In practice, this +// may prove to be a somewhat common pattern in other cases: consider the +// intermediate index needed when adding and dropping columns as part of the +// same transaction. +func (sb stageBuilder) computeExtraJobOps(s Stage, after []scpb.Status) []scop.Op { switch s.Phase { case scop.PreCommitPhase: // If this pre-commit stage is non-terminal, this means there will be at // least one post-commit stage, so we need to create a schema changer job // and update references for the affected descriptors. if !sb.bc.isStateTerminal(after) { - s.ExtraOps = append(sb.bc.addJobReferenceOps(), sb.bc.createSchemaChangeJobOp(after)) + const initialize = true + return append(sb.bc.setJobStateOnDescriptorOps(initialize, after), + sb.bc.createSchemaChangeJobOp()) } + return nil case scop.PostCommitPhase, scop.PostCommitNonRevertiblePhase: - if sb.opType == scop.MutationType { - if sb.bc.isStateTerminal(after) { - // The terminal mutation stage needs to remove references to the schema - // changer job in the affected descriptors. - s.ExtraOps = sb.bc.removeJobReferenceOps() - } - // Post-commit mutation stages all update the progress of the schema - // changer job. - s.ExtraOps = append(s.ExtraOps, sb.bc.updateJobProgressOp(after, s.Phase > scop.PostCommitPhase)) + if sb.opType != scop.MutationType { + return nil + } + var ops []scop.Op + if sb.bc.isStateTerminal(after) { + // The terminal mutation stage needs to remove references to the schema + // changer job in the affected descriptors. + ops = sb.bc.removeJobReferenceOps() + } else { + const initialize = false + ops = sb.bc.setJobStateOnDescriptorOps(initialize, after) } + // If we just moved to a non-cancelable phase, we need to tell the job + // that it cannot be canceled. Ideally we'd do this just once. + // + // TODO(ajwerner): Track the previous stage's phase so we can know when + // we need to emit this op. Also, this is buggy, we need to mark the job + // as non-cancelable *before* we start to execute the first NonRevertible + // phase. + ops = append(ops, sb.bc.updateJobProgressOp(s.Phase > scop.PostCommitPhase)) + return ops + default: + return nil } - return s } -func (bc buildContext) createSchemaChangeJobOp(current []scpb.Status) scop.Op { - return &scop.CreateDeclarativeSchemaChangerJob{ - JobID: bc.scJobIDSupplier(), - TargetState: bc.targetState, - Current: current, +func (bc buildContext) createSchemaChangeJobOp() scop.Op { + return &scop.CreateSchemaChangerJob{ + JobID: bc.scJobIDSupplier(), + Statements: bc.targetState.Statements, + Authorization: bc.targetState.Authorization, + DescriptorIDs: screl.GetDescIDs(bc.targetState).Ordered(), } } -func (bc buildContext) addJobReferenceOps() []scop.Op { - jobID := bc.scJobIDSupplier() - return generateOpsForJobIDs( - screl.GetDescIDs(bc.targetState), - jobID, - func(descID descpb.ID, id jobspb.JobID) scop.Op { - return &scop.AddJobReference{DescriptorID: descID, JobID: jobID} - }, - ) -} - -func (bc buildContext) updateJobProgressOp(current []scpb.Status, isNonCancellable bool) scop.Op { +func (bc buildContext) updateJobProgressOp(isNonCancellable bool) scop.Op { return &scop.UpdateSchemaChangerJob{ JobID: bc.scJobIDSupplier(), - Current: current, IsNonCancelable: isNonCancellable, } } -func (bc buildContext) removeJobReferenceOps() []scop.Op { +func (bc buildContext) removeJobReferenceOps() (ops []scop.Op) { jobID := bc.scJobIDSupplier() - return generateOpsForJobIDs( - screl.GetDescIDs(bc.targetState), - jobID, - func(descID descpb.ID, id jobspb.JobID) scop.Op { - return &scop.RemoveJobReference{DescriptorID: descID, JobID: jobID} - }, - ) -} - -func generateOpsForJobIDs( - descIDs []descpb.ID, jobID jobspb.JobID, f func(descID descpb.ID, id jobspb.JobID) scop.Op, -) []scop.Op { - ops := make([]scop.Op, len(descIDs)) - for i, descID := range descIDs { - ops[i] = f(descID, jobID) - } + screl.GetDescIDs(bc.targetState).ForEach(func(descID descpb.ID) { + ops = append(ops, &scop.RemoveJobStateFromDescriptor{ + DescriptorID: descID, + JobID: jobID, + }) + }) return ops } @@ -467,6 +471,56 @@ func (bc buildContext) nodes(current []scpb.Status) []*screl.Node { return nodes } +func (bc buildContext) setJobStateOnDescriptorOps(initialize bool, after []scpb.Status) []scop.Op { + descIDs, states := makeDescriptorStates(bc.scJobIDSupplier(), bc.targetState, after) + ops := make([]scop.Op, 0, descIDs.Len()) + descIDs.ForEach(func(descID descpb.ID) { + ops = append(ops, &scop.SetJobStateOnDescriptor{ + DescriptorID: descID, + Initialize: initialize, + State: *states[descID], + }) + }) + return ops +} + +func makeDescriptorStates( + jobID jobspb.JobID, ts scpb.TargetState, statuses []scpb.Status, +) (catalog.DescriptorIDSet, map[descpb.ID]*scpb.DescriptorState) { + descIDs := screl.GetDescIDs(ts) + states := make(map[descpb.ID]*scpb.DescriptorState, descIDs.Len()) + descIDs.ForEach(func(id descpb.ID) { + states[id] = &scpb.DescriptorState{ + Authorization: ts.Authorization, + } + }) + noteRelevantStatement := func(state *scpb.DescriptorState, stmtRank uint32) { + for i := range state.RelevantStatements { + stmt := &state.RelevantStatements[i] + if stmt.StatementRank != stmtRank { + continue + } + return + } + state.RelevantStatements = append(state.RelevantStatements, + scpb.DescriptorState_Statement{ + Statement: ts.Statements[stmtRank], + StatementRank: stmtRank, + }) + } + for i, t := range ts.Targets { + descID := screl.GetDescID(t.Element()) + state := states[descID] + state.JobID = jobID + stmtID := t.Metadata.StatementID + noteRelevantStatement(state, stmtID) + state.Targets = append(state.Targets, t) + state.TargetRanks = append(state.TargetRanks, uint32(i)) + state.CurrentStatuses = append(state.CurrentStatuses, statuses[i]) + } + return descIDs, states +} + // decorateStages decorates stages with position in plan. func decorateStages(stages []Stage) []Stage { if len(stages) == 0 { diff --git a/pkg/sql/schemachanger/scplan/plan_test.go b/pkg/sql/schemachanger/scplan/plan_test.go index 750ecf6f5ad9..419190dd4e9b 100644 --- a/pkg/sql/schemachanger/scplan/plan_test.go +++ b/pkg/sql/schemachanger/scplan/plan_test.go @@ -101,6 +101,7 @@ func TestPlanDataDriven(t *testing.T) { } plan = sctestutils.MakePlan(t, state, scop.EarliestPhase) + sctestutils.TruncateJobOps(&plan) validatePlan(t, &plan) }) @@ -155,6 +156,7 @@ func validatePlan(t *testing.T, plan *scplan.Plan) { Current: stage.Before, } truncatedPlan := sctestutils.MakePlan(t, cs, stage.Phase) + sctestutils.TruncateJobOps(&truncatedPlan) a := marshalOps(t, plan.TargetState, truncatedPlan.Stages) require.Equalf(t, e, a, "plan mismatch when re-planning %d stage(s) later", i) } @@ -233,6 +235,11 @@ func marshalOps(t *testing.T, ts scpb.TargetState, stages []scstage.Stage) strin sb.WriteString(" ops:\n") stageOps := "" for _, op := range ops { + if setJobStateOp, ok := op.(*scop.SetJobStateOnDescriptor); ok { + clone := *setJobStateOp + clone.State = scpb.DescriptorState{} + op = &clone + } opMap, err := scgraphviz.ToMap(op) require.NoError(t, err) data, err := yaml.Marshal(opMap) diff --git a/pkg/sql/schemachanger/scplan/testdata/alter_table b/pkg/sql/schemachanger/scplan/testdata/alter_table index a0e06f6d193b..e34d92d2e7a2 100644 --- a/pkg/sql/schemachanger/scplan/testdata/alter_table +++ b/pkg/sql/schemachanger/scplan/testdata/alter_table @@ -51,19 +51,20 @@ PreCommitPhase stage 1 of 1 with 5 MutationType ops - 2 TableID: 104 Unique: true - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: ALTER TABLE defaultdb.foo ADD COLUMN j INT8 - redactedstatement: ALTER TABLE ‹defaultdb›.public.‹foo› ADD COLUMN ‹j› INT8 - statementtag: ALTER TABLE -PostCommitPhase stage 1 of 4 with 3 MutationType ops + Statements: + - statement: ALTER TABLE defaultdb.foo ADD COLUMN j INT8 + redactedstatement: ALTER TABLE ‹defaultdb›.public.‹foo› ADD COLUMN ‹j› INT8 + statementtag: ALTER TABLE +PostCommitPhase stage 1 of 4 with 4 MutationType ops transitions: [[Column:{DescID: 104, ColumnID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY [[PrimaryIndex:{DescID: 104, IndexID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY @@ -74,6 +75,8 @@ PostCommitPhase stage 1 of 4 with 3 MutationType ops *scop.MakeAddedColumnDeleteAndWriteOnly ColumnID: 2 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob JobID: 1 PostCommitPhase stage 2 of 4 with 1 BackfillType ops @@ -91,7 +94,7 @@ PostCommitPhase stage 3 of 4 with 1 ValidationType ops *scop.ValidateUniqueIndex IndexID: 2 TableID: 104 -PostCommitPhase stage 4 of 4 with 7 MutationType ops +PostCommitPhase stage 4 of 4 with 8 MutationType ops transitions: [[Column:{DescID: 104, ColumnID: 2}, PUBLIC], DELETE_AND_WRITE_ONLY] -> PUBLIC [[ColumnName:{DescID: 104, ColumnID: 2, Name: j}, PUBLIC], ABSENT] -> PUBLIC @@ -121,15 +124,19 @@ PostCommitPhase stage 4 of 4 with 7 MutationType ops *scop.MakeColumnPublic ColumnID: 2 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob JobID: 1 -PostCommitNonRevertiblePhase stage 1 of 2 with 2 MutationType ops +PostCommitNonRevertiblePhase stage 1 of 2 with 3 MutationType ops transitions: [[PrimaryIndex:{DescID: 104, IndexID: 1}, ABSENT], VALIDATED] -> DELETE_ONLY ops: *scop.MakeDroppedIndexDeleteOnly IndexID: 1 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob IsNonCancelable: true JobID: 1 @@ -143,7 +150,7 @@ PostCommitNonRevertiblePhase stage 2 of 2 with 4 MutationType ops *scop.CreateGcJobForIndex IndexID: 1 TableID: 104 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 *scop.UpdateSchemaChangerJob @@ -201,20 +208,21 @@ PreCommitPhase stage 1 of 1 with 5 MutationType ops - 2 TableID: 104 Unique: true - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 - JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: ALTER TABLE defaultdb.foo ADD COLUMN j INT8 DEFAULT 123 - redactedstatement: ALTER TABLE ‹defaultdb›.public.‹foo› ADD COLUMN ‹j› INT8 DEFAULT - ‹123› - statementtag: ALTER TABLE -PostCommitPhase stage 1 of 4 with 3 MutationType ops + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 + JobID: 1 + Statements: + - statement: ALTER TABLE defaultdb.foo ADD COLUMN j INT8 DEFAULT 123 + redactedstatement: ALTER TABLE ‹defaultdb›.public.‹foo› ADD COLUMN ‹j› INT8 DEFAULT + ‹123› + statementtag: ALTER TABLE +PostCommitPhase stage 1 of 4 with 4 MutationType ops transitions: [[Column:{DescID: 104, ColumnID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY [[PrimaryIndex:{DescID: 104, IndexID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY @@ -225,6 +233,8 @@ PostCommitPhase stage 1 of 4 with 3 MutationType ops *scop.MakeAddedColumnDeleteAndWriteOnly ColumnID: 2 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob JobID: 1 PostCommitPhase stage 2 of 4 with 1 BackfillType ops @@ -242,7 +252,7 @@ PostCommitPhase stage 3 of 4 with 1 ValidationType ops *scop.ValidateUniqueIndex IndexID: 2 TableID: 104 -PostCommitPhase stage 4 of 4 with 7 MutationType ops +PostCommitPhase stage 4 of 4 with 8 MutationType ops transitions: [[Column:{DescID: 104, ColumnID: 2}, PUBLIC], DELETE_AND_WRITE_ONLY] -> PUBLIC [[ColumnName:{DescID: 104, ColumnID: 2, Name: j}, PUBLIC], ABSENT] -> PUBLIC @@ -272,15 +282,19 @@ PostCommitPhase stage 4 of 4 with 7 MutationType ops *scop.MakeColumnPublic ColumnID: 2 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob JobID: 1 -PostCommitNonRevertiblePhase stage 1 of 2 with 2 MutationType ops +PostCommitNonRevertiblePhase stage 1 of 2 with 3 MutationType ops transitions: [[PrimaryIndex:{DescID: 104, IndexID: 1}, ABSENT], VALIDATED] -> DELETE_ONLY ops: *scop.MakeDroppedIndexDeleteOnly IndexID: 1 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob IsNonCancelable: true JobID: 1 @@ -294,7 +308,7 @@ PostCommitNonRevertiblePhase stage 2 of 2 with 4 MutationType ops *scop.CreateGcJobForIndex IndexID: 1 TableID: 104 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 *scop.UpdateSchemaChangerJob @@ -388,24 +402,25 @@ PreCommitPhase stage 1 of 1 with 7 MutationType ops - 3 TableID: 104 Unique: true - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 - JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: ALTER TABLE defaultdb.foo ADD COLUMN j INT8 DEFAULT 123 - redactedstatement: ALTER TABLE ‹defaultdb›.public.‹foo› ADD COLUMN ‹j› INT8 DEFAULT - ‹123› - statementtag: ALTER TABLE - - statement: ALTER TABLE defaultdb.foo ADD COLUMN k INT8 DEFAULT 456 - redactedstatement: ALTER TABLE ‹defaultdb›.public.‹foo› ADD COLUMN ‹k› INT8 DEFAULT - ‹456› - statementtag: ALTER TABLE -PostCommitPhase stage 1 of 4 with 4 MutationType ops + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 + JobID: 1 + Statements: + - statement: ALTER TABLE defaultdb.foo ADD COLUMN j INT8 DEFAULT 123 + redactedstatement: ALTER TABLE ‹defaultdb›.public.‹foo› ADD COLUMN ‹j› INT8 DEFAULT + ‹123› + statementtag: ALTER TABLE + - statement: ALTER TABLE defaultdb.foo ADD COLUMN k INT8 DEFAULT 456 + redactedstatement: ALTER TABLE ‹defaultdb›.public.‹foo› ADD COLUMN ‹k› INT8 DEFAULT + ‹456› + statementtag: ALTER TABLE +PostCommitPhase stage 1 of 4 with 5 MutationType ops transitions: [[Column:{DescID: 104, ColumnID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY [[PrimaryIndex:{DescID: 104, IndexID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY @@ -420,6 +435,8 @@ PostCommitPhase stage 1 of 4 with 4 MutationType ops *scop.MakeAddedColumnDeleteAndWriteOnly ColumnID: 2 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob JobID: 1 PostCommitPhase stage 2 of 4 with 1 BackfillType ops @@ -437,7 +454,7 @@ PostCommitPhase stage 3 of 4 with 1 ValidationType ops *scop.ValidateUniqueIndex IndexID: 2 TableID: 104 -PostCommitPhase stage 4 of 4 with 9 MutationType ops +PostCommitPhase stage 4 of 4 with 10 MutationType ops transitions: [[Column:{DescID: 104, ColumnID: 2}, PUBLIC], DELETE_AND_WRITE_ONLY] -> PUBLIC [[ColumnName:{DescID: 104, ColumnID: 2, Name: j}, PUBLIC], ABSENT] -> PUBLIC @@ -476,15 +493,19 @@ PostCommitPhase stage 4 of 4 with 9 MutationType ops *scop.MakeColumnPublic ColumnID: 2 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob JobID: 1 -PostCommitNonRevertiblePhase stage 1 of 2 with 2 MutationType ops +PostCommitNonRevertiblePhase stage 1 of 2 with 3 MutationType ops transitions: [[PrimaryIndex:{DescID: 104, IndexID: 1}, ABSENT], VALIDATED] -> DELETE_ONLY ops: *scop.MakeDroppedIndexDeleteOnly IndexID: 1 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob IsNonCancelable: true JobID: 1 @@ -498,7 +519,7 @@ PostCommitNonRevertiblePhase stage 2 of 2 with 4 MutationType ops *scop.CreateGcJobForIndex IndexID: 1 TableID: 104 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 *scop.UpdateSchemaChangerJob @@ -557,20 +578,21 @@ PreCommitPhase stage 1 of 1 with 5 MutationType ops - 2 TableID: 104 Unique: true - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 - JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: ALTER TABLE defaultdb.foo ADD COLUMN a INT8 AS (i + 1) STORED - redactedstatement: ALTER TABLE ‹defaultdb›.public.‹foo› ADD COLUMN ‹a› INT8 AS - (‹i› + ‹1›) STORED - statementtag: ALTER TABLE -PostCommitPhase stage 1 of 4 with 3 MutationType ops + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 + JobID: 1 + Statements: + - statement: ALTER TABLE defaultdb.foo ADD COLUMN a INT8 AS (i + 1) STORED + redactedstatement: ALTER TABLE ‹defaultdb›.public.‹foo› ADD COLUMN ‹a› INT8 AS (‹i› + + ‹1›) STORED + statementtag: ALTER TABLE +PostCommitPhase stage 1 of 4 with 4 MutationType ops transitions: [[Column:{DescID: 104, ColumnID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY [[PrimaryIndex:{DescID: 104, IndexID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY @@ -581,6 +603,8 @@ PostCommitPhase stage 1 of 4 with 3 MutationType ops *scop.MakeAddedColumnDeleteAndWriteOnly ColumnID: 2 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob JobID: 1 PostCommitPhase stage 2 of 4 with 1 BackfillType ops @@ -598,7 +622,7 @@ PostCommitPhase stage 3 of 4 with 1 ValidationType ops *scop.ValidateUniqueIndex IndexID: 2 TableID: 104 -PostCommitPhase stage 4 of 4 with 7 MutationType ops +PostCommitPhase stage 4 of 4 with 8 MutationType ops transitions: [[Column:{DescID: 104, ColumnID: 2}, PUBLIC], DELETE_AND_WRITE_ONLY] -> PUBLIC [[ColumnName:{DescID: 104, ColumnID: 2, Name: a}, PUBLIC], ABSENT] -> PUBLIC @@ -628,15 +652,19 @@ PostCommitPhase stage 4 of 4 with 7 MutationType ops *scop.MakeColumnPublic ColumnID: 2 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob JobID: 1 -PostCommitNonRevertiblePhase stage 1 of 2 with 2 MutationType ops +PostCommitNonRevertiblePhase stage 1 of 2 with 3 MutationType ops transitions: [[PrimaryIndex:{DescID: 104, IndexID: 1}, ABSENT], VALIDATED] -> DELETE_ONLY ops: *scop.MakeDroppedIndexDeleteOnly IndexID: 1 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob IsNonCancelable: true JobID: 1 @@ -650,7 +678,7 @@ PostCommitNonRevertiblePhase stage 2 of 2 with 4 MutationType ops *scop.CreateGcJobForIndex IndexID: 1 TableID: 104 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 *scop.UpdateSchemaChangerJob @@ -757,25 +785,27 @@ PreCommitPhase stage 1 of 1 with 9 MutationType ops - 3 TableID: 105 Unique: true - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 105 - JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: ALTER TABLE defaultdb.foo ADD COLUMN a INT8 - redactedstatement: ALTER TABLE ‹defaultdb›.public.‹foo› ADD COLUMN ‹a› INT8 - statementtag: ALTER TABLE - - statement: ALTER TABLE defaultdb.bar ADD COLUMN b INT8 - redactedstatement: ALTER TABLE ‹defaultdb›.public.‹bar› ADD COLUMN ‹b› INT8 - statementtag: ALTER TABLE -PostCommitPhase stage 1 of 4 with 5 MutationType ops + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 + - 105 + JobID: 1 + Statements: + - statement: ALTER TABLE defaultdb.foo ADD COLUMN a INT8 + redactedstatement: ALTER TABLE ‹defaultdb›.public.‹foo› ADD COLUMN ‹a› INT8 + statementtag: ALTER TABLE + - statement: ALTER TABLE defaultdb.bar ADD COLUMN b INT8 + redactedstatement: ALTER TABLE ‹defaultdb›.public.‹bar› ADD COLUMN ‹b› INT8 + statementtag: ALTER TABLE +PostCommitPhase stage 1 of 4 with 7 MutationType ops transitions: [[Column:{DescID: 104, ColumnID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY [[PrimaryIndex:{DescID: 104, IndexID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY @@ -794,6 +824,10 @@ PostCommitPhase stage 1 of 4 with 5 MutationType ops *scop.MakeAddedColumnDeleteAndWriteOnly ColumnID: 3 TableID: 105 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 105 *scop.UpdateSchemaChangerJob JobID: 1 PostCommitPhase stage 2 of 4 with 2 BackfillType ops @@ -820,7 +854,7 @@ PostCommitPhase stage 3 of 4 with 2 ValidationType ops *scop.ValidateUniqueIndex IndexID: 2 TableID: 105 -PostCommitPhase stage 4 of 4 with 13 MutationType ops +PostCommitPhase stage 4 of 4 with 15 MutationType ops transitions: [[Column:{DescID: 104, ColumnID: 2}, PUBLIC], DELETE_AND_WRITE_ONLY] -> PUBLIC [[ColumnName:{DescID: 104, ColumnID: 2, Name: a}, PUBLIC], ABSENT] -> PUBLIC @@ -877,9 +911,13 @@ PostCommitPhase stage 4 of 4 with 13 MutationType ops *scop.MakeColumnPublic ColumnID: 3 TableID: 105 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 105 *scop.UpdateSchemaChangerJob JobID: 1 -PostCommitNonRevertiblePhase stage 1 of 2 with 3 MutationType ops +PostCommitNonRevertiblePhase stage 1 of 2 with 5 MutationType ops transitions: [[PrimaryIndex:{DescID: 104, IndexID: 1}, ABSENT], VALIDATED] -> DELETE_ONLY [[PrimaryIndex:{DescID: 105, IndexID: 1}, ABSENT], VALIDATED] -> DELETE_ONLY @@ -890,6 +928,10 @@ PostCommitNonRevertiblePhase stage 1 of 2 with 3 MutationType ops *scop.MakeDroppedIndexDeleteOnly IndexID: 1 TableID: 105 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 105 *scop.UpdateSchemaChangerJob IsNonCancelable: true JobID: 1 @@ -910,10 +952,10 @@ PostCommitNonRevertiblePhase stage 2 of 2 with 7 MutationType ops *scop.CreateGcJobForIndex IndexID: 1 TableID: 105 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 105 JobID: 1 *scop.UpdateSchemaChangerJob diff --git a/pkg/sql/schemachanger/scplan/testdata/create_index b/pkg/sql/schemachanger/scplan/testdata/create_index index e7046acd8312..3203efc9baa9 100644 --- a/pkg/sql/schemachanger/scplan/testdata/create_index +++ b/pkg/sql/schemachanger/scplan/testdata/create_index @@ -21,26 +21,29 @@ PreCommitPhase stage 1 of 1 with 3 MutationType ops StoreColumnIDs: - 3 TableID: 104 - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: CREATE INDEX id1 ON defaultdb.t1 (id, name) STORING (money) - redactedstatement: CREATE INDEX ‹id1› ON ‹defaultdb›.public.‹t1› (‹id›, ‹name›) - STORING (‹money›) - statementtag: CREATE INDEX -PostCommitPhase stage 1 of 4 with 2 MutationType ops + Statements: + - statement: CREATE INDEX id1 ON defaultdb.t1 (id, name) STORING (money) + redactedstatement: CREATE INDEX ‹id1› ON ‹defaultdb›.public.‹t1› (‹id›, ‹name›) + STORING (‹money›) + statementtag: CREATE INDEX +PostCommitPhase stage 1 of 4 with 3 MutationType ops transitions: [[SecondaryIndex:{DescID: 104, IndexID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY ops: *scop.MakeAddedIndexDeleteAndWriteOnly IndexID: 2 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob JobID: 1 PostCommitPhase stage 2 of 4 with 1 BackfillType ops @@ -70,7 +73,7 @@ PostCommitPhase stage 4 of 4 with 4 MutationType ops *scop.MakeAddedSecondaryIndexPublic IndexID: 2 TableID: 104 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 *scop.UpdateSchemaChangerJob @@ -109,27 +112,30 @@ PreCommitPhase stage 1 of 1 with 3 MutationType ops StoreColumnIDs: - 3 TableID: 104 - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: CREATE INVERTED INDEX CONCURRENTLY id1 ON defaultdb.t1 (id, name) STORING - (money) - redactedstatement: CREATE INVERTED INDEX CONCURRENTLY ‹id1› ON ‹defaultdb›.public.‹t1› - (‹id›, ‹name›) STORING (‹money›) - statementtag: CREATE INDEX -PostCommitPhase stage 1 of 4 with 2 MutationType ops + Statements: + - statement: CREATE INVERTED INDEX CONCURRENTLY id1 ON defaultdb.t1 (id, name) STORING + (money) + redactedstatement: CREATE INVERTED INDEX CONCURRENTLY ‹id1› ON ‹defaultdb›.public.‹t1› + (‹id›, ‹name›) STORING (‹money›) + statementtag: CREATE INDEX +PostCommitPhase stage 1 of 4 with 3 MutationType ops transitions: [[SecondaryIndex:{DescID: 104, IndexID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY ops: *scop.MakeAddedIndexDeleteAndWriteOnly IndexID: 2 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob JobID: 1 PostCommitPhase stage 2 of 4 with 1 BackfillType ops @@ -159,7 +165,7 @@ PostCommitPhase stage 4 of 4 with 4 MutationType ops *scop.MakeAddedSecondaryIndexPublic IndexID: 2 TableID: 104 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 *scop.UpdateSchemaChangerJob @@ -207,27 +213,30 @@ PreCommitPhase stage 1 of 1 with 4 MutationType ops - id RangePartitions: [] TableID: 104 - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: CREATE INDEX id1 ON defaultdb.t1 (id, name) STORING (money) PARTITION - BY LIST (id) (PARTITION p1 VALUES IN (1)) - redactedstatement: CREATE INDEX ‹id1› ON ‹defaultdb›.public.‹t1› (‹id›, ‹name›) - STORING (‹money›) PARTITION BY LIST (‹id›) (PARTITION ‹p1› VALUES IN (‹1›)) - statementtag: CREATE INDEX -PostCommitPhase stage 1 of 4 with 2 MutationType ops + Statements: + - statement: CREATE INDEX id1 ON defaultdb.t1 (id, name) STORING (money) PARTITION + BY LIST (id) (PARTITION p1 VALUES IN (1)) + redactedstatement: CREATE INDEX ‹id1› ON ‹defaultdb›.public.‹t1› (‹id›, ‹name›) + STORING (‹money›) PARTITION BY LIST (‹id›) (PARTITION ‹p1› VALUES IN (‹1›)) + statementtag: CREATE INDEX +PostCommitPhase stage 1 of 4 with 3 MutationType ops transitions: [[SecondaryIndex:{DescID: 104, IndexID: 2}, PUBLIC], DELETE_ONLY] -> DELETE_AND_WRITE_ONLY ops: *scop.MakeAddedIndexDeleteAndWriteOnly IndexID: 2 TableID: 104 + *scop.SetJobStateOnDescriptor + DescriptorID: 104 *scop.UpdateSchemaChangerJob JobID: 1 PostCommitPhase stage 2 of 4 with 1 BackfillType ops @@ -257,7 +266,7 @@ PostCommitPhase stage 4 of 4 with 4 MutationType ops *scop.MakeAddedSecondaryIndexPublic IndexID: 2 TableID: 104 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 *scop.UpdateSchemaChangerJob diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_database b/pkg/sql/schemachanger/scplan/testdata/drop_database index 57e80324aace..94c56ead6944 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_database +++ b/pkg/sql/schemachanger/scplan/testdata/drop_database @@ -378,57 +378,71 @@ PreCommitPhase stage 1 of 1 with 56 MutationType ops *scop.DeleteDatabaseSchemaEntry DatabaseID: 104 SchemaID: 106 - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 105 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 106 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 107 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 108 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 109 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 110 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 111 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 112 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 113 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 114 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 115 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 116 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 117 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 + - 105 + - 106 + - 107 + - 108 + - 109 + - 110 + - 111 + - 112 + - 113 + - 114 + - 115 + - 116 + - 117 JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: DROP DATABASE db1 CASCADE - redactedstatement: DROP DATABASE ‹db1› CASCADE - statementtag: DROP DATABASE + Statements: + - statement: DROP DATABASE db1 CASCADE + redactedstatement: DROP DATABASE ‹db1› CASCADE + statementtag: DROP DATABASE PostCommitNonRevertiblePhase stage 1 of 1 with 46 MutationType ops transitions: [[Sequence:{DescID: 107}, ABSENT], DROPPED] -> ABSENT @@ -672,46 +686,46 @@ PostCommitNonRevertiblePhase stage 1 of 1 with 46 MutationType ops TargetStatus: 1 *scop.CreateGcJobForDatabase DatabaseID: 104 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 105 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 106 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 107 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 108 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 109 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 110 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 111 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 112 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 113 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 114 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 115 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 116 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 117 JobID: 1 *scop.UpdateSchemaChangerJob diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_schema b/pkg/sql/schemachanger/scplan/testdata/drop_schema index 95034edab7d9..6cde404746bf 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_schema +++ b/pkg/sql/schemachanger/scplan/testdata/drop_schema @@ -622,48 +622,59 @@ PreCommitPhase stage 1 of 1 with 43 MutationType ops *scop.RemoveTypeBackRef DescID: 113 TypeID: 112 - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 100 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 104 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 105 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 106 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 107 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 108 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 109 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 110 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 111 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 112 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 113 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 100 + - 104 + - 105 + - 106 + - 107 + - 108 + - 109 + - 110 + - 111 + - 112 + - 113 JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: DROP SCHEMA defaultdb.sc1 CASCADE - redactedstatement: DROP SCHEMA ‹defaultdb›.‹sc1› CASCADE - statementtag: DROP SCHEMA + Statements: + - statement: DROP SCHEMA defaultdb.sc1 CASCADE + redactedstatement: DROP SCHEMA ‹defaultdb›.‹sc1› CASCADE + statementtag: DROP SCHEMA PostCommitNonRevertiblePhase stage 1 of 1 with 33 MutationType ops transitions: [[Sequence:{DescID: 105}, ABSENT], DROPPED] -> ABSENT @@ -833,37 +844,37 @@ PostCommitNonRevertiblePhase stage 1 of 1 with 33 MutationType ops TargetStatus: 1 *scop.DeleteDescriptor DescriptorID: 104 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 100 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 105 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 106 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 107 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 108 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 109 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 110 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 111 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 112 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 113 JobID: 1 *scop.UpdateSchemaChangerJob diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_sequence b/pkg/sql/schemachanger/scplan/testdata/drop_sequence index 00a28a315081..52677fff0ce8 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_sequence +++ b/pkg/sql/schemachanger/scplan/testdata/drop_sequence @@ -28,18 +28,19 @@ PreCommitPhase stage 1 of 1 with 4 MutationType ops DescID: 104 *scop.DrainDescriptorName TableID: 104 - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: DROP SEQUENCE defaultdb.sq1 CASCADE - redactedstatement: DROP SEQUENCE ‹defaultdb›.public.‹sq1› CASCADE - statementtag: DROP SEQUENCE + Statements: + - statement: DROP SEQUENCE defaultdb.sq1 CASCADE + redactedstatement: DROP SEQUENCE ‹defaultdb›.public.‹sq1› CASCADE + statementtag: DROP SEQUENCE PostCommitNonRevertiblePhase stage 1 of 1 with 4 MutationType ops transitions: [[Sequence:{DescID: 104}, ABSENT], DROPPED] -> ABSENT @@ -58,7 +59,7 @@ PostCommitNonRevertiblePhase stage 1 of 1 with 4 MutationType ops TargetStatus: 1 *scop.CreateGcJobForTable TableID: 104 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 *scop.UpdateSchemaChangerJob @@ -125,24 +126,27 @@ PreCommitPhase stage 1 of 1 with 12 MutationType ops SequenceIDs: - 104 TableID: 106 - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 105 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 106 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 + - 105 + - 106 JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: DROP SEQUENCE defaultdb.sq1 CASCADE - redactedstatement: DROP SEQUENCE ‹defaultdb›.public.‹sq1› CASCADE - statementtag: DROP SEQUENCE + Statements: + - statement: DROP SEQUENCE defaultdb.sq1 CASCADE + redactedstatement: DROP SEQUENCE ‹defaultdb›.public.‹sq1› CASCADE + statementtag: DROP SEQUENCE PostCommitNonRevertiblePhase stage 1 of 1 with 6 MutationType ops transitions: [[Sequence:{DescID: 104}, ABSENT], DROPPED] -> ABSENT @@ -161,13 +165,13 @@ PostCommitNonRevertiblePhase stage 1 of 1 with 6 MutationType ops TargetStatus: 1 *scop.CreateGcJobForTable TableID: 104 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 105 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 106 JobID: 1 *scop.UpdateSchemaChangerJob diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_table b/pkg/sql/schemachanger/scplan/testdata/drop_table index 16471b40bfd4..5865c5bbee01 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_table +++ b/pkg/sql/schemachanger/scplan/testdata/drop_table @@ -181,33 +181,39 @@ PreCommitPhase stage 1 of 1 with 22 MutationType ops *scop.RemoveRelationDependedOnBy DependedOnBy: 109 TableID: 107 - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 105 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 106 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 107 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 108 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 109 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 + - 105 + - 106 + - 107 + - 108 + - 109 JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: DROP TABLE defaultdb.shipments CASCADE - redactedstatement: DROP TABLE ‹defaultdb›.public.‹shipments› CASCADE - statementtag: DROP TABLE + Statements: + - statement: DROP TABLE defaultdb.shipments CASCADE + redactedstatement: DROP TABLE ‹defaultdb›.public.‹shipments› CASCADE + statementtag: DROP TABLE PostCommitNonRevertiblePhase stage 1 of 1 with 13 MutationType ops transitions: [[Table:{DescID: 107}, ABSENT], DROPPED] -> ABSENT @@ -262,22 +268,22 @@ PostCommitNonRevertiblePhase stage 1 of 1 with 13 MutationType ops TargetStatus: 1 *scop.CreateGcJobForTable TableID: 108 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 105 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 106 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 107 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 108 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 109 JobID: 1 *scop.UpdateSchemaChangerJob diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_type b/pkg/sql/schemachanger/scplan/testdata/drop_type index 3ff4c29a11bb..4ac01c9bc82d 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_type +++ b/pkg/sql/schemachanger/scplan/testdata/drop_type @@ -29,21 +29,23 @@ PreCommitPhase stage 1 of 1 with 7 MutationType ops DescID: 105 *scop.DrainDescriptorName TableID: 105 - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 105 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 + - 105 JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: DROP TYPE defaultdb.typ - redactedstatement: DROP TYPE ‹defaultdb›.‹public›.‹typ› - statementtag: DROP TYPE + Statements: + - statement: DROP TYPE defaultdb.typ + redactedstatement: DROP TYPE ‹defaultdb›.‹public›.‹typ› + statementtag: DROP TYPE PostCommitNonRevertiblePhase stage 1 of 1 with 7 MutationType ops transitions: [[Type:{DescID: 104}, ABSENT], DROPPED] -> ABSENT @@ -77,10 +79,10 @@ PostCommitNonRevertiblePhase stage 1 of 1 with 7 MutationType ops TargetStatus: 1 *scop.DeleteDescriptor DescriptorID: 105 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 105 JobID: 1 *scop.UpdateSchemaChangerJob diff --git a/pkg/sql/schemachanger/scplan/testdata/drop_view b/pkg/sql/schemachanger/scplan/testdata/drop_view index 61047c106f1c..0c51954384fa 100644 --- a/pkg/sql/schemachanger/scplan/testdata/drop_view +++ b/pkg/sql/schemachanger/scplan/testdata/drop_view @@ -36,21 +36,23 @@ PreCommitPhase stage 1 of 1 with 6 MutationType ops *scop.RemoveRelationDependedOnBy DependedOnBy: 105 TableID: 104 - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 105 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 + - 105 JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: DROP VIEW defaultdb.v1 - redactedstatement: DROP VIEW ‹defaultdb›.public.‹v1› - statementtag: DROP VIEW + Statements: + - statement: DROP VIEW defaultdb.v1 + redactedstatement: DROP VIEW ‹defaultdb›.public.‹v1› + statementtag: DROP VIEW PostCommitNonRevertiblePhase stage 1 of 1 with 5 MutationType ops transitions: [[View:{DescID: 105}, ABSENT], DROPPED] -> ABSENT @@ -69,10 +71,10 @@ PostCommitNonRevertiblePhase stage 1 of 1 with 5 MutationType ops TargetStatus: 1 *scop.CreateGcJobForTable TableID: 105 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 105 JobID: 1 *scop.UpdateSchemaChangerJob @@ -261,33 +263,39 @@ PreCommitPhase stage 1 of 1 with 25 MutationType ops *scop.RemoveRelationDependedOnBy DependedOnBy: 111 TableID: 108 - *scop.AddJobReference + *scop.SetJobStateOnDescriptor DescriptorID: 104 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 105 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 106 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 107 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 108 - JobID: 1 - *scop.AddJobReference + Initialize: true + *scop.SetJobStateOnDescriptor DescriptorID: 111 + Initialize: true + *scop.CreateSchemaChangerJob + Authorization: + UserName: root + DescriptorIDs: + - 104 + - 105 + - 106 + - 107 + - 108 + - 111 JobID: 1 - *scop.CreateDeclarativeSchemaChangerJob - JobID: 1 - TargetState: - Authorization: - UserName: root - Statements: - - statement: DROP VIEW defaultdb.v1 CASCADE - redactedstatement: DROP VIEW ‹defaultdb›.public.‹v1› CASCADE - statementtag: DROP VIEW + Statements: + - statement: DROP VIEW defaultdb.v1 CASCADE + redactedstatement: DROP VIEW ‹defaultdb›.public.‹v1› CASCADE + statementtag: DROP VIEW PostCommitNonRevertiblePhase stage 1 of 1 with 17 MutationType ops transitions: [[View:{DescID: 105}, ABSENT], DROPPED] -> ABSENT @@ -366,22 +374,22 @@ PostCommitNonRevertiblePhase stage 1 of 1 with 17 MutationType ops TargetStatus: 1 *scop.CreateGcJobForTable TableID: 111 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 104 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 105 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 106 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 107 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 108 JobID: 1 - *scop.RemoveJobReference + *scop.RemoveJobStateFromDescriptor DescriptorID: 111 JobID: 1 *scop.UpdateSchemaChangerJob diff --git a/pkg/sql/schemachanger/screl/scalars.go b/pkg/sql/schemachanger/screl/scalars.go index b861e0c3d38e..cbf14c5242ee 100644 --- a/pkg/sql/schemachanger/screl/scalars.go +++ b/pkg/sql/schemachanger/screl/scalars.go @@ -31,7 +31,7 @@ func GetDescID(e scpb.Element) descpb.ID { } // GetDescIDs returns the descriptor IDs referenced in the state's elements. -func GetDescIDs(s scpb.TargetState) descpb.IDs { +func GetDescIDs(s scpb.TargetState) catalog.DescriptorIDSet { descIDSet := catalog.MakeDescriptorIDSet() for i := range s.Targets { // Depending on the element type either a single descriptor ID @@ -40,5 +40,5 @@ func GetDescIDs(s scpb.TargetState) descpb.IDs { descIDSet.Add(id) } } - return descIDSet.Ordered() + return descIDSet } diff --git a/pkg/sql/schemachanger/scrun/BUILD.bazel b/pkg/sql/schemachanger/scrun/BUILD.bazel index c3120428054d..3422938cfa66 100644 --- a/pkg/sql/schemachanger/scrun/BUILD.bazel +++ b/pkg/sql/schemachanger/scrun/BUILD.bazel @@ -12,11 +12,13 @@ go_library( deps = [ "//pkg/jobs/jobspb", "//pkg/settings/cluster", + "//pkg/sql/catalog", + "//pkg/sql/catalog/descpb", "//pkg/sql/schemachanger/scexec", "//pkg/sql/schemachanger/scop", "//pkg/sql/schemachanger/scpb", "//pkg/sql/schemachanger/scplan", - "//pkg/util/log/logcrash", + "//pkg/util/log", "@com_github_cockroachdb_errors//:errors", ], ) diff --git a/pkg/sql/schemachanger/scrun/scrun.go b/pkg/sql/schemachanger/scrun/scrun.go index d3c177d70987..c7b82194f187 100644 --- a/pkg/sql/schemachanger/scrun/scrun.go +++ b/pkg/sql/schemachanger/scrun/scrun.go @@ -12,14 +12,17 @@ package scrun import ( "context" + "sort" "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" "github.com/cockroachdb/cockroach/pkg/settings/cluster" + "github.com/cockroachdb/cockroach/pkg/sql/catalog" + "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scpb" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scplan" - "github.com/cockroachdb/cockroach/pkg/util/log/logcrash" + "github.com/cockroachdb/cockroach/pkg/util/log" "github.com/cockroachdb/errors" ) @@ -81,11 +84,13 @@ func RunSchemaChangesInJob( settings *cluster.Settings, deps JobRunDependencies, jobID jobspb.JobID, - jobDetails jobspb.NewSchemaChangeDetails, - jobProgress jobspb.NewSchemaChangeProgress, + descriptorIDs []descpb.ID, rollback bool, ) error { - state := makeState(ctx, settings, jobDetails.TargetState, jobProgress.Current, rollback) + state, err := makeState(ctx, settings, deps, descriptorIDs, rollback) + if err != nil { + return errors.Wrapf(err, "failed to construct state for job %d", jobID) + } sc, err := scplan.MakePlan(state, scplan.Params{ ExecutionPhase: scop.PostCommitPhase, SchemaChangerJobIDSupplier: func() jobspb.JobID { return jobID }, @@ -118,24 +123,118 @@ func executeStage( return err } } + + log.Infof(ctx, "executing stage %d/%d in phase %v, %d ops of type %s", stage.Ordinal, stage.StagesInPhase, stage.Phase, len(stage.Ops()), stage.Ops()[0].Type()) if err := scexec.ExecuteStage(ctx, deps, stage.Ops()); err != nil { return errors.Wrapf(p.DecorateErrorWithPlanDetails(err), "error executing %s", stage.String()) } return nil } +type stateAndRanks struct { + *scpb.CurrentState + ranks []uint32 +} + +var _ sort.Interface = (*stateAndRanks)(nil) + +func (s *stateAndRanks) Len() int { return len(s.Targets) } +func (s *stateAndRanks) Less(i, j int) bool { return s.ranks[i] < s.ranks[j] } +func (s *stateAndRanks) Swap(i, j int) { + s.ranks[i], s.ranks[j] = s.ranks[j], s.ranks[i] + s.Targets[i], s.Targets[j] = s.Targets[j], s.Targets[i] + s.Current[i], s.Current[j] = s.Current[j], s.Current[i] +} + +type stmtsAndRanks struct { + stmts []scpb.Statement + ranks []uint32 +} + +func (s *stmtsAndRanks) Len() int { return len(s.stmts) } +func (s *stmtsAndRanks) Less(i, j int) bool { return s.ranks[i] < s.ranks[j] } +func (s stmtsAndRanks) Swap(i, j int) { + s.ranks[i], s.ranks[j] = s.ranks[j], s.ranks[i] + s.stmts[i], s.stmts[j] = s.stmts[j], s.stmts[i] +} + +var _ sort.Interface = (*stmtsAndRanks)(nil) + func makeState( ctx context.Context, sv *cluster.Settings, - targetState scpb.TargetState, - incumbent []scpb.Status, + deps JobRunDependencies, + descriptors []descpb.ID, rollback bool, -) scpb.CurrentState { - if len(targetState.Targets) != len(incumbent) { - logcrash.ReportOrPanic(ctx, &sv.SV, "unexpected slice size mismatch %d and %d", - len(targetState.Targets), len(incumbent)) +) (scpb.CurrentState, error) { + var s scpb.CurrentState + var targetRanks []uint32 + var stmts map[uint32]scpb.Statement + if err := deps.WithTxnInJob(ctx, func(ctx context.Context, txnDeps scexec.Dependencies) error { + + // Reset for restarts. + s = scpb.CurrentState{} + targetRanks = nil + stmts = make(map[uint32]scpb.Statement) + + // TODO(ajwerner): This would be a great place for a batched read. + for _, id := range descriptors { + // TODO(ajwerner): Verify that the job ID matches on all of the + // descriptors. Also verify that the Authorization matches. + + desc, err := txnDeps.Catalog().MustReadImmutableDescriptor(ctx, id) + + // If the descriptor was not found, it's not exactly clear what to do. + // Probably this means that deleting descriptors needs to be the very + // last step of a schema change. + // + // TODO(ajwerner): Sort this out. + if errors.Is(err, catalog.ErrDescriptorNotFound) { + return err + } + if err != nil { + return err + } + cs := desc.GetDeclarativeSchemaChangerState() + if cs == nil { + return errors.Errorf( + "descriptor %d does not contain schema changer state", id, + ) + } + s.Current = append(s.Current, cs.CurrentStatuses...) + s.Targets = append(s.Targets, cs.Targets...) + targetRanks = append(targetRanks, cs.TargetRanks...) + for _, stmt := range cs.RelevantStatements { + if existing, ok := stmts[stmt.StatementRank]; ok { + if existing.Statement != stmt.Statement.Statement { + return errors.AssertionFailedf( + "job %d: statement %q does not match %q for rank %d", + cs.JobID, + existing.Statement, + stmt.Statement, + stmt.StatementRank, + ) + } + } + stmts[stmt.StatementRank] = stmt.Statement + } + s.Authorization = cs.Authorization + } + return nil + }); err != nil { + return scpb.CurrentState{}, err + } + sort.Sort(&stateAndRanks{ + CurrentState: &s, + ranks: targetRanks, + }) + var sr stmtsAndRanks + for rank, stmt := range stmts { + sr.stmts = append(sr.stmts, stmt) + sr.ranks = append(sr.ranks, rank) } - s := scpb.CurrentState{TargetState: targetState, Current: incumbent} + sort.Sort(&sr) + s.Statements = sr.stmts if rollback { for i := range s.Targets { t := &s.Targets[i] @@ -147,5 +246,5 @@ func makeState( } } } - return s + return s, nil } diff --git a/pkg/sql/schemachanger/testdata/alter_table_add_column b/pkg/sql/schemachanger/testdata/alter_table_add_column index 59ada409de77..0f41c4022530 100644 --- a/pkg/sql/schemachanger/testdata/alter_table_add_column +++ b/pkg/sql/schemachanger/testdata/alter_table_add_column @@ -25,7 +25,102 @@ upsert descriptor #106 wallTime: "1" + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DELETE_ONLY + + - ABSENT + + - DELETE_ONLY + + - ABSENT + + - PUBLIC + + - PUBLIC + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: ALTER TABLE ‹db›.‹public›.‹tbl› ADD COLUMN ‹j› INT8 NOT + + NULL DEFAULT ‹42› + + statement: ALTER TABLE db.public.tbl ADD COLUMN j INT8 NOT NULL DEFAULT 42 + + statementTag: ALTER TABLE + + targetRanks: + + - 0 + + - 1 + + - 2 + + - 3 + + - 4 + + - 5 + + targets: + + - elementProto: + + column: + + columnId: 2 + + defaultExpr: 42:::INT8 + + familyName: primary + + pgAttributeNum: 2 + + tableId: 106 + + type: + + family: IntFamily + + oid: 20 + + width: 64 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: PUBLIC + + - elementProto: + + columnName: + + columnId: 2 + + name: j + + tableId: 106 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: PUBLIC + + - elementProto: + + primaryIndex: + + indexId: 2 + + keyColumnDirections: + + - ASC + + keyColumnIds: + + - 1 + + shardedDescriptor: {} + + sourceIndexId: 1 + + storingColumnIds: + + - 2 + + tableId: 106 + + unique: true + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: PUBLIC + + - elementProto: + + indexName: + + indexId: 2 + + name: tbl_pkey + + tableId: 106 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: PUBLIC + + - elementProto: + + primaryIndex: + + indexId: 1 + + keyColumnDirections: + + - ASC + + keyColumnIds: + + - 1 + + shardedDescriptor: {} + + sourceIndexId: 1 + + tableId: 106 + + unique: true + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + indexName: + + indexId: 1 + + name: tbl_pkey + + tableId: 106 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT families: - columnIds: - 1 @@ -95,9 +190,21 @@ upsert descriptor #106 commit transaction #1 # begin PostCommitPhase begin transaction #2 -## PostCommitPhase stage 1 of 4 with 3 MutationType ops +commit transaction #2 +begin transaction #3 +## PostCommitPhase stage 1 of 4 with 4 MutationType ops update progress of schema change job #1 upsert descriptor #106 + ... + authorization: {} + currentStatuses: + - - DELETE_ONLY + + - DELETE_AND_WRITE_ONLY + - ABSENT + - - DELETE_ONLY + + - DELETE_AND_WRITE_ONLY + - ABSENT + - PUBLIC ... direction: ADD mutationId: 1 @@ -117,17 +224,17 @@ upsert descriptor #106 unexposedParentSchemaId: 105 - version: "2" + version: "3" -commit transaction #2 -begin transaction #3 -## PostCommitPhase stage 2 of 4 with 1 BackfillType ops -backfill indexes [2] from index #1 in table #106 commit transaction #3 begin transaction #4 -## PostCommitPhase stage 3 of 4 with 1 ValidationType ops -validate forward indexes [2] in table #106 +## PostCommitPhase stage 2 of 4 with 1 BackfillType ops +backfill indexes [2] from index #1 in table #106 commit transaction #4 begin transaction #5 -## PostCommitPhase stage 4 of 4 with 7 MutationType ops +## PostCommitPhase stage 3 of 4 with 1 ValidationType ops +validate forward indexes [2] in table #106 +commit transaction #5 +begin transaction #6 +## PostCommitPhase stage 4 of 4 with 8 MutationType ops update progress of schema change job #1 upsert descriptor #106 ... @@ -143,6 +250,21 @@ upsert descriptor #106 + width: 64 createAsOfTime: wallTime: "1" + ... + authorization: {} + currentStatuses: + - - DELETE_AND_WRITE_ONLY + - - ABSENT + - - DELETE_AND_WRITE_ONLY + - - ABSENT + - PUBLIC + - PUBLIC + + - PUBLIC + + - PUBLIC + + - VALIDATED + + - ABSENT + jobId: "1" + relevantStatements: ... columnNames: - i @@ -220,12 +342,19 @@ upsert descriptor #106 unexposedParentSchemaId: 105 - version: "3" + version: "4" -commit transaction #5 -begin transaction #6 -## PostCommitNonRevertiblePhase stage 1 of 2 with 2 MutationType ops +commit transaction #6 +begin transaction #7 +## PostCommitNonRevertiblePhase stage 1 of 2 with 3 MutationType ops update progress of schema change job #1 set schema change job #1 to non-cancellable upsert descriptor #106 + ... + - PUBLIC + - PUBLIC + - - VALIDATED + + - DELETE_ONLY + - ABSENT + jobId: "1" ... version: 4 mutationId: 1 @@ -238,8 +367,8 @@ upsert descriptor #106 unexposedParentSchemaId: 105 - version: "4" + version: "5" -commit transaction #6 -begin transaction #7 +commit transaction #7 +begin transaction #8 ## PostCommitNonRevertiblePhase stage 2 of 2 with 4 MutationType ops create job #2: "GC for dropping table 106 index 1" descriptor IDs: [106] @@ -250,7 +379,102 @@ upsert descriptor #106 wallTime: "1" - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - PUBLIC + - - PUBLIC + - - PUBLIC + - - PUBLIC + - - DELETE_ONLY + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: ALTER TABLE ‹db›.‹public›.‹tbl› ADD COLUMN ‹j› INT8 NOT + - NULL DEFAULT ‹42› + - statement: ALTER TABLE db.public.tbl ADD COLUMN j INT8 NOT NULL DEFAULT 42 + - statementTag: ALTER TABLE + - targetRanks: + - - 0 + - - 1 + - - 2 + - - 3 + - - 4 + - - 5 + - targets: + - - elementProto: + - column: + - columnId: 2 + - defaultExpr: 42:::INT8 + - familyName: primary + - pgAttributeNum: 2 + - tableId: 106 + - type: + - family: IntFamily + - oid: 20 + - width: 64 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: PUBLIC + - - elementProto: + - columnName: + - columnId: 2 + - name: j + - tableId: 106 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: PUBLIC + - - elementProto: + - primaryIndex: + - indexId: 2 + - keyColumnDirections: + - - ASC + - keyColumnIds: + - - 1 + - shardedDescriptor: {} + - sourceIndexId: 1 + - storingColumnIds: + - - 2 + - tableId: 106 + - unique: true + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: PUBLIC + - - elementProto: + - indexName: + - indexId: 2 + - name: tbl_pkey + - tableId: 106 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: PUBLIC + - - elementProto: + - primaryIndex: + - indexId: 1 + - keyColumnDirections: + - - ASC + - keyColumnIds: + - - 1 + - shardedDescriptor: {} + - sourceIndexId: 1 + - tableId: 106 + - unique: true + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - indexName: + - indexId: 1 + - name: tbl_pkey + - tableId: 106 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT families: - columnIds: ... @@ -287,5 +511,5 @@ upsert descriptor #106 unexposedParentSchemaId: 105 - version: "5" + version: "6" -commit transaction #7 +commit transaction #8 # end PostCommitPhase diff --git a/pkg/sql/schemachanger/testdata/drop b/pkg/sql/schemachanger/testdata/drop index e6cd5e7b3f5c..1df608344d7a 100644 --- a/pkg/sql/schemachanger/testdata/drop +++ b/pkg/sql/schemachanger/testdata/drop @@ -24,7 +24,25 @@ upsert descriptor #104 database: + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP SCHEMA ‹db›.‹sc› + + statement: DROP SCHEMA db.sc + + statementTag: DROP SCHEMA + + targetRanks: + + - 1 + + targets: + + - elementProto: + + schemaEntry: + + databaseId: 104 + + schemaId: 106 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT id: 104 modificationTime: {} ... @@ -38,7 +56,35 @@ upsert descriptor #106 schema: + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP SCHEMA ‹db›.‹sc› + + statement: DROP SCHEMA db.sc + + statementTag: DROP SCHEMA + + targetRanks: + + - 0 + + - 2 + + targets: + + - elementProto: + + schema: + + schemaId: 106 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + schemaComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + schemaId: 106 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT id: 106 modificationTime: {} ... @@ -51,6 +97,8 @@ upsert descriptor #106 commit transaction #1 # begin PostCommitPhase begin transaction #2 +commit transaction #2 +begin transaction #3 ## PostCommitNonRevertiblePhase stage 1 of 1 with 6 MutationType ops write *eventpb.DropSchema to event log for descriptor #106: DROP SCHEMA ‹db›.‹sc› update progress of schema change job #1 @@ -60,7 +108,25 @@ upsert descriptor #104 database: - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP SCHEMA ‹db›.‹sc› + - statement: DROP SCHEMA db.sc + - statementTag: DROP SCHEMA + - targetRanks: + - - 1 + - targets: + - - elementProto: + - schemaEntry: + - databaseId: 104 + - schemaId: 106 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT id: 104 modificationTime: {} ... @@ -69,7 +135,7 @@ upsert descriptor #104 - version: "3" + version: "4" delete descriptor #106 -commit transaction #2 +commit transaction #3 # end PostCommitPhase setup @@ -107,7 +173,232 @@ upsert descriptor #108 wallTime: "1" + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - DELETE_AND_WRITE_ONLY + + - ABSENT + + - ABSENT + + - DELETE_AND_WRITE_ONLY + + - ABSENT + + - VALIDATED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP TABLE ‹db›.‹sc›.‹t› + + statement: DROP TABLE db.sc.t + + statementTag: DROP TABLE + + targetRanks: + + - 0 + + - 1 + + - 2 + + - 3 + + - 4 + + - 5 + + - 6 + + - 7 + + - 8 + + - 9 + + - 10 + + - 11 + + - 12 + + - 13 + + - 14 + + - 15 + + - 16 + + - 17 + + targets: + + - elementProto: + + table: + + tableId: 108 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 104 + + descriptorId: 108 + + name: t + + schemaId: 107 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + owner: + + descriptorId: 108 + + owner: root + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 108 + + privileges: 2 + + username: admin + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 108 + + username: public + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 108 + + privileges: 2 + + username: root + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + tableComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 108 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnName: + + columnId: 1 + + name: k + + tableId: 108 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + column: + + columnId: 1 + + familyName: primary + + nullable: true + + pgAttributeNum: 1 + + tableId: 108 + + type: + + family: IntFamily + + oid: 20 + + width: 64 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnComment: + + columnId: 1 + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 108 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnName: + + columnId: 2 + + name: v + + tableId: 108 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + column: + + columnId: 2 + + familyName: primary + + nullable: true + + pgAttributeNum: 2 + + tableId: 108 + + type: + + family: StringFamily + + oid: 25 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnComment: + + columnId: 2 + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 108 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + primaryIndex: + + indexId: 1 + + keyColumnDirections: + + - ASC + + keyColumnIds: + + - 3 + + shardedDescriptor: {} + + sourceIndexId: 1 + + storingColumnIds: + + - 1 + + - 2 + + tableId: 108 + + unique: true + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + indexName: + + indexId: 1 + + name: t_pkey + + tableId: 108 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + constraintComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + constraintId: 1 + + tableId: 108 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + indexComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + indexId: 1 + + tableId: 108 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + locality: + + descriptorId: 108 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT families: - columnIds: ... @@ -121,6 +412,8 @@ upsert descriptor #108 commit transaction #1 # begin PostCommitPhase begin transaction #2 +commit transaction #2 +begin transaction #3 ## PostCommitNonRevertiblePhase stage 1 of 1 with 4 MutationType ops create job #2: "GC for dropping descriptor 108" descriptor IDs: [108] @@ -133,7 +426,232 @@ upsert descriptor #108 wallTime: "1" - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - DROPPED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - DELETE_AND_WRITE_ONLY + - - ABSENT + - - ABSENT + - - DELETE_AND_WRITE_ONLY + - - ABSENT + - - VALIDATED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP TABLE ‹db›.‹sc›.‹t› + - statement: DROP TABLE db.sc.t + - statementTag: DROP TABLE + - targetRanks: + - - 0 + - - 1 + - - 2 + - - 3 + - - 4 + - - 5 + - - 6 + - - 7 + - - 8 + - - 9 + - - 10 + - - 11 + - - 12 + - - 13 + - - 14 + - - 15 + - - 16 + - - 17 + - targets: + - - elementProto: + - table: + - tableId: 108 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - namespace: + - databaseId: 104 + - descriptorId: 108 + - name: t + - schemaId: 107 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - owner: + - descriptorId: 108 + - owner: root + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 108 + - privileges: 2 + - username: admin + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 108 + - username: public + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 108 + - privileges: 2 + - username: root + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - tableComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 108 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnName: + - columnId: 1 + - name: k + - tableId: 108 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - column: + - columnId: 1 + - familyName: primary + - nullable: true + - pgAttributeNum: 1 + - tableId: 108 + - type: + - family: IntFamily + - oid: 20 + - width: 64 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnComment: + - columnId: 1 + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 108 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnName: + - columnId: 2 + - name: v + - tableId: 108 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - column: + - columnId: 2 + - familyName: primary + - nullable: true + - pgAttributeNum: 2 + - tableId: 108 + - type: + - family: StringFamily + - oid: 25 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnComment: + - columnId: 2 + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 108 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - primaryIndex: + - indexId: 1 + - keyColumnDirections: + - - ASC + - keyColumnIds: + - - 3 + - shardedDescriptor: {} + - sourceIndexId: 1 + - storingColumnIds: + - - 1 + - - 2 + - tableId: 108 + - unique: true + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - indexName: + - indexId: 1 + - name: t_pkey + - tableId: 108 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - constraintComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - constraintId: 1 + - tableId: 108 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - indexComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - indexId: 1 + - tableId: 108 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - locality: + - descriptorId: 108 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT families: - columnIds: ... @@ -141,7 +659,7 @@ upsert descriptor #108 unexposedParentSchemaId: 107 - version: "2" + version: "3" -commit transaction #2 +commit transaction #3 # end PostCommitPhase test @@ -163,7 +681,25 @@ upsert descriptor #104 database: + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP SCHEMA ‹db›.‹sc› CASCADE + + statement: DROP SCHEMA db.sc CASCADE + + statementTag: DROP SCHEMA + + targetRanks: + + - 5 + + targets: + + - elementProto: + + schemaEntry: + + databaseId: 104 + + schemaId: 107 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT id: 104 modificationTime: {} ... @@ -177,7 +713,38 @@ upsert descriptor #107 schema: + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP SCHEMA ‹db›.‹sc› CASCADE + + statement: DROP SCHEMA db.sc CASCADE + + statementTag: DROP SCHEMA + + targetRanks: + + - 4 + + - 6 + + targets: + + - elementProto: + + schema: + + dependentObjects: + + - 109 + + - 110 + + schemaId: 107 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + schemaComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + schemaId: 107 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT id: 107 modificationTime: {} ... @@ -191,7 +758,36 @@ upsert descriptor #109 arrayTypeId: 110 + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP SCHEMA ‹db›.‹sc› CASCADE + + statement: DROP SCHEMA db.sc CASCADE + + statementTag: DROP SCHEMA + + targetRanks: + + - 0 + + - 1 + + targets: + + - elementProto: + + type: + + typeId: 109 + + metadata: + + sourceElementId: 2 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 104 + + descriptorId: 109 + + name: e + + schemaId: 107 + + metadata: + + sourceElementId: 2 + + subWorkId: 1 + + targetStatus: ABSENT enumMembers: - logicalRepresentation: a ... @@ -206,7 +802,36 @@ upsert descriptor #110 oid: 100110 + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP SCHEMA ‹db›.‹sc› CASCADE + + statement: DROP SCHEMA db.sc CASCADE + + statementTag: DROP SCHEMA + + targetRanks: + + - 2 + + - 3 + + targets: + + - elementProto: + + type: + + typeId: 110 + + metadata: + + sourceElementId: 2 + + subWorkId: 2 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 104 + + descriptorId: 110 + + name: _e + + schemaId: 107 + + metadata: + + sourceElementId: 2 + + subWorkId: 2 + + targetStatus: ABSENT id: 110 kind: ALIAS ... @@ -219,6 +844,8 @@ upsert descriptor #110 commit transaction #1 # begin PostCommitPhase begin transaction #2 +commit transaction #2 +begin transaction #3 ## PostCommitNonRevertiblePhase stage 1 of 1 with 12 MutationType ops write *eventpb.DropSchema to event log for descriptor #107: DROP SCHEMA ‹db›.‹sc› CASCADE write *eventpb.DropType to event log for descriptor #110: DROP SCHEMA ‹db›.‹sc› CASCADE @@ -229,7 +856,25 @@ upsert descriptor #104 database: - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP SCHEMA ‹db›.‹sc› CASCADE + - statement: DROP SCHEMA db.sc CASCADE + - statementTag: DROP SCHEMA + - targetRanks: + - - 5 + - targets: + - - elementProto: + - schemaEntry: + - databaseId: 104 + - schemaId: 107 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT id: 104 modificationTime: {} ... @@ -240,7 +885,7 @@ upsert descriptor #104 delete descriptor #107 delete descriptor #109 delete descriptor #110 -commit transaction #2 +commit transaction #3 # end PostCommitPhase test @@ -262,7 +907,57 @@ upsert descriptor #104 database: + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - ABSENT + + - DROPPED + + - ABSENT + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db› CASCADE + + statement: DROP DATABASE db CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 1 + + - 3 + + - 4 + + - 5 + + targets: + + - elementProto: + + schemaEntry: + + databaseId: 104 + + schemaId: 105 + + metadata: + + sourceElementId: 2 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + database: + + databaseId: 104 + + dependentObjects: + + - 105 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + databaseComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + databaseId: 104 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + databaseRoleSetting: + + databaseId: 104 + + roleName: TODO(fqazi) Roles are not currently fetched from database_role_settings + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT id: 104 modificationTime: {} ... @@ -279,7 +974,35 @@ upsert descriptor #105 schema: + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db› CASCADE + + statement: DROP DATABASE db CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 0 + + - 2 + + targets: + + - elementProto: + + schema: + + schemaId: 105 + + metadata: + + sourceElementId: 2 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + schemaComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + schemaId: 105 + + metadata: + + sourceElementId: 2 + + subWorkId: 1 + + targetStatus: ABSENT id: 105 modificationTime: {} ... @@ -292,6 +1015,8 @@ upsert descriptor #105 commit transaction #1 # begin PostCommitPhase begin transaction #2 +commit transaction #2 +begin transaction #3 ## PostCommitNonRevertiblePhase stage 1 of 1 with 9 MutationType ops create job #2: "GC for dropping descriptors and parent database 104" descriptor IDs: [] @@ -304,7 +1029,57 @@ upsert descriptor #104 database: - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - ABSENT + - - DROPPED + - - ABSENT + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP DATABASE ‹db› CASCADE + - statement: DROP DATABASE db CASCADE + - statementTag: DROP DATABASE + - targetRanks: + - - 1 + - - 3 + - - 4 + - - 5 + - targets: + - - elementProto: + - schemaEntry: + - databaseId: 104 + - schemaId: 105 + - metadata: + - sourceElementId: 2 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - database: + - databaseId: 104 + - dependentObjects: + - - 105 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - databaseComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - databaseId: 104 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - databaseRoleSetting: + - databaseId: 104 + - roleName: TODO(fqazi) Roles are not currently fetched from database_role_settings + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT id: 104 modificationTime: {} ... @@ -313,7 +1088,7 @@ upsert descriptor #104 - version: "6" + version: "7" delete descriptor #105 -commit transaction #2 +commit transaction #3 # end PostCommitPhase setup @@ -396,7 +1171,68 @@ upsert descriptor #111 database: + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - ABSENT + + - ABSENT + + - DROPPED + + - ABSENT + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 32 + + - 118 + + - 120 + + - 121 + + - 122 + + targets: + + - elementProto: + + schemaEntry: + + databaseId: 111 + + schemaId: 112 + + metadata: + + sourceElementId: 2 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + schemaEntry: + + databaseId: 111 + + schemaId: 113 + + metadata: + + sourceElementId: 2 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + database: + + databaseId: 111 + + dependentObjects: + + - 112 + + - 113 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + databaseComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + databaseId: 111 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + databaseRoleSetting: + + databaseId: 111 + + roleName: TODO(fqazi) Roles are not currently fetched from database_role_settings + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: ABSENT id: 111 modificationTime: {} ... @@ -415,7 +1251,38 @@ upsert descriptor #112 schema: + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 31 + + - 33 + + targets: + + - elementProto: + + schema: + + dependentObjects: + + - 114 + + - 117 + + schemaId: 112 + + metadata: + + sourceElementId: 2 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + schemaComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + schemaId: 112 + + metadata: + + sourceElementId: 2 + + subWorkId: 1 + + targetStatus: ABSENT id: 112 modificationTime: {} ... @@ -428,7 +1295,45 @@ upsert descriptor #113 schema: + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 117 + + - 119 + + targets: + + - elementProto: + + schema: + + dependentObjects: + + - 115 + + - 116 + + - 118 + + - 119 + + - 120 + + - 121 + + - 122 + + - 123 + + - 124 + + schemaId: 113 + + metadata: + + sourceElementId: 2 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + schemaComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + schemaId: 113 + + metadata: + + sourceElementId: 2 + + subWorkId: 1 + + targetStatus: ABSENT id: 113 modificationTime: {} ... @@ -444,11 +1349,112 @@ upsert descriptor #114 - dependedOnBy: - - byId: true - columnIds: - - - 3 - - id: 117 + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 0 + + - 1 + + - 2 + - 3 + - id: 117 + + - 4 + + - 5 + + - 6 + + - 7 + + - 8 + + targets: + + - elementProto: + + sequence: + + sequenceId: 114 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 111 + + descriptorId: 114 + + name: sq1 + + schemaId: 112 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + owner: + + descriptorId: 114 + + owner: root + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 114 + + privileges: 2 + + username: admin + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 114 + + username: public + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 114 + + privileges: 2 + + username: root + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + tableComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 114 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + locality: + + descriptorId: 114 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + relationDependedOnBy: + + columnId: 3 + + dependedOn: 117 + + tableId: 114 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT families: - columnIds: ... @@ -469,7 +1475,109 @@ upsert descriptor #115 - id: 116 + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 34 + + - 35 + + - 36 + + - 37 + + - 38 + + - 39 + + - 40 + + - 41 + + - 42 + + targets: + + - elementProto: + + sequence: + + sequenceId: 115 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 111 + + descriptorId: 115 + + name: sq1 + + schemaId: 113 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + owner: + + descriptorId: 115 + + owner: root + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 115 + + privileges: 2 + + username: admin + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 115 + + username: public + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 115 + + privileges: 2 + + username: root + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + tableComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 115 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + locality: + + descriptorId: 115 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + relationDependedOnBy: + + columnId: 3 + + dependedOn: 116 + + tableId: 115 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT families: - columnIds: ... @@ -501,7 +1609,300 @@ upsert descriptor #116 - id: 118 + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - ABSENT + + - DROPPED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - DELETE_AND_WRITE_ONLY + + - ABSENT + + - ABSENT + + - DELETE_AND_WRITE_ONLY + + - ABSENT + + - ABSENT + + - DELETE_AND_WRITE_ONLY + + - ABSENT + + - VALIDATED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 43 + + - 44 + + - 45 + + - 46 + + - 47 + + - 48 + + - 49 + + - 50 + + - 51 + + - 52 + + - 53 + + - 54 + + - 55 + + - 56 + + - 57 + + - 58 + + - 59 + + - 60 + + - 61 + + - 62 + + - 63 + + - 64 + + - 65 + + targets: + + - elementProto: + + defaultExpression: + + columnId: 3 + + defaultExpr: nextval(115:::REGCLASS) + + tableId: 116 + + usesSequenceIds: + + - 115 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + table: + + tableId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 111 + + descriptorId: 116 + + name: t1 + + schemaId: 113 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + owner: + + descriptorId: 116 + + owner: root + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 116 + + privileges: 2 + + username: admin + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 116 + + username: public + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 116 + + privileges: 2 + + username: root + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + tableComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnName: + + columnId: 1 + + name: id + + tableId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + column: + + columnId: 1 + + familyName: primary + + pgAttributeNum: 1 + + tableId: 116 + + type: + + family: IntFamily + + oid: 20 + + width: 64 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnComment: + + columnId: 1 + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnName: + + columnId: 2 + + name: name + + tableId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + column: + + columnId: 2 + + familyName: primary + + nullable: true + + pgAttributeNum: 2 + + tableId: 116 + + type: + + family: StringFamily + + oid: 1043 + + visibleType: 7 + + width: 256 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnComment: + + columnId: 2 + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnName: + + columnId: 3 + + name: val + + tableId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + column: + + columnId: 3 + + defaultExpr: nextval(115:::REGCLASS) + + familyName: primary + + nullable: true + + pgAttributeNum: 3 + + tableId: 116 + + type: + + family: IntFamily + + oid: 20 + + width: 64 + + usesSequenceIds: + + - 115 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnComment: + + columnId: 3 + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + primaryIndex: + + indexId: 1 + + keyColumnDirections: + + - ASC + + keyColumnIds: + + - 1 + + shardedDescriptor: {} + + sourceIndexId: 1 + + storingColumnIds: + + - 2 + + - 3 + + tableId: 116 + + unique: true + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + indexName: + + indexId: 1 + + name: t1_pkey + + tableId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + constraintComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + constraintId: 1 + + tableId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + indexComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + indexId: 1 + + tableId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + locality: + + descriptorId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + relationDependedOnBy: + + columnId: 2 + + dependedOn: 118 + + tableId: 116 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT families: - columnIds: ... @@ -529,7 +1930,289 @@ upsert descriptor #117 wallTime: "1" + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - ABSENT + + - DROPPED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - DELETE_AND_WRITE_ONLY + + - ABSENT + + - ABSENT + + - DELETE_AND_WRITE_ONLY + + - ABSENT + + - ABSENT + + - DELETE_AND_WRITE_ONLY + + - ABSENT + + - VALIDATED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 9 + + - 10 + + - 11 + + - 12 + + - 13 + + - 14 + + - 15 + + - 16 + + - 17 + + - 18 + + - 19 + + - 20 + + - 21 + + - 22 + + - 23 + + - 24 + + - 25 + + - 26 + + - 27 + + - 28 + + - 29 + + - 30 + + targets: + + - elementProto: + + defaultExpression: + + columnId: 3 + + defaultExpr: nextval(114:::REGCLASS) + + tableId: 117 + + usesSequenceIds: + + - 114 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + table: + + tableId: 117 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 111 + + descriptorId: 117 + + name: t1 + + schemaId: 112 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + owner: + + descriptorId: 117 + + owner: root + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 117 + + privileges: 2 + + username: admin + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 117 + + username: public + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 117 + + privileges: 2 + + username: root + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + tableComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 117 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnName: + + columnId: 1 + + name: id + + tableId: 117 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + column: + + columnId: 1 + + familyName: primary + + pgAttributeNum: 1 + + tableId: 117 + + type: + + family: IntFamily + + oid: 20 + + width: 64 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnComment: + + columnId: 1 + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 117 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnName: + + columnId: 2 + + name: name + + tableId: 117 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + column: + + columnId: 2 + + familyName: primary + + nullable: true + + pgAttributeNum: 2 + + tableId: 117 + + type: + + family: StringFamily + + oid: 1043 + + visibleType: 7 + + width: 256 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnComment: + + columnId: 2 + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 117 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnName: + + columnId: 3 + + name: val + + tableId: 117 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + column: + + columnId: 3 + + defaultExpr: nextval(114:::REGCLASS) + + familyName: primary + + nullable: true + + pgAttributeNum: 3 + + tableId: 117 + + type: + + family: IntFamily + + oid: 20 + + width: 64 + + usesSequenceIds: + + - 114 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + columnComment: + + columnId: 3 + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 117 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + primaryIndex: + + indexId: 1 + + keyColumnDirections: + + - ASC + + keyColumnIds: + + - 1 + + shardedDescriptor: {} + + sourceIndexId: 1 + + storingColumnIds: + + - 2 + + - 3 + + tableId: 117 + + unique: true + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + indexName: + + indexId: 1 + + name: t1_pkey + + tableId: 117 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + constraintComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + constraintId: 1 + + tableId: 117 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + indexComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + indexId: 1 + + tableId: 117 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + locality: + + descriptorId: 117 + + metadata: + + sourceElementId: 3 + + subWorkId: 1 + + targetStatus: ABSENT families: - columnIds: ... @@ -554,7 +2237,120 @@ upsert descriptor #118 - - 116 + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 66 + + - 67 + + - 68 + + - 69 + + - 70 + + - 71 + + - 72 + + - 73 + + - 74 + + - 75 + + targets: + + - elementProto: + + view: + + tableId: 118 + + metadata: + + sourceElementId: 8 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 111 + + descriptorId: 118 + + name: v1 + + schemaId: 113 + + metadata: + + sourceElementId: 8 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + owner: + + descriptorId: 118 + + owner: root + + metadata: + + sourceElementId: 8 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 118 + + privileges: 2 + + username: admin + + metadata: + + sourceElementId: 8 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 118 + + username: public + + metadata: + + sourceElementId: 8 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 118 + + privileges: 2 + + username: root + + metadata: + + sourceElementId: 8 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + tableComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 118 + + metadata: + + sourceElementId: 8 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + locality: + + descriptorId: 118 + + metadata: + + sourceElementId: 8 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + relationDependedOnBy: + + columnId: 1 + + dependedOn: 119 + + tableId: 118 + + metadata: + + sourceElementId: 8 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + relationDependedOnBy: + + columnId: 1 + + dependedOn: 120 + + tableId: 118 + + metadata: + + sourceElementId: 8 + + subWorkId: 1 + + targetStatus: ABSENT formatVersion: 3 id: 118 ... @@ -582,7 +2378,120 @@ upsert descriptor #119 - - 118 + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 76 + + - 77 + + - 78 + + - 79 + + - 80 + + - 81 + + - 82 + + - 83 + + - 84 + + - 85 + + targets: + + - elementProto: + + view: + + tableId: 119 + + metadata: + + sourceElementId: 9 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 111 + + descriptorId: 119 + + name: v2 + + schemaId: 113 + + metadata: + + sourceElementId: 9 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + owner: + + descriptorId: 119 + + owner: root + + metadata: + + sourceElementId: 9 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 119 + + privileges: 2 + + username: admin + + metadata: + + sourceElementId: 9 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 119 + + username: public + + metadata: + + sourceElementId: 9 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 119 + + privileges: 2 + + username: root + + metadata: + + sourceElementId: 9 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + tableComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 119 + + metadata: + + sourceElementId: 9 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + locality: + + descriptorId: 119 + + metadata: + + sourceElementId: 9 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + relationDependedOnBy: + + columnId: 1 + + dependedOn: 120 + + tableId: 119 + + metadata: + + sourceElementId: 9 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + relationDependedOnBy: + + columnId: 1 + + dependedOn: 121 + + tableId: 119 + + metadata: + + sourceElementId: 9 + + subWorkId: 1 + + targetStatus: ABSENT formatVersion: 3 id: 119 ... @@ -602,7 +2511,98 @@ upsert descriptor #120 - - 119 + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 86 + + - 87 + + - 88 + + - 89 + + - 90 + + - 91 + + - 92 + + - 93 + + targets: + + - elementProto: + + view: + + tableId: 120 + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 111 + + descriptorId: 120 + + name: v3 + + schemaId: 113 + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + owner: + + descriptorId: 120 + + owner: root + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 120 + + privileges: 2 + + username: admin + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 120 + + username: public + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 120 + + privileges: 2 + + username: root + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + tableComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 120 + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + locality: + + descriptorId: 120 + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT formatVersion: 3 id: 120 ... @@ -626,7 +2626,109 @@ upsert descriptor #121 - - 119 + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 94 + + - 95 + + - 96 + + - 97 + + - 98 + + - 99 + + - 100 + + - 101 + + - 102 + + targets: + + - elementProto: + + view: + + tableId: 121 + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 111 + + descriptorId: 121 + + name: v4 + + schemaId: 113 + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + owner: + + descriptorId: 121 + + owner: root + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 121 + + privileges: 2 + + username: admin + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 121 + + username: public + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 121 + + privileges: 2 + + username: root + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + tableComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 121 + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + locality: + + descriptorId: 121 + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + relationDependedOnBy: + + columnId: 1 + + dependedOn: 124 + + tableId: 121 + + metadata: + + sourceElementId: 10 + + subWorkId: 1 + + targetStatus: ABSENT formatVersion: 3 id: 121 ... @@ -642,7 +2744,36 @@ upsert descriptor #122 arrayTypeId: 123 + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 113 + + - 114 + + targets: + + - elementProto: + + type: + + typeId: 122 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 111 + + descriptorId: 122 + + name: typ + + schemaId: 113 + + metadata: + + sourceElementId: 6 + + subWorkId: 1 + + targetStatus: ABSENT enumMembers: - logicalRepresentation: a ... @@ -660,7 +2791,36 @@ upsert descriptor #123 oid: 100123 + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 115 + + - 116 + + targets: + + - elementProto: + + type: + + typeId: 123 + + metadata: + + sourceElementId: 6 + + subWorkId: 2 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 111 + + descriptorId: 123 + + name: _typ + + schemaId: 113 + + metadata: + + sourceElementId: 6 + + subWorkId: 2 + + targetStatus: ABSENT id: 123 kind: ALIAS ... @@ -680,7 +2840,118 @@ upsert descriptor #124 - - 121 + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DROPPED + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: DROP DATABASE ‹db1› CASCADE + + statement: DROP DATABASE db1 CASCADE + + statementTag: DROP DATABASE + + targetRanks: + + - 103 + + - 104 + + - 105 + + - 106 + + - 107 + + - 108 + + - 109 + + - 110 + + - 111 + + - 112 + + targets: + + - elementProto: + + view: + + tableId: 124 + + metadata: + + sourceElementId: 12 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + namespace: + + databaseId: 111 + + descriptorId: 124 + + name: v5 + + schemaId: 113 + + metadata: + + sourceElementId: 12 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + owner: + + descriptorId: 124 + + owner: root + + metadata: + + sourceElementId: 12 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 124 + + privileges: 2 + + username: admin + + metadata: + + sourceElementId: 12 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 124 + + username: public + + metadata: + + sourceElementId: 12 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + userPrivileges: + + descriptorId: 124 + + privileges: 2 + + username: root + + metadata: + + sourceElementId: 12 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + tableComment: + + comment: TODO(fqazi) Comments are not currently fetched from system.comments + + when doing decomposition + + tableId: 124 + + metadata: + + sourceElementId: 12 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + viewDependsOnType: + + tableId: 124 + + typeId: 122 + + metadata: + + sourceElementId: 12 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + viewDependsOnType: + + tableId: 124 + + typeId: 123 + + metadata: + + sourceElementId: 12 + + subWorkId: 1 + + targetStatus: ABSENT + + - elementProto: + + locality: + + descriptorId: 124 + + metadata: + + sourceElementId: 12 + + subWorkId: 1 + + targetStatus: ABSENT dependsOnTypes: - 122 ... @@ -695,6 +2966,8 @@ upsert descriptor #124 commit transaction #1 # begin PostCommitPhase begin transaction #2 +commit transaction #2 +begin transaction #3 ## PostCommitNonRevertiblePhase stage 1 of 1 with 46 MutationType ops create job #2: "GC for dropping descriptors 114 117 115 116 118 119 120 121 124 and parent database 111" descriptor IDs: [114 117 115 116 118 119 120 121 124] @@ -709,7 +2982,68 @@ upsert descriptor #111 database: - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - ABSENT + - - ABSENT + - - DROPPED + - - ABSENT + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP DATABASE ‹db1› CASCADE + - statement: DROP DATABASE db1 CASCADE + - statementTag: DROP DATABASE + - targetRanks: + - - 32 + - - 118 + - - 120 + - - 121 + - - 122 + - targets: + - - elementProto: + - schemaEntry: + - databaseId: 111 + - schemaId: 112 + - metadata: + - sourceElementId: 2 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - schemaEntry: + - databaseId: 111 + - schemaId: 113 + - metadata: + - sourceElementId: 2 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - database: + - databaseId: 111 + - dependentObjects: + - - 112 + - - 113 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - databaseComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - databaseId: 111 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - databaseRoleSetting: + - databaseId: 111 + - roleName: TODO(fqazi) Roles are not currently fetched from database_role_settings + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: ABSENT id: 111 modificationTime: {} ... @@ -723,7 +3057,109 @@ upsert descriptor #114 wallTime: "1" - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - DROPPED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP DATABASE ‹db1› CASCADE + - statement: DROP DATABASE db1 CASCADE + - statementTag: DROP DATABASE + - targetRanks: + - - 0 + - - 1 + - - 2 + - - 3 + - - 4 + - - 5 + - - 6 + - - 7 + - - 8 + - targets: + - - elementProto: + - sequence: + - sequenceId: 114 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - namespace: + - databaseId: 111 + - descriptorId: 114 + - name: sq1 + - schemaId: 112 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - owner: + - descriptorId: 114 + - owner: root + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 114 + - privileges: 2 + - username: admin + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 114 + - username: public + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 114 + - privileges: 2 + - username: root + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - tableComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 114 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - locality: + - descriptorId: 114 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - relationDependedOnBy: + - columnId: 3 + - dependedOn: 117 + - tableId: 114 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT families: - columnIds: ... @@ -737,7 +3173,109 @@ upsert descriptor #115 wallTime: "1" - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - DROPPED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP DATABASE ‹db1› CASCADE + - statement: DROP DATABASE db1 CASCADE + - statementTag: DROP DATABASE + - targetRanks: + - - 34 + - - 35 + - - 36 + - - 37 + - - 38 + - - 39 + - - 40 + - - 41 + - - 42 + - targets: + - - elementProto: + - sequence: + - sequenceId: 115 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - namespace: + - databaseId: 111 + - descriptorId: 115 + - name: sq1 + - schemaId: 113 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - owner: + - descriptorId: 115 + - owner: root + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 115 + - privileges: 2 + - username: admin + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 115 + - username: public + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 115 + - privileges: 2 + - username: root + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - tableComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 115 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - locality: + - descriptorId: 115 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - relationDependedOnBy: + - columnId: 3 + - dependedOn: 116 + - tableId: 115 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT families: - columnIds: ... @@ -751,7 +3289,300 @@ upsert descriptor #116 wallTime: "1" - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - ABSENT + - - DROPPED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - DELETE_AND_WRITE_ONLY + - - ABSENT + - - ABSENT + - - DELETE_AND_WRITE_ONLY + - - ABSENT + - - ABSENT + - - DELETE_AND_WRITE_ONLY + - - ABSENT + - - VALIDATED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP DATABASE ‹db1› CASCADE + - statement: DROP DATABASE db1 CASCADE + - statementTag: DROP DATABASE + - targetRanks: + - - 43 + - - 44 + - - 45 + - - 46 + - - 47 + - - 48 + - - 49 + - - 50 + - - 51 + - - 52 + - - 53 + - - 54 + - - 55 + - - 56 + - - 57 + - - 58 + - - 59 + - - 60 + - - 61 + - - 62 + - - 63 + - - 64 + - - 65 + - targets: + - - elementProto: + - defaultExpression: + - columnId: 3 + - defaultExpr: nextval(115:::REGCLASS) + - tableId: 116 + - usesSequenceIds: + - - 115 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - table: + - tableId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - namespace: + - databaseId: 111 + - descriptorId: 116 + - name: t1 + - schemaId: 113 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - owner: + - descriptorId: 116 + - owner: root + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 116 + - privileges: 2 + - username: admin + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 116 + - username: public + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 116 + - privileges: 2 + - username: root + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - tableComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnName: + - columnId: 1 + - name: id + - tableId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - column: + - columnId: 1 + - familyName: primary + - pgAttributeNum: 1 + - tableId: 116 + - type: + - family: IntFamily + - oid: 20 + - width: 64 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnComment: + - columnId: 1 + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnName: + - columnId: 2 + - name: name + - tableId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - column: + - columnId: 2 + - familyName: primary + - nullable: true + - pgAttributeNum: 2 + - tableId: 116 + - type: + - family: StringFamily + - oid: 1043 + - visibleType: 7 + - width: 256 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnComment: + - columnId: 2 + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnName: + - columnId: 3 + - name: val + - tableId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - column: + - columnId: 3 + - defaultExpr: nextval(115:::REGCLASS) + - familyName: primary + - nullable: true + - pgAttributeNum: 3 + - tableId: 116 + - type: + - family: IntFamily + - oid: 20 + - width: 64 + - usesSequenceIds: + - - 115 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnComment: + - columnId: 3 + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - primaryIndex: + - indexId: 1 + - keyColumnDirections: + - - ASC + - keyColumnIds: + - - 1 + - shardedDescriptor: {} + - sourceIndexId: 1 + - storingColumnIds: + - - 2 + - - 3 + - tableId: 116 + - unique: true + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - indexName: + - indexId: 1 + - name: t1_pkey + - tableId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - constraintComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - constraintId: 1 + - tableId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - indexComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - indexId: 1 + - tableId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - locality: + - descriptorId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - relationDependedOnBy: + - columnId: 2 + - dependedOn: 118 + - tableId: 116 + - metadata: + - sourceElementId: 6 + - subWorkId: 1 + - targetStatus: ABSENT families: - columnIds: ... @@ -765,7 +3596,289 @@ upsert descriptor #117 wallTime: "1" - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - ABSENT + - - DROPPED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - DELETE_AND_WRITE_ONLY + - - ABSENT + - - ABSENT + - - DELETE_AND_WRITE_ONLY + - - ABSENT + - - ABSENT + - - DELETE_AND_WRITE_ONLY + - - ABSENT + - - VALIDATED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP DATABASE ‹db1› CASCADE + - statement: DROP DATABASE db1 CASCADE + - statementTag: DROP DATABASE + - targetRanks: + - - 9 + - - 10 + - - 11 + - - 12 + - - 13 + - - 14 + - - 15 + - - 16 + - - 17 + - - 18 + - - 19 + - - 20 + - - 21 + - - 22 + - - 23 + - - 24 + - - 25 + - - 26 + - - 27 + - - 28 + - - 29 + - - 30 + - targets: + - - elementProto: + - defaultExpression: + - columnId: 3 + - defaultExpr: nextval(114:::REGCLASS) + - tableId: 117 + - usesSequenceIds: + - - 114 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - table: + - tableId: 117 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - namespace: + - databaseId: 111 + - descriptorId: 117 + - name: t1 + - schemaId: 112 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - owner: + - descriptorId: 117 + - owner: root + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 117 + - privileges: 2 + - username: admin + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 117 + - username: public + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 117 + - privileges: 2 + - username: root + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - tableComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 117 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnName: + - columnId: 1 + - name: id + - tableId: 117 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - column: + - columnId: 1 + - familyName: primary + - pgAttributeNum: 1 + - tableId: 117 + - type: + - family: IntFamily + - oid: 20 + - width: 64 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnComment: + - columnId: 1 + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 117 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnName: + - columnId: 2 + - name: name + - tableId: 117 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - column: + - columnId: 2 + - familyName: primary + - nullable: true + - pgAttributeNum: 2 + - tableId: 117 + - type: + - family: StringFamily + - oid: 1043 + - visibleType: 7 + - width: 256 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnComment: + - columnId: 2 + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 117 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnName: + - columnId: 3 + - name: val + - tableId: 117 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - column: + - columnId: 3 + - defaultExpr: nextval(114:::REGCLASS) + - familyName: primary + - nullable: true + - pgAttributeNum: 3 + - tableId: 117 + - type: + - family: IntFamily + - oid: 20 + - width: 64 + - usesSequenceIds: + - - 114 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - columnComment: + - columnId: 3 + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 117 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - primaryIndex: + - indexId: 1 + - keyColumnDirections: + - - ASC + - keyColumnIds: + - - 1 + - shardedDescriptor: {} + - sourceIndexId: 1 + - storingColumnIds: + - - 2 + - - 3 + - tableId: 117 + - unique: true + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - indexName: + - indexId: 1 + - name: t1_pkey + - tableId: 117 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - constraintComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - constraintId: 1 + - tableId: 117 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - indexComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - indexId: 1 + - tableId: 117 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - locality: + - descriptorId: 117 + - metadata: + - sourceElementId: 3 + - subWorkId: 1 + - targetStatus: ABSENT families: - columnIds: ... @@ -779,7 +3892,120 @@ upsert descriptor #118 wallTime: "1" - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - DROPPED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP DATABASE ‹db1› CASCADE + - statement: DROP DATABASE db1 CASCADE + - statementTag: DROP DATABASE + - targetRanks: + - - 66 + - - 67 + - - 68 + - - 69 + - - 70 + - - 71 + - - 72 + - - 73 + - - 74 + - - 75 + - targets: + - - elementProto: + - view: + - tableId: 118 + - metadata: + - sourceElementId: 8 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - namespace: + - databaseId: 111 + - descriptorId: 118 + - name: v1 + - schemaId: 113 + - metadata: + - sourceElementId: 8 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - owner: + - descriptorId: 118 + - owner: root + - metadata: + - sourceElementId: 8 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 118 + - privileges: 2 + - username: admin + - metadata: + - sourceElementId: 8 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 118 + - username: public + - metadata: + - sourceElementId: 8 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 118 + - privileges: 2 + - username: root + - metadata: + - sourceElementId: 8 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - tableComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 118 + - metadata: + - sourceElementId: 8 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - locality: + - descriptorId: 118 + - metadata: + - sourceElementId: 8 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - relationDependedOnBy: + - columnId: 1 + - dependedOn: 119 + - tableId: 118 + - metadata: + - sourceElementId: 8 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - relationDependedOnBy: + - columnId: 1 + - dependedOn: 120 + - tableId: 118 + - metadata: + - sourceElementId: 8 + - subWorkId: 1 + - targetStatus: ABSENT formatVersion: 3 id: 118 ... @@ -794,7 +4020,120 @@ upsert descriptor #119 wallTime: "1" - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - DROPPED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP DATABASE ‹db1› CASCADE + - statement: DROP DATABASE db1 CASCADE + - statementTag: DROP DATABASE + - targetRanks: + - - 76 + - - 77 + - - 78 + - - 79 + - - 80 + - - 81 + - - 82 + - - 83 + - - 84 + - - 85 + - targets: + - - elementProto: + - view: + - tableId: 119 + - metadata: + - sourceElementId: 9 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - namespace: + - databaseId: 111 + - descriptorId: 119 + - name: v2 + - schemaId: 113 + - metadata: + - sourceElementId: 9 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - owner: + - descriptorId: 119 + - owner: root + - metadata: + - sourceElementId: 9 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 119 + - privileges: 2 + - username: admin + - metadata: + - sourceElementId: 9 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 119 + - username: public + - metadata: + - sourceElementId: 9 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 119 + - privileges: 2 + - username: root + - metadata: + - sourceElementId: 9 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - tableComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 119 + - metadata: + - sourceElementId: 9 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - locality: + - descriptorId: 119 + - metadata: + - sourceElementId: 9 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - relationDependedOnBy: + - columnId: 1 + - dependedOn: 120 + - tableId: 119 + - metadata: + - sourceElementId: 9 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - relationDependedOnBy: + - columnId: 1 + - dependedOn: 121 + - tableId: 119 + - metadata: + - sourceElementId: 9 + - subWorkId: 1 + - targetStatus: ABSENT formatVersion: 3 id: 119 ... @@ -809,7 +4148,98 @@ upsert descriptor #120 wallTime: "1" - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - DROPPED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP DATABASE ‹db1› CASCADE + - statement: DROP DATABASE db1 CASCADE + - statementTag: DROP DATABASE + - targetRanks: + - - 86 + - - 87 + - - 88 + - - 89 + - - 90 + - - 91 + - - 92 + - - 93 + - targets: + - - elementProto: + - view: + - tableId: 120 + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - namespace: + - databaseId: 111 + - descriptorId: 120 + - name: v3 + - schemaId: 113 + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - owner: + - descriptorId: 120 + - owner: root + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 120 + - privileges: 2 + - username: admin + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 120 + - username: public + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 120 + - privileges: 2 + - username: root + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - tableComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 120 + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - locality: + - descriptorId: 120 + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT formatVersion: 3 id: 120 ... @@ -824,7 +4254,109 @@ upsert descriptor #121 wallTime: "1" - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - DROPPED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP DATABASE ‹db1› CASCADE + - statement: DROP DATABASE db1 CASCADE + - statementTag: DROP DATABASE + - targetRanks: + - - 94 + - - 95 + - - 96 + - - 97 + - - 98 + - - 99 + - - 100 + - - 101 + - - 102 + - targets: + - - elementProto: + - view: + - tableId: 121 + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - namespace: + - databaseId: 111 + - descriptorId: 121 + - name: v4 + - schemaId: 113 + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - owner: + - descriptorId: 121 + - owner: root + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 121 + - privileges: 2 + - username: admin + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 121 + - username: public + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 121 + - privileges: 2 + - username: root + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - tableComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 121 + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - locality: + - descriptorId: 121 + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - relationDependedOnBy: + - columnId: 1 + - dependedOn: 124 + - tableId: 121 + - metadata: + - sourceElementId: 10 + - subWorkId: 1 + - targetStatus: ABSENT formatVersion: 3 id: 121 ... @@ -839,7 +4371,118 @@ upsert descriptor #124 wallTime: "1" - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - DROPPED + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: DROP DATABASE ‹db1› CASCADE + - statement: DROP DATABASE db1 CASCADE + - statementTag: DROP DATABASE + - targetRanks: + - - 103 + - - 104 + - - 105 + - - 106 + - - 107 + - - 108 + - - 109 + - - 110 + - - 111 + - - 112 + - targets: + - - elementProto: + - view: + - tableId: 124 + - metadata: + - sourceElementId: 12 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - namespace: + - databaseId: 111 + - descriptorId: 124 + - name: v5 + - schemaId: 113 + - metadata: + - sourceElementId: 12 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - owner: + - descriptorId: 124 + - owner: root + - metadata: + - sourceElementId: 12 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 124 + - privileges: 2 + - username: admin + - metadata: + - sourceElementId: 12 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 124 + - username: public + - metadata: + - sourceElementId: 12 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - userPrivileges: + - descriptorId: 124 + - privileges: 2 + - username: root + - metadata: + - sourceElementId: 12 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - tableComment: + - comment: TODO(fqazi) Comments are not currently fetched from system.comments + - when doing decomposition + - tableId: 124 + - metadata: + - sourceElementId: 12 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - viewDependsOnType: + - tableId: 124 + - typeId: 122 + - metadata: + - sourceElementId: 12 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - viewDependsOnType: + - tableId: 124 + - typeId: 123 + - metadata: + - sourceElementId: 12 + - subWorkId: 1 + - targetStatus: ABSENT + - - elementProto: + - locality: + - descriptorId: 124 + - metadata: + - sourceElementId: 12 + - subWorkId: 1 + - targetStatus: ABSENT dependsOnTypes: - 122 ... @@ -852,5 +4495,5 @@ delete descriptor #112 delete descriptor #113 delete descriptor #122 delete descriptor #123 -commit transaction #2 +commit transaction #3 # end PostCommitPhase diff --git a/pkg/sql/schemachanger/testdata/index b/pkg/sql/schemachanger/testdata/index index 4c51bcd89586..3240b02b3494 100644 --- a/pkg/sql/schemachanger/testdata/index +++ b/pkg/sql/schemachanger/testdata/index @@ -21,7 +21,44 @@ upsert descriptor #104 wallTime: "1" + declarativeSchemaChangerState: + authorization: {} + + currentStatuses: + + - DELETE_ONLY + + - ABSENT + jobId: "1" + + relevantStatements: + + - statement: + + redactedStatement: CREATE INDEX ‹idx1› ON ‹defaultdb›.‹public›.‹t› (‹v›) + + statement: CREATE INDEX idx1 ON t (v) + + statementTag: CREATE INDEX + + targetRanks: + + - 0 + + - 1 + + targets: + + - elementProto: + + secondaryIndex: + + indexId: 2 + + keyColumnDirections: + + - ASC + + keyColumnIds: + + - 2 + + keySuffixColumnIds: + + - 1 + + sourceIndexId: 1 + + storingColumnIds: [] + + tableId: 104 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: PUBLIC + + - elementProto: + + indexName: + + indexId: 2 + + name: idx1 + + tableId: 104 + + metadata: + + sourceElementId: 1 + + subWorkId: 1 + + targetStatus: PUBLIC families: - columnIds: ... @@ -70,9 +107,18 @@ upsert descriptor #104 commit transaction #1 # begin PostCommitPhase begin transaction #2 -## PostCommitPhase stage 1 of 4 with 2 MutationType ops +commit transaction #2 +begin transaction #3 +## PostCommitPhase stage 1 of 4 with 3 MutationType ops update progress of schema change job #1 upsert descriptor #104 + ... + authorization: {} + currentStatuses: + - - DELETE_ONLY + + - DELETE_AND_WRITE_ONLY + - ABSENT + jobId: "1" ... version: 3 mutationId: 1 @@ -85,16 +131,16 @@ upsert descriptor #104 unexposedParentSchemaId: 101 - version: "2" + version: "3" -commit transaction #2 -begin transaction #3 -## PostCommitPhase stage 2 of 4 with 1 BackfillType ops -backfill indexes [2] from index #1 in table #104 commit transaction #3 begin transaction #4 -## PostCommitPhase stage 3 of 4 with 1 ValidationType ops -validate forward indexes [2] in table #104 +## PostCommitPhase stage 2 of 4 with 1 BackfillType ops +backfill indexes [2] from index #1 in table #104 commit transaction #4 begin transaction #5 +## PostCommitPhase stage 3 of 4 with 1 ValidationType ops +validate forward indexes [2] in table #104 +commit transaction #5 +begin transaction #6 ## PostCommitPhase stage 4 of 4 with 4 MutationType ops update progress of schema change job #1 upsert descriptor #104 @@ -103,7 +149,44 @@ upsert descriptor #104 wallTime: "1" - declarativeSchemaChangerState: - authorization: {} + - currentStatuses: + - - DELETE_AND_WRITE_ONLY + - - ABSENT - jobId: "1" + - relevantStatements: + - - statement: + - redactedStatement: CREATE INDEX ‹idx1› ON ‹defaultdb›.‹public›.‹t› (‹v›) + - statement: CREATE INDEX idx1 ON t (v) + - statementTag: CREATE INDEX + - targetRanks: + - - 0 + - - 1 + - targets: + - - elementProto: + - secondaryIndex: + - indexId: 2 + - keyColumnDirections: + - - ASC + - keyColumnIds: + - - 2 + - keySuffixColumnIds: + - - 1 + - sourceIndexId: 1 + - storingColumnIds: [] + - tableId: 104 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: PUBLIC + - - elementProto: + - indexName: + - indexId: 2 + - name: idx1 + - tableId: 104 + - metadata: + - sourceElementId: 1 + - subWorkId: 1 + - targetStatus: PUBLIC families: - columnIds: ... @@ -163,5 +246,5 @@ upsert descriptor #104 unexposedParentSchemaId: 101 - version: "3" + version: "4" -commit transaction #5 +commit transaction #6 # end PostCommitPhase From 0dfd6eec913d2b0ebbe81813dae51d50dfc53cd5 Mon Sep 17 00:00:00 2001 From: Andrew Werner Date: Wed, 9 Feb 2022 01:18:19 -0500 Subject: [PATCH 5/5] sql/schemachanger: adopt batched retrieval of descriptors I'm not sure I'm in love with the function I've added being in scmutationexec but alas. Probably in `scexec` we ought to just go and retreive all of the descriptors in a batch for each stage. Leaving that as follow-up. Release note: None --- pkg/sql/schemachanger/scdeps/exec_deps.go | 8 ++--- .../scdeps/sctestdeps/test_deps.go | 18 ++++++++--- pkg/sql/schemachanger/scexec/dependencies.go | 2 +- pkg/sql/schemachanger/scexec/exec_backfill.go | 2 +- .../scexec/exec_backfill_test.go | 7 ++-- .../schemachanger/scexec/exec_validation.go | 5 ++- .../scexec/mocks_generated_test.go | 19 +++++++---- .../scexec/scmutationexec/scmutationexec.go | 32 +++++++++++++------ pkg/sql/schemachanger/scrun/BUILD.bazel | 1 - pkg/sql/schemachanger/scrun/scrun.go | 30 +++++++---------- 10 files changed, 72 insertions(+), 52 deletions(-) diff --git a/pkg/sql/schemachanger/scdeps/exec_deps.go b/pkg/sql/schemachanger/scdeps/exec_deps.go index 5cf75a713095..713c562b4696 100644 --- a/pkg/sql/schemachanger/scdeps/exec_deps.go +++ b/pkg/sql/schemachanger/scdeps/exec_deps.go @@ -116,9 +116,9 @@ func (d *txnDeps) UpdateSchemaChangeJob( var _ scexec.Catalog = (*txnDeps)(nil) // MustReadImmutableDescriptor implements the scmutationexec.CatalogReader interface. -func (d *txnDeps) MustReadImmutableDescriptor( - ctx context.Context, id descpb.ID, -) (catalog.Descriptor, error) { +func (d *txnDeps) MustReadImmutableDescriptors( + ctx context.Context, ids ...descpb.ID, +) ([]catalog.Descriptor, error) { flags := tree.CommonLookupFlags{ Required: true, RequireMutable: false, @@ -126,7 +126,7 @@ func (d *txnDeps) MustReadImmutableDescriptor( IncludeOffline: true, IncludeDropped: true, } - return d.descsCollection.GetImmutableDescriptorByID(ctx, d.txn, id, flags) + return d.descsCollection.GetImmutableDescriptorsByID(ctx, d.txn, flags, ids...) } // GetFullyQualifiedName implements the scmutationexec.CatalogReader interface diff --git a/pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go b/pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go index bc359747ace0..43410a68a6b2 100644 --- a/pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go +++ b/pkg/sql/schemachanger/scdeps/sctestdeps/test_deps.go @@ -473,11 +473,19 @@ func (s *TestState) Catalog() scexec.Catalog { var _ scmutationexec.CatalogReader = (*TestState)(nil) -// MustReadImmutableDescriptor implements the scmutationexec.CatalogReader interface. -func (s *TestState) MustReadImmutableDescriptor( - ctx context.Context, id descpb.ID, -) (catalog.Descriptor, error) { - return s.mustReadImmutableDescriptor(id) +// MustReadImmutableDescriptors implements the scmutationexec.CatalogReader interface. +func (s *TestState) MustReadImmutableDescriptors( + ctx context.Context, ids ...descpb.ID, +) ([]catalog.Descriptor, error) { + out := make([]catalog.Descriptor, 0, len(ids)) + for _, id := range ids { + d, err := s.mustReadImmutableDescriptor(id) + if err != nil { + return nil, err + } + out = append(out, d) + } + return out, nil } // AddSyntheticDescriptor implements the scmutationexec.CatalogReader interface. diff --git a/pkg/sql/schemachanger/scexec/dependencies.go b/pkg/sql/schemachanger/scexec/dependencies.go index f51d91bbeff2..c14582a1eac8 100644 --- a/pkg/sql/schemachanger/scexec/dependencies.go +++ b/pkg/sql/schemachanger/scexec/dependencies.go @@ -53,7 +53,7 @@ type Catalog interface { scmutationexec.CatalogReader // MustReadMutableDescriptor the mutable equivalent to - // MustReadImmutableDescriptor in scmutationexec.CatalogReader. + // MustReadImmutableDescriptors in scmutationexec.CatalogReader. // This method should be used carefully. MustReadMutableDescriptor(ctx context.Context, id descpb.ID) (catalog.MutableDescriptor, error) diff --git a/pkg/sql/schemachanger/scexec/exec_backfill.go b/pkg/sql/schemachanger/scexec/exec_backfill.go index d971939a2e6e..afa10fc42e45 100644 --- a/pkg/sql/schemachanger/scexec/exec_backfill.go +++ b/pkg/sql/schemachanger/scexec/exec_backfill.go @@ -52,7 +52,7 @@ func getTableDescriptorsForBackfills( } tables := make(map[descpb.ID]catalog.TableDescriptor, descIDs.Len()) for _, id := range descIDs.Ordered() { - desc, err := cat.MustReadImmutableDescriptor(ctx, id) + desc, err := scmutationexec.MustReadImmutableDescriptor(ctx, cat, id) if err != nil { return nil, err } diff --git a/pkg/sql/schemachanger/scexec/exec_backfill_test.go b/pkg/sql/schemachanger/scexec/exec_backfill_test.go index b30708be3725..bdd94a12e20f 100644 --- a/pkg/sql/schemachanger/scexec/exec_backfill_test.go +++ b/pkg/sql/schemachanger/scexec/exec_backfill_test.go @@ -106,9 +106,9 @@ func TestExecBackfill(t *testing.T) { return tab } getTableDescriptor := func(ctx context.Context, t *testing.T, deps *sctestdeps.TestState, id descpb.ID) catalog.TableDescriptor { - desc, err := deps.Catalog().MustReadImmutableDescriptor(ctx, id) + descs, err := deps.Catalog().MustReadImmutableDescriptors(ctx, id) require.NoError(t, err) - tab, ok := desc.(catalog.TableDescriptor) + tab, ok := descs[0].(catalog.TableDescriptor) require.True(t, ok) return tab } @@ -131,8 +131,9 @@ func TestExecBackfill(t *testing.T) { descs.UpsertDescriptorEntry(mut) mc, bt, bf, deps := setupTestDeps(t, tdb, descs.Catalog) defer mc.Finish() - desc, err := deps.Catalog().MustReadImmutableDescriptor(ctx, mut.GetID()) + read, err := deps.Catalog().MustReadImmutableDescriptors(ctx, mut.GetID()) require.NoError(t, err) + desc := read[0] backfill := scexec.Backfill{ TableID: tab.GetID(), diff --git a/pkg/sql/schemachanger/scexec/exec_validation.go b/pkg/sql/schemachanger/scexec/exec_validation.go index 2eb00440fe93..48dabc5f09bc 100644 --- a/pkg/sql/schemachanger/scexec/exec_validation.go +++ b/pkg/sql/schemachanger/scexec/exec_validation.go @@ -16,6 +16,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/security" "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" + "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec/scmutationexec" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" "github.com/cockroachdb/cockroach/pkg/sql/sessiondata" "github.com/cockroachdb/errors" @@ -24,7 +25,9 @@ import ( func executeValidateUniqueIndex( ctx context.Context, deps Dependencies, op *scop.ValidateUniqueIndex, ) error { - desc, err := deps.Catalog().MustReadImmutableDescriptor(ctx, op.TableID) + desc, err := scmutationexec.MustReadImmutableDescriptor( + ctx, deps.Catalog(), op.TableID, + ) if err != nil { return err } diff --git a/pkg/sql/schemachanger/scexec/mocks_generated_test.go b/pkg/sql/schemachanger/scexec/mocks_generated_test.go index da5e699d16e0..95e78220cbc5 100644 --- a/pkg/sql/schemachanger/scexec/mocks_generated_test.go +++ b/pkg/sql/schemachanger/scexec/mocks_generated_test.go @@ -66,19 +66,24 @@ func (mr *MockCatalogMockRecorder) GetFullyQualifiedName(arg0, arg1 interface{}) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetFullyQualifiedName", reflect.TypeOf((*MockCatalog)(nil).GetFullyQualifiedName), arg0, arg1) } -// MustReadImmutableDescriptor mocks base method. -func (m *MockCatalog) MustReadImmutableDescriptor(arg0 context.Context, arg1 catid.DescID) (catalog.Descriptor, error) { +// MustReadImmutableDescriptors mocks base method. +func (m *MockCatalog) MustReadImmutableDescriptors(arg0 context.Context, arg1 ...catid.DescID) ([]catalog.Descriptor, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "MustReadImmutableDescriptor", arg0, arg1) - ret0, _ := ret[0].(catalog.Descriptor) + varargs := []interface{}{arg0} + for _, a := range arg1 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "MustReadImmutableDescriptors", varargs...) + ret0, _ := ret[0].([]catalog.Descriptor) ret1, _ := ret[1].(error) return ret0, ret1 } -// MustReadImmutableDescriptor indicates an expected call of MustReadImmutableDescriptor. -func (mr *MockCatalogMockRecorder) MustReadImmutableDescriptor(arg0, arg1 interface{}) *gomock.Call { +// MustReadImmutableDescriptors indicates an expected call of MustReadImmutableDescriptors. +func (mr *MockCatalogMockRecorder) MustReadImmutableDescriptors(arg0 interface{}, arg1 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MustReadImmutableDescriptor", reflect.TypeOf((*MockCatalog)(nil).MustReadImmutableDescriptor), arg0, arg1) + varargs := append([]interface{}{arg0}, arg1...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "MustReadImmutableDescriptors", reflect.TypeOf((*MockCatalog)(nil).MustReadImmutableDescriptors), varargs...) } // MustReadMutableDescriptor mocks base method. diff --git a/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go b/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go index 56a40264d79f..d1e6fc43ee5f 100644 --- a/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go +++ b/pkg/sql/schemachanger/scexec/scmutationexec/scmutationexec.go @@ -45,8 +45,8 @@ type Clock interface { // CatalogReader describes catalog read operations as required by the mutation // visitor. type CatalogReader interface { - // MustReadImmutableDescriptor reads a descriptor from the catalog by ID. - MustReadImmutableDescriptor(ctx context.Context, id descpb.ID) (catalog.Descriptor, error) + // MustReadImmutableDescriptors reads descriptors from the catalog by ID. + MustReadImmutableDescriptors(ctx context.Context, ids ...descpb.ID) ([]catalog.Descriptor, error) // GetFullyQualifiedName gets the fully qualified name from a descriptor ID. GetFullyQualifiedName(ctx context.Context, id descpb.ID) (string, error) @@ -60,6 +60,18 @@ type CatalogReader interface { RemoveSyntheticDescriptor(id descpb.ID) } +// MustReadImmutableDescriptor is a shorthand for invoking +// CatalogReader.MustReadImmutableDescriptors for a single ID. +func MustReadImmutableDescriptor( + ctx context.Context, cr CatalogReader, id descpb.ID, +) (catalog.Descriptor, error) { + descs, err := cr.MustReadImmutableDescriptors(ctx, id) + if err != nil { + return nil, err + } + return descs[0], nil +} + // Partitioner is the interface for adding partitioning to a table descriptor. type Partitioner interface { AddPartitioning( @@ -146,7 +158,7 @@ func (m *visitor) RemoveJobStateFromDescriptor( ctx context.Context, op scop.RemoveJobStateFromDescriptor, ) error { { - _, err := m.cr.MustReadImmutableDescriptor(ctx, op.DescriptorID) + _, err := MustReadImmutableDescriptor(ctx, m.cr, op.DescriptorID) // If we're clearing the status, we might have already deleted the // descriptor. Permit that by detecting the prior deletion and @@ -354,7 +366,7 @@ func (m *visitor) AddTypeBackRef(ctx context.Context, op scop.AddTypeBackRef) er } typ.AddReferencingDescriptorID(op.DescID) // Sanity: Validate that a back reference exists by now. - desc, err := m.cr.MustReadImmutableDescriptor(ctx, op.DescID) + desc, err := MustReadImmutableDescriptor(ctx, m.cr, op.DescID) if err != nil { return err } @@ -480,7 +492,7 @@ func (m *visitor) CreateGcJobForDatabase( } func (m *visitor) CreateGcJobForIndex(ctx context.Context, op scop.CreateGcJobForIndex) error { - desc, err := m.cr.MustReadImmutableDescriptor(ctx, op.TableID) + desc, err := MustReadImmutableDescriptor(ctx, m.cr, op.TableID) if err != nil { return err } @@ -512,7 +524,7 @@ func (m *visitor) MarkDescriptorAsDropped( func (m *visitor) MarkDescriptorAsDroppedSynthetically( ctx context.Context, op scop.MarkDescriptorAsDroppedSynthetically, ) error { - desc, err := m.cr.MustReadImmutableDescriptor(ctx, op.DescID) + desc, err := MustReadImmutableDescriptor(ctx, m.cr, op.DescID) if err != nil { return err } @@ -523,7 +535,7 @@ func (m *visitor) MarkDescriptorAsDroppedSynthetically( } func (m *visitor) DrainDescriptorName(ctx context.Context, op scop.DrainDescriptorName) error { - descriptor, err := m.cr.MustReadImmutableDescriptor(ctx, op.TableID) + descriptor, err := MustReadImmutableDescriptor(ctx, m.cr, op.TableID) if err != nil { return err } @@ -1052,7 +1064,7 @@ func (m *visitor) DeleteDatabaseSchemaEntry( if err != nil { return err } - sc, err := m.cr.MustReadImmutableDescriptor(ctx, op.SchemaID) + sc, err := MustReadImmutableDescriptor(ctx, m.cr, op.SchemaID) if err != nil { return err } @@ -1088,7 +1100,7 @@ func (m *visitor) RemoveColumnComment(_ context.Context, op scop.RemoveColumnCom func (m *visitor) RemoveConstraintComment( ctx context.Context, op scop.RemoveConstraintComment, ) error { - tbl, err := m.cr.MustReadImmutableDescriptor(ctx, op.TableID) + tbl, err := MustReadImmutableDescriptor(ctx, m.cr, op.TableID) if err != nil { return err } @@ -1098,7 +1110,7 @@ func (m *visitor) RemoveConstraintComment( func (m *visitor) RemoveDatabaseRoleSettings( ctx context.Context, op scop.RemoveDatabaseRoleSettings, ) error { - db, err := m.cr.MustReadImmutableDescriptor(ctx, op.DatabaseID) + db, err := MustReadImmutableDescriptor(ctx, m.cr, op.DatabaseID) if err != nil { return err } diff --git a/pkg/sql/schemachanger/scrun/BUILD.bazel b/pkg/sql/schemachanger/scrun/BUILD.bazel index 3422938cfa66..364d11d515ec 100644 --- a/pkg/sql/schemachanger/scrun/BUILD.bazel +++ b/pkg/sql/schemachanger/scrun/BUILD.bazel @@ -12,7 +12,6 @@ go_library( deps = [ "//pkg/jobs/jobspb", "//pkg/settings/cluster", - "//pkg/sql/catalog", "//pkg/sql/catalog/descpb", "//pkg/sql/schemachanger/scexec", "//pkg/sql/schemachanger/scop", diff --git a/pkg/sql/schemachanger/scrun/scrun.go b/pkg/sql/schemachanger/scrun/scrun.go index c7b82194f187..67ff4a0c454a 100644 --- a/pkg/sql/schemachanger/scrun/scrun.go +++ b/pkg/sql/schemachanger/scrun/scrun.go @@ -16,7 +16,6 @@ import ( "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" "github.com/cockroachdb/cockroach/pkg/settings/cluster" - "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec" "github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scop" @@ -164,7 +163,7 @@ func makeState( ctx context.Context, sv *cluster.Settings, deps JobRunDependencies, - descriptors []descpb.ID, + descriptorIDs []descpb.ID, rollback bool, ) (scpb.CurrentState, error) { var s scpb.CurrentState @@ -177,28 +176,21 @@ func makeState( targetRanks = nil stmts = make(map[uint32]scpb.Statement) - // TODO(ajwerner): This would be a great place for a batched read. - for _, id := range descriptors { + descs, err := txnDeps.Catalog().MustReadImmutableDescriptors(ctx, descriptorIDs...) + if err != nil { + // TODO(ajwerner): It seems possible that a descriptor could be deleted + // and the schema change is in a happy place. Ideally we'd enforce that + // descriptors may only be deleted on the very last step of the schema + // change. + return err + } + for _, desc := range descs { // TODO(ajwerner): Verify that the job ID matches on all of the // descriptors. Also verify that the Authorization matches. - - desc, err := txnDeps.Catalog().MustReadImmutableDescriptor(ctx, id) - - // If the descriptor was not found, it's not exactly clear what to do. - // Probably this means that deleting descriptors needs to be the very - // last step of a schema change. - // - // TODO(ajwerner): Sort this out. - if errors.Is(err, catalog.ErrDescriptorNotFound) { - return err - } - if err != nil { - return err - } cs := desc.GetDeclarativeSchemaChangerState() if cs == nil { return errors.Errorf( - "descriptor %d does not contain schema changer state", id, + "descriptor %d does not contain schema changer state", desc.GetID(), ) } s.Current = append(s.Current, cs.CurrentStatuses...)