106 lines
3.2 KiB
JavaScript
106 lines
3.2 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const { query, connectDB } = require('../database/connection');
|
|
|
|
async function migratePincodes() {
|
|
try {
|
|
await connectDB();
|
|
console.log('Database connected for migration.');
|
|
|
|
const csvFilePath = path.join(__dirname, '../../data/pincode.csv');
|
|
const fileContent = fs.readFileSync(csvFilePath, 'utf8');
|
|
const lines = fileContent.split('\n').filter(line => line.trim() !== '');
|
|
|
|
if (lines.length === 0) {
|
|
console.log('No data found in pincode.csv');
|
|
return;
|
|
}
|
|
|
|
const headers = lines[0].split(',').map(header => header.trim().toLowerCase());
|
|
const dataRows = lines.slice(1);
|
|
|
|
console.log(`Starting migration of ${dataRows.length} pincode records.`);
|
|
|
|
for (const row of dataRows) {
|
|
const values = parseCsvLine(row);
|
|
|
|
if (values.length !== headers.length) {
|
|
console.warn('Skipping row due to column mismatch:', row);
|
|
continue;
|
|
}
|
|
|
|
const record = {};
|
|
headers.forEach((header, index) => {
|
|
record[header] = values[index];
|
|
});
|
|
|
|
const pincode = record.pincode;
|
|
const office_name = record.officename;
|
|
const office_type = record.officetype;
|
|
const district = record.district;
|
|
const division = record.divisionname;
|
|
const region = record.regionname;
|
|
const state = record.statename;
|
|
const latitude = record.latitude && record.latitude.toLowerCase() !== 'na' ? parseFloat(record.latitude) : null;
|
|
const longitude = record.longitude && record.longitude.toLowerCase() !== 'na' ? parseFloat(record.longitude) : null;
|
|
|
|
const insertQuery = `
|
|
INSERT INTO pincodes (pincode, office_name, office_type, district, division, region, state, latitude, longitude)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
|
ON CONFLICT (pincode) DO UPDATE SET
|
|
office_name = EXCLUDED.office_name,
|
|
office_type = EXCLUDED.office_type,
|
|
district = EXCLUDED.district,
|
|
division = EXCLUDED.division,
|
|
region = EXCLUDED.region,
|
|
state = EXCLUDED.state,
|
|
latitude = EXCLUDED.latitude,
|
|
longitude = EXCLUDED.longitude,
|
|
updated_at = NOW()
|
|
;
|
|
`;
|
|
const insertValues = [
|
|
pincode,
|
|
office_name,
|
|
office_type,
|
|
district,
|
|
division,
|
|
region,
|
|
state,
|
|
latitude,
|
|
longitude
|
|
];
|
|
|
|
await query(insertQuery, insertValues);
|
|
}
|
|
|
|
console.log('Pincode migration completed successfully.');
|
|
} catch (error) {
|
|
console.error('Error during pincode migration:', error);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// Basic CSV parser to handle commas within quoted strings
|
|
function parseCsvLine(line) {
|
|
const values = [];
|
|
let inQuote = false;
|
|
let currentVal = '';
|
|
for (let i = 0; i < line.length; i++) {
|
|
const char = line[i];
|
|
if (char === '"' && (i === 0 || line[i - 1] === ',')) {
|
|
inQuote = !inQuote;
|
|
} else if (char === ',' && !inQuote) {
|
|
values.push(currentVal.trim());
|
|
currentVal = '';
|
|
} else {
|
|
currentVal += char;
|
|
}
|
|
}
|
|
values.push(currentVal.trim());
|
|
return values.map(val => val.startsWith('"') && val.endsWith('"') ? val.slice(1, -1) : val);
|
|
}
|
|
|
|
migratePincodes();
|
|
|