From 44e2c1adf7ba8aa640feaef630e255a9d4c9e80c Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 8 Jun 2023 14:00:04 +0200 Subject: [PATCH] feat(backend): migration to add postType property to existing posts --- .../20230608130637-add-postType-property.js | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 backend/src/db/migrations/20230608130637-add-postType-property.js diff --git a/backend/src/db/migrations/20230608130637-add-postType-property.js b/backend/src/db/migrations/20230608130637-add-postType-property.js new file mode 100644 index 000000000..433577715 --- /dev/null +++ b/backend/src/db/migrations/20230608130637-add-postType-property.js @@ -0,0 +1,53 @@ +import { getDriver } from '../../db/neo4j' + +export const description = 'Add postType property Article to all posts' + +export async function up(next) { + const driver = getDriver() + const session = driver.session() + const transaction = session.beginTransaction() + + try { + await transaction.run(` + MATCH (post:Post) + SET post.postType = 'Article' + RETURN post + `) + 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(` + MATCH (post:Post) + REMOVE post.postType + RETURN post + `) + 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() + } +}