-
Notifications
You must be signed in to change notification settings - Fork 142
tapdb: fix postgres sequence desyncs #2047
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -- Sequence reset is forward-only; no rollback needed. | ||
| SELECT 1; |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,11 @@ | ||||||||||||||
| -- Reset BIGSERIAL sequences that were left behind by migrations | ||||||||||||||
| -- that inserted rows with explicit id values. | ||||||||||||||
| -- | ||||||||||||||
| -- Migration 31 copied universe_leaves rows with explicit ids. | ||||||||||||||
| -- Migration 40 inserted supply_commit_states (0-6) and | ||||||||||||||
| -- supply_commit_update_types (0-2) with explicit ids. | ||||||||||||||
| -- | ||||||||||||||
| -- On SQLite these are no-ops (replaced via sqliteSchemaReplacements). | ||||||||||||||
| SELECT setval(pg_get_serial_sequence('universe_leaves', 'id'), COALESCE((SELECT MAX(id) FROM universe_leaves), 0)); | ||||||||||||||
| SELECT setval(pg_get_serial_sequence('supply_commit_states', 'id'), COALESCE((SELECT MAX(id) FROM supply_commit_states), 0)); | ||||||||||||||
| SELECT setval(pg_get_serial_sequence('supply_commit_update_types', 'id'), COALESCE((SELECT MAX(id) FROM supply_commit_update_types), 0)); | ||||||||||||||
|
Comment on lines
+9
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In PostgreSQL,
Suggested change
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,9 +39,21 @@ const ( | |
|
|
||
| var ( | ||
| // sqliteSchemaReplacements is a map of schema strings that need to be | ||
| // replaced for sqlite. There currently aren't any replacements, because | ||
| // the SQL files are written with SQLite compatibility in mind. | ||
| sqliteSchemaReplacements = map[string]string{} | ||
| // replaced for sqlite. | ||
| sqliteSchemaReplacements = map[string]string{ | ||
| "SELECT setval(pg_get_serial_sequence(" + | ||
| "'universe_leaves', 'id'), COALESCE((" + | ||
| "SELECT MAX(id) FROM universe_leaves" + | ||
| "), 0));": "SELECT 1;", | ||
| "SELECT setval(pg_get_serial_sequence(" + | ||
| "'supply_commit_states', 'id'), " + | ||
| "COALESCE((SELECT MAX(id) FROM " + | ||
| "supply_commit_states), 0));": "SELECT 1;", | ||
| "SELECT setval(pg_get_serial_sequence(" + | ||
| "'supply_commit_update_types', 'id'), " + | ||
| "COALESCE((SELECT MAX(id) FROM " + | ||
| "supply_commit_update_types), 0));": "SELECT 1;", | ||
| } | ||
|
Comment on lines
+44
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The strings in |
||
| ) | ||
|
|
||
| // SqliteConfig holds all the config arguments needed to interact with our | ||
|
|
||
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.
The check
lastValue >= maxIDis not sufficient to guarantee that the next auto-incremented ID will not collide with existing data. In PostgreSQL, iflast_valueis equal tomaxIDbut theis_calledflag isfalse, the next call tonextval()will returnmaxID, causing a primary key violation. A more robust check would involve verifying that the effective next value (accounting foris_called) is strictly greater thanmaxID. Additionally, when constructing SQL queries with identifiers dynamically, it is safer to use quoted identifiers (e.g., via%qinfmt.Sprintf) to avoid issues with reserved words or case sensitivity.