114 lines
3.4 KiB
JavaScript
114 lines
3.4 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
const logger = require("../lib/logger");
|
|
|
|
// Use a separate database instance for migration to avoid conflicts
|
|
const Database = require("../lib/database");
|
|
|
|
async function migrateJsonToDatabase() {
|
|
const migrationDb = new Database();
|
|
|
|
try {
|
|
console.log("🚀 Starting migration from JSON to database...");
|
|
|
|
// Initialize migration database
|
|
await migrationDb.init();
|
|
|
|
// Check if firm.json exists
|
|
const firmJsonPath = path.join(__dirname, "..", "firm.json");
|
|
if (!fs.existsSync(firmJsonPath)) {
|
|
console.log("❌ firm.json not found. No migration needed.");
|
|
return;
|
|
}
|
|
|
|
// Read and parse firm.json
|
|
const firmData = JSON.parse(fs.readFileSync(firmJsonPath, "utf8"));
|
|
console.log("📖 Read firm.json successfully");
|
|
|
|
// Flatten the data structure (it's organized by state)
|
|
const allFirms = [];
|
|
for (const [state, firms] of Object.entries(firmData)) {
|
|
if (Array.isArray(firms)) {
|
|
firms.forEach((firm) => {
|
|
// Normalize firm data
|
|
allFirms.push({
|
|
firmName: firm.firmName,
|
|
location: firm.location,
|
|
website: firm.website,
|
|
contactEmail: firm.contactEmail || firm.email,
|
|
state: state,
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
console.log(
|
|
`📊 Found ${allFirms.length} firms across ${
|
|
Object.keys(firmData).length
|
|
} states`
|
|
);
|
|
|
|
// Filter out firms without email addresses
|
|
const validFirms = allFirms.filter((firm) => firm.contactEmail);
|
|
console.log(`✅ ${validFirms.length} firms have valid email addresses`);
|
|
|
|
if (validFirms.length !== allFirms.length) {
|
|
console.log(
|
|
`⚠️ Skipped ${
|
|
allFirms.length - validFirms.length
|
|
} firms without email addresses`
|
|
);
|
|
}
|
|
|
|
// Insert firms into database
|
|
const inserted = await migrationDb.insertFirmsBatch(validFirms);
|
|
console.log(`✨ Inserted ${inserted} new firms into database`);
|
|
|
|
// Remove duplicates
|
|
const duplicatesRemoved = await migrationDb.removeDuplicateFirms();
|
|
if (duplicatesRemoved > 0) {
|
|
console.log(`🧹 Removed ${duplicatesRemoved} duplicate firms`);
|
|
}
|
|
|
|
// Get final counts
|
|
const counts = await migrationDb.getTableCounts();
|
|
console.log("📈 Final database counts:", counts);
|
|
|
|
// Create backup of JSON file
|
|
const backupPath = path.join(
|
|
__dirname,
|
|
"..",
|
|
`firm.json.backup.${Date.now()}`
|
|
);
|
|
fs.copyFileSync(firmJsonPath, backupPath);
|
|
console.log(`💾 Created backup: ${path.basename(backupPath)}`);
|
|
|
|
console.log("✅ Migration completed successfully!");
|
|
} catch (error) {
|
|
console.error("❌ Migration failed:", error.message);
|
|
logger.error("Migration failed", {
|
|
error: error.message,
|
|
stack: error.stack,
|
|
});
|
|
throw error;
|
|
} finally {
|
|
// Only close if not called from main app
|
|
await migrationDb.close();
|
|
}
|
|
}
|
|
|
|
// Run migration if script is called directly
|
|
if (require.main === module) {
|
|
migrateJsonToDatabase()
|
|
.then(() => {
|
|
console.log("Migration script completed");
|
|
process.exit(0);
|
|
})
|
|
.catch((error) => {
|
|
console.error("Migration script failed:", error);
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
module.exports = { migrateJsonToDatabase };
|