Skip to content

Instantly share code, notes, and snippets.

@gustavo-rodrigues-dev
Last active February 9, 2018 08:10
Show Gist options
  • Save gustavo-rodrigues-dev/bd2e0ab226279ff4283688fbc8a24491 to your computer and use it in GitHub Desktop.
Save gustavo-rodrigues-dev/bd2e0ab226279ff4283688fbc8a24491 to your computer and use it in GitHub Desktop.
Script de correção de dados em lote com node js
let UpdateMsg = {};
module.exports = (app) => {
const maxQueryPerTransaction = 1000;
let count = 0;
let HowManyUpdatesWereFinished = 0;
const countWrongData = () => {
let query = "SELECT count(1) " +
" FROM child_table ct " +
" INNER JOIN main_table mt " +
" ON( " +
" mt.id = ct.fk_id " +
" AND ct.msg <> mt.msg";
return new Promise( (resolve, reject) => {
app.db.query(query)
.then(res => {
resolve(+res.rows[0].count);
})
.catch(err => {
reject(err);
})
})
}
const hasWrongData = () =>{
return new Promise((resolve, reject) => {
if(count > 0) {
resolve(true);
return ;
}
console.log('Check if exists data to fix');
countWrongData()
.then(countWrongData => {
console.log('exists %d data to fix', countWrongData);
count = countWrongData;
if(countWrongData === 0){
resolve(false);
return ;
}
resolve(true);
return ;
})
.catch(err => {
reject(err);
return ;
});
})
}
const shouldAbort = (err) => {
if (err) {
console.error('Error in transaction', err.stack)
app.db.query('ROLLBACK', (err) => {
if (err) {
console.error('Error rolling back client', err.stack)
}
done()
})
}
return !!err
}
const updateWrongData = function(){
hasWrongData()
.then(hasDateToUpdate => {
if(!hasDateToUpdate){
console.log("Finish Process");
return ;
}
app.db.query('BEGIN', (err) => {
if (shouldAbort(err)) return;
app.db.query(
"UPDATE child_table ct " +
" SET msg = mt.msg " +
" FROM main_table mt " +
" WHERE mt.id = ct.fk_id " +
" AND ct.id IN ( " +
" SELECT cts.id " +
" FROM child_table cts " +
" INNER JOIN main_table mts " +
" ON( " +
" mts.id = cts.fk_id " +
" AND AND cts.msg <> mts.msg " +
" ) " +
" LIMIT " + maxQueryPerTransaction +
" )",
(err, res) => {
if (shouldAbort(err)) return;
app.db.query('COMMIT', (err) => {
if (err) {
console.error('Error committing transaction', err.stack)
}
HowManyUpdatesWereFinished += res.rowCount;
console.log("How many updates were finished so far? %d", HowManyUpdatesWereFinished)
count -= res.rowCount;
updateWrongData();
})
}
)
})
})
.catch(err => {
console.error('Error on check if exists data to update')
})
;
}
UpdateMsg = {
updateWrongData: updateWrongDate
}
return UpdateMsg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment