diff --git a/modules/persistence/DOP-4571.ts b/modules/persistence/DOP-4571.ts new file mode 100644 index 000000000..dc14e6a76 --- /dev/null +++ b/modules/persistence/DOP-4571.ts @@ -0,0 +1,43 @@ +import * as dotenv from 'dotenv'; +// dotenv.config() should be invoked immediately, before any other imports, to ensure config is present +dotenv.config(); + +import { pool, teardown } from './src/services/connector'; + +const migrate = async () => { + const db = await pool(); + const collection = db.collection('repos_branches'); + const repos = await collection.find().toArray(); + + for (let idx = 0; idx < repos.length; idx++) { + const problematicBranches: string[] = []; + const repo = repos[idx]; + // if (repo.branches.length === 1) { + // continue; + // } + repo.branches.forEach((branch) => { + if ( + !branch.publishOriginalBranchName && + branch.urlSlug === branch.gitBranchName && + !branch.urlAliases?.includes(branch.urlSlug) + ) { + problematicBranches.push(branch.gitBranchName); + branch.publishOriginalBranchName = true; + } + }); + if (problematicBranches.length) { + console.log(`updating repo with id ${repo._id} (${repo.repoName}) for branches ${problematicBranches}`); + await collection.updateOne({ _id: repo._id }, { $set: { branches: repo.branches } }); + } + } + + await teardown(); +}; + +migrate() + .then(() => { + console.log('finished'); + }) + .catch((e) => { + console.error(e); + }); diff --git a/modules/persistence/src/services/connector/index.ts b/modules/persistence/src/services/connector/index.ts index e06c7890f..84568894c 100644 --- a/modules/persistence/src/services/connector/index.ts +++ b/modules/persistence/src/services/connector/index.ts @@ -8,7 +8,7 @@ import { ObjectId, Db, Document } from 'mongodb'; import { db as poolDb } from './pool'; // We should only ever have one client active at a time. -const atlasURL = `mongodb+srv://${process.env.MONGO_ATLAS_USERNAME}:${process.env.MONGO_ATLAS_PASSWORD}@${process.env.MONGO_ATLAS_HOST}/?retryWrites=true&w=majority&maxPoolSize=20`; +const atlasURL = `mongodb://${process.env.MONGO_ATLAS_HOST}`; const client = new mongodb.MongoClient(atlasURL); export const teardown = async () => { diff --git a/modules/persistence/tsconfig.json b/modules/persistence/tsconfig.json index cc9fa7f7b..f5486a953 100644 --- a/modules/persistence/tsconfig.json +++ b/modules/persistence/tsconfig.json @@ -9,6 +9,6 @@ "baseUrl": "./", "types": ["adm-zip"] }, - "include": ["src/**/*.ts", "index.ts"], + "include": ["src/**/*.ts", "index.ts", "DOP-4571.ts"], "exclude": ["node_modules", "tests/**/*.ts"] }