From 2ef5d101bbf235ae2cae17aca492c6368ce17cca Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Fri, 17 Jun 2022 22:07:41 +0800 Subject: [PATCH] Create index if not exists in migration --- docker-compose-test.yml | 2 +- packages/db/migrate.ts | 114 ++++++++++++++++++++++++---------------- 2 files changed, 70 insertions(+), 46 deletions(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 8772ffc50..7b51fb797 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -32,7 +32,7 @@ services: - http.cors.allow-credentials=true - http.port=9201 volumes: - - ./.docker/elastic-data:/usr/share/elasticsearch/data + - ./.docker/elastic-test-data:/usr/share/elasticsearch-test/data ports: - "9201:9201" diff --git a/packages/db/migrate.ts b/packages/db/migrate.ts index 4359959b6..8a4386940 100755 --- a/packages/db/migrate.ts +++ b/packages/db/migrate.ts @@ -82,7 +82,7 @@ const logAppliedMigrations = ( console.log(` ${actionLabel} ${migration.name}`) } } else { - log(`No migrations applied.`) + log(`No Postgres migrations applied.`) } } @@ -95,14 +95,21 @@ export const esClient = new Client({ password: process.env.ELASTIC_PASSWORD || '', }, }) +// read index settings from file +const indexSettings = readFileSync( + join(__dirname, 'elastic_migrations', 'index_settings.json'), + 'utf8' +) +const INDEX_NAME = 'pages' +const createIndex = async (): Promise => { + // create index + await esClient.indices.create({ + index: INDEX_NAME, + body: JSON.parse(indexSettings), + }) +} const updateMappings = async (): Promise => { - // read index settings from file - const indexSettings = readFileSync( - join(__dirname, 'elastic_migrations', 'index_settings.json'), - 'utf8' - ) - // update mappings await esClient.indices.putMapping({ index: INDEX_ALIAS, @@ -110,56 +117,73 @@ const updateMappings = async (): Promise => { }) } -postgrator +// postgres migration +const postgresMigration = postgrator .migrate(targetMigration) .then(logAppliedMigrations) .catch((error) => { - log(`${chalk.red('Migration failed: ')}${error.message}`, chalk.red) + log( + `${chalk.red('Postgres migration failed: ')}${error.message}`, + chalk.red + ) const { appliedMigrations } = error logAppliedMigrations(appliedMigrations) process.exit(1) }) - .then(() => console.log('\nExiting...')) -log('Starting updating elasticsearch index mappings...') - -updateMappings() - .then(() => console.log('\nUpdating elastic mappings completed.')) - .catch((error) => { - log(`${chalk.red('Updating failed: ')}${error.message}`, chalk.red) - process.exit(1) +// elastic migration +log('Creating elastic index...') +const elasticMigration = esClient.indices + .exists({ index: INDEX_ALIAS }) + .then(({ body: exists }) => { + if (!exists) { + return createIndex().then(() => log('Elastic index created.')) + } else { + log('Elastic index already exists.') + } }) - -log('Starting adding default state to pages in elasticsearch...') -esClient - .update_by_query({ - index: INDEX_ALIAS, - requests_per_second: 250, - scroll_size: 500, - timeout: '30m', - body: { - script: { - source: 'ctx._source.state = params.state', - lang: 'painless', - params: { - state: 'SUCCEEDED', - }, - }, - query: { - bool: { - must_not: [ - { - exists: { - field: 'state', - }, + .then(() => { + log('Updating elastic index mappings...') + return updateMappings().then(() => { + log('Elastic index mappings updated.') + }) + }) + .then(() => { + log('Adding default state to pages in elastic...') + return esClient + .update_by_query({ + index: INDEX_ALIAS, + requests_per_second: 250, + scroll_size: 500, + timeout: '30m', + body: { + script: { + source: 'ctx._source.state = params.state', + lang: 'painless', + params: { + state: 'SUCCEEDED', }, - ], + }, + query: { + bool: { + must_not: [ + { + exists: { + field: 'state', + }, + }, + ], + }, + }, }, - }, - }, + }) + .then(() => log('Default state added.')) }) - .then(() => console.log('\nAdding default state completed.')) .catch((error) => { - log(`${chalk.red('Adding failed: ')}${error.message}`, chalk.red) + log(`${chalk.red('Elastic migration failed: ')}${error.message}`, chalk.red) + const { appliedMigrations } = error + logAppliedMigrations(appliedMigrations) process.exit(1) }) + +Promise.all([postgresMigration, elasticMigration]).then(() => log('Exiting...'))