Various fixes for data migrations

* Add unique index for `Migration`s
* Fix proper use of `next` callback. First argument is potential error.
* Update migration template
This commit is contained in:
roschaefer 2020-01-20 15:22:51 +01:00
parent 98a4521ecc
commit a86b26a756
6 changed files with 56 additions and 20 deletions

View File

@ -4,8 +4,10 @@
"description": "GraphQL Backend for Human Connection",
"main": "src/index.js",
"scripts": {
"build": "babel src/ -d dist/ --copy-files",
"__migrate": "migrate --compiler 'js:@babel/register' --migrations-dir ./src/db/migrations",
"prod:migrate": "migrate --migrations-dir ./dist/db/migrations --store ./dist/db/migrate/store.js",
"start": "node dist/",
"build": "babel src/ -d dist/ --copy-files",
"dev": "nodemon --exec babel-node src/ -e js,gql",
"dev:debug": "nodemon --exec babel-node --inspect=0.0.0.0:9229 src/ -e js,gql",
"lint": "eslint src --config .eslintrc.js",
@ -13,10 +15,8 @@
"db:clean": "babel-node src/db/clean.js",
"db:reset": "yarn run db:clean",
"db:seed": "babel-node src/db/seed.js",
"__migrate": "migrate --compiler 'js:@babel/register' --migrations-dir ./src/db/migrations",
"db:migrate": "yarn run __migrate --store ./src/db/migrate/store.js",
"db:migrate:create": "yarn run __migrate --template-file ./src/db/migrate/template.js create",
"production:db:migrate": "migrate --migrations-dir ./dist/db/migrations --store ./dist/db/migrate/store.js"
"db:migrate:create": "yarn run __migrate --template-file ./src/db/migrate/template.js create"
},
"author": "Human Connection gGmbH",
"license": "MIT",

View File

@ -1,32 +1,36 @@
import { getDriver, getNeode } from '../../db/neo4j'
class Store {
async init(fn) {
async init(next) {
const neode = getNeode()
const { driver } = neode
const session = driver.session()
// eslint-disable-next-line no-console
const writeTxResultPromise = session.writeTransaction(async txc => {
await txc.run('CALL apoc.schema.assert({},{},true)') // drop all indices
return Promise.all([
'CALL db.index.fulltext.createNodeIndex("post_fulltext_search",["Post"],["title", "content"])',
'CALL db.index.fulltext.createNodeIndex("user_fulltext_search",["User"],["name", "slug"])'
].map(statement => txc.run(statement)))
return Promise.all(
[
'CALL db.index.fulltext.createNodeIndex("post_fulltext_search",["Post"],["title", "content"])',
'CALL db.index.fulltext.createNodeIndex("user_fulltext_search",["User"],["name", "slug"])',
].map(statement => txc.run(statement)),
)
})
try {
await writeTxResultPromise
await getNeode().schema.install()
// eslint-disable-next-line no-console
console.log('Successfully created database indices and constraints!')
next()
} catch (error) {
console.log(error) // eslint-disable-line no-console
next(error, null)
} finally {
session.close()
driver.close()
fn()
}
}
async load(fn) {
async load(next) {
const driver = getDriver()
const session = driver.session()
const readTxResultPromise = session.readTransaction(async txc => {
@ -42,18 +46,19 @@ class Store {
console.log(
"No migrations found in database. If it's the first time you run migrations, then this is normal.",
)
return fn(null, {})
return next(null, {})
}
const [{ title: lastRun }] = migrations
fn(null, { lastRun, migrations })
next(null, { lastRun, migrations })
} catch (error) {
console.log(error) // eslint-disable-line no-console
next(error)
} finally {
session.close()
}
}
async save(set, fn) {
async save(set, next) {
const driver = getDriver()
const session = driver.session()
const { migrations } = set
@ -70,11 +75,12 @@ class Store {
})
try {
await writeTxResultPromise
next()
} catch (error) {
console.log(error) // eslint-disable-line no-console
next(error)
} finally {
session.close()
fn()
}
}
}

View File

@ -1,7 +1,31 @@
import { getDriver } from '../../db/neo4j'
export const description = ''
export function up(next) {
next()
const driver = getDriver()
const session = driver.session()
try {
// Implement your migration here.
next()
} catch (err) {
next(err)
} finally {
session.close()
driver.close()
}
}
export function down(next) {
next()
const driver = getDriver()
const session = driver.session()
try {
// Rollback your migration here.
next()
} catch (err) {
next(err)
} finally {
session.close()
driver.close()
}
}

View File

@ -74,11 +74,11 @@ export function up(next) {
next()
},
error: error => {
throw new Error(error)
next(new Error(error), null)
},
})
}
export function down() {
throw new Error('Irreversible migration')
export function down(next) {
next(new Error('Irreversible migration'))
}

View File

@ -0,0 +1,5 @@
export default {
title: { type: 'string', primary: true, token: true },
description: { type: 'string' },
timestamp: { type: 'number', unique: true },
}

View File

@ -13,4 +13,5 @@ export default {
Location: require('./Location.js').default,
Donations: require('./Donations.js').default,
Report: require('./Report.js').default,
Migration: require('./Migration.js').default,
}