From 8f60a9f905474e8d991e815b37204b00a368a402 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Wed, 4 May 2022 21:54:28 +0800 Subject: [PATCH] Add default state to migration script --- packages/db/migrate.ts | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/db/migrate.ts b/packages/db/migrate.ts index 04659182b..9d5b2bfd6 100755 --- a/packages/db/migrate.ts +++ b/packages/db/migrate.ts @@ -87,7 +87,7 @@ const logAppliedMigrations = ( } export const INDEX_ALIAS = 'pages_alias' -export const client = new Client({ +export const esClient = new Client({ node: process.env.ELASTIC_URL || 'http://localhost:9200', auth: { username: process.env.ELASTIC_USERNAME || '', @@ -103,7 +103,7 @@ const updateMappings = async (): Promise => { ) // update mappings - await client.indices.putMapping({ + await esClient.indices.putMapping({ index: INDEX_ALIAS, body: JSON.parse(indexSettings).mappings, }) @@ -123,8 +123,39 @@ postgrator log('Starting updating elasticsearch index mappings...') updateMappings() - .then(() => console.log('\nUpdating elastic completed.')) + .then(() => console.log('\nUpdating elastic mappings completed.')) .catch((error) => { log(`${chalk.red('Updating failed: ')}${error.message}`, chalk.red) process.exit(1) }) + +log('Starting adding default state to pages in elasticsearch...') +esClient + .update_by_query({ + index: INDEX_ALIAS, + body: { + script: { + source: 'ctx._source.state = params.state', + lang: 'painless', + params: { + state: 'SUCCEEDED', + }, + }, + query: { + bool: { + must_not: [ + { + exists: { + field: 'state', + }, + }, + ], + }, + }, + }, + }) + .then(() => console.log('\nAdding default state completed.')) + .catch((error) => { + log(`${chalk.red('Adding failed: ')}${error.message}`, chalk.red) + process.exit(1) + })