-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path1749998690421-change-mediawiki-namespace-field.ts
More file actions
144 lines (113 loc) · 4.25 KB
/
1749998690421-change-mediawiki-namespace-field.ts
File metadata and controls
144 lines (113 loc) · 4.25 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import 'dotenv/config';
import mongoose from 'mongoose';
import { Module, ModuleNames, Platform, PlatformNames } from '@togethercrew.dev/db';
import config from '../../config';
import logger from '../../config/logger';
async function connectToMongoDB() {
try {
await mongoose.connect(config.mongoose.serverURL);
logger.info('Connected to MongoDB!');
} catch (error) {
logger.fatal('Failed to connect to MongoDB!');
throw error;
}
}
export const up = async () => {
await connectToMongoDB();
try {
const platforms = await Platform.find({ name: PlatformNames.MediaWiki });
logger.info(`Found ${platforms.length} MediaWiki platforms to update.`);
for (const platform of platforms) {
const metadata = platform.metadata || {};
if ('namespace' in metadata) {
metadata.namespaces = [0];
delete metadata.namespace;
} else {
metadata.namespaces = [0];
}
await Platform.updateOne({ _id: platform._id }, { $set: { metadata } });
logger.info(`Updated platform ${platform._id} metadata`);
}
const modules = await Module.find({ name: ModuleNames.Hivemind }).exec();
logger.info(`Found ${modules.length} hivemind modules to update.`);
for (const module of modules) {
if (!module.options?.platforms) continue;
const mediaWikiPlatform = module.options.platforms.find((p) => p.name === PlatformNames.MediaWiki);
if (!mediaWikiPlatform) continue;
const metadata = mediaWikiPlatform.metadata || {};
let needsUpdate = false;
if (!mediaWikiPlatform.metadata || 'namespace' in metadata || !('namespaces' in metadata)) {
needsUpdate = true;
if ('namespace' in metadata) {
metadata.namespaces = [0];
delete metadata.namespace;
} else {
metadata.namespaces = [0];
}
}
if (needsUpdate) {
await Module.updateOne(
{ _id: module._id },
{ $set: { 'options.platforms.$[platform].metadata': metadata } },
{ arrayFilters: [{ 'platform.name': 'mediaWiki' }] },
);
logger.info(`Updated hivemind module ${module._id} platform metadata`);
}
}
logger.info('Successfully updated all MediaWiki platforms and modules');
} catch (error) {
logger.error('Error during migration up:', error);
throw error;
} finally {
await mongoose.connection.close();
logger.info('MongoDB connection closed.');
}
};
export const down = async () => {
await connectToMongoDB();
try {
const platforms = await Platform.find({ name: PlatformNames.MediaWiki });
logger.info(`Found ${platforms.length} MediaWiki platforms to revert.`);
for (const platform of platforms) {
const metadata = platform.metadata || {};
if ('namespaces' in metadata) {
metadata.namespace = metadata.namespaces[0];
delete metadata.namespaces;
}
await Platform.updateOne({ _id: platform._id }, { $set: { metadata } });
logger.info(`Reverted platform ${platform._id} metadata`);
}
const modules = await Module.find({ name: ModuleNames.Hivemind });
logger.info(`Found ${modules.length} hivemind modules to revert.`);
for (const module of modules) {
if (!module.options?.platforms) continue;
let needsUpdate = false;
const updatedPlatforms = module.options.platforms.map((platform) => {
if (platform.name === PlatformNames.MediaWiki) {
const metadata = platform.metadata || {};
if ('namespaces' in metadata) {
metadata.namespace = metadata.namespaces[0];
delete metadata.namespaces;
needsUpdate = true;
}
return {
...platform,
metadata,
};
}
return platform;
});
if (needsUpdate) {
await Module.updateOne({ _id: module._id }, { $set: { 'options.platforms': updatedPlatforms } });
logger.info(`Reverted hivemind module ${module._id} platform metadata`);
}
}
logger.info('Successfully reverted all MediaWiki platforms and modules');
} catch (error) {
logger.error('Error during migration down:', error);
throw error;
} finally {
await mongoose.connection.close();
logger.info('MongoDB connection closed.');
}
};