migration adds OBSERVES relation between author and post (#8307)

This commit is contained in:
Moriz Wahl 2025-04-03 18:46:02 +02:00 committed by GitHub
parent 130da7509b
commit 72f469d823
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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()
}
}