Skip to content

sql: disallow table/view/sequence rename from making cross DB references#61741

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
fqazi:renameCrossBug
Mar 15, 2021
Merged

sql: disallow table/view/sequence rename from making cross DB references#61741
craig[bot] merged 1 commit intocockroachdb:masterfrom
fqazi:renameCrossBug

Conversation

@fqazi
Copy link
Copy Markdown
Collaborator

@fqazi fqazi commented Mar 9, 2021

Fixes: #55709

Previously, disallowed cross DB references could be created
using a ALTER TABLE/VIEW/SEQUENCE rename operations. This
was inadequate because users could accidentally start using
deprecated functionality. To address this, this patch detects
such scenarios are returns an appropriate error.

Release note (bug fix): ALTER TABLE/VIEW/SEQUENCE can no longer
be used to incorrectly create cross DB references.

@fqazi fqazi requested a review from a team March 9, 2021 21:34
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@fqazi fqazi force-pushed the renameCrossBug branch 3 times, most recently from c7ed2c3 to 5ed5961 Compare March 10, 2021 14:04
Copy link
Copy Markdown
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 2 files at r1.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @fqazi)


pkg/sql/rename_table.go, line 190 at r1 (raw file):

		}
	}

nit: this is a pretty big chunk of new logic. Any interest in reworking this into some functions? It'll also allow for some early returns which I suspect will aid readability.

One thing I lament is how hard it is to write unit tests for this stuff. The reason is pretty clear: constructing descriptors is awful. My sincere dream is that we'll get to a point where we can write SQL strings to build descriptors in memory and then unit test stuff like this. The fact that we go all the way to the logic tests pretty much always makes me

Copy link
Copy Markdown
Contributor

@ajwerner ajwerner 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 @fqazi)


pkg/sql/rename_table.go, line 190 at r1 (raw file):

Previously, ajwerner wrote…

nit: this is a pretty big chunk of new logic. Any interest in reworking this into some functions? It'll also allow for some early returns which I suspect will aid readability.

One thing I lament is how hard it is to write unit tests for this stuff. The reason is pretty clear: constructing descriptors is awful. My sincere dream is that we'll get to a point where we can write SQL strings to build descriptors in memory and then unit test stuff like this. The fact that we go all the way to the logic tests pretty much always makes me

sad, I was missing the word sad.

Copy link
Copy Markdown
Collaborator Author

@fqazi fqazi 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 @ajwerner)


pkg/sql/rename_table.go, line 190 at r1 (raw file):

Previously, ajwerner wrote…

sad, I was missing the word sad.

Let me rework this into different functions, it would aid with readability.

@fqazi fqazi force-pushed the renameCrossBug branch 3 times, most recently from d691fad to e9c4ce4 Compare March 11, 2021 17:08
Copy link
Copy Markdown
Collaborator Author

@fqazi fqazi 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 @ajwerner and @postamar)


pkg/sql/logictest/testdata/logic_test/rename_table, line 466 at r1 (raw file):

Previously, postamar (Marius Posta) wrote…

This comment applies to all the lines added above: can you please conform the style of your SQL statements & queries to the existing standard? The existing standard is defined by https://sqlfum.pt/ which I'm told was built using the CRDB SQL parser.

Done.

@fqazi fqazi requested review from ajwerner and postamar March 11, 2021 17:09
Copy link
Copy Markdown
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @fqazi and @postamar)


pkg/sql/rename_table.go, line 325 at r2 (raw file):


					Required: true,

I think this should skip the cache. Generally in the context of schema changes we want a consistent view of descriptors.


pkg/sql/rename_table.go, line 429 at r2 (raw file):


			allDescriptors, err := p.Descriptors().GetAllDescriptors(ctx, p.txn)

This feels egregiously expensive. I'm pretty certain we track the backreference on the columns. See descpb.ColumnDescriptor.OwnsSequenceIDs

Fixes: cockroachdb#55709

Previously, disallowed cross DB references could be created
using a ALTER TABLE/VIEW/SEQUENCE rename operations. This
was inadequate because users could accidentally start using
deprecated functionality. To address this, this patch detects
such scenarios are returns an appropriate error.

Release note (bug fix): ALTER TABLE/VIEW/SEQUENCE can no longer
be used to incorrectly create cross DB references.

Release justification: Low risk fix to avoid users from accidentally,
using deprecated functionality.
Copy link
Copy Markdown
Collaborator Author

@fqazi fqazi 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 @ajwerner and @postamar)


pkg/sql/rename_table.go, line 325 at r2 (raw file):

Previously, ajwerner wrote…

					Required: true,

I think this should skip the cache. Generally in the context of schema changes we want a consistent view of descriptors.

Done.


pkg/sql/rename_table.go, line 429 at r2 (raw file):

Previously, ajwerner wrote…

			allDescriptors, err := p.Descriptors().GetAllDescriptors(ctx, p.txn)

This feels egregiously expensive. I'm pretty certain we track the backreference on the columns. See descpb.ColumnDescriptor.OwnsSequenceIDs

Done

@fqazi fqazi requested a review from ajwerner March 15, 2021 15:05
Copy link
Copy Markdown
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 1 of 1 files at r3.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ajwerner and @postamar)

@fqazi
Copy link
Copy Markdown
Collaborator Author

fqazi commented Mar 15, 2021

bors r+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Mar 15, 2021

Build succeeded:

@craig craig bot merged commit a9b2779 into cockroachdb:master Mar 15, 2021
@rafiss rafiss added this to the 21.1 milestone Apr 22, 2021
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: renaming tables into a different parent database allows creating prohibited cross-database references

5 participants