migrate commenting users to observe commented posts (#8308)

This commit is contained in:
Moriz Wahl 2025-04-10 19:36:26 +02:00 committed by GitHub
parent ab6fe37c3e
commit abd13a6cff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,62 @@
import { getDriver } from '@db/neo4j'
export const description = `
All users commenting a post observe the post.
`
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 (commenter:User)-[:WROTE]->(:Comment)-[:COMMENTS]->(post:Post)
MERGE (commenter)-[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 (u:User)-[obs:OBSERVES]->(p:Post)<-[:COMMENTS]-(:Comment)<-[:WROTE]-(u)
WHERE NOT (u)-[:WROTE]->(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()
}
}