Skip to content

Commit

Permalink
fix(Policies): nested data inception
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Jan 30, 2025
1 parent 421e385 commit 39caeeb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
41 changes: 41 additions & 0 deletions migrations/20250130072503-fix-nested-collecives-data.js
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');
},
};
4 changes: 2 additions & 2 deletions server/models/Collective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3746,7 +3746,7 @@ Collective.init(
receiptPolicy: sanitizeHTML(expensePolicy, optsSanitizeHtmlForSimplified),
},
};
this.setDataValue('data', { data: { ...data, policies: newPolicies } });
this.setDataValue('data', { ...data, policies: newPolicies });
} else {
const data = this.getDataValue('data');
const newPolicies = {
Expand All @@ -3757,7 +3757,7 @@ Collective.init(
receiptPolicy: '',
},
};
this.setDataValue('data', { data: { ...data, policies: newPolicies } });
this.setDataValue('data', { ...data, policies: newPolicies });
}
},
},
Expand Down

0 comments on commit 39caeeb

Please sign in to comment.