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

riverdatabasesql driver inserts empty unique key arrays instead of null #650

Open
wydengyre opened this issue Oct 17, 2024 · 6 comments
Open
Labels
bug Something isn't working

Comments

@wydengyre
Copy link

wydengyre commented Oct 17, 2024

River migration 6 down fails in river 0.13.0, at least when using the riverdatabasesql driver, when there are periodic jobs in the job table. It fails with the following message:

ERROR: could not create unique index "river_job_kind_unique_key_idx" (SQLSTATE 23505))

The relevant line from the migration is here:

CREATE UNIQUE INDEX IF NOT EXISTS river_job_kind_unique_key_idx ON river_job (kind, unique_key) WHERE unique_key IS NOT NULL;

It appears that the periodic jobs, rather than have their unique key set to NULL, instead just have an empty byte array (\x). As a result, they violate the unique constraint. Perhaps this was different in past versions of river?

@bgentry
Copy link
Contributor

bgentry commented Oct 19, 2024

Dang, I think this ends up being a case where the migration isn't fully reversible if there's existing data conflicting with the index. 🤔 For performing the reverse migration, it's probably only going to be possible if you first clear or finish processing the existing unique jobs.

I'm not sure if there's anything that can be done to make this cleaner—really I wish we had just gotten this more flexible unique jobs implementation in the first place.

@brandur
Copy link
Contributor

brandur commented Dec 18, 2024

@bgentry Is this possibly indicative of a bug or at least unexpected behavior? If we were to be storing an empty byte array, that probably means there wasn't supposed to be any unique considerations, and I would've expected that to a NULL.

@bgentry
Copy link
Contributor

bgentry commented Dec 18, 2024

@brandur most likely yes, wish I had thought about that before. As I've verified this extensively on the pgx driver (just checked a long running live installation locally) it's probably something specific to dbsql.

@bgentry
Copy link
Contributor

bgentry commented Dec 18, 2024

@brandur yep, confirmed it's a dbsql specific thing with the extra driver test coverage here. Working on figuring out the cause/fix.

@bgentry
Copy link
Contributor

bgentry commented Dec 18, 2024

@brandur it seems to be that the pq.Array type being used for the [][]byte input for the multiple unique key byte slices isn't handling nil slices correctly, inserting an empty slice instead of a NULL. Haven't yet been able to figure out a fix, might be related to sqlc-dev/sqlc#3021 or some other sqlc + pq issue.

@bgentry bgentry changed the title migration 6 down failure riverdatabasesql driver inserts empty unique key arrays instead of null Dec 18, 2024
@bgentry bgentry added the bug Something isn't working label Dec 18, 2024
@brandur
Copy link
Contributor

brandur commented Dec 18, 2024

Ah dang it. Yeah, that pq.Array is awful to work with and has caused problems before. Not surprised.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants