improved migration

This commit is contained in:
Moriz Wahl 2020-02-11 14:37:10 +01:00
parent 1ae6769c7d
commit 31cec2cf0c

View File

@ -3,11 +3,10 @@ import { getDriver } from '../../db/neo4j'
export const description =
'This migration swaps the value stored in Location.lat with the value of Location.lng.'
export async function up(next) {
const swap = async function(next) {
const driver = getDriver()
const session = driver.session()
const transaction = session.beginTransaction()
try {
// Implement your migration here.
await transaction.run(`
@ -23,33 +22,16 @@ export async function up(next) {
await transaction.rollback()
// eslint-disable-next-line no-console
console.log('rolled back')
throw new Error(error)
} finally {
session.close()
}
}
export async function up(next) {
swap(next)
}
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 (l:Location) WHERE NOT(l.lat IS NULL)
WITH l.lng AS longitude, l.lat AS latitude, l AS Location
SET Location.lat = longitude, Location.lng = latitude `,
)
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')
} finally {
session.close()
}
swap(next)
}