auto_check_model_and_table.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. 提取文件路径://home/fy/work/xiaoding/owl-admin/app/Models
  3. 下所有的文件中
  4. protected $table = '要提取的表名';
  5. 所有的表名,然后去数据库中查询是否存在这些表,如果不存在,就打印出来
  6. */
  7. const fs = require('fs');
  8. const path = require('path');
  9. const db = require('../util/db');
  10. // 模型文件目录路径
  11. const MODELS_DIR = '/home/fy/work/xiaoding/owl-admin/app/Models';
  12. // 从PHP文件中提取表名
  13. function extractTableName(fileContent) {
  14. const tableMatch = fileContent.match(/protected\s+\$table\s*=\s*['"]([^'"]+)['"]/);
  15. return tableMatch ? tableMatch[1] : null;
  16. }
  17. // 递归获取所有PHP文件
  18. async function getAllPhpFiles(dir) {
  19. const files = [];
  20. const entries = fs.readdirSync(dir, { withFileTypes: true });
  21. for (const entry of entries) {
  22. const fullPath = path.join(dir, entry.name);
  23. if (entry.isDirectory()) {
  24. files.push(...await getAllPhpFiles(fullPath));
  25. } else if (entry.isFile() && entry.name.endsWith('.php')) {
  26. files.push(fullPath);
  27. }
  28. }
  29. return files;
  30. }
  31. // 检查表是否存在于数据库中
  32. async function checkTableExists(tableName) {
  33. try {
  34. const [rows] = await db.query(`
  35. SELECT COUNT(*) as count
  36. FROM information_schema.tables
  37. WHERE table_schema = 'xiaoding_test'
  38. AND table_name = ?
  39. `, [tableName]);
  40. return rows[0].count > 0;
  41. } catch (error) {
  42. console.error(`检查表 ${tableName} 时出错:`, error);
  43. return false;
  44. }
  45. }
  46. // 检查数据库中存在但没有对应模型的表
  47. async function checkMissingModels() {
  48. try {
  49. const [rows] = await db.query(`
  50. SELECT table_name AS table_name
  51. FROM information_schema.tables
  52. WHERE
  53. table_schema = 'xiaoding_test'
  54. AND table_name NOT LIKE 'admin_%'
  55. AND table_name NOT LIKE 'manage_%'
  56. AND table_name NOT IN(
  57. 'cache',
  58. 'cache_locks',
  59. 'failed_jobs',
  60. 'job_batches',
  61. 'jobs',
  62. 'migrations',
  63. 'password_reset_tokens',
  64. 'personal_access_tokens'
  65. ) `);
  66. const existingTables = rows.map(row => row.table_name);
  67. const phpFiles = await getAllPhpFiles(MODELS_DIR);
  68. const modelNames = phpFiles.map(file => {
  69. const content = fs.readFileSync(file, 'utf8');
  70. return extractTableName(content);
  71. }).filter(name => name !== null);
  72. const modelNamesWithUnderscore = phpFiles.map(file => path.basename(file, '.php').toLowerCase());
  73. const missingModels = existingTables.filter(table => {
  74. if (table === undefined) {
  75. return false; // Skip undefined table names
  76. }
  77. const tableWithoutPrefix = table.replace(/^admin_/, '');
  78. return !modelNames.includes(tableWithoutPrefix) && !modelNamesWithUnderscore.includes(tableWithoutPrefix);
  79. });
  80. if (missingModels.length > 0) {
  81. console.log('\n以下表在数据库中存在,但没有对应的模型:');
  82. missingModels.forEach(table => {
  83. console.log(`缺失模型的表名: ${table}`);
  84. });
  85. } else {
  86. console.log('\n所有存在的表都有对应的模型');
  87. }
  88. } catch (error) {
  89. console.error('检查缺失模型时出错:', error);
  90. }
  91. }
  92. // 主函数
  93. async function main() {
  94. try {
  95. // 获取所有PHP文件
  96. const phpFiles = await getAllPhpFiles(MODELS_DIR);
  97. console.log(`找到 ${phpFiles.length} 个PHP文件`);
  98. // 存储不存在的表
  99. const missingTables = [];
  100. // 遍历所有文件
  101. for (const file of phpFiles) {
  102. const content = fs.readFileSync(file, 'utf8');
  103. const tableName = extractTableName(content);
  104. if (tableName) {
  105. const exists = await checkTableExists(tableName);
  106. if (!exists) {
  107. missingTables.push({
  108. model: path.relative(MODELS_DIR, file),
  109. table: tableName
  110. });
  111. }
  112. }
  113. }
  114. // 打印结果
  115. if (missingTables.length > 0) {
  116. console.log('\n以下表在数据库中不存在:');
  117. missingTables.forEach(({ model, table }) => {
  118. console.log(`模型文件: ${model}`);
  119. console.log(`缺失表名: ${table}`);
  120. console.log('---');
  121. });
  122. } else {
  123. console.log('\n所有表都存在于数据库中');
  124. }
  125. // 检查数据库中存在但没有对应模型的表
  126. await checkMissingModels();
  127. } catch (error) {
  128. console.error('执行过程中出错:', error);
  129. } finally {
  130. // 关闭数据库连接
  131. await db.end();
  132. }
  133. }
  134. // 执行主函数
  135. main().catch(err => {
  136. console.error('主函数执行出错:', err);
  137. process.exit(1);
  138. });