mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-30 14:22:07 +03:00
24bfb5567b
- I'm on a mission to make this code comprehensible so we can work it into something better with new boot - Who else loves async/await? :D - Dried up a block of duplicated code
87 lines
2.6 KiB
JavaScript
87 lines
2.6 KiB
JavaScript
const debug = require('ghost-ignition').debug('test:dbUtils');
|
|
|
|
// Utility Packages
|
|
const Promise = require('bluebird');
|
|
const KnexMigrator = require('knex-migrator');
|
|
const knexMigrator = new KnexMigrator();
|
|
|
|
// Ghost Internals
|
|
const {events} = require('../../core/server/lib/common');
|
|
const config = require('../../core/shared/config');
|
|
const db = require('../../core/server/data/db');
|
|
const schema = require('../../core/server/data/schema').tables;
|
|
const schemaTables = Object.keys(schema);
|
|
|
|
// Other Test Utilities
|
|
const urlServiceUtils = require('./url-service-utils');
|
|
|
|
module.exports.initData = async () => {
|
|
await knexMigrator.init();
|
|
await urlServiceUtils.isFinished();
|
|
};
|
|
|
|
module.exports.truncate = async (tableName) => {
|
|
if (config.get('database:client') === 'sqlite3') {
|
|
await db.knex(tableName).truncate();
|
|
return;
|
|
}
|
|
|
|
await db.knex.raw('SET FOREIGN_KEY_CHECKS=0;');
|
|
await db.knex(tableName).truncate();
|
|
await db.knex.raw('SET FOREIGN_KEY_CHECKS=1;');
|
|
};
|
|
|
|
// we must always try to delete all tables
|
|
module.exports.clearData = async () => {
|
|
debug('Database reset');
|
|
await knexMigrator.reset({force: true});
|
|
urlServiceUtils.reset();
|
|
};
|
|
|
|
/**
|
|
* Has to run in a transaction for MySQL, otherwise the foreign key check does not work.
|
|
* Sqlite3 has no truncate command.
|
|
*/
|
|
module.exports.teardown = () => {
|
|
debug('Database teardown');
|
|
urlServiceUtils.reset();
|
|
|
|
const tables = schemaTables.concat(['migrations']);
|
|
|
|
if (config.get('database:client') === 'sqlite3') {
|
|
return Promise
|
|
.mapSeries(tables, function createTable(table) {
|
|
return db.knex.raw('DELETE FROM ' + table + ';');
|
|
})
|
|
.catch(function (err) {
|
|
// CASE: table does not exist
|
|
if (err.errno === 1) {
|
|
return Promise.resolve();
|
|
}
|
|
|
|
throw err;
|
|
});
|
|
}
|
|
|
|
return db.knex.transaction(function (trx) {
|
|
return db.knex.raw('SET FOREIGN_KEY_CHECKS=0;').transacting(trx)
|
|
.then(function () {
|
|
return Promise
|
|
.each(tables, function createTable(table) {
|
|
return db.knex.raw('TRUNCATE ' + table + ';').transacting(trx);
|
|
});
|
|
})
|
|
.then(function () {
|
|
return db.knex.raw('SET FOREIGN_KEY_CHECKS=1;').transacting(trx);
|
|
})
|
|
.catch(function (err) {
|
|
// CASE: table does not exist
|
|
if (err.errno === 1146) {
|
|
return Promise.resolve();
|
|
}
|
|
|
|
throw err;
|
|
});
|
|
});
|
|
};
|