-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathsanitize-collective-tags.ts
48 lines (43 loc) · 1.36 KB
/
sanitize-collective-tags.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import '../server/env';
import { sanitizeTags } from '../server/lib/tags';
import models, { Op } from '../server/models';
const sanitizeAllCollectiveTags = async () => {
console.log('Sanitizing all Collective tags...');
const collectives = await models.Collective.findAll({
attributes: ['id', 'tags'],
where: {
tags: {
[Op.ne]: null,
},
},
});
for (const collective of collectives) {
const sanitizedTags = sanitizeTags(collective.tags);
// Check if sanitized tags are different from the current tags
if (JSON.stringify(sanitizedTags) !== JSON.stringify(collective.tags)) {
try {
await models.Collective.update(
{
tags: sanitizedTags,
},
{
hooks: false,
where: {
id: collective.id,
},
},
);
console.log(
`Successfully updated tags for Collective with id: ${collective.id} - from [${collective.tags?.map(
tag => `"${tag}"`,
)}] to ${sanitizedTags ? `[${sanitizedTags.map(tag => `"${tag}"`)}]` : 'NULL'}`,
);
} catch (error) {
console.error(`Error while updating tags for Collective with id: ${collective.id} - ${error.message}`);
}
}
}
console.log('Done sanitizing Collective tags!');
process.exit();
};
sanitizeAllCollectiveTags();