From 20dd610e27cf6f59027f218f52bb518498ac5694 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Thu, 9 Apr 2020 10:38:50 +0200 Subject: [PATCH] Define database migration to create implemented notifications - Create notifications for all old filed reports to all of their submitters. --- ...ications_from_reports_to_filing_usersjs.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 backend/src/db/migrations/20200409100436-create_notifications_from_reports_to_filing_usersjs.js diff --git a/backend/src/db/migrations/20200409100436-create_notifications_from_reports_to_filing_usersjs.js b/backend/src/db/migrations/20200409100436-create_notifications_from_reports_to_filing_usersjs.js new file mode 100644 index 000000000..353b4188f --- /dev/null +++ b/backend/src/db/migrations/20200409100436-create_notifications_from_reports_to_filing_usersjs.js @@ -0,0 +1,45 @@ +import { getDriver } from '../../db/neo4j' + +export const description = 'Create notifications for all old filed reports to all of their submitters.' + +export async function up(next) { + const driver = getDriver() + const session = driver.session() + const transaction = session.beginTransaction() + const updateDeletedUserAttributes = await transaction.run(` + MATCH (submitter:User)-[filed:FILED]->(report:Report) + WHERE NOT (submitter)<-[:NOTIFIED]-(report) + CREATE (submitter)<-[notification:NOTIFIED]-(report) + SET notification.createdAt = filed.createdAt, + notification.updatedAt = notification.createdAt, + notification.read = FALSE, + notification.reason = 'filed_report_on_resource' + RETURN notification {.*}; + `) + + try { + // Implement your migration here. + const notifications = await updateDeletedUserAttributes.records.map((record) => + record.get('notification'), + ) + // eslint-disable-next-line no-console + console.log(notifications) + 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) { + // eslint-disable-next-line no-console + console.log('Irreversible migration') + next() +}