Skip to content

Commit

Permalink
Merge pull request #2270 from HHS/TTAHUB-3187/fix_old_session_data
Browse files Browse the repository at this point in the history
[TTAHUB-3187] populate regionId on old session report
  • Loading branch information
hardwarehuman authored Jul 15, 2024
2 parents cbfcc54 + a6139fb commit 45f3b6e
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/migrations/20240715000000-fix-old-session-regions.js
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
},
),
};

0 comments on commit 45f3b6e

Please sign in to comment.