This commit is contained in:
hashbro
2026-09-12 03:42:48 +08:00
parent b212332828
commit 8c5724ff3b
50 changed files with 73924 additions and 657 deletions
@@ -0,0 +1,56 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
/**
* Convert existing 32-char plain-hex device identifiers to the canonical
* dashed UUID format (8-4-4-4-12, uppercase) so that storage and display
* are always the same format across devices, page_visits, and ds_chain_logs.
*
* Only 32-char hex values are real UUIDs — shorter IDs (e.g. old xxbb
* 16-hex device IDs) are left untouched.
*/
return new class extends Migration
{
private function dashedKey(string $plain): ?string
{
$hex = strtoupper(preg_replace('/[^0-9A-Fa-f]/', '', $plain) ?? '');
if (strlen($hex) !== 32) {
return null;
}
return substr($hex, 0, 8).'-'.substr($hex, 8, 4).'-'.substr($hex, 12, 4).'-'.substr($hex, 16, 4).'-'.substr($hex, 20, 12);
}
private function convertColumn(string $table, string $column): void
{
$rows = DB::table($table)->get(['id', $column]);
foreach ($rows as $row) {
$dashed = $this->dashedKey((string) $row->{$column});
if ($dashed !== null) {
DB::table($table)->where('id', $row->id)->update([$column => $dashed]);
}
}
}
public function up(): void
{
$this->convertColumn('devices', 'device_id');
$this->convertColumn('page_visits', 'client_uid');
$this->convertColumn('ds_chain_logs', 'client_uid');
}
public function down(): void
{
foreach (['devices' => 'device_id', 'page_visits' => 'client_uid', 'ds_chain_logs' => 'client_uid'] as $table => $column) {
$rows = DB::table($table)->get(['id', $column]);
foreach ($rows as $row) {
$plain = strtoupper(preg_replace('/[^0-9A-Fa-f]/', '', (string) $row->{$column}) ?? '');
if (strlen($plain) === 32) {
DB::table($table)->where('id', $row->id)->update([$column => $plain]);
}
}
}
}
};