From 72f469d823bc2eef3bfea1ceedbc57cf2d65fdae Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 3 Apr 2025 18:46:02 +0200 Subject: [PATCH] migration adds OBSERVES relation between author and post (#8307) --- ...20250331130323-author-observes-own-post.ts | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 backend/src/db/migrations/20250331130323-author-observes-own-post.ts diff --git a/backend/src/db/migrations/20250331130323-author-observes-own-post.ts b/backend/src/db/migrations/20250331130323-author-observes-own-post.ts new file mode 100644 index 000000000..7343d5010 --- /dev/null +++ b/backend/src/db/migrations/20250331130323-author-observes-own-post.ts @@ -0,0 +1,61 @@ +import { getDriver } from '../../db/neo4j' + +export const description = ` +All authors observe their posts. +` + +export async function up(next) { + const driver = getDriver() + const session = driver.session() + const transaction = session.beginTransaction() + + try { + // Implement your migration here. + await transaction.run(` + MATCH (author:User)-[:WROTE]->(post:Post) + MERGE (author)-[obs:OBSERVES]->(post) + ON CREATE SET + obs.active = true, + obs.createdAt = toString(datetime()), + obs.updatedAt = toString(datetime()) + 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 { + // Implement your migration here. + await transaction.run(` + MATCH (:User)-[obs:OBSERVES]->(p:Post) + DELETE obs + RETURN p + `) + 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() + } +}