From b3f5bf4e6a7e4d0a923de80cbb00b1093db38c3b Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Mon, 20 Mar 2023 14:06:10 +0100 Subject: [PATCH 1/4] new migration to create search indexes --- backend/src/db/migrate/store.js | 7 --- .../20230320130345-fulltext-search-indexes.js | 54 +++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 backend/src/db/migrations/20230320130345-fulltext-search-indexes.js diff --git a/backend/src/db/migrate/store.js b/backend/src/db/migrate/store.js index ac762647b..0c0b63943 100644 --- a/backend/src/db/migrate/store.js +++ b/backend/src/db/migrate/store.js @@ -86,13 +86,6 @@ class Store { if (CONFIG.CATEGORIES_ACTIVE) await createCategories(session) const writeTxResultPromise = session.writeTransaction(async (txc) => { await txc.run('CALL apoc.schema.assert({},{},true)') // drop all indices and constraints - return Promise.all( - [ - 'CALL db.index.fulltext.createNodeIndex("user_fulltext_search",["User"],["name", "slug"])', - 'CALL db.index.fulltext.createNodeIndex("post_fulltext_search",["Post"],["title", "content"])', - 'CALL db.index.fulltext.createNodeIndex("tag_fulltext_search",["Tag"],["id"])', - ].map((statement) => txc.run(statement)), - ) }) try { await writeTxResultPromise diff --git a/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js b/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js new file mode 100644 index 000000000..3e4866618 --- /dev/null +++ b/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js @@ -0,0 +1,54 @@ +import { getDriver } from '../../db/neo4j' + +export const description = '' + +export async function up(next) { + const driver = getDriver() + const session = driver.session() + const transaction = session.beginTransaction() + + try { + // Drop all indexes because due to legacy code they might be set already + await transaction.run(`CALL db.index.fulltext.drop("user_fulltext_search")`) + await transaction.run(`CALL db.index.fulltext.drop("post_fulltext_search")`) + await transaction.run(`CALL db.index.fulltext.drop("tag_fulltext_search")`) + // Create indexes + await transaction.run(`CALL db.index.fulltext.createNodeIndex("user_fulltext_search",["User"],["name", "slug"])`) + await transaction.run(`CALL db.index.fulltext.createNodeIndex("post_fulltext_search",["Post"],["title", "content"])`) + await transaction.run(`CALL db.index.fulltext.createNodeIndex("tag_fulltext_search",["Tag"],["id"])`) + await transaction.commit() + next() + } catch (error) { + // eslint-disable-next-line no-console + console.log(error) + await transaction.rollback() + // eslint-disable-next-line no-console + console.log('rolled back') + throw new Error(error) + } finally { + session.close() + } +} + +export async function down(next) { + const driver = getDriver() + const session = driver.session() + const transaction = session.beginTransaction() + + try { + await transaction.run(`CALL db.index.fulltext.drop("user_fulltext_search")`) + await transaction.run(`CALL db.index.fulltext.drop("post_fulltext_search")`) + await transaction.run(`CALL db.index.fulltext.drop("tag_fulltext_search")`) + await transaction.commit() + next() + } catch (error) { + // eslint-disable-next-line no-console + console.log(error) + await transaction.rollback() + // eslint-disable-next-line no-console + console.log('rolled back') + throw new Error(error) + } finally { + session.close() + } +} From 6e2b65e124996f6799216f511df986df47cda387 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Mon, 20 Mar 2023 21:40:57 +0100 Subject: [PATCH 2/4] lint fixes --- .../20230320130345-fulltext-search-indexes.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js b/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js index 3e4866618..27fcc7db5 100644 --- a/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js +++ b/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js @@ -13,9 +13,15 @@ export async function up(next) { await transaction.run(`CALL db.index.fulltext.drop("post_fulltext_search")`) await transaction.run(`CALL db.index.fulltext.drop("tag_fulltext_search")`) // Create indexes - await transaction.run(`CALL db.index.fulltext.createNodeIndex("user_fulltext_search",["User"],["name", "slug"])`) - await transaction.run(`CALL db.index.fulltext.createNodeIndex("post_fulltext_search",["Post"],["title", "content"])`) - await transaction.run(`CALL db.index.fulltext.createNodeIndex("tag_fulltext_search",["Tag"],["id"])`) + await transaction.run( + `CALL db.index.fulltext.createNodeIndex("user_fulltext_search",["User"],["name", "slug"])`, + ) + await transaction.run( + `CALL db.index.fulltext.createNodeIndex("post_fulltext_search",["Post"],["title", "content"])`, + ) + await transaction.run( + `CALL db.index.fulltext.createNodeIndex("tag_fulltext_search",["Tag"],["id"])`, + ) await transaction.commit() next() } catch (error) { From bfca086e7be335a44fb129a61837b7186380c2d7 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Mon, 20 Mar 2023 22:17:07 +0100 Subject: [PATCH 3/4] only drop indexes if they exist --- .../20230320130345-fulltext-search-indexes.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js b/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js index 27fcc7db5..73040adf7 100644 --- a/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js +++ b/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js @@ -8,10 +8,19 @@ export async function up(next) { const transaction = session.beginTransaction() try { - // Drop all indexes because due to legacy code they might be set already - await transaction.run(`CALL db.index.fulltext.drop("user_fulltext_search")`) - await transaction.run(`CALL db.index.fulltext.drop("post_fulltext_search")`) - await transaction.run(`CALL db.index.fulltext.drop("tag_fulltext_search")`) + // Drop indexes if they exist because due to legacy code they might be set already + const indexesResponse = await transaction.run(`CALL db.indexes()`) + const indexes = indexesResponse.records.map((record) => record.get('indexName')) + console.log(indexes) + if(indexes.indexOf('user_fulltext_search') > -1){ + await transaction.run(`CALL db.index.fulltext.drop("user_fulltext_search")`) + } + if(indexes.indexOf('post_fulltext_search') > -1){ + await transaction.run(`CALL db.index.fulltext.drop("post_fulltext_search")`) + } + if(indexes.indexOf('tag_fulltext_search') > -1){ + await transaction.run(`CALL db.index.fulltext.drop("tag_fulltext_search")`) + } // Create indexes await transaction.run( `CALL db.index.fulltext.createNodeIndex("user_fulltext_search",["User"],["name", "slug"])`, From ceed23893ad8c495021ec407c713cd52b7673c38 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Mon, 20 Mar 2023 22:36:43 +0100 Subject: [PATCH 4/4] remove console statement, lint fixes --- .../migrations/20230320130345-fulltext-search-indexes.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js b/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js index 73040adf7..11029bea6 100644 --- a/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js +++ b/backend/src/db/migrations/20230320130345-fulltext-search-indexes.js @@ -11,14 +11,13 @@ export async function up(next) { // Drop indexes if they exist because due to legacy code they might be set already const indexesResponse = await transaction.run(`CALL db.indexes()`) const indexes = indexesResponse.records.map((record) => record.get('indexName')) - console.log(indexes) - if(indexes.indexOf('user_fulltext_search') > -1){ + if (indexes.indexOf('user_fulltext_search') > -1) { await transaction.run(`CALL db.index.fulltext.drop("user_fulltext_search")`) } - if(indexes.indexOf('post_fulltext_search') > -1){ + if (indexes.indexOf('post_fulltext_search') > -1) { await transaction.run(`CALL db.index.fulltext.drop("post_fulltext_search")`) } - if(indexes.indexOf('tag_fulltext_search') > -1){ + if (indexes.indexOf('tag_fulltext_search') > -1) { await transaction.run(`CALL db.index.fulltext.drop("tag_fulltext_search")`) } // Create indexes