mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Copied DB for faster SQLite tests
refs https://github.com/TryGhost/Toolbox/issues/136 - we nuke and reinitialize the DB many times between tests - this forms a good portion of the time taken and we shouldn't be spending so much time on just resetting the DB back to a known state - this commit switches out the knex-migrator reset + init calls for SQLite to a function which keeps a clean copy of the DB and copies the file back when we "reset" - for MySQL, existing functionality is kept - this massively speeds up tests because it saves ~700ms+ for every reset - whilst this change seems to work, it's just the start so there's a lot more refactoring needed. this change is currently gated to CI until it's deemed safe/sane enough to run on local machines without blowing up
This commit is contained in:
parent
40fee069c9
commit
a3cc66be50
2 changed files with 35 additions and 9 deletions
|
@ -1,6 +1,7 @@
|
||||||
const debug = require('@tryghost/debug')('test:dbUtils');
|
const debug = require('@tryghost/debug')('test:dbUtils');
|
||||||
|
|
||||||
// Utility Packages
|
// Utility Packages
|
||||||
|
const fs = require('fs-extra');
|
||||||
const Promise = require('bluebird');
|
const Promise = require('bluebird');
|
||||||
const KnexMigrator = require('knex-migrator');
|
const KnexMigrator = require('knex-migrator');
|
||||||
const knexMigrator = new KnexMigrator();
|
const knexMigrator = new KnexMigrator();
|
||||||
|
@ -14,6 +15,34 @@ const schemaTables = Object.keys(schema);
|
||||||
// Other Test Utilities
|
// Other Test Utilities
|
||||||
const urlServiceUtils = require('./url-service-utils');
|
const urlServiceUtils = require('./url-service-utils');
|
||||||
|
|
||||||
|
const dbHash = Date.now();
|
||||||
|
|
||||||
|
module.exports.reset = async () => {
|
||||||
|
// Only run this copy in CI until it gets fleshed out
|
||||||
|
if (process.env.CI && config.get('database:client') === 'sqlite3') {
|
||||||
|
const filename = config.get('database:connection:filename');
|
||||||
|
const filenameOrig = `${filename}.${dbHash}-orig`;
|
||||||
|
|
||||||
|
const dbExists = await fs.pathExists(filenameOrig);
|
||||||
|
|
||||||
|
if (dbExists) {
|
||||||
|
await fs.copyFile(filenameOrig, filename);
|
||||||
|
} else {
|
||||||
|
await knexMigrator.reset({force: true});
|
||||||
|
|
||||||
|
// Do a full database initialisation
|
||||||
|
await knexMigrator.init();
|
||||||
|
|
||||||
|
await fs.copyFile(filename, filenameOrig);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
await knexMigrator.reset({force: true});
|
||||||
|
|
||||||
|
// Do a full database initialisation
|
||||||
|
await knexMigrator.init();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports.initData = async () => {
|
module.exports.initData = async () => {
|
||||||
await knexMigrator.init();
|
await knexMigrator.init();
|
||||||
await urlServiceUtils.reset();
|
await urlServiceUtils.reset();
|
||||||
|
|
|
@ -101,11 +101,12 @@ const prepareContentFolder = (options) => {
|
||||||
// - reload affected services
|
// - reload affected services
|
||||||
const restartModeGhostStart = async ({frontend}) => {
|
const restartModeGhostStart = async ({frontend}) => {
|
||||||
debug('Reload Mode');
|
debug('Reload Mode');
|
||||||
// Teardown truncates all tables and also calls urlServiceUtils.reset();
|
|
||||||
await dbUtils.teardown();
|
|
||||||
|
|
||||||
// The tables have been truncated, this runs the fixture init task (init file 2) to re-add our default fixtures
|
// TODO: figure out why we need this if we reset again later?
|
||||||
await knexMigrator.init({only: 2});
|
urlServiceUtils.reset();
|
||||||
|
|
||||||
|
await dbUtils.reset();
|
||||||
|
|
||||||
debug('init done');
|
debug('init done');
|
||||||
|
|
||||||
// Reset the settings cache
|
// Reset the settings cache
|
||||||
|
@ -154,17 +155,13 @@ const freshModeGhostStart = async (options) => {
|
||||||
debug('Fresh Start Mode');
|
debug('Fresh Start Mode');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the DB
|
|
||||||
await knexMigrator.reset({force: true});
|
|
||||||
|
|
||||||
// Stop the server (forceStart Mode)
|
// Stop the server (forceStart Mode)
|
||||||
await stopGhost();
|
await stopGhost();
|
||||||
|
|
||||||
// Reset the settings cache and disable listeners so they don't get triggered further
|
// Reset the settings cache and disable listeners so they don't get triggered further
|
||||||
settingsService.shutdown();
|
settingsService.shutdown();
|
||||||
|
|
||||||
// Do a full database initialization
|
await dbUtils.reset();
|
||||||
await knexMigrator.init();
|
|
||||||
|
|
||||||
await settingsService.init();
|
await settingsService.init();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue