migration added to swap lat and lng in db

This commit is contained in:
Moriz Wahl 2020-02-06 20:47:45 +01:00
parent cb8b2acffd
commit 4905b09b3e

View File

@ -0,0 +1,54 @@
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 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()
}
}
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()
}
}