Skip to content

Commit

Permalink
feat: db migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Aug 23, 2019
1 parent 0208c32 commit 4b76719
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 6 deletions.
30 changes: 30 additions & 0 deletions packages/@nodepack/db-migrator/package.json
@@ -0,0 +1,30 @@
{
"name": "@nodepack/db-migrator",
"version": "0.4.4",
"description": "Database Migration system for Nodepack",
"author": "Guillaume Chau <guillaume.b.chau@gmail.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/Akryum/nodepack.git"
},
"bugs": {
"url": "https://github.com/Akryum/nodepack/issues"
},
"homepage": "https://github.com/Akryum/nodepack#readme",
"publishConfig": {
"access": "public"
},
"main": "src/index.js",
"typings": "types/index.d.ts",
"scripts": {
"test": "yarn test:lint",
"test:lint": "eslint src"
},
"dependencies": {
"@nodepack/env-migrator": "^0.4.4"
},
"devDependencies": {
"@nodepack/service": "^0.4.4"
}
}
1 change: 1 addition & 0 deletions packages/@nodepack/db-migrator/src/index.js
@@ -0,0 +1 @@
exports.Migrator = require('./lib/Migrator')
40 changes: 40 additions & 0 deletions packages/@nodepack/db-migrator/src/lib/Migrator.js
@@ -0,0 +1,40 @@
/**
* @typedef FileMigrationRecord
* @prop {string} file
* @prop {string} date
*/

const { Migrator: EnvMigrator } = require('@nodepack/env-migrator')

module.exports = class Migrator extends EnvMigrator {
/**
* @private
*/
async setup () {
await this.readMigrationRecords()
}

/**
* @private
*/
async readMigrationRecords () {
if (!this.context.readDbMigrationRecords) {
throw new Error(`No 'readDbMigrationRecords' method provided by context`)
}
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`)
}
await this.context.writeDbMigrationRecords({
files: this.fileMigrationRecords,
plugins: [],
})
}
}
1 change: 1 addition & 0 deletions packages/@nodepack/db-migrator/types/index.d.ts
@@ -0,0 +1 @@
export { default as Migrator } from '../src/lib/Migrator'
Empty file.
6 changes: 4 additions & 2 deletions packages/@nodepack/env-migrator/src/lib/Migrator.js
Expand Up @@ -22,9 +22,11 @@ module.exports = class Migrator {
*/
constructor (cwd, {
migrationsFolder,
context,
}) {
this.cwd = cwd
this.migrationsFolder = migrationsFolder
this.context = context
/** @type {FileMigrationRecord[]} */
this.fileMigrationRecords = []
this.upPrepared = false
Expand All @@ -46,7 +48,7 @@ module.exports = class Migrator {
}
}

async up (context) {
async up () {
await this.prepareUp()

// Files
Expand All @@ -55,7 +57,7 @@ module.exports = class Migrator {
if (module.up) {
try {
logWithSpinner('✔️', chalk.grey(module.file))
await module.up(context)
await module.up(this.context)
stopSpinner()
this.fileMigrationRecords.push({
file: module.file,
Expand Down
1 change: 1 addition & 0 deletions packages/@nodepack/maintenance/package.json
Expand Up @@ -23,6 +23,7 @@
"dependencies": {
"@nodepack/app-migrator": "^0.4.3",
"@nodepack/env-migrator": "^0.4.3",
"@nodepack/db-migrator": "^0.4.4",
"@nodepack/fragment": "^0.4.2",
"@nodepack/hookable": "^0.4.2",
"@nodepack/utils": "^0.4.3",
Expand Down
37 changes: 33 additions & 4 deletions packages/@nodepack/maintenance/src/lib/Maintenance.js
Expand Up @@ -3,6 +3,7 @@

const { Migrator: AppMigrator, getMigratorPlugins: getAppMigratorPlugins } = require('@nodepack/app-migrator')
const { Migrator: EnvMigrator } = require('@nodepack/env-migrator')
const { Migrator: DbMigrator } = require('@nodepack/db-migrator')
const {
log,
error,
Expand All @@ -28,6 +29,7 @@ const FRAGMENTS = [
]

const ENV_MIGRATION_FOLDER = 'migration/env'
const DB_MIGRATION_FOLDER = 'migration/db'

/**
* @typedef MaintenanceHookAPI
Expand Down Expand Up @@ -67,6 +69,7 @@ const ENV_MIGRATION_FOLDER = 'migration/env'
* @prop {AppMigrationAllOptions?} appMigrationAllOptions
* @prop {number} appMigrationCount
* @prop {number} envMigrationCount
* @prop {number} dbMigrationCount
*/

/**
Expand Down Expand Up @@ -114,6 +117,7 @@ class Maintenance {
appMigrationAllOptions: null,
appMigrationCount: 0,
envMigrationCount: 0,
dbMigrationCount: 0,
}
/** @type {function[]} */
this.completeCbs = []
Expand All @@ -138,10 +142,18 @@ class Maintenance {
await this.runAppMigrations()
await this.callHook('afterAppMigrations')

// Prepare context
await this.buildFragments(FRAGMENTS)
await this.createContext()

// Env Migrations
await this.runEnvMigrations()
await this.callHook('afterEnvMigrations')

// Database Migrations
await this.runDbMigrations()
await this.callHook('afterDbMigrations')

log(`🔧 Maintenance complete!`)

await this.callHook('after')
Expand Down Expand Up @@ -174,17 +186,16 @@ class Maintenance {
}

async runEnvMigrations () {
const { cwd } = this
const { cwd, context } = this
const migrator = new EnvMigrator(cwd, {
migrationsFolder: ENV_MIGRATION_FOLDER,
context,
})
const { files } = await migrator.prepareUp()
if (files.length) {
await this.buildFragments(FRAGMENTS)
await this.createContext()
await this.shouldCommitState(`[nodepack] before env migration`)
log(`🚀 Migrating env...`)
const { migrationCount } = await migrator.up(this.context)
const { migrationCount } = await migrator.up()
log(`💻️ ${migrationCount} env migration${migrationCount > 1 ? 's' : ''} applied!`)
this.results.envMigrationCount = migrationCount
// install additional deps (injected by migrations)
Expand All @@ -193,6 +204,24 @@ class Maintenance {
}
}

async runDbMigrations () {
const { cwd, context } = this
if (!context.readDbMigrationRecords) return
const migrator = new DbMigrator(cwd, {
migrationsFolder: DB_MIGRATION_FOLDER,
context,
})
const { files } = await migrator.prepareUp()
if (files.length) {
await this.shouldCommitState(`[nodepack] before db migration`)
log(`🚀 Migrating db...`)
const { migrationCount } = await migrator.up()
log(`🗄️ ${migrationCount} db migration${migrationCount > 1 ? 's' : ''} applied!`)
this.results.dbMigrationCount = migrationCount
await this.shouldCommitState(`[nodepack] after db migration`)
}
}

/**
* @param {string} id
*/
Expand Down

0 comments on commit 4b76719

Please sign in to comment.