Fix JSON normalization in live SQL import

This commit is contained in:
Dwindi Ramadhana
2026-02-07 15:03:34 +07:00
parent 3c8f061819
commit 1301349b3a

View File

@@ -187,7 +187,14 @@ class LiveSqlImportService
if (isset($this->tableColumnMap[$table])) {
$mapped = [];
foreach ($this->tableColumnMap[$table] as $from => $to) {
$mapped[$to] = $row[$from] ?? null;
$value = $row[$from] ?? null;
if ($table === 'licenses' && $to === 'meta_json') {
$value = $this->normalizeJson($value);
}
if ($table === 'usage_logs' && $to === 'seen_signatures') {
$value = $this->normalizeJson($value);
}
$mapped[$to] = $value;
}
if ($table === 'licenses') {
@@ -219,6 +226,19 @@ class LiveSqlImportService
];
}
if ($table === 'ai_judgments' && isset($row['raw_response'])) {
$row['raw_response'] = $this->normalizeJson($row['raw_response']);
}
if ($table === 'contributor_rewards' && isset($row['meta'])) {
$row['meta'] = $this->normalizeJson($row['meta']);
}
if ($table === 'email_queue' && isset($row['payload'])) {
$row['payload'] = $this->normalizeJson($row['payload']);
}
if ($table === 'user_prefs' && isset($row['preferred_languages'])) {
$row['preferred_languages'] = $this->normalizeJson($row['preferred_languages']);
}
return $row;
}
@@ -325,4 +345,28 @@ class LiveSqlImportService
default => $ch,
};
}
private function normalizeJson(mixed $value): ?string
{
if ($value === null) {
return null;
}
if (!is_string($value)) {
return json_encode($value);
}
$decoded = json_decode($value, true);
if (json_last_error() === JSON_ERROR_NONE) {
return json_encode($decoded);
}
$stripped = stripslashes($value);
$decoded = json_decode($stripped, true);
if (json_last_error() === JSON_ERROR_NONE) {
return json_encode($decoded);
}
return null;
}
}