auto_update_label.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const db = require('../util/db');
  2. async function getAllLabels() {
  3. console.log('开始执行脚本');
  4. let map = {};
  5. try {
  6. // 获取所有表名
  7. const [tables] = await db.query(`
  8. select table_name
  9. from information_schema.tables
  10. where table_schema = 'owl'
  11. AND table_name like 'biz_%'
  12. and table_name not LIKE 'admin_%'
  13. and table_name not LIKE 'user_%'
  14. and table_name not LIKE 'label_%'
  15. and table_name not LIKE 'label_record_%'
  16. and table_name not LIKE 'label_record_detail_%';
  17. `);
  18. if (tables.length === 0) {
  19. console.log('没有找到符合条件的表');
  20. return {};
  21. }
  22. // 使用 for...of 循环来按顺序处理每个表
  23. for (const table of tables) {
  24. // 获取每个表的所有字段的名称和备注
  25. const [columns] = await db.query(`
  26. select column_name, column_comment
  27. from information_schema.columns
  28. where table_schema = 'owl'
  29. and table_name = '${table.TABLE_NAME}'
  30. order by ordinal_position;
  31. `);
  32. if (columns.length === 0) {
  33. continue;
  34. }
  35. columns.forEach(column => {
  36. // 处理注释中包含冒号的情况
  37. const comment = column.COLUMN_COMMENT || '';
  38. const processedComment = comment.split(/[::]/, 1)[0];
  39. map[column.COLUMN_NAME] = processedComment;
  40. });
  41. }
  42. return map;
  43. } catch (err) {
  44. console.error('执行脚本时出错:', err);
  45. return null;
  46. }
  47. }
  48. async function getAllPagesJson() {
  49. try {
  50. // 查询所有页面json
  51. const [pages] = await db.query(`
  52. select id, \`schema\`
  53. from admin_pages;
  54. `);
  55. if (pages.length === 0) {
  56. console.log('没有找到页面json');
  57. return null;
  58. }
  59. return pages.map(page => { return { id: page.id, schema: page.schema } });
  60. } catch (err) {
  61. console.error('查询页面json时出错:', err);
  62. return null;
  63. }
  64. }
  65. // 创建主函数并执行
  66. async function main() {
  67. const dict = await getAllLabels();
  68. dict['id'] = '编号';
  69. dict['created_at'] = '创建时间';
  70. dict['updated_at'] = '更新时间';
  71. dict['deleted_at'] = '删除时间';
  72. for (const key in dict) {
  73. if (key.indexOf('_id') != -1) {
  74. dict[key] = key.split('_id')[0] + '关联对象';
  75. }
  76. }
  77. console.log(JSON.stringify(dict, null, 2));
  78. const pages = await getAllPagesJson();
  79. console.log(JSON.stringify(pages, null, 2));
  80. if (pages && dict) {
  81. for (const page of pages) {
  82. let { id, schema } = page;
  83. for (const [columnName, columnComment] of Object.entries(dict)) {
  84. const labelRegex = new RegExp(`"label":"${columnName}"`, 'g');
  85. const titleRegex = new RegExp(`"title":"${columnName}"`, 'g');
  86. schema = schema.replace(labelRegex, `"label":"${columnComment}"`);
  87. schema = schema.replace(titleRegex, `"title":"${columnComment}"`);
  88. }
  89. try {
  90. await db.query(`
  91. UPDATE admin_pages
  92. SET \`schema\` = ?
  93. WHERE id = ?;
  94. `, [schema, id]);
  95. console.log(`页面ID ${id} 的schema更新成功`);
  96. } catch (err) {
  97. console.error(`更新页面ID ${id} 的schema时出错:`, err);
  98. }
  99. }
  100. }
  101. }
  102. main().catch(err => {
  103. console.error('主函数执行出错:', err);
  104. });