Skip to content

Commit

Permalink
fix(db-migrator): don't crash on read/write migration records error
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Jan 12, 2020
1 parent 0dd825f commit 9c44deb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/@nodepack/db-migrator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"prepublishOnly": "yarn build"
},
"dependencies": {
"@nodepack/env-migrator": "^0.8.9"
"@nodepack/env-migrator": "^0.8.9",
"consola": "^2.11.3"
},
"devDependencies": {
"@nodepack/service": "^0.8.9"
Expand Down
30 changes: 20 additions & 10 deletions packages/@nodepack/db-migrator/src/lib/Migrator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Migrator as EnvMigrator } from '@nodepack/env-migrator'
import consola from 'consola'

export interface FileMigrationRecord {
file: string
Expand All @@ -17,23 +18,32 @@ export class Migrator extends EnvMigrator {
* @private
*/
async readMigrationRecords () {
if (!this.context.readDbMigrationRecords) {
throw new Error(`No 'readDbMigrationRecords' method provided by context`)
try {
if (!this.context.readDbMigrationRecords) {
throw new Error(`No 'readDbMigrationRecords' method provided by context`)
}
const data = await this.context.readDbMigrationRecords()
this.fileMigrationRecords = data.files
} catch (e) {
consola.error('Could not read migration records. Error:', e.stack)
this.fileMigrationRecords = []
}
const data = await this.context.readDbMigrationRecords()
this.fileMigrationRecords = data.files
}

/**
* @private
*/
async writeMigrationRecords () {
if (!this.context.writeDbMigrationRecords) {
throw new Error(`No 'writeDbMigrationRecords' method provided by context`)
try {
if (!this.context.writeDbMigrationRecords) {
throw new Error(`No 'writeDbMigrationRecords' method provided by context`)
}
await this.context.writeDbMigrationRecords({
files: this.fileMigrationRecords,
plugins: [],
})
} catch (e) {
consola.error('Could not write migration records. Error:', e.stack)
}
await this.context.writeDbMigrationRecords({
files: this.fileMigrationRecords,
plugins: [],
})
}
}

0 comments on commit 9c44deb

Please sign in to comment.