Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: Support CREATE DATABASE WITH OWNER #74867

Merged
merged 1 commit into from
Jan 21, 2022

Conversation

Fenil-P
Copy link
Contributor

@Fenil-P Fenil-P commented Jan 14, 2022

fixes #67817

Release note (sql change): Allow users to specify the owner when creating a database.
Similar to postgresql: CREATE DATABASE name [ [ WITH ] [ OWNER [=] user_name ]

@Fenil-P Fenil-P requested a review from RichardJCai January 14, 2022 21:00
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@RichardJCai RichardJCai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, looks mostly good to me after minor comments are addressed.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Fenil-P and @RichardJCai)


pkg/sql/descriptor.go, line 147 at r1 (raw file):

	// database-level zone configuration if there is a region config on the
	// descriptor.
	//p.setNewDatabaseOwner(ctx)

Remove this comment.


pkg/sql/descriptor.go, line 148 at r1 (raw file):

	// descriptor.
	//p.setNewDatabaseOwner(ctx)
	if database.Owner.Name != "" {

Use Owner.IsUndefined()


pkg/sql/descriptor.go, line 154 at r1 (raw file):

		}

		if err := p.checkCanAlterToNewOwner(ctx, desc, newOwner); err != nil {

Not 100% sure if we need this check if we're creating a new database here.
Can you double check with Postgres - you'll have to download PG and verify.

Namely, check if you can CREATE DATABASE ... WITH OWNER ... where the current user is not a member of the owner role.

Also please add a test for this if it turns out we do need this check (verify that we need to be a member of the owner role)


pkg/sql/descriptor.go, line 161 at r1 (raw file):

			return nil, true, err
		}
		if err := p.writeNonDropDatabaseChange(

Don't need this since the database descriptor gets written later in this function.

@rafiss rafiss requested a review from a team January 18, 2022 21:13
@Fenil-P Fenil-P force-pushed the create_database_with_owner branch from 07937d9 to 85e30e3 Compare January 19, 2022 18:05
Copy link
Contributor Author

@Fenil-P Fenil-P left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @RichardJCai)


pkg/sql/descriptor.go, line 147 at r1 (raw file):

Previously, RichardJCai (Richard Cai) wrote…

Remove this comment.

Done.


pkg/sql/descriptor.go, line 148 at r1 (raw file):

Previously, RichardJCai (Richard Cai) wrote…

Use Owner.IsUndefined()

Done.


pkg/sql/descriptor.go, line 154 at r1 (raw file):

Previously, RichardJCai (Richard Cai) wrote…

Not 100% sure if we need this check if we're creating a new database here.
Can you double check with Postgres - you'll have to download PG and verify.

Namely, check if you can CREATE DATABASE ... WITH OWNER ... where the current user is not a member of the owner role.

Also please add a test for this if it turns out we do need this check (verify that we need to be a member of the owner role)

Done.


pkg/sql/descriptor.go, line 161 at r1 (raw file):

Previously, RichardJCai (Richard Cai) wrote…

Don't need this since the database descriptor gets written later in this function.

Done.

Copy link
Contributor

@RichardJCai RichardJCai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! LGTM after addressing nit

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Fenil-P and @RichardJCai)


pkg/sql/descriptor.go, line 140 at r2 (raw file):

	} else {
		owner = p.SessionData().User()

nit: we can just remove this else block and initially set owner to p.SessionData.User() and update it if the if cond is met

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Fenil-P, @rafiss, and @RichardJCai)


pkg/sql/descriptor.go, line 140 at r2 (raw file):

Previously, RichardJCai (Richard Cai) wrote…

nit: we can just remove this else block and initially set owner to p.SessionData.User() and update it if the if cond is met

+1

actually when is database.Owner ever undefined?


pkg/sql/parser/sql.y, line 8923 at r2 (raw file):

  OWNER opt_equal role_spec
  {
    $$ = $3

should this be $$.val = $3.roleSpec()

maybe it's the same thing?


pkg/sql/parser/sql.y, line 8928 at r2 (raw file):

   {
		 $$.val = tree.RoleSpec{
		   RoleSpecType: tree.SessionUser,

why SessionUser and not CurrentUser?

Release note (sql change): Allow users to specify the owner when creating a database.
			   Similar to postgresql: CREATE DATABASE name
    							[ [ WITH ] [ OWNER [=] user_name ]
@Fenil-P Fenil-P force-pushed the create_database_with_owner branch from 85e30e3 to bf237fd Compare January 20, 2022 17:32
Copy link
Contributor Author

@Fenil-P Fenil-P left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss and @RichardJCai)


pkg/sql/descriptor.go, line 140 at r2 (raw file):

actually when is database.Owner ever undefined?
They're undefined when defaultdb and postgres are being created


pkg/sql/parser/sql.y, line 8923 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

should this be $$.val = $3.roleSpec()

maybe it's the same thing?

I kept the opt_owner_clause the same type as role_spec so its done later on in the create db statement.


pkg/sql/parser/sql.y, line 8928 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

why SessionUser and not CurrentUser?

Updated

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Fenil-P, @rafiss, and @RichardJCai)


pkg/sql/descriptor.go, line 140 at r2 (raw file):

Previously, Fenil-P (Fenil Patel) wrote…

actually when is database.Owner ever undefined?
They're undefined when defaultdb and postgres are being created

that happens in createDefaultDbs in startupmigrations with CREATE DATABASE IF NOT EXISTS "%s"

but based on how you added the new syntax, shouldn't an empty owner clause already default to using the current user?

anyway, this doesn't need to block the PR, i was just curious. having the undefined fallback case seems good regardless

@Fenil-P
Copy link
Contributor Author

Fenil-P commented Jan 20, 2022

bors r+

@craig
Copy link
Contributor

craig bot commented Jan 20, 2022

Build failed (retrying...):

@Fenil-P
Copy link
Contributor Author

Fenil-P commented Jan 20, 2022

bors retry

@craig
Copy link
Contributor

craig bot commented Jan 20, 2022

Already running a review

@Fenil-P
Copy link
Contributor Author

Fenil-P commented Jan 21, 2022

bors r+

@craig
Copy link
Contributor

craig bot commented Jan 21, 2022

Already running a review

@craig craig bot merged commit 488a06b into cockroachdb:master Jan 21, 2022
@craig
Copy link
Contributor

craig bot commented Jan 21, 2022

Build succeeded:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: Support CREATE DATABASE WITH OWNER
4 participants