-
Notifications
You must be signed in to change notification settings - Fork 85
Use postgres UUID types for actee ids #1589
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
Open
alxndrsn
wants to merge
17
commits into
getodk:master
Choose a base branch
from
alxndrsn:use-postgres-uuid-types-for-actee-ids
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6b8cd1a
Actees: use postgres native UUID type for actees.id
e3dc1e0
Fix Datasets.canReadForOpenRosa()
f342e22
Merge branch 'master' into use-postgres-uuid-types-for-actee-ids
fe018df
skip migration tests which rely on prod code
e994a09
Merge branch 'master' into use-postgres-uuid-types-for-actee-ids
alxndrsn d9aec4f
Add missing file header
1ceaee0
Merge branch 'master' into use-postgres-uuid-types-for-actee-ids
alxndrsn 88a7679
use UUID v8
8ca97af
Merge branch 'master' into use-postgres-uuid-types-for-actee-ids
83ebf49
add missing foreign keys
aba23f6
Merge branch 'master' into use-postgres-uuid-types-for-actee-ids
alxndrsn 2c6dcaa
Merge branch 'master' into use-postgres-uuid-types-for-actee-ids
alxndrsn 431f7dd
test: add comment why it's now skipped
e3655b5
skip specific test and add comment explaining
04645f0
remove no-op skip
7358c81
remove unnecessary commit
f9fd902
Merge branch 'master' into use-postgres-uuid-types-for-actee-ids
alxndrsn 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Copyright 2025 ODK Central Developers | ||
| // See the NOTICE file at the top-level directory of this distribution and at | ||
| // https://github.com/getodk/central-backend/blob/master/NOTICE. | ||
| // This file is part of ODK Central. It is subject to the license terms in | ||
| // the LICENSE file found in the top-level directory of this distribution and at | ||
| // https://www.apache.org/licenses/LICENSE-2.0. No part of ODK Central, | ||
| // including this file, may be copied, modified, propagated, or distributed | ||
| // except according to the terms contained in the LICENSE file. | ||
|
|
||
| const { sql } = require('slonik'); | ||
|
|
||
| // Take care when changing these values - they should probably remain identical to those in the db | ||
| // migration where they were first defined - "actee-id-as-uuid". | ||
| const sqlSpecial = { | ||
| '*': sql`'00000000-0000-0000-0000-000000000001'`, // eslint-disable-line key-spacing | ||
| actor: sql`'00000000-0000-0000-0000-000000000002'`, // eslint-disable-line key-spacing | ||
| group: sql`'00000000-0000-0000-0000-000000000003'`, // eslint-disable-line key-spacing | ||
| user: sql`'00000000-0000-0000-0000-000000000004'`, // eslint-disable-line key-spacing | ||
| form: sql`'00000000-0000-0000-0000-000000000005'`, // eslint-disable-line key-spacing | ||
| submission: sql`'00000000-0000-0000-0000-000000000006'`, // eslint-disable-line key-spacing | ||
| field_key: sql`'00000000-0000-0000-0000-000000000007'`, // eslint-disable-line key-spacing | ||
| config: sql`'00000000-0000-0000-0000-000000000008'`, // eslint-disable-line key-spacing | ||
| project: sql`'00000000-0000-0000-0000-000000000009'`, // eslint-disable-line key-spacing | ||
| role: sql`'00000000-0000-0000-0000-000000000010'`, // eslint-disable-line key-spacing | ||
| assignment: sql`'00000000-0000-0000-0000-000000000011'`, // eslint-disable-line key-spacing | ||
| audit: sql`'00000000-0000-0000-0000-000000000012'`, // eslint-disable-line key-spacing | ||
| system: sql`'00000000-0000-0000-0000-000000000013'`, // eslint-disable-line key-spacing | ||
| singleUse: sql`'00000000-0000-0000-0000-000000000014'`, // eslint-disable-line key-spacing | ||
| dataset: sql`'00000000-0000-0000-0000-000000000015'`, // eslint-disable-line key-spacing | ||
| public_link: sql`'00000000-0000-0000-0000-000000000016'`, // eslint-disable-line key-spacing | ||
| }; | ||
|
|
||
| const uuidFor = acteeId => { | ||
| if (acteeId == null) return null; | ||
| else if (Object.prototype.hasOwnProperty.call(sqlSpecial, acteeId)) return sqlSpecial[acteeId]; | ||
| else if (acteeId.length === 36) return acteeId; | ||
| else throw new Error(`Unexpected acteeId: '${acteeId}'`); | ||
| }; | ||
|
|
||
| module.exports = { uuidFor }; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Copyright 2025 ODK Central Developers | ||
| // See the NOTICE file at the top-level directory of this distribution and at | ||
| // https://github.com/getodk/central-backend/blob/master/NOTICE. | ||
| // This file is part of ODK Central. It is subject to the license terms in | ||
| // the LICENSE file found in the top-level directory of this distribution and at | ||
| // https://www.apache.org/licenses/LICENSE-2.0. No part of ODK Central, | ||
| // including this file, may be copied, modified, propagated, or distributed | ||
| // except according to the terms contained in the LICENSE file. | ||
|
|
||
| /* eslint-disable key-spacing, indent */ | ||
|
|
||
| const fkTables = [ | ||
| 'actors', | ||
| 'assignments', | ||
| 'audits', | ||
| 'datasets', | ||
| 'forms', | ||
| 'projects', | ||
| ]; | ||
|
|
||
| // These values must never change. If special actees are added or removed in | ||
| // future, it is of no concern to this migration. | ||
| const specialActees = { | ||
| '*': '00000000-0000-0000-0000-000000000001', | ||
| actor: '00000000-0000-0000-0000-000000000002', | ||
| group: '00000000-0000-0000-0000-000000000003', | ||
| user: '00000000-0000-0000-0000-000000000004', | ||
| form: '00000000-0000-0000-0000-000000000005', | ||
| submission: '00000000-0000-0000-0000-000000000006', | ||
| field_key: '00000000-0000-0000-0000-000000000007', | ||
| config: '00000000-0000-0000-0000-000000000008', | ||
| project: '00000000-0000-0000-0000-000000000009', | ||
| role: '00000000-0000-0000-0000-000000000010', | ||
| assignment: '00000000-0000-0000-0000-000000000011', | ||
| audit: '00000000-0000-0000-0000-000000000012', | ||
| system: '00000000-0000-0000-0000-000000000013', | ||
| singleUse: '00000000-0000-0000-0000-000000000014', | ||
| dataset: '00000000-0000-0000-0000-000000000015', | ||
| public_link: '00000000-0000-0000-0000-000000000016', | ||
| }; | ||
| if (Object.keys(specialActees).length !== | ||
| new Set(Object.values(specialActees)).size) { | ||
| throw new Error('Check specialActees values are unique'); | ||
| } | ||
|
|
||
| const tableName = t => t.padStart(16, ' '); | ||
|
|
||
| const up = (db) => db.raw(` | ||
| -- drop constraints | ||
| ${fkTables.map(t => `ALTER TABLE ${tableName(t)} DROP CONSTRAINT IF EXISTS ${t}_acteeid_foreign;`).join('\n ')} | ||
|
|
||
| -- update references to their special values | ||
| ${Object.entries(specialActees).flatMap(([ str, uuid ]) => [ | ||
| `UPDATE actees SET id='${uuid}' WHERE id='${str}';`, | ||
| `UPDATE actees SET species='${uuid}' WHERE species='${str}';`, | ||
| ...fkTables.map(t => `UPDATE ${tableName(t)} SET "acteeId"='${uuid}' WHERE "acteeId"='${str}';`), | ||
| ]).join('\n ')} | ||
|
|
||
| -- change column types | ||
| ALTER TABLE actees ALTER COLUMN id TYPE UUID USING id::UUID; | ||
| ALTER TABLE actees ALTER COLUMN parent TYPE UUID USING parent::UUID; | ||
| ALTER TABLE actees ALTER COLUMN species TYPE UUID USING species::UUID; | ||
| ${fkTables.map(t => `ALTER TABLE ${tableName(t)} ALTER COLUMN "acteeId" TYPE UUID USING "acteeId"::UUID;`).join('\n ')} | ||
|
|
||
| -- add missing special actees | ||
| INSERT INTO actees (id, species) VALUES('${specialActees.system}', '${specialActees['*']}'); | ||
| INSERT INTO actees (id, species) VALUES('${specialActees.singleUse}', '${specialActees['*']}'); | ||
| INSERT INTO actees (id, species) VALUES('${specialActees.dataset}', '${specialActees['*']}'); | ||
| INSERT INTO actees (id, species) VALUES('${specialActees.public_link}', '${specialActees['*']}'); | ||
|
|
||
| -- re-add constraints | ||
| ALTER TABLE actees ADD CONSTRAINT actees_parent_foreign FOREIGN KEY(parent) REFERENCES actees(id); | ||
| ALTER TABLE actees ADD CONSTRAINT actees_species_foreign FOREIGN KEY(species) REFERENCES actees(id); | ||
| ${fkTables.map(t => `ALTER TABLE ${tableName(t)} ADD CONSTRAINT ${tableName(t)}_acteeid_foreign FOREIGN KEY("acteeId") REFERENCES actees(id);`).join('\n ')} | ||
| `); | ||
|
|
||
| const down = (db) => db.raw(` | ||
| -- TODO reverse all statements from up() | ||
| `); | ||
|
|
||
| module.exports = { up, down }; |
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
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
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.
I would not store these constants as sql-templates. That's a boxing (of sorts) for a particular use case for a particular DB driver. So I'd leave the base definition assumption-free which in this case would mean storing the UUIDs as string values as there is no UUID type in the stdlib.
And then of course you can construct this
sqlSpecialspecial purpose mapping automatically from that base assumption-free definition. Alternatively the ensql-ing it could take place insideuuidFor()though of course from a performance-aesthetics POV it's nicer to not repeat that work, which speaks for creating a mapping once on initialization. And then that sql-specific stuff should then probably go intoquery/actees.js, and read the table of constants from a newlib/constants/actees.jswhich contains just the mapping constants and no presumptions about the usage. (actees.jsis imo preferable toactz.js).