|
| 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 | +}; |
0 commit comments