Guest User

Untitled

a guest
Nov 16th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. const mysql = require('mysql');
  2. const json2csv = require('json2csv');
  3. const fs = require('fs');
  4. const await = require('asyncawait/await');
  5. const async = require('asyncawait/async');
  6.  
  7. var knex = require('knex')({
  8. client: 'mysql',
  9. connection: {
  10. host: '*',
  11. user: 'root',
  12. password: '*',
  13. database: '*'
  14. }
  15. });
  16. var getTables = async(function () {
  17. console.log('getTables');
  18. let tables = [];
  19. const res = await (knex.raw('show tables;'));
  20. res[0].map((c) => {
  21. tables.push(c['Tables_in_shelfmintdev2']);
  22. })
  23. return tables;
  24. });
  25. var getFields = async(function (tables) {
  26. console.log('getFields');
  27. let tablesWithFields = [];
  28. tables.map((t, i) => {
  29. const res = await (knex.raw(`SHOW COLUMNS FROM \`${t}\``));
  30. tablesWithFields.push({
  31. tableName: t,
  32. fields: res[0] //Field:
  33. })
  34.  
  35. })
  36. return tablesWithFields;
  37. });
  38.  
  39. var getSuspiciousFields = async(function (tablesWithFields) {
  40. console.log('getSuspiciousFields');
  41. let tableWithSuspicious = [];
  42.  
  43. tablesWithFields.map((t, i) => {
  44. try {
  45. let w = t.fields.map((f, j) => {
  46. return f.Field + ' IS NULL OR ';
  47. });
  48. w[w.length - 1] = w[w.length - 1].replace('OR ', '');
  49. w = w.join('');
  50. const q = `select * from \`${t.tableName}\` where ${w} order by Id desc limit 200`;
  51. console.log('q', q);
  52. const res = await (knex.raw(q));
  53.  
  54. if (res[0].length == 0) {
  55. //check for last insert
  56. } else {
  57. tableWithSuspicious.push({
  58. tableName:t.tableName,
  59. query:q,
  60. reason:"null"
  61. })
  62. //find nulls fields
  63. // for (let k = 0; k < res[0].length; k++) {
  64. // for (let key in res[0][k]) {
  65. // if (res[0][k][key] == null) {
  66. // tableWithSuspicious.push({
  67. // tableName:t.tableName,
  68. // field:key,
  69. // reason:"null"
  70. // })
  71. // }
  72.  
  73. // }
  74. // }
  75. }
  76.  
  77. } catch (e) {
  78. console.log(e)
  79. }
  80.  
  81. })
  82. return tableWithSuspicious;
  83. });
  84.  
  85. function createCsv(name, fields, data) {
  86. console.log('createCsv');
  87. var csv = json2csv({
  88. data: data,
  89. fields: fields
  90. });
  91. fs.writeFile(name + '.csv', csv, function (err) {
  92. if (err)
  93. throw err;
  94. console.log('csv saved');
  95. });
  96. fs
  97. .writeFile(name + '.json', JSON.stringify(data), function (err) {
  98. if (err)
  99. throw err;
  100. console.log('json saved');
  101. });
  102. }
  103.  
  104. var main = async(function () {
  105. console.log('main');
  106. let tables = await (getTables());
  107. let tablesWithFields = await (getFields(tables));
  108. let tableWithSuspicious = await (getSuspiciousFields(tablesWithFields));
  109. createCsv('tableWithSuspicious', [
  110. 'tableName', 'query', 'reason'
  111. ], tableWithSuspicious);
  112. return ;
  113. });
  114.  
  115. main();
Add Comment
Please, Sign In to add comment