buider
This commit is contained in:
115
backend/fix-location-data.js
Normal file
115
backend/fix-location-data.js
Normal file
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* 修复数据库中的location字段格式
|
||||
* 将JSON字符串转换为JSON对象
|
||||
* @file fix-location-data.js
|
||||
*/
|
||||
|
||||
const { Farm } = require('./models');
|
||||
const { sequelize } = require('./config/database-simple');
|
||||
|
||||
async function fixLocationData() {
|
||||
try {
|
||||
console.log('连接数据库...');
|
||||
await sequelize.authenticate();
|
||||
|
||||
console.log('\n查询所有养殖场记录...');
|
||||
const farms = await Farm.findAll({
|
||||
attributes: ['id', 'name', 'location']
|
||||
});
|
||||
|
||||
console.log(`找到 ${farms.length} 条记录`);
|
||||
|
||||
let fixedCount = 0;
|
||||
let errorCount = 0;
|
||||
|
||||
for (const farm of farms) {
|
||||
try {
|
||||
console.log(`\n处理 ID: ${farm.id}, 名称: ${farm.name}`);
|
||||
console.log(`原始 location: ${JSON.stringify(farm.location)}`);
|
||||
console.log(`location 类型: ${typeof farm.location}`);
|
||||
|
||||
let needsUpdate = false;
|
||||
let newLocation = {};
|
||||
|
||||
// 检查location字段的格式
|
||||
if (typeof farm.location === 'string') {
|
||||
// 如果是字符串,尝试解析为JSON
|
||||
try {
|
||||
newLocation = JSON.parse(farm.location);
|
||||
needsUpdate = true;
|
||||
console.log(` ✅ 解析JSON字符串成功: ${JSON.stringify(newLocation)}`);
|
||||
} catch (parseError) {
|
||||
console.log(` ❌ 解析JSON字符串失败: ${parseError.message}`);
|
||||
errorCount++;
|
||||
continue;
|
||||
}
|
||||
} else if (farm.location && typeof farm.location === 'object') {
|
||||
// 如果已经是对象,检查是否需要格式化
|
||||
newLocation = farm.location;
|
||||
console.log(` ✅ 已经是对象格式`);
|
||||
} else {
|
||||
// 如果为空或其他类型,设置为空对象
|
||||
newLocation = {};
|
||||
needsUpdate = true;
|
||||
console.log(` ⚠️ 设置为空对象`);
|
||||
}
|
||||
|
||||
// 验证经纬度数据
|
||||
if (newLocation.lng !== undefined) {
|
||||
newLocation.lng = parseFloat(newLocation.lng);
|
||||
}
|
||||
if (newLocation.lat !== undefined) {
|
||||
newLocation.lat = parseFloat(newLocation.lat);
|
||||
}
|
||||
|
||||
// 更新数据库记录
|
||||
if (needsUpdate) {
|
||||
await farm.update({ location: newLocation });
|
||||
console.log(` ✅ 更新成功: ${JSON.stringify(newLocation)}`);
|
||||
fixedCount++;
|
||||
} else {
|
||||
console.log(` ⏭️ 无需更新`);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error(` ❌ 处理记录 ${farm.id} 时出错:`, error.message);
|
||||
errorCount++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n' + '='.repeat(60));
|
||||
console.log(`修复完成!`);
|
||||
console.log(`总记录数: ${farms.length}`);
|
||||
console.log(`修复成功: ${fixedCount}`);
|
||||
console.log(`修复失败: ${errorCount}`);
|
||||
console.log(`无需修复: ${farms.length - fixedCount - errorCount}`);
|
||||
|
||||
// 验证修复结果
|
||||
console.log('\n验证修复结果...');
|
||||
const verifyFarms = await Farm.findAll({
|
||||
attributes: ['id', 'name', 'location'],
|
||||
limit: 5
|
||||
});
|
||||
|
||||
verifyFarms.forEach(farm => {
|
||||
console.log(`ID: ${farm.id}, Location类型: ${typeof farm.location}, 值: ${JSON.stringify(farm.location)}`);
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('修复失败:', error.message);
|
||||
if (error.sql) {
|
||||
console.error('SQL:', error.sql);
|
||||
}
|
||||
} finally {
|
||||
await sequelize.close();
|
||||
console.log('\n数据库连接已关闭');
|
||||
}
|
||||
}
|
||||
|
||||
// 运行修复
|
||||
if (require.main === module) {
|
||||
console.log('开始修复数据库中的location字段格式...');
|
||||
fixLocationData();
|
||||
}
|
||||
|
||||
module.exports = { fixLocationData };
|
||||
Reference in New Issue
Block a user