mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Refactored truncateAll
util to async-await
refs https://github.com/TryGhost/Toolbox/issues/592 - async-await makes the code easier to read - also performs a small optimization to only load the foreign_keys pragma once for SQLite
This commit is contained in:
parent
9f1279b58e
commit
082ab6dc3e
1 changed files with 33 additions and 50 deletions
|
@ -126,64 +126,44 @@ const forceReinit = async () => {
|
||||||
* Has to run in a transaction for MySQL, otherwise the foreign key check does not work.
|
* Has to run in a transaction for MySQL, otherwise the foreign key check does not work.
|
||||||
* Sqlite3 has no truncate command.
|
* Sqlite3 has no truncate command.
|
||||||
*/
|
*/
|
||||||
const truncateAll = () => {
|
const truncateAll = async () => {
|
||||||
debug('Database teardown');
|
debug('Database teardown');
|
||||||
urlServiceUtils.reset();
|
urlServiceUtils.reset();
|
||||||
|
|
||||||
const tables = schemaTables.concat(['migrations']);
|
const tables = schemaTables.concat(['migrations']);
|
||||||
|
|
||||||
if (module.exports.isSQLite()) {
|
if (module.exports.isSQLite()) {
|
||||||
return Promise
|
try {
|
||||||
.mapSeries(tables, function createTable(table) {
|
const [foreignKeysEnabled] = await db.knex.raw('PRAGMA foreign_keys;');
|
||||||
return (async function () {
|
if (foreignKeysEnabled.foreign_keys) {
|
||||||
const [foreignKeysEnabled] = await db.knex.raw('PRAGMA foreign_keys;');
|
await db.knex.raw('PRAGMA foreign_keys = OFF;');
|
||||||
if (foreignKeysEnabled.foreign_keys) {
|
}
|
||||||
await db.knex.raw('PRAGMA foreign_keys = OFF;');
|
|
||||||
}
|
|
||||||
await db.knex.raw('DELETE FROM ' + table + ';');
|
|
||||||
if (foreignKeysEnabled.foreign_keys) {
|
|
||||||
await db.knex.raw('PRAGMA foreign_keys = ON;');
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
})
|
|
||||||
.catch(function (err) {
|
|
||||||
// CASE: table does not exist
|
|
||||||
if (err.errno === 1) {
|
|
||||||
return Promise.resolve();
|
|
||||||
}
|
|
||||||
|
|
||||||
throw err;
|
await Promise.each(tables, table => db.knex.raw('DELETE FROM ' + table + ';'));
|
||||||
})
|
|
||||||
.finally(() => {
|
if (foreignKeysEnabled.foreign_keys) {
|
||||||
debug('Database teardown end');
|
await db.knex.raw('PRAGMA foreign_keys = ON;');
|
||||||
});
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
} catch (err) {
|
||||||
|
// CASE: table does not exist
|
||||||
|
if (err.errno === 1) {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
debug('Database teardown end');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return db.knex.transaction(function (trx) {
|
await db.knex.transaction(async (trx) => {
|
||||||
return db.knex.raw('SET FOREIGN_KEY_CHECKS=0;').transacting(trx)
|
try {
|
||||||
.then(function () {
|
await db.knex.raw('SET FOREIGN_KEY_CHECKS=0;').transacting(trx);
|
||||||
return Promise
|
await Promise.each(tables, table => db.knex.raw('TRUNCATE ' + table + ';').transacting(trx));
|
||||||
.each(tables, function createTable(table) {
|
await db.knex.raw('SET FOREIGN_KEY_CHECKS=1;').transacting(trx);
|
||||||
return db.knex.raw('TRUNCATE ' + table + ';').transacting(trx);
|
} catch (err) {
|
||||||
});
|
|
||||||
})
|
|
||||||
.then(function () {
|
|
||||||
return db.knex.raw('SET FOREIGN_KEY_CHECKS=1;').transacting(trx);
|
|
||||||
})
|
|
||||||
.catch(function (err) {
|
|
||||||
// CASE: table does not exist || DB does not exist
|
|
||||||
// If the table or DB are not present, we can safely ignore
|
|
||||||
if (err.errno === 1146 || err.errno === 1049) {
|
|
||||||
return Promise.resolve();
|
|
||||||
}
|
|
||||||
|
|
||||||
throw err;
|
|
||||||
})
|
|
||||||
.finally(() => {
|
|
||||||
debug('Database teardown end');
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch(function (err) {
|
|
||||||
// CASE: table does not exist || DB does not exist
|
// CASE: table does not exist || DB does not exist
|
||||||
// If the table or DB are not present, we can safely ignore
|
// If the table or DB are not present, we can safely ignore
|
||||||
if (err.errno === 1146 || err.errno === 1049) {
|
if (err.errno === 1146 || err.errno === 1049) {
|
||||||
|
@ -191,7 +171,10 @@ const truncateAll = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
throw err;
|
throw err;
|
||||||
});
|
} finally {
|
||||||
|
debug('Database teardown end');
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue