-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 🎨 knex-migrator reset [ci skip] * ✨ add migration example - hooks - 1.0 [ci skip] * 🛠 knex-migrator tarball - remove when released [ci skip] * 🎨 jscs/jshint * 🕵🏻 do not drop the database connection when running tests - please read the comments in the commit * 🔥 remove example migration * 🛠 knex-migrator 0.1.0 * 🛠 knex-migrator 0.1.1 - fix a single test to ensure we catch the error * 🛠 knex-migrator 0.1.2 * 🎨 make tests green - added my keyword: kate-migrations - i will go over all TODO's when removing the old migrations code * 🛠 knex-migrator update * 🛠 knex-migrator 0.2.0
- Loading branch information
Showing
17 changed files
with
165 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
#!/usr/bin/env node | ||
|
||
var config = require('./core/server/config'); | ||
var config = require('./core/server/config'), | ||
versioning = require('./core/server/data/schema/versioning'); | ||
|
||
module.exports = { | ||
currentVersion: versioning.getNewestDatabaseVersion(), | ||
database: config.get('database'), | ||
migrationPath: config.get('paths:migrationPath') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var errors = require('../../../../errors'), | ||
config = require('../../../../config'), | ||
versioning = require('../../../schema/versioning'), | ||
database = require('../../../db'); | ||
|
||
module.exports = function after(options) { | ||
return versioning.getDatabaseVersion(options) | ||
.catch(function (err) { | ||
if (err instanceof errors.DatabaseVersionError && err.code === 'VERSION_DOES_NOT_EXIST') { | ||
return versioning.setDatabaseVersion(options); | ||
} | ||
|
||
throw err; | ||
}) | ||
.finally(function destroyConnection() { | ||
// do not close database connection in test mode, because all tests are executed one after another | ||
// this check is not nice, but there is only one other solution i can think of: | ||
// forward a custom object to knex-migrator, which get's forwarded to the hooks | ||
if (config.get('env').match(/testing/g)) { | ||
return; | ||
} | ||
|
||
// we need to close the database connection | ||
// the after hook signals the last step of a knex-migrator command | ||
// Example: | ||
// Ghost-CLI calls knexMigrator.init and afterwards it starts Ghost, but Ghost-CLI can't shutdown | ||
// if Ghost keeps a connection alive | ||
return database.knex.destroy(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
var Promise = require('bluebird'), | ||
models = require('../../../../models'); | ||
|
||
module.exports = function before() { | ||
models.init(); | ||
return Promise.resolve(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
exports.after = require('./after'); | ||
exports.before = require('./before'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var errors = require('../../../../errors'), | ||
config = require('../../../../config'), | ||
versioning = require('../../../schema/versioning'), | ||
database = require('../../../db'); | ||
|
||
module.exports = function after(options) { | ||
return versioning.getDatabaseVersion(options) | ||
.catch(function (err) { | ||
if (err instanceof errors.DatabaseVersionError && err.code === 'VERSION_DOES_NOT_EXIST') { | ||
return versioning.setDatabaseVersion(options); | ||
} | ||
|
||
throw err; | ||
}) | ||
.finally(function destroyConnection() { | ||
// do not close database connection in test mode, because all tests are executed one after another | ||
// this check is not nice, but there is only one other solution i can think of: | ||
// forward a custom object to knex-migrator, which get's forwarded to the hooks | ||
if (config.get('env').match(/testing/g)) { | ||
return; | ||
} | ||
|
||
// we need to close the database connection | ||
// the after hook signals the last step of a knex-migrator command | ||
// Example: | ||
// Ghost-CLI calls knexMigrator.init and afterwards it starts Ghost, but Ghost-CLI can't shutdown | ||
// if Ghost keeps a connection alive | ||
return database.knex.destroy(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var Promise = require('bluebird'); | ||
|
||
module.exports = function afterEach() { | ||
return Promise.resolve(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
var backup = require('../../../schema/backup'), | ||
models = require('../../../../models'); | ||
|
||
module.exports = function before(options) { | ||
models.init(); | ||
return backup(options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var Promise = require('bluebird'); | ||
|
||
module.exports = function beforeEach() { | ||
return Promise.resolve(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
exports.before = require('./before'); | ||
exports.after = require('./after'); | ||
exports.beforeEach = require('./beforeEach'); | ||
exports.afterEach = require('./afterEach'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,7 @@ | ||
var _ = require('lodash'), | ||
models = require('../../../models'), | ||
errors = require('../../../errors'), | ||
versioning = require('../../schema/versioning'); | ||
models = require('../../../models'); | ||
|
||
// @TODO: models.init | ||
module.exports = function insertSettings(options) { | ||
var localOptions = _.merge({context: {internal: true}}, options); | ||
|
||
models.init(); | ||
|
||
return models.Settings.populateDefaults(localOptions) | ||
.then(function () { | ||
return versioning.getDatabaseVersion(localOptions); | ||
}) | ||
.catch(function (err) { | ||
if (err instanceof errors.DatabaseVersionError && err.code === 'VERSION_DOES_NOT_EXIST') { | ||
return versioning.setDatabaseVersion(localOptions); | ||
} | ||
|
||
throw err; | ||
}); | ||
return models.Settings.populateDefaults(localOptions); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// # Backup Database | ||
// Provides for backing up the database before making potentially destructive changes | ||
var _ = require('lodash'), | ||
fs = require('fs'), | ||
path = require('path'), | ||
Promise = require('bluebird'), | ||
config = require('../../config'), | ||
exporter = require('../export'), | ||
|
||
writeExportFile, | ||
backup; | ||
|
||
writeExportFile = function writeExportFile(exportResult) { | ||
var filename = path.resolve(config.get('paths').contentPath + '/data/' + exportResult.filename); | ||
|
||
return Promise.promisify(fs.writeFile)(filename, JSON.stringify(exportResult.data)).return(filename); | ||
}; | ||
|
||
/** | ||
* ## Backup | ||
* does an export, and stores this in a local file | ||
* | ||
* @param {{info: logger.info, warn: logger.warn}} [logger] | ||
* @returns {Promise<*>} | ||
*/ | ||
backup = function backup(logger) { | ||
// If we get passed a function, use it to output notices, else don't do anything | ||
logger = logger && _.isFunction(logger.info) ? logger : {info: _.noop}; | ||
|
||
logger.info('Creating database backup'); | ||
|
||
var props = { | ||
data: exporter.doExport(), | ||
filename: exporter.fileName() | ||
}; | ||
|
||
return Promise.props(props) | ||
.then(writeExportFile) | ||
.then(function successMessage(filename) { | ||
logger.info('Database backup written to: ' + filename); | ||
}); | ||
}; | ||
|
||
module.exports = backup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters