-
Notifications
You must be signed in to change notification settings - Fork 184
Make SQLite primary keys AUTOINCREMENT
#1390
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
riverdriver/riverdatabasesql/migration/main/008_job_id_autoincrement.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| -- No-op. PostgreSQL sequences already prevent automatically generated job IDs | ||
| -- from being reused. | ||
| SELECT 1; | ||
3 changes: 3 additions & 0 deletions
3
riverdriver/riverdatabasesql/migration/main/008_job_id_autoincrement.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| -- No-op. PostgreSQL sequences already prevent automatically generated job IDs | ||
| -- from being reused. | ||
| SELECT 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
riverdriver/riverpgxv5/migration/main/008_job_id_autoincrement.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| -- No-op. PostgreSQL sequences already prevent automatically generated job IDs | ||
| -- from being reused. | ||
| SELECT 1; |
3 changes: 3 additions & 0 deletions
3
riverdriver/riverpgxv5/migration/main/008_job_id_autoincrement.up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| -- No-op. PostgreSQL sequences already prevent automatically generated job IDs | ||
| -- from being reused. | ||
| SELECT 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
riverdriver/riversqlite/migration/main/008_job_id_autoincrement.down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| -- Rebuild river_job to restore SQLite's default ROWID allocation behavior. | ||
|
|
||
| -- Rebuilding river_job would discard schema installed by River Pro. Check | ||
| -- schema objects instead of migration records to also catch manually applied | ||
| -- Pro migrations and the legacy workflow migration line. | ||
| CREATE TEMP TABLE river_job_pro_schema_guard ( | ||
| id integer NOT NULL | ||
| ); | ||
|
|
||
| CREATE TEMP TRIGGER river_job_pro_schema_guard_enforce | ||
| BEFORE INSERT ON river_job_pro_schema_guard | ||
| WHEN EXISTS ( | ||
| SELECT 1 | ||
| FROM /* TEMPLATE: schema */sqlite_master | ||
| WHERE name IN ('river_job_sequence', 'river_job_workflow_scheduling', 'river_workflow') | ||
| ) | ||
| BEGIN | ||
| SELECT RAISE(ABORT, 'River SQLite migration 008 cannot run while River Pro schema is installed'); | ||
| END; | ||
|
|
||
| INSERT INTO river_job_pro_schema_guard (id) VALUES (1); | ||
|
|
||
| DROP TRIGGER river_job_pro_schema_guard_enforce; | ||
| DROP TABLE river_job_pro_schema_guard; | ||
|
|
||
| DROP INDEX /* TEMPLATE: schema */river_job_kind; | ||
| DROP INDEX /* TEMPLATE: schema */river_job_state_and_finalized_at_index; | ||
| DROP INDEX /* TEMPLATE: schema */river_job_prioritized_fetching_index; | ||
| DROP INDEX /* TEMPLATE: schema */river_job_unique_idx; | ||
|
|
||
| ALTER TABLE /* TEMPLATE: schema */river_job RENAME TO river_job_old; | ||
|
|
||
| CREATE TABLE /* TEMPLATE: schema */river_job ( | ||
| id integer PRIMARY KEY, | ||
| args blob NOT NULL DEFAULT (jsonb('{}')), | ||
| attempt integer NOT NULL DEFAULT 0, | ||
| attempted_at timestamp, | ||
| attempted_by blob, -- json | ||
| created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| errors blob, -- json | ||
| finalized_at timestamp, | ||
| kind text NOT NULL, | ||
| max_attempts integer NOT NULL DEFAULT 25, | ||
| metadata blob NOT NULL DEFAULT (jsonb('{}')), | ||
| priority integer NOT NULL DEFAULT 1, | ||
| queue text NOT NULL DEFAULT 'default', | ||
| state text NOT NULL DEFAULT 'available', | ||
| scheduled_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| tags blob NOT NULL DEFAULT (jsonb('[]')), | ||
| unique_key blob, | ||
| unique_states integer, | ||
| CONSTRAINT finalized_or_finalized_at_null CHECK ( | ||
| (finalized_at IS NULL AND state NOT IN ('cancelled', 'completed', 'discarded')) OR | ||
| (finalized_at IS NOT NULL AND state IN ('cancelled', 'completed', 'discarded')) | ||
| ), | ||
| CONSTRAINT priority_in_range CHECK (priority >= 1 AND priority <= 4), | ||
| CONSTRAINT queue_length CHECK (length(queue) > 0 AND length(queue) < 128), | ||
| CONSTRAINT kind_length CHECK (length(kind) > 0 AND length(kind) < 128), | ||
| CONSTRAINT state_valid CHECK (state IN ('available', 'cancelled', 'completed', 'discarded', 'pending', 'retryable', 'running', 'scheduled')) | ||
| ); | ||
|
|
||
| INSERT INTO /* TEMPLATE: schema */river_job ( | ||
| id, | ||
| args, | ||
| attempt, | ||
| attempted_at, | ||
| attempted_by, | ||
| created_at, | ||
| errors, | ||
| finalized_at, | ||
| kind, | ||
| max_attempts, | ||
| metadata, | ||
| priority, | ||
| queue, | ||
| state, | ||
| scheduled_at, | ||
| tags, | ||
| unique_key, | ||
| unique_states | ||
| ) | ||
| SELECT | ||
| id, | ||
| args, | ||
| attempt, | ||
| attempted_at, | ||
| attempted_by, | ||
| created_at, | ||
| errors, | ||
| finalized_at, | ||
| kind, | ||
| max_attempts, | ||
| metadata, | ||
| priority, | ||
| queue, | ||
| state, | ||
| scheduled_at, | ||
| tags, | ||
| unique_key, | ||
| unique_states | ||
| FROM /* TEMPLATE: schema */river_job_old; | ||
|
|
||
| DROP TABLE /* TEMPLATE: schema */river_job_old; | ||
|
|
||
| CREATE INDEX /* TEMPLATE: schema */river_job_kind ON river_job (kind); | ||
| CREATE INDEX /* TEMPLATE: schema */river_job_state_and_finalized_at_index ON river_job (state, finalized_at) WHERE finalized_at IS NOT NULL; | ||
| CREATE INDEX /* TEMPLATE: schema */river_job_prioritized_fetching_index ON river_job (state, queue, priority, scheduled_at, id); | ||
| CREATE UNIQUE INDEX /* TEMPLATE: schema */river_job_unique_idx ON river_job (unique_key) | ||
| WHERE unique_key IS NOT NULL | ||
| AND unique_states IS NOT NULL | ||
| AND CASE state | ||
| WHEN 'available' THEN unique_states & (1 << 0) | ||
| WHEN 'cancelled' THEN unique_states & (1 << 1) | ||
| WHEN 'completed' THEN unique_states & (1 << 2) | ||
| WHEN 'discarded' THEN unique_states & (1 << 3) | ||
| WHEN 'pending' THEN unique_states & (1 << 4) | ||
| WHEN 'retryable' THEN unique_states & (1 << 5) | ||
| WHEN 'running' THEN unique_states & (1 << 6) | ||
| WHEN 'scheduled' THEN unique_states & (1 << 7) | ||
| ELSE 0 | ||
| END >= 1; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we include #1012 and #875 in this release so these aren't just no-ops?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh doh, forgot to close those, but actually they went out in migration 007. See:
https://github.com/riverqueue/river/blob/master/riverdriver/riverpgxv5/migration/main/007_notification_outbox_sqlite_jsonb_and_sql_cleanup.up.sql#L25-L37
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we actually got all the SQL cleanup back then and with nothing new coming in since.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should add to that I think this is kind of better because we can make the claim that if you're on Postgres, 008 is a pure no-op, like you don't need to do anything at all.