42 lines
1.0 KiB
JavaScript
42 lines
1.0 KiB
JavaScript
let mysql = require('mysql2');
|
|
|
|
let connection = mysql.createConnection({
|
|
multipleStatements: true,
|
|
host: 'localhost',
|
|
user: 'root',
|
|
password: '',
|
|
database: 'salix'
|
|
});
|
|
|
|
let errorHandler = callback => {
|
|
return error => {
|
|
if (error)
|
|
throw error;
|
|
callback();
|
|
};
|
|
};
|
|
|
|
let insertFixtures = (inserts, callback) => {
|
|
connection.query(inserts[0], errorHandler(callback));
|
|
// connection.query('SET FOREIGN_KEY_CHECKS = 1');
|
|
};
|
|
|
|
let truncate = (tables, callback) => {
|
|
let truncatesSQL = tables.reduce((accumulator, currentValue, i) => {
|
|
let sql = 'TRUNCATE TABLE ' + currentValue + '; ';
|
|
return (`${accumulator}${sql}`);
|
|
}, '');
|
|
|
|
connection.query(truncatesSQL.slice(0, -2), errorHandler(callback));
|
|
};
|
|
|
|
connection.connect();
|
|
|
|
module.exports = function restoreFixtures(fixturesToApply, callback) {
|
|
connection.query('SET FOREIGN_KEY_CHECKS = 0', () => {
|
|
truncate(fixturesToApply.tables, () => {
|
|
insertFixtures(fixturesToApply.inserts, callback);
|
|
});
|
|
});
|
|
};
|