Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate collective's expense & contribution policy to html
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
migrations/20201125113750-migrate-collective-policies-to-html.js
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,73 @@ | ||
'use strict'; | ||
|
||
import showdown from 'showdown'; | ||
import { buildSanitizerOptions, sanitizeHTML } from '../server/lib/sanitize-html'; | ||
|
||
const converter = new showdown.Converter(); | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
const collectivesWithExpensePolicy = await queryInterface.sequelize.query( | ||
` | ||
SELECT "id", "expensePolicy" FROM "Collectives" | ||
WHERE LENGTH("expensePolicy") > 0 AND "expensePolicy" NOT LIKE '<%' | ||
AND "deletedAt" IS NULL; | ||
`, | ||
{ type: Sequelize.QueryTypes.SELECT }, | ||
); | ||
|
||
const collectivesWithContribPolicy = await queryInterface.sequelize.query( | ||
` | ||
SELECT "id", "contributionPolicy" FROM "Collectives" | ||
WHERE LENGTH("contributionPolicy") > 0 AND "contributionPolicy" NOT LIKE '<%' | ||
AND "deletedAt" IS NULL; | ||
`, | ||
{ type: Sequelize.QueryTypes.SELECT }, | ||
); | ||
|
||
const sanitizeOptions = buildSanitizerOptions({ | ||
basicTextFormatting: true, | ||
links: true, | ||
}); | ||
|
||
for (const collective of collectivesWithExpensePolicy) { | ||
const htmlContent = converter.makeHtml(collective.expensePolicy); | ||
const sanitizedContent = sanitizeHTML(htmlContent, sanitizeOptions); | ||
await queryInterface.sequelize.query( | ||
` | ||
UPDATE "Collectives" c | ||
SET "expensePolicy" = :expensePolicy | ||
WHERE c.id = :id | ||
`, | ||
{ | ||
replacements: { | ||
expensePolicy: sanitizedContent, | ||
id: collective.id, | ||
}, | ||
}, | ||
); | ||
} | ||
|
||
for (const collective of collectivesWithContribPolicy) { | ||
const htmlContent = converter.makeHtml(collective.contributionPolicy); | ||
const sanitizedContent = sanitizeHTML(htmlContent, sanitizeOptions); | ||
await queryInterface.sequelize.query( | ||
` | ||
UPDATE "Collectives" c | ||
SET "contributionPolicy" = :contributionPolicy | ||
WHERE c.id = :id | ||
`, | ||
{ | ||
replacements: { | ||
contributionPolicy: sanitizedContent, | ||
id: collective.id, | ||
}, | ||
}, | ||
); | ||
} | ||
}, | ||
|
||
down: async () => { | ||
// can't rollback this one | ||
}, | ||
}; |