-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Policies): nested data inception
- Loading branch information
Showing
2 changed files
with
43 additions
and
2 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,41 @@ | ||
'use strict'; | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
await queryInterface.sequelize.query(` | ||
WITH updated_collectives AS ( | ||
UPDATE "Collectives" | ||
SET "data" = | ||
-- Merge "data" into nested data, to make sure newly created keys aren't dropped | ||
(data->'data' || data) | ||
-- Make sure any policy created in (data->'data'->'policies'->'EXPENSE_POLICIES') is merged with the top level policies | ||
|| JSONB_BUILD_OBJECT( | ||
'policies', | ||
COALESCE(data->'data'->'policies', '{}') | ||
|| COALESCE(data->'policies', '{}') | ||
|| JSONB_BUILD_OBJECT('EXPENSE_POLICIES', COALESCE(data->'data'->'policies'->'EXPENSE_POLICIES', '{}')) | ||
) | ||
WHERE data ? 'data' | ||
RETURNING id, data | ||
) INSERT INTO "MigrationLogs" ("type", "description", "data", "createdAt") | ||
SELECT | ||
'MIGRATION', | ||
'20250130072503-fix-nested-collecives-data', | ||
jsonb_agg(jsonb_build_object('id', id, 'data', data)), | ||
NOW() | ||
FROM updated_collectives | ||
RETURNING id | ||
`); | ||
|
||
await queryInterface.sequelize.query(` | ||
UPDATE "Collectives" | ||
SET "data" = data - 'data' | ||
WHERE data ? 'data' | ||
`); | ||
}, | ||
|
||
async down(queryInterface, Sequelize) { | ||
console.log('Please look at the migration logs to see the data that was migrated'); | ||
}, | ||
}; |
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