From 4905b09b3e722655c65051ece7ee12c3c38c1390 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 6 Feb 2020 20:47:45 +0100 Subject: [PATCH] migration added to swap lat and lng in db --- ...0206190233-swap_latitude_with_longitude.js | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 backend/src/db/migrations/20200206190233-swap_latitude_with_longitude.js diff --git a/backend/src/db/migrations/20200206190233-swap_latitude_with_longitude.js b/backend/src/db/migrations/20200206190233-swap_latitude_with_longitude.js new file mode 100644 index 000000000..35d85430e --- /dev/null +++ b/backend/src/db/migrations/20200206190233-swap_latitude_with_longitude.js @@ -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() + } +}