Skip to content

Commit c2dd333

Browse files
Merge pull request #477 from TogetherCrew/mediawiki-changes
Mediawiki changes
2 parents 90eaac8 + bdd3aca commit c2dd333

6 files changed

Lines changed: 159 additions & 100 deletions

.migrate

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
{
2-
"lastRun": "1744651967487-add-modules.ts",
2+
"lastRun": "1749998690421-change-mediawiki-namespace-field.ts",
33
"migrations": [
4+
{
5+
"title": "1738072787797-add-activated-field-to-modules.ts",
6+
"timestamp": 1750008625370
7+
},
48
{
59
"title": "1744651967487-add-modules.ts",
6-
"timestamp": 1744652563722
10+
"timestamp": 1750008625414
11+
},
12+
{
13+
"title": "1749998690421-change-mediawiki-namespace-field.ts",
14+
"timestamp": 1750008625431
715
}
816
]
9-
}
17+
}

src/docs/platform.doc.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ paths:
165165
type: string
166166
description: Metadata for Notion.
167167
- type: object
168-
required: [baseURL, path, namespace]
168+
required: [baseURL, path, namespaces]
169169
properties:
170170
baseURL:
171171
type: string
172172
path:
173173
type: string
174-
namespace:
174+
namespaces:
175175
type: array
176176
items:
177177
type: number

src/migrations/db/1738072787797-add-activated-field-to-modules.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/migrations/db/1744651967487-add-modules.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import 'dotenv/config';
2+
3+
import mongoose from 'mongoose';
4+
5+
import { Module, ModuleNames, Platform, PlatformNames } 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 platforms = await Platform.find({ name: PlatformNames.MediaWiki });
25+
logger.info(`Found ${platforms.length} MediaWiki platforms to update.`);
26+
27+
for (const platform of platforms) {
28+
const metadata = platform.metadata || {};
29+
30+
if ('namespace' in metadata) {
31+
metadata.namespaces = [0];
32+
delete metadata.namespace;
33+
} else {
34+
metadata.namespaces = [0];
35+
}
36+
37+
await Platform.updateOne({ _id: platform._id }, { $set: { metadata } });
38+
39+
logger.info(`Updated platform ${platform._id} metadata`);
40+
}
41+
42+
const modules = await Module.find({ name: ModuleNames.Hivemind }).exec();
43+
logger.info(`Found ${modules.length} hivemind modules to update.`);
44+
45+
for (const module of modules) {
46+
if (!module.options?.platforms) continue;
47+
48+
const mediaWikiPlatform = module.options.platforms.find((p) => p.name === PlatformNames.MediaWiki);
49+
if (!mediaWikiPlatform) continue;
50+
51+
const metadata = mediaWikiPlatform.metadata || {};
52+
let needsUpdate = false;
53+
54+
if (!mediaWikiPlatform.metadata || 'namespace' in metadata || !('namespaces' in metadata)) {
55+
needsUpdate = true;
56+
57+
if ('namespace' in metadata) {
58+
metadata.namespaces = [0];
59+
delete metadata.namespace;
60+
} else {
61+
metadata.namespaces = [0];
62+
}
63+
}
64+
65+
if (needsUpdate) {
66+
await Module.updateOne(
67+
{ _id: module._id },
68+
{ $set: { 'options.platforms.$[platform].metadata': metadata } },
69+
{ arrayFilters: [{ 'platform.name': 'mediaWiki' }] },
70+
);
71+
logger.info(`Updated hivemind module ${module._id} platform metadata`);
72+
}
73+
}
74+
75+
logger.info('Successfully updated all MediaWiki platforms and modules');
76+
} catch (error) {
77+
logger.error('Error during migration up:', error);
78+
throw error;
79+
} finally {
80+
await mongoose.connection.close();
81+
logger.info('MongoDB connection closed.');
82+
}
83+
};
84+
85+
export const down = async () => {
86+
await connectToMongoDB();
87+
88+
try {
89+
const platforms = await Platform.find({ name: PlatformNames.MediaWiki });
90+
logger.info(`Found ${platforms.length} MediaWiki platforms to revert.`);
91+
92+
for (const platform of platforms) {
93+
const metadata = platform.metadata || {};
94+
95+
if ('namespaces' in metadata) {
96+
metadata.namespace = metadata.namespaces[0];
97+
delete metadata.namespaces;
98+
}
99+
100+
await Platform.updateOne({ _id: platform._id }, { $set: { metadata } });
101+
102+
logger.info(`Reverted platform ${platform._id} metadata`);
103+
}
104+
105+
const modules = await Module.find({ name: ModuleNames.Hivemind });
106+
logger.info(`Found ${modules.length} hivemind modules to revert.`);
107+
108+
for (const module of modules) {
109+
if (!module.options?.platforms) continue;
110+
111+
let needsUpdate = false;
112+
const updatedPlatforms = module.options.platforms.map((platform) => {
113+
if (platform.name === PlatformNames.MediaWiki) {
114+
const metadata = platform.metadata || {};
115+
116+
if ('namespaces' in metadata) {
117+
metadata.namespace = metadata.namespaces[0];
118+
delete metadata.namespaces;
119+
needsUpdate = true;
120+
}
121+
122+
return {
123+
...platform,
124+
metadata,
125+
};
126+
}
127+
return platform;
128+
});
129+
130+
if (needsUpdate) {
131+
await Module.updateOne({ _id: module._id }, { $set: { 'options.platforms': updatedPlatforms } });
132+
logger.info(`Reverted hivemind module ${module._id} platform metadata`);
133+
}
134+
}
135+
136+
logger.info('Successfully reverted all MediaWiki platforms and modules');
137+
} catch (error) {
138+
logger.error('Error during migration down:', error);
139+
throw error;
140+
} finally {
141+
await mongoose.connection.close();
142+
logger.info('MongoDB connection closed.');
143+
}
144+
};

src/validations/platform.validation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const mediaWikiUpdateMetadata = () => {
3939
return Joi.object().keys({
4040
baseURL: Joi.string().required(),
4141
path: Joi.string().required(),
42-
namespace: Joi.array().items(Joi.number()).required(),
42+
namespaces: Joi.array().items(Joi.number()).required(),
4343
});
4444
};
4545

@@ -94,7 +94,7 @@ const mediaWikiMetadata = () => {
9494
return Joi.object().keys({
9595
baseURL: Joi.string().required(),
9696
path: Joi.string().required(),
97-
namespace: Joi.array().items(Joi.number()).required(),
97+
namespaces: Joi.array().items(Joi.number()).required(),
9898
});
9999
};
100100

0 commit comments

Comments
 (0)