Skip to content

Commit

Permalink
sql: refactor alter table locality privileges check
Browse files Browse the repository at this point in the history
This patch calls into the `ensureCorrectMultiRegionPrivilegesForTable`
function instead of checking privileges. This doesn't change any
behavior, but I've added tests ensuring the semantics are sane.

Release note: None
  • Loading branch information
arulajmani committed Mar 19, 2021
1 parent 0ea0538 commit 6a508f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
41 changes: 41 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/multi_region_privileges
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,44 @@ ALTER DATABASE db SET PRIMARY REGION "us-east-1"

statement ok
ALTER DATABASE db DROP REGION "us-east-1"

subtest alter_table_locality_privs

user root

statement ok
CREATE DATABASE alter_db PRIMARY REGION "us-east-1";
CREATE TABLE alter_db.t();

user testuser

statement error pq: user testuser must be owner of t or have CREATE privilege on t
ALTER TABLE alter_db.t SET LOCALITY GLOBAL

user root

statement ok
GRANT CREATE ON TABLE alter_db.t TO testuser

user testuser

statement ok
ALTER TABLE alter_db.t SET LOCALITY GLOBAL

# Same thing, this time make testuser the owner.

user root

statement ok
REVOKE CREATE ON TABLE alter_db.t FROM testuser

# To be able to gain ownership of the table, testuser needs to have CREATE
# privilege on the database.
statement ok
GRANT CREATE ON DATABASE alter_db to testuser;
ALTER TABLE alter_db.t OWNER TO testuser

user testuser

statement ok
ALTER TABLE alter_db.t SET LOCALITY REGIONAL
8 changes: 2 additions & 6 deletions pkg/sql/alter_table_locality.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
"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/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
Expand Down Expand Up @@ -61,11 +60,8 @@ func (p *planner) AlterTableLocality(
return newZeroNode(nil /* columns */), nil
}

// This check for CREATE privilege is kept for backwards compatibility.
if err := p.CheckPrivilege(ctx, tableDesc, privilege.CREATE); err != nil {
return nil, pgerror.Newf(pgcode.InsufficientPrivilege,
"must be owner of table %s or have CREATE privilege on table %s",
tree.Name(tableDesc.GetName()), tree.Name(tableDesc.GetName()))
if err := p.ensureCorrectMultiRegionPrivilegesForTable(ctx, tableDesc); err != nil {
return nil, err
}

// Ensure that the database is multi-region enabled.
Expand Down

0 comments on commit 6a508f4

Please sign in to comment.