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,backupccl: set system.role_members ID columns to NOT NULL #93301

Merged
merged 1 commit into from
Jan 17, 2023

Conversation

andyyang890
Copy link
Collaborator

@andyyang890 andyyang890 commented Dec 9, 2022

This patch sets the newly added role_id and member_id columns in
system.role_members to be NOT NULL. It also changes the RESTORE
logic to be able to handle the case when restoring from a backup where
the system.role_members table did not have the columns.

Part of #87079

Release note: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@andyyang890 andyyang890 force-pushed the role_members_notnull branch 10 times, most recently from d29b1db to 0bcd48a Compare December 14, 2022 17:59
@andyyang890 andyyang890 changed the title sql: set system.role_members ID columns to NOT NULL sql,backupccl: set system.role_members ID columns to NOT NULL Dec 14, 2022
@andyyang890 andyyang890 marked this pull request as ready for review December 14, 2022 18:06
@andyyang890 andyyang890 requested a review from a team December 14, 2022 18:06
@andyyang890 andyyang890 requested review from a team as code owners December 14, 2022 18:06
@andyyang890 andyyang890 requested review from rhu713, rafiss and a team and removed request for a team December 14, 2022 18:06
@andyyang890 andyyang890 force-pushed the role_members_notnull branch 2 times, most recently from a728aa8 to c4ef9af Compare December 15, 2022 04:34
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 @andyyang890 and @rhu713)


pkg/ccl/backupccl/restore_job.go line 2858 at r1 (raw file):

		}

		userIDMap := make(map[tree.DString]oid.Oid)

nit: can you make this use tree.DOid instead of oid.Oid? i think it's better to treat these all as datums


pkg/ccl/backupccl/system_schema.go line 252 at r1 (raw file):

	txn *kv.Txn,
	systemTableName, tempTableName string,
) error {

can you make this a named return parameter, retErr? (see below comment)


pkg/ccl/backupccl/system_schema.go line 283 at r1 (raw file):

	if err != nil {
		return err
	}

there should be a call like this here:

	defer func() { retErr = errors.CombineErrors(retErr, it.Close()) }()

actually, we'd want the same fix to be in roleOptionsRestoreFunc. the reason is that some errors are only returned when the iterator is closed, and this is important for correct error handling.


pkg/ccl/backupccl/system_schema.go line 318 at r1 (raw file):

		}

		restoreQuery := fmt.Sprintf(`INSERT INTO system.%s ("role", "member", "isAdmin", role_id, member_id) VALUES ($1, $2, $3, $4, $5)`, systemTableName)

i think you can avoid the need for the ids map, if you use an update ... from statement: https://www.cockroachlabs.com/docs/stable/update.html#update-using-values-from-a-different-table

also, it feels like it would be cleaner if the INSERT was done in a different loop after the one that scans through it. i don't think we are allowed to execute other statements while the internal executor still has an iterator open.

@andyyang890 andyyang890 force-pushed the role_members_notnull branch 3 times, most recently from cf68983 to 747025a Compare December 22, 2022 20:26
Copy link
Collaborator Author

@andyyang890 andyyang890 left a comment

Choose a reason for hiding this comment

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

Added another test for restoring from a backup that did not have the ID columns!

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


pkg/ccl/backupccl/restore_job.go line 2858 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: can you make this use tree.DOid instead of oid.Oid? i think it's better to treat these all as datums

Done.


pkg/ccl/backupccl/system_schema.go line 252 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

can you make this a named return parameter, retErr? (see below comment)

Done.


pkg/ccl/backupccl/system_schema.go line 283 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

there should be a call like this here:

	defer func() { retErr = errors.CombineErrors(retErr, it.Close()) }()

actually, we'd want the same fix to be in roleOptionsRestoreFunc. the reason is that some errors are only returned when the iterator is closed, and this is important for correct error handling.

Done.


pkg/ccl/backupccl/system_schema.go line 318 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

i think you can avoid the need for the ids map, if you use an update ... from statement: https://www.cockroachlabs.com/docs/stable/update.html#update-using-values-from-a-different-table

also, it feels like it would be cleaner if the INSERT was done in a different loop after the one that scans through it. i don't think we are allowed to execute other statements while the internal executor still has an iterator open.

I've moved it into a separate loop after the iterator loop. I think you can't actually use UPDATE ... FROM here because the new table starts empty and we are inserting rows into it.

Copy link
Collaborator

@stevendanna stevendanna left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this up and adding the cross-version tests. I left a couple of questions.

pkg/ccl/backupccl/restore_job.go Outdated Show resolved Hide resolved
pkg/ccl/backupccl/system_schema.go Outdated Show resolved Hide resolved
pkg/ccl/backupccl/system_schema.go Outdated Show resolved Hide resolved
@andyyang890 andyyang890 force-pushed the role_members_notnull branch 2 times, most recently from bf6431b to 8b7d0fa Compare January 10, 2023 19:28
@andyyang890 andyyang890 force-pushed the role_members_notnull branch 3 times, most recently from 938f4e3 to c06835e Compare January 11, 2023 03:11
Copy link
Collaborator Author

@andyyang890 andyyang890 left a comment

Choose a reason for hiding this comment

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

Addressed comments and ready for another review!

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

Copy link
Collaborator

@stevendanna stevendanna left a comment

Choose a reason for hiding this comment

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

Thanks for adding the backup/restore tests. Overall the backup/restore parts look reasonable to me.

This patch sets the newly added `role_id` and `member_id` columns in
`system.role_members` to be NOT NULL. It also changes the `RESTORE`
logic to be able to handle the case when restoring from a backup where
the `system.role_members` table did not have the columns.

Release note: None
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.

lgtm!

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

@andyyang890
Copy link
Collaborator Author

TFTR!

bors r=stevendanna,rafiss

@craig
Copy link
Contributor

craig bot commented Jan 17, 2023

Build succeeded:

@craig craig bot merged commit c036635 into cockroachdb:master Jan 17, 2023
@andyyang890 andyyang890 deleted the role_members_notnull branch January 17, 2023 18:03
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.

4 participants