Skip to content

Commit d54f5e1

Browse files
committed
chore: add migration script to add module names
1 parent 7a690d8 commit d54f5e1

4 files changed

Lines changed: 75 additions & 7 deletions

File tree

.migrate

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
{
2-
"lastRun": "1738072787797-add-activated-field-to-modules.ts",
2+
"lastRun": "1744651967487-add-modules.ts",
33
"migrations": [
44
{
55
"title": "1738072787797-add-activated-field-to-modules.ts",
6-
"timestamp": 1744199444528
6+
"timestamp": 1744652563680
7+
},
8+
{
9+
"title": "1744651967487-add-modules.ts",
10+
"timestamp": 1744652563722
711
}
812
]
913
}

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"@notionhq/client": "^2.2.3",
2929
"@sentry/node": "^7.50.0",
3030
"@temporalio/client": "^1.11.3",
31-
"@togethercrew.dev/db": "^3.4.0",
31+
"@togethercrew.dev/db": "^3.5.0",
3232
"@togethercrew.dev/tc-messagebroker": "^0.0.50",
3333
"@types/express-session": "^1.17.7",
3434
"@types/morgan": "^1.9.5",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import 'dotenv/config';
2+
3+
import mongoose from 'mongoose';
4+
5+
import { Community, Module, ModuleNames } from '@togethercrew.dev/db';
6+
7+
import config from '../../config';
8+
import logger from '../../config/logger';
9+
10+
async function connectToMongoDB() {
11+
try {
12+
await mongoose.connect(config.mongoose.serverURL);
13+
logger.info('Connected to MongoDB!');
14+
} catch (error) {
15+
logger.fatal('Failed to connect to MongoDB!');
16+
throw error;
17+
}
18+
}
19+
20+
export const up = async () => {
21+
await connectToMongoDB();
22+
23+
try {
24+
const communities = await Community.find({}).exec();
25+
logger.info(`Found ${communities.length} communities.`);
26+
27+
const newModuleNames = [ModuleNames.Announcements, ModuleNames.CommunityHealth, ModuleNames.CommunityInsights];
28+
29+
for (const community of communities) {
30+
for (const moduleName of newModuleNames) {
31+
const newModule = new Module({
32+
name: moduleName,
33+
community: community._id,
34+
activated: true,
35+
options: { platforms: [] },
36+
});
37+
await newModule.save();
38+
logger.info(`Created module "${moduleName}" for community ${community._id}`);
39+
}
40+
}
41+
} catch (error) {
42+
logger.error('Error during migration up:', error);
43+
throw error;
44+
} finally {
45+
await mongoose.connection.close();
46+
logger.info('MongoDB connection closed.');
47+
}
48+
};
49+
50+
export const down = async () => {
51+
await connectToMongoDB();
52+
53+
try {
54+
const moduleNamesToDelete = [ModuleNames.Announcements, ModuleNames.CommunityHealth, ModuleNames.CommunityInsights];
55+
const result = await Module.deleteMany({ name: { $in: moduleNamesToDelete } });
56+
logger.info(`Migration down: deleted ${result.deletedCount} modules.`);
57+
} catch (error) {
58+
logger.error('Error during migration down:', error);
59+
throw error;
60+
} finally {
61+
await mongoose.connection.close();
62+
logger.info('MongoDB connection closed.');
63+
}
64+
};

0 commit comments

Comments
 (0)