style: 리팩토링 및 CSS 통합 작업 완료 (하드코딩 스타일 제거)
This commit is contained in:
63
fix_all_dates.cjs
Normal file
63
fix_all_dates.cjs
Normal file
@@ -0,0 +1,63 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
require('dotenv').config({ override: true });
|
||||
|
||||
function formatToYYYYMMDD(val) {
|
||||
if (!val) return null;
|
||||
const s = String(val).trim().replace(/[^0-9]/g, '');
|
||||
if (s.length === 8) {
|
||||
// YYYYMMDD
|
||||
return `${s.substring(0, 4)}-${s.substring(4, 6)}-${s.substring(6, 8)}`;
|
||||
} else if (s.length === 6) {
|
||||
// YYMMDD -> Assume 20XX
|
||||
const year = parseInt(s.substring(0, 2)) > 50 ? '19' + s.substring(0, 2) : '20' + s.substring(0, 2);
|
||||
return `${year}-${s.substring(2, 4)}-${s.substring(4, 6)}`;
|
||||
}
|
||||
|
||||
// Try to split by dots or slashes if original had them
|
||||
const parts = String(val).trim().split(/[\.\-\/]/);
|
||||
if (parts.length === 3) {
|
||||
let y = parts[0];
|
||||
let m = parts[1].padStart(2, '0');
|
||||
let d = parts[2].padStart(2, '0');
|
||||
if (y.length === 2) y = '20' + y;
|
||||
if (y.length === 4 && m.length <= 2 && d.length <= 2) {
|
||||
return `${y}-${m}-${d}`;
|
||||
}
|
||||
}
|
||||
|
||||
return val; // Return as is if format is unknown
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const pool = mysql.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
database: process.env.DB_NAME,
|
||||
port: parseInt(process.env.DB_PORT || '3306')
|
||||
});
|
||||
|
||||
const connection = await pool.getConnection();
|
||||
try {
|
||||
const [rows] = await connection.query('SELECT id, purchase_date FROM asset_core WHERE purchase_date IS NOT NULL AND purchase_date != \'\'');
|
||||
console.log(`Found ${rows.length} rows to check.`);
|
||||
|
||||
let updatedCount = 0;
|
||||
for (const row of rows) {
|
||||
const original = row.purchase_date;
|
||||
const formatted = formatToYYYYMMDD(original);
|
||||
|
||||
if (formatted !== original && /^\d{4}-\d{2}-\d{2}$/.test(formatted)) {
|
||||
await connection.query('UPDATE asset_core SET purchase_date = ? WHERE id = ?', [formatted, row.id]);
|
||||
updatedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`✅ Successfully updated ${updatedCount} rows to YYYY-MM-DD format.`);
|
||||
} catch (err) {
|
||||
console.error('❌ Error during date migration:', err);
|
||||
} finally {
|
||||
connection.release();
|
||||
await pool.end();
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user