From 65df9976d448ba81c051437ee9f2bc7187083a28 Mon Sep 17 00:00:00 2001 From: Nonumpa Date: Thu, 28 Mar 2024 16:48:27 +0800 Subject: [PATCH] Migrate db, fill article.contributors with empty array --- migration/addContributors.ts | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/migration/addContributors.ts b/migration/addContributors.ts index e98bb52..20f823d 100644 --- a/migration/addContributors.ts +++ b/migration/addContributors.ts @@ -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, @@ -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;