diff --git a/backend/src/db/migrations/20200207080200-fulltext_index_for_tags.js b/backend/src/db/migrations/20200207080200-fulltext_index_for_tags.js index 5064a8b17..ffcd3d4b6 100644 --- a/backend/src/db/migrations/20200207080200-fulltext_index_for_tags.js +++ b/backend/src/db/migrations/20200207080200-fulltext_index_for_tags.js @@ -15,12 +15,18 @@ export async function up(next) { 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) + const { message } = error + if (message.includes('There already exists an index')) { + // all fine + // eslint-disable-next-line no-console + console.log(message) + next() + } else { + await transaction.rollback() + // eslint-disable-next-line no-console + console.log('rolled back') + throw new Error(error) + } } finally { session.close() } diff --git a/backend/src/db/migrations/20200213230248-add_unique_index_to_image_url.js b/backend/src/db/migrations/20200213230248-add_unique_index_to_image_url.js index 60d67432f..4582d938c 100644 --- a/backend/src/db/migrations/20200213230248-add_unique_index_to_image_url.js +++ b/backend/src/db/migrations/20200213230248-add_unique_index_to_image_url.js @@ -19,11 +19,18 @@ export async function up(next) { 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') + const { message } = error + if (message.includes('There already exists an index')) { + // all fine + // eslint-disable-next-line no-console + console.log(message) + next() + } else { + await transaction.rollback() + // eslint-disable-next-line no-console + console.log('rolled back') + throw new Error(error) + } } finally { session.close() } diff --git a/backend/src/db/migrations/20200326160326-remove_dangling_image_urls.js b/backend/src/db/migrations/20200326160326-remove_dangling_image_urls.js new file mode 100644 index 000000000..a77ac360c --- /dev/null +++ b/backend/src/db/migrations/20200326160326-remove_dangling_image_urls.js @@ -0,0 +1,61 @@ +import { getDriver } from '../../db/neo4j' +import { existsSync } from 'fs' + +export const description = ` + In this review: + https://github.com/Human-Connection/Human-Connection/pull/3262#discussion_r398634249 + I brought up that we may have image nodes with danling urls (urls that don't + point to local file on disk). I would prefer to remove those urls to avoid + unnecessary 404 errors. +` + +export async function up(next) { + const driver = getDriver() + const session = driver.session() + const transaction = session.beginTransaction() + + try { + // Implement your migration here. + const { records } = await transaction.run(` + MATCH(image:Image) + WHERE image.url STARTS WITH '/' + RETURN image.url as url + `) + const urls = records.map((record) => record.get('url')) + const danglingUrls = urls.filter((url) => { + const fileLocation = `public${url}` + return !existsSync(fileLocation) + }) + await transaction.run( + ` + MATCH(image:Image) + WHERE image.url IN $danglingUrls + DETACH DELETE image + `, + { danglingUrls }, + ) + await transaction.commit() + if (danglingUrls.length) { + // eslint-disable-next-line no-console + console.log(` + Removed ${danglingUrls.length} dangling urls.\n + =============================================== + ${danglingUrls.join('\n')} + `) + } + 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() { + throw new Error('Irreversible migration') +}