-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2270 from HHS/TTAHUB-3187/fix_old_session_data
[TTAHUB-3187] populate regionId on old session report
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,45 @@ | ||
const { | ||
prepMigration, | ||
} = require('../lib/migration'); | ||
|
||
module.exports = { | ||
up: async (queryInterface) => queryInterface.sequelize.transaction( | ||
async (transaction) => { | ||
await prepMigration(queryInterface, transaction, __filename); | ||
await queryInterface.sequelize.query(/* sql */` | ||
-- One very old session lacks the regionId value | ||
-- This finds and sets it | ||
DROP TABLE IF EXISTS sr_updates; | ||
CREATE TEMP TABLE sr_updates | ||
AS | ||
WITH updater AS ( | ||
UPDATE "SessionReportPilots" srp | ||
SET data = JSONB_SET(srp.data,'{regionId}',TO_JSONB(erp."regionId")) | ||
FROM "EventReportPilots" erp | ||
WHERE erp.id = srp."eventId" | ||
AND srp.data->>'regionId' = '' | ||
RETURNING | ||
srp.id srpid, | ||
erp."regionId" | ||
) SELECT * FROM updater | ||
; | ||
SELECT * FROM sr_updates; | ||
-- Looks like: | ||
---------------------- | ||
-- srpid | regionId | ||
-- -------+---------- | ||
-- 2 | 3 | ||
`, { transaction }); | ||
}, | ||
), | ||
|
||
down: async (queryInterface) => queryInterface.sequelize.transaction( | ||
async (transaction) => { | ||
await prepMigration(queryInterface, transaction, __filename); | ||
// If we end up needing to revert this, it would be easier to use a separate | ||
// migration using the txid (or a similar identifier) after it's already set | ||
}, | ||
), | ||
}; |