123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /*
- 提取文件路径://home/fy/work/xiaoding/owl-admin/app/Models
- 下所有的文件中
- protected $table = '要提取的表名';
- 所有的表名,然后去数据库中查询是否存在这些表,如果不存在,就打印出来
- */
- const fs = require('fs');
- const path = require('path');
- const db = require('../util/db');
- // 模型文件目录路径
- const MODELS_DIR = '/home/fy/work/xiaoding/owl-admin/app/Models';
- // 从PHP文件中提取表名
- function extractTableName(fileContent) {
- const tableMatch = fileContent.match(/protected\s+\$table\s*=\s*['"]([^'"]+)['"]/);
- return tableMatch ? tableMatch[1] : null;
- }
- // 递归获取所有PHP文件
- async function getAllPhpFiles(dir) {
- const files = [];
- const entries = fs.readdirSync(dir, { withFileTypes: true });
- for (const entry of entries) {
- const fullPath = path.join(dir, entry.name);
- if (entry.isDirectory()) {
- files.push(...await getAllPhpFiles(fullPath));
- } else if (entry.isFile() && entry.name.endsWith('.php')) {
- files.push(fullPath);
- }
- }
- return files;
- }
- // 检查表是否存在于数据库中
- async function checkTableExists(tableName) {
- try {
- const [rows] = await db.query(`
- SELECT COUNT(*) as count
- FROM information_schema.tables
- WHERE table_schema = 'xiaoding_test'
- AND table_name = ?
- `, [tableName]);
- return rows[0].count > 0;
- } catch (error) {
- console.error(`检查表 ${tableName} 时出错:`, error);
- return false;
- }
- }
- // 检查数据库中存在但没有对应模型的表
- async function checkMissingModels() {
- try {
- const [rows] = await db.query(`
- SELECT table_name AS table_name
- FROM information_schema.tables
- WHERE
- table_schema = 'xiaoding_test'
- AND table_name NOT LIKE 'admin_%'
- AND table_name NOT LIKE 'manage_%'
- AND table_name NOT LIKE 'view_%'
- AND table_name NOT IN(
- 'cache',
- 'cache_locks',
- 'failed_jobs',
- 'job_batches',
- 'jobs',
- 'migrations',
- 'password_reset_tokens',
- 'personal_access_tokens',
- 'sessions'
- ) `);
- const existingTables = rows.map(row => row.table_name);
- const phpFiles = await getAllPhpFiles(MODELS_DIR);
- const modelNames = phpFiles.map(file => {
- const content = fs.readFileSync(file, 'utf8');
- return extractTableName(content);
- }).filter(name => name !== null);
- const modelNamesWithUnderscore = phpFiles.map(file => path.basename(file, '.php').toLowerCase());
-
- const missingModels = existingTables.filter(table => {
- if (table === undefined) {
- return false; // Skip undefined table names
- }
- const tableWithoutPrefix = table.replace(/^admin_/, '');
- return !modelNames.includes(tableWithoutPrefix) && !modelNamesWithUnderscore.includes(tableWithoutPrefix);
- });
- if (missingModels.length > 0) {
- console.log('\n以下表在数据库中存在,但没有对应的模型:');
- missingModels.forEach(table => {
- console.log(`缺失模型的表名: ${table}`);
- });
- } else {
- console.log('\n所有存在的表都有对应的模型');
- }
- } catch (error) {
- console.error('检查缺失模型时出错:', error);
- }
- }
- // 主函数
- async function main() {
- try {
- // 获取所有PHP文件
- const phpFiles = await getAllPhpFiles(MODELS_DIR);
- console.log(`找到 ${phpFiles.length} 个PHP文件`);
- // 存储不存在的表
- const missingTables = [];
- // 遍历所有文件
- for (const file of phpFiles) {
- const content = fs.readFileSync(file, 'utf8');
- const tableName = extractTableName(content);
- if (tableName) {
- const exists = await checkTableExists(tableName);
- if (!exists) {
- missingTables.push({
- model: path.relative(MODELS_DIR, file),
- table: tableName
- });
- }
- }
- }
- // 打印结果
- if (missingTables.length > 0) {
- console.log('\n以下表在数据库中不存在:');
- missingTables.forEach(({ model, table }) => {
- console.log(`模型文件: ${model}`);
- console.log(`缺失表名: ${table}`);
- console.log('---');
- });
- } else {
- console.log('\n所有表都存在于数据库中');
- }
- // 检查数据库中存在但没有对应模型的表
- await checkMissingModels();
- } catch (error) {
- console.error('执行过程中出错:', error);
- } finally {
- // 关闭数据库连接
- await db.end();
- }
- }
- // 执行主函数
- main().catch(err => {
- console.error('主函数执行出错:', err);
- process.exit(1);
- });
|