Skip to content

Commit

Permalink
Migrate db, fill article.contributors with empty array
Browse files Browse the repository at this point in the history
  • Loading branch information
nonumpa committed Mar 28, 2024
1 parent 0d5c7b5 commit 65df997
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions migration/addContributors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ export const getAllContributors = async (
return [...contributors.values()];
};

/**
* Get all ydocs with a scroll size of 100
*/
const forEachYdoc = async (callback) => {
let scroll_id,
processedCount = 0,
Expand Down Expand Up @@ -210,14 +213,43 @@ const getUserId = async (user: string) => {
return userId;
};

/**
* Fill contributors filed with empty array
*/
async function initContributors() {
await client
.updateByQuery({
index: 'articles',
type: 'doc',
body: {
script: {
lang: 'painless',
source: `
if (ctx._source.contributors === null) {
ctx._source.contributors = new ArrayList();
} else {
ctx.op = 'noop';
}
`,
},
},
refresh: true,
conflicts: 'proceed',
})
.then((resp) => console.log(resp))
.catch((e) => console.error(JSON.stringify(e, null, ' ')));
}

async function main() {
// list all ydocs
await initContributors();

// list all ydocs with a scroll size of 100 and batch update the contributors field
await forEachYdoc(async (hits) => {
const operations = [];
await Promise.all(
hits.map(async ({ _id: id, _source: { ydoc: data, versions } }) => {
if (!id || !data || !versions) {
// maybe user click the transcribe button but did not save the transcript
// maybe the case is user click the transcribe button but did not save the transcript
console.log('ydoc error: id, data or versions null: ', id);
errorArticles.push(id);
return;
Expand Down

0 comments on commit 65df997

Please sign in to comment.