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

fix(schema_statements): add unique_rowid to pk_and_sequence_for #311

Merged
merged 2 commits into from
Jul 1, 2024

Conversation

BuonOmo
Copy link
Collaborator

@BuonOmo BuonOmo commented Jan 27, 2024

Allow to get the primary key in the default case.

This fixes two of the four failing test_pk_and_sequence_for*. The two still failing are:

test_pk_and_sequence_for_returns_nil_if_no_pk

      def test_pk_and_sequence_for_returns_nil_if_no_pk
        with_example_table "id integer" do
          assert_nil @connection.pk_and_sequence_for("ex") # => `["rowid", nil]`
        end
      end

the example table creation query:

CREATE TABLE ex(id integer)

The second query of #pk_and_sequence_for returns ["rowid", "public", nil]. And the SHOW CREATE TABLE ex confirms it:

CREATE TABLE public.ex (
	id INT8 NULL,
	rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(),
	CONSTRAINT ex_pkey PRIMARY KEY (rowid ASC)
)

test_pk_and_sequence_for_with_collision_pg_class_oid

create table ex(id serial primary key);
create table ex2(id serial primary key);
DELETE FROM pg_depend WHERE objid = 'ex_id_seq'::regclass AND refobjid = 'ex'::regclass AND deptype = 'a';

Gives the error:

user root does not have DELETE privilege on relation pg_depend

The bug is not related to the code, but just to the tests. @rafiss could we give a user this privilege ? I didn't see it skimming the doc... (show grants gives f for is_grantable on pg_depend...):

grant DELETE on pg_depend to root;
--> ERROR: invalid privilege type DELETE for virtual_table

On meta-testing. This took me longer than expected. I'm a bit unsure about the return on investment for simple tests, as manipulating AST can feel a bit tricky. I still pursued it because it seems that the gems (parser and unparser) are quite stable, and if we could use this not only in tests, that would be tremendous. We would need more guaranties though. And we'd need to verify the performances..

I was thinking about checking the hash of the method to be changed against a saved hash (which would be stored in an argument). If there is a change we warn. So:

CopyCat.copy_methods(..., hash: "somehash") do ... end
# => WARN: the method XXX was updated, here's the new changed method: ...
#      If it is ok change the hash to "somenewhash"

I think this is the way to go, but there are definitely some tweaks to make it easier.

# First try looking for a sequence with a dependency on the
# given table's primary key.
result = query(<<~SQL, "SCHEMA")[0]
SELECT attr.attname, nsp.nspname, seq.relname
Copy link
Collaborator Author

@BuonOmo BuonOmo Jan 27, 2024

Choose a reason for hiding this comment

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

@rafiss does this query make sense for CRDB ? All the tests I'm working with until now are giving a void result...

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, this looks correct

if you are getting no results, it's probably because CRDB defaults to not using sequences for SERIAL types. this can be changed with the serial_normalization setting: https://www.cockroachlabs.com/docs/stable/serial#modes-of-operation

@BuonOmo BuonOmo force-pushed the fix/pk_and_sequence_for branch 2 times, most recently from dd54d68 to c6e953e Compare January 27, 2024 21:24
@BuonOmo BuonOmo self-assigned this Jan 28, 2024
@BuonOmo
Copy link
Collaborator Author

BuonOmo commented Mar 29, 2024

Somehow the test test_pk_and_sequence_for_returns_nil_if_no_pk works locally and not on CI...

Copy link
Contributor

@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.

Gives the error:

user root does not have DELETE privilege on relation pg_depend

The bug is not related to the code, but just to the tests. @rafiss could we give a user this privilege ? I didn't see it skimming the doc... (show grants gives f for is_grantable on pg_depend...):

Unfortunately, pg_depend is view-only in CRDB, unlike PG. In CRDB there is no way to "artificially break" the dependency that the sequence has on the table.

# First try looking for a sequence with a dependency on the
# given table's primary key.
result = query(<<~SQL, "SCHEMA")[0]
SELECT attr.attname, nsp.nspname, seq.relname
Copy link
Contributor

Choose a reason for hiding this comment

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

yes, this looks correct

if you are getting no results, it's probably because CRDB defaults to not using sequences for SERIAL types. this can be changed with the serial_normalization setting: https://www.cockroachlabs.com/docs/stable/serial#modes-of-operation

Allow to get the primary key in the default case.
@BuonOmo
Copy link
Collaborator Author

BuonOmo commented Jun 26, 2024

About test_pk_and_sequence_for_returns_nil_if_no_pk. The table creation returns a line:

rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid()

This line is the one that is failing us...

@@ -68,14 +68,19 @@ def pk_and_sequence_for(table) # :nodoc:
pg_attribute attr,
pg_depend dep,
pg_constraint cons,
pg_namespace nsp
pg_namespace nsp,
-- TODO: use the pg_catalog.pg_attribute(attishidden) column when it's added instead of joining on crdb_internal.
Copy link
Contributor

Choose a reason for hiding this comment

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

it could be useful to link to cockroachdb/cockroach#126397 in the TODO, so we can easily see when the new column is in a CRDB release.

@BuonOmo
Copy link
Collaborator Author

BuonOmo commented Jun 29, 2024

It should be good for merging now! I think I'll work on the transaction related issues next, and then we'll almost be done with those red tests.

Copy link
Contributor

@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!

@rafiss rafiss merged commit 0f9a418 into master Jul 1, 2024
0 of 4 checks passed
@rafiss rafiss deleted the fix/pk_and_sequence_for branch July 1, 2024 14:39
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.

2 participants