2018-01-17 11:46:26 +00:00
|
|
|
let mysql = require('mysql');
|
2018-01-16 20:39:40 +00:00
|
|
|
|
2018-01-17 11:46:26 +00:00
|
|
|
let connection = mysql.createConnection({
|
2018-01-21 16:47:24 +00:00
|
|
|
multipleStatements: true,
|
2018-01-17 11:46:26 +00:00
|
|
|
host: 'localhost',
|
|
|
|
user: 'root',
|
|
|
|
password: '',
|
|
|
|
database: 'salix'
|
|
|
|
});
|
2018-01-16 20:39:40 +00:00
|
|
|
|
2018-01-21 16:47:24 +00:00
|
|
|
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) => {
|
2018-01-23 13:18:46 +00:00
|
|
|
let sql = 'TRUNCATE TABLE ' + currentValue + '; ';
|
2018-01-21 16:47:24 +00:00
|
|
|
return (`${accumulator}${sql}`);
|
|
|
|
}, '');
|
|
|
|
|
|
|
|
connection.query(truncatesSQL.slice(0, -2), errorHandler(callback));
|
|
|
|
};
|
|
|
|
|
|
|
|
connection.connect();
|
|
|
|
|
|
|
|
export default function restoreFixtures(fixturesToApply, callback) {
|
|
|
|
connection.query('SET FOREIGN_KEY_CHECKS = 0', () => {
|
|
|
|
truncate(fixturesToApply.tables, () => {
|
|
|
|
insertFixtures(fixturesToApply.inserts, callback);
|
|
|
|
});
|
|
|
|
});
|
2018-01-16 20:39:40 +00:00
|
|
|
}
|
2018-01-21 16:47:24 +00:00
|
|
|
|