1
This commit is contained in:
120
scripts/sync_job_postings_table.js
Normal file
120
scripts/sync_job_postings_table.js
Normal file
@@ -0,0 +1,120 @@
|
||||
/**
|
||||
* 同步 job_postings 表结构
|
||||
* 用于创建或更新数据库表
|
||||
*/
|
||||
|
||||
const Sequelize = require('sequelize');
|
||||
|
||||
// 加载配置
|
||||
const config = require('../config/config');
|
||||
const dbConfig = config.db;
|
||||
|
||||
async function syncJobPostingsTable() {
|
||||
console.log('🔄 开始同步 job_postings 表结构...\n');
|
||||
|
||||
try {
|
||||
// 创建 Sequelize 实例
|
||||
const sequelize = new Sequelize(
|
||||
dbConfig.database,
|
||||
dbConfig.username,
|
||||
dbConfig.password,
|
||||
{
|
||||
host: dbConfig.host,
|
||||
port: dbConfig.port,
|
||||
dialect: dbConfig.dialect,
|
||||
logging: false
|
||||
}
|
||||
);
|
||||
|
||||
// 测试连接
|
||||
await sequelize.authenticate();
|
||||
console.log('✅ 数据库连接成功\n');
|
||||
|
||||
// 加载模型定义
|
||||
const job_postings = require('../api/model/job_postings')(sequelize);
|
||||
|
||||
// 同步表结构(alter: true 会自动添加缺失的字段,保留现有数据)
|
||||
console.log('⚠️ 注意:使用 alter: true 模式同步(保留现有数据)');
|
||||
console.log('如果需要完全重建表,请修改为 force: true\n');
|
||||
|
||||
await job_postings.sync({ alter: true });
|
||||
|
||||
console.log('✅ job_postings 表同步成功!\n');
|
||||
|
||||
// 显示表结构
|
||||
const tableInfo = await sequelize.query(
|
||||
`DESCRIBE job_postings`,
|
||||
{ type: Sequelize.QueryTypes.SELECT }
|
||||
);
|
||||
|
||||
console.log('📋 当前表结构:');
|
||||
console.table(tableInfo.map(field => ({
|
||||
字段名: field.Field,
|
||||
类型: field.Type,
|
||||
允许空: field.Null,
|
||||
默认值: field.Default
|
||||
})));
|
||||
|
||||
// 检查关键字段
|
||||
const requiredFields = [
|
||||
'id',
|
||||
'sn_code',
|
||||
'platform',
|
||||
'keyword',
|
||||
'encryptBossId',
|
||||
'jobId',
|
||||
'jobTitle',
|
||||
'companyId',
|
||||
'companyName',
|
||||
'companySize',
|
||||
'companyIndustry',
|
||||
'salary',
|
||||
'jobRequirements',
|
||||
'jobDescription',
|
||||
'location',
|
||||
'experience',
|
||||
'education',
|
||||
'aiMatchScore',
|
||||
'applyStatus',
|
||||
'chatStatus',
|
||||
'originalData'
|
||||
];
|
||||
const existingFields = tableInfo.map(f => f.Field);
|
||||
|
||||
console.log('\n🔍 检查关键字段:');
|
||||
const missingFields = [];
|
||||
requiredFields.forEach(field => {
|
||||
const exists = existingFields.includes(field);
|
||||
console.log(` ${exists ? '✅' : '❌'} ${field}`);
|
||||
if (!exists) {
|
||||
missingFields.push(field);
|
||||
}
|
||||
});
|
||||
|
||||
if (missingFields.length > 0) {
|
||||
console.log('\n⚠️ 缺少以下字段:', missingFields.join(', '));
|
||||
console.log('建议:重新运行同步或手动添加这些字段');
|
||||
} else {
|
||||
console.log('\n✅ 所有必需字段都存在!');
|
||||
}
|
||||
|
||||
// 关闭连接
|
||||
await sequelize.close();
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 同步失败:', error.message);
|
||||
console.error('\n详细错误:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 执行同步
|
||||
syncJobPostingsTable()
|
||||
.then(() => {
|
||||
console.log('\n✨ 同步完成!');
|
||||
process.exit(0);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('\n💥 执行失败:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user