You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.5 KiB
106 lines
3.5 KiB
const { Sequelize } = require('sequelize');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// 读取环境变量
|
|
const envPath = path.join(__dirname, '.env');
|
|
if (fs.existsSync(envPath)) {
|
|
const envContent = fs.readFileSync(envPath, 'utf8');
|
|
const envVars = envContent.split('\n').filter(line => line.trim() && !line.startsWith('#'));
|
|
envVars.forEach(line => {
|
|
const [key, value] = line.split('=').map(part => part.trim());
|
|
process.env[key] = value;
|
|
});
|
|
}
|
|
|
|
// 数据库连接配置
|
|
const sequelize = new Sequelize(
|
|
process.env.DB_NAME || 'wechat_app',
|
|
process.env.DB_USER || 'root',
|
|
process.env.DB_PASSWORD || '',
|
|
{
|
|
host: process.env.DB_HOST || 'localhost',
|
|
port: process.env.DB_PORT || 3306,
|
|
dialect: 'mysql',
|
|
logging: false,
|
|
timezone: '+08:00' // 设置时区为UTC+8
|
|
}
|
|
);
|
|
|
|
// 查看usermanagements表结构
|
|
async function viewUserManagementsTableStructure() {
|
|
try {
|
|
console.log('========================================');
|
|
console.log('查看usermanagements表结构');
|
|
console.log('========================================');
|
|
|
|
// 连接数据库
|
|
await sequelize.authenticate();
|
|
console.log('✅ 数据库连接成功');
|
|
|
|
// 查询表结构
|
|
const tableStructure = await sequelize.query(
|
|
'SHOW COLUMNS FROM usermanagements',
|
|
{ type: sequelize.QueryTypes.SELECT }
|
|
);
|
|
|
|
console.log('\nusermanagements表字段信息:');
|
|
tableStructure.forEach(column => {
|
|
console.log(`- ${column.Field}: ${column.Type} ${column.Null === 'NO' ? '(NOT NULL)' : ''} ${column.Key === 'PRI' ? '(PRIMARY KEY)' : ''}`);
|
|
});
|
|
|
|
// 查询表索引
|
|
const indexes = await sequelize.query(
|
|
'SHOW INDEX FROM usermanagements',
|
|
{ type: sequelize.QueryTypes.SELECT }
|
|
);
|
|
|
|
if (indexes.length > 0) {
|
|
console.log('\nusermanagements表索引信息:');
|
|
indexes.forEach(index => {
|
|
console.log(`- 索引名: ${index.Key_name}, 字段: ${index.Column_name}, 唯一: ${index.Non_unique === 0 ? '是' : '否'}`);
|
|
});
|
|
}
|
|
|
|
// 查询表中的最新5条记录
|
|
console.log('\nusermanagements表中的最新5条记录:');
|
|
|
|
// 尝试查找一个可能的时间字段
|
|
const possibleTimeFields = tableStructure
|
|
.map(col => col.Field)
|
|
.filter(field => field.toLowerCase().includes('time') || field.toLowerCase().includes('date'));
|
|
|
|
let latestRecords;
|
|
|
|
if (possibleTimeFields.length > 0) {
|
|
console.log(`找到可能的时间字段: ${possibleTimeFields.join(', ')}`);
|
|
// 使用第一个找到的时间字段排序
|
|
latestRecords = await sequelize.query(
|
|
`SELECT userId FROM usermanagements ORDER BY ${possibleTimeFields[0]} DESC LIMIT 5`,
|
|
{ type: sequelize.QueryTypes.SELECT }
|
|
);
|
|
} else {
|
|
console.log('未找到明显的时间字段,按userId排序');
|
|
latestRecords = await sequelize.query(
|
|
'SELECT userId FROM usermanagements ORDER BY userId DESC LIMIT 5',
|
|
{ type: sequelize.QueryTypes.SELECT }
|
|
);
|
|
}
|
|
|
|
latestRecords.forEach((record, index) => {
|
|
console.log(` ${index + 1}. userId: ${record.userId}`);
|
|
});
|
|
|
|
console.log('\n========================================');
|
|
|
|
} catch (error) {
|
|
console.error('❌ 查看表结构过程中发生错误:', error.message);
|
|
console.error('错误详情:', error);
|
|
} finally {
|
|
// 关闭数据库连接
|
|
await sequelize.close();
|
|
}
|
|
}
|
|
|
|
// 运行查看
|
|
viewUserManagementsTableStructure();
|