From e4c676a5e61e2e28c4c6049bb549adf19f509db7 Mon Sep 17 00:00:00 2001 From: Seung Park Date: Fri, 3 May 2024 10:12:44 -0400 Subject: [PATCH 1/2] migration script for bad repo branches --- modules/persistence/DOP-4571.ts | 43 +++++++++++++++++++ .../src/services/connector/index.ts | 2 +- modules/persistence/tsconfig.json | 2 +- 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 modules/persistence/DOP-4571.ts diff --git a/modules/persistence/DOP-4571.ts b/modules/persistence/DOP-4571.ts new file mode 100644 index 000000000..3927425c7 --- /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.publishOriginalBranch = true; + } + }); + if (problematicBranches.length) { + console.log(`updating repo with id ${repo._id} (${repo.repoName}) for branches ${problematicBranches}`); + await collection.updateOne({ _id: repo._id }, repo); + } + } + + 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"] } From e62711103ecc4a175faefd28eebe629d94b2ee78 Mon Sep 17 00:00:00 2001 From: Seung Park Date: Fri, 3 May 2024 11:09:01 -0400 Subject: [PATCH 2/2] fix update fn --- modules/persistence/DOP-4571.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/persistence/DOP-4571.ts b/modules/persistence/DOP-4571.ts index 3927425c7..dc14e6a76 100644 --- a/modules/persistence/DOP-4571.ts +++ b/modules/persistence/DOP-4571.ts @@ -22,12 +22,12 @@ const migrate = async () => { !branch.urlAliases?.includes(branch.urlSlug) ) { problematicBranches.push(branch.gitBranchName); - branch.publishOriginalBranch = true; + 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 }, repo); + await collection.updateOne({ _id: repo._id }, { $set: { branches: repo.branches } }); } }