Guest User

Untitled

a guest
Feb 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. let UpdateMsg = {};
  2.  
  3. module.exports = (app) => {
  4. const maxQueryPerTransaction = 1000;
  5. let count = 0;
  6. let HowManyUpdatesWereFinished = 0;
  7.  
  8. const countWrongData = () => {
  9. let query = "SELECT count(1) " +
  10. " FROM child_table ct " +
  11. " INNER JOIN main_table mt " +
  12. " ON( " +
  13. " mt.id = ct.fk_id " +
  14. " AND ct.msg <> mt.msg";
  15. return new Promise( (resolve, reject) => {
  16. app.db.query(query)
  17. .then(res => {
  18. resolve(+res.rows[0].count);
  19. })
  20. .catch(err => {
  21. reject(err);
  22. })
  23. })
  24. }
  25.  
  26. const hasWrongData = () =>{
  27. return new Promise((resolve, reject) => {
  28. if(count > 0) {
  29. resolve(true);
  30. return ;
  31. }
  32.  
  33. console.log('Check if exists data to fix');
  34. countWrongData()
  35. .then(countWrongData => {
  36. console.log('exists %d data to fix', countWrongData);
  37. count = countWrongData;
  38. if(countWrongData === 0){
  39. resolve(false);
  40. return ;
  41. }
  42.  
  43. resolve(true);
  44. return ;
  45. })
  46. .catch(err => {
  47. reject(err);
  48. return ;
  49. });
  50. })
  51. }
  52.  
  53. const shouldAbort = (err) => {
  54. if (err) {
  55. console.error('Error in transaction', err.stack)
  56. app.db.query('ROLLBACK', (err) => {
  57. if (err) {
  58. console.error('Error rolling back client', err.stack)
  59. }
  60.  
  61. done()
  62. })
  63. }
  64. return !!err
  65. }
  66.  
  67. const updateWrongData = function(){
  68. hasWrongData()
  69. .then(hasDateToUpdate => {
  70. if(!hasDateToUpdate){
  71. console.log("Finish Process");
  72. return ;
  73. }
  74.  
  75. app.db.query('BEGIN', (err) => {
  76. if (shouldAbort(err)) return;
  77.  
  78. app.db.query(
  79. "UPDATE child_table ct " +
  80. " SET msg = mt.msg " +
  81. " FROM main_table mt " +
  82. " WHERE mt.id = ct.fk_id " +
  83. " AND ct.id IN ( " +
  84. " SELECT cts.id " +
  85. " FROM child_table cts " +
  86. " INNER JOIN main_table mts " +
  87. " ON( " +
  88. " mts.id = cts.fk_id " +
  89. " AND AND cts.msg <> mts.msg " +
  90. " ) " +
  91. " LIMIT " + maxQueryPerTransaction +
  92. " )",
  93. (err, res) => {
  94. if (shouldAbort(err)) return;
  95.  
  96. app.db.query('COMMIT', (err) => {
  97. if (err) {
  98. console.error('Error committing transaction', err.stack)
  99. }
  100.  
  101. HowManyUpdatesWereFinished += res.rowCount;
  102. console.log("How many updates were finished so far? %d", HowManyUpdatesWereFinished)
  103. count -= res.rowCount;
  104. updateWrongData();
  105. })
  106. }
  107. )
  108. })
  109. })
  110. .catch(err => {
  111. console.error('Error on check if exists data to update')
  112. })
  113. ;
  114. }
  115.  
  116. UpdateMsg = {
  117. updateWrongData: updateWrongDate
  118. }
  119.  
  120. return UpdateMsg;
  121. }
Add Comment
Please, Sign In to add comment