Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions .migrate
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
{
"lastRun": "1744651967487-add-modules.ts",
"lastRun": "1749998690421-change-mediawiki-namespace-field.ts",
"migrations": [
{
"title": "1738072787797-add-activated-field-to-modules.ts",
"timestamp": 1750008625370
},
{
"title": "1744651967487-add-modules.ts",
"timestamp": 1744652563722
"timestamp": 1750008625414
},
{
"title": "1749998690421-change-mediawiki-namespace-field.ts",
"timestamp": 1750008625431
}
]
}
}
4 changes: 2 additions & 2 deletions src/docs/platform.doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ paths:
type: string
description: Metadata for Notion.
- type: object
required: [baseURL, path, namespace]
required: [baseURL, path, namespaces]
properties:
baseURL:
type: string
path:
type: string
namespace:
namespaces:
type: array
items:
type: number
Expand Down
29 changes: 0 additions & 29 deletions src/migrations/db/1738072787797-add-activated-field-to-modules.ts

This file was deleted.

64 changes: 0 additions & 64 deletions src/migrations/db/1744651967487-add-modules.ts

This file was deleted.

144 changes: 144 additions & 0 deletions src/migrations/db/1749998690421-change-mediawiki-namespace-field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.');
}
};
4 changes: 2 additions & 2 deletions src/validations/platform.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const mediaWikiUpdateMetadata = () => {
return Joi.object().keys({
baseURL: Joi.string().required(),
path: Joi.string().required(),
namespace: Joi.array().items(Joi.number()).required(),
namespaces: Joi.array().items(Joi.number()).required(),
});
};

Expand Down Expand Up @@ -94,7 +94,7 @@ const mediaWikiMetadata = () => {
return Joi.object().keys({
baseURL: Joi.string().required(),
path: Joi.string().required(),
namespace: Joi.array().items(Joi.number()).required(),
namespaces: Joi.array().items(Joi.number()).required(),
});
};

Expand Down