jasan/FIX_AND_MIGRATE.php
2026-03-04 22:21:38 +09:00

76 lines
No EOL
2.5 KiB
PHP

<?php
/**
* Synology Autonomous Architect - Deep Migration & Repair Tool
* Target: dda/assets.db
* Function: Fix Mojibake/URL-encoding in names & Apply latest schema
*/
header('Content-Type: text/plain; charset=utf-8');
try {
$dbPath = __DIR__ . DIRECTORY_SEPARATOR . 'dda' . DIRECTORY_SEPARATOR . 'assets.db';
if (!file_exists($dbPath)) {
throw new Exception("File not found at $dbPath");
}
$db = new PDO("sqlite:$dbPath");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "--- 🛠 심층 마이그레이션 및 데이터 복구 시작 ---\n";
// 1. 스키마 강제 보정
$newColumns = [
'disposal_recipient' => 'TEXT',
'disposal_date' => 'TEXT',
'real_user' => 'TEXT',
'disposal_user_id' => 'INTEGER'
];
foreach ($newColumns as $col => $type) {
try {
$db->exec("ALTER TABLE laptop_assets ADD COLUMN $col $type");
echo "✅ 컬럼 보정: $col\n";
} catch (PDOException $e) {
// Already exists or other error
}
}
// 2. 인코딩 복구 (URL-Encoded 데이터가 식별되어 수정 시도)
$stmt = $db->query("SELECT id, assigned_user_name, disposal_recipient FROM laptop_assets");
$assets = $stmt->fetchAll(PDO::FETCH_ASSOC);
$fixCount = 0;
$updateStmt = $db->prepare("UPDATE laptop_assets SET assigned_user_name = ?, disposal_recipient = ? WHERE id = ?");
foreach ($assets as $asset) {
$uName = $asset['assigned_user_name'];
$dRec = $asset['disposal_recipient'];
$newUName = $uName;
$newDRec = $dRec;
// URL 인코딩 탐지 및 변환
if ($uName && strpos($uName, '%') !== false) {
$newUName = urldecode($uName);
}
if ($dRec && strpos($dRec, '%') !== false) {
$newDRec = urldecode($dRec);
}
if ($newUName !== $uName || $newDRec !== $dRec) {
$updateStmt->execute([$newUName, $newDRec, $asset['id']]);
$fixCount++;
}
}
echo "✅ Mojibake/URL-Encoding 데이터 복구: {$fixCount}건 수정 완료.\n";
// 3. STOCK 자산 명칙 동기화
$stmtStock = $db->prepare("UPDATE laptop_assets SET assigned_user_name = '업무용(TEMP_44666b)', current_user_id = NULL WHERE status = 'stock'");
$stmtStock->execute();
echo "✅ 재고(STOCK) 명칭 동기화 완료.\n";
echo "--- 🎉 모든 복구 및 마이그레이션이 완료되었습니다. ---";
} catch (Exception $e) {
echo "❌ 오류: " . $e->getMessage() . "\n";
}
?>