142 lines
5.1 KiB
PHP
142 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Korrigiert created_at / updated_at auf bereits importierten Datensätzen.
|
|
*
|
|
* Verwendet Cross-DB-JOINs (gleicher MySQL-Server), um die Legacy-Timestamps
|
|
* in einer einzigen SQL-Operation auf die neuen Tabellen zu übertragen.
|
|
* Viel schneller als ein erneuter --force Import.
|
|
*
|
|
* Verwendung:
|
|
* php artisan legacy:fix-timestamps --dry-run # nur zählen
|
|
* php artisan legacy:fix-timestamps # alle Entitäten, beide Portale
|
|
* php artisan legacy:fix-timestamps --entity=companies --portal=presseecho
|
|
*/
|
|
class FixLegacyTimestamps extends Command
|
|
{
|
|
protected $signature = 'legacy:fix-timestamps
|
|
{--entity=all : Entität (users|companies|contacts|press-releases|all)}
|
|
{--portal=all : Portal (presseecho|businessportal24|all)}
|
|
{--dry-run : Nur zählen, nichts schreiben}';
|
|
|
|
protected $description = 'Korrigiert created_at/updated_at auf importierten Datensätzen aus den Legacy-DBs.';
|
|
|
|
private const PORTAL_DB = [
|
|
'presseecho' => 'presseecho',
|
|
'businessportal24' => 'businessportal',
|
|
];
|
|
|
|
/** Mapping: neue Tabelle → (legacy_portal, legacy_db, legacy_table, legacy_created_col) */
|
|
private const ENTITY_CONFIG = [
|
|
'users' => [
|
|
'new_table' => 'users',
|
|
'legacy_table_name' => 'sf_guard_user',
|
|
'legacy_created' => 'created_at',
|
|
'legacy_updated' => 'updated_at',
|
|
],
|
|
'companies' => [
|
|
'new_table' => 'companies',
|
|
'legacy_table_name' => 'company',
|
|
'legacy_created' => 'created_at',
|
|
'legacy_updated' => 'updated_at',
|
|
],
|
|
'contacts' => [
|
|
'new_table' => 'contacts',
|
|
'legacy_table_name' => 'contact',
|
|
'legacy_created' => 'created_at',
|
|
'legacy_updated' => 'updated_at',
|
|
],
|
|
'press-releases' => [
|
|
'new_table' => 'press_releases',
|
|
'legacy_table_name' => 'press_release',
|
|
'legacy_created' => 'created_at',
|
|
'legacy_updated' => 'updated_at',
|
|
],
|
|
];
|
|
|
|
public function handle(): int
|
|
{
|
|
$entityOpt = $this->option('entity');
|
|
$portalOpt = $this->option('portal');
|
|
$isDryRun = (bool) $this->option('dry-run');
|
|
|
|
$entities = $entityOpt === 'all' ? array_keys(self::ENTITY_CONFIG) : [$entityOpt];
|
|
$portals = $portalOpt === 'all' ? array_keys(self::PORTAL_DB) : [$portalOpt];
|
|
|
|
if ($isDryRun) {
|
|
$this->warn('[DRY-RUN] Kein tatsächlicher Schreibvorgang.');
|
|
}
|
|
|
|
$totalUpdated = 0;
|
|
$start = microtime(true);
|
|
|
|
foreach ($entities as $entity) {
|
|
$config = self::ENTITY_CONFIG[$entity] ?? null;
|
|
if (! $config) {
|
|
$this->error("Unbekannte Entität: {$entity}");
|
|
|
|
continue;
|
|
}
|
|
|
|
foreach ($portals as $portal) {
|
|
$legacyDb = self::PORTAL_DB[$portal] ?? null;
|
|
if (! $legacyDb) {
|
|
$this->error("Unbekanntes Portal: {$portal}");
|
|
|
|
continue;
|
|
}
|
|
|
|
$updated = $this->fixEntity($config, $portal, $legacyDb, $isDryRun);
|
|
$totalUpdated += $updated;
|
|
|
|
$this->line(" [{$entity}] [{$portal}] → {$updated} Datensätze ".($isDryRun ? 'würden aktualisiert' : 'aktualisiert'));
|
|
}
|
|
}
|
|
|
|
$elapsed = round(microtime(true) - $start, 1);
|
|
$this->newLine();
|
|
$this->info("Gesamt: {$totalUpdated} Datensätze in {$elapsed}s.");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function fixEntity(array $config, string $portal, string $legacyDb, bool $isDryRun): int
|
|
{
|
|
$newTable = $config['new_table'];
|
|
$legacyTable = $config['legacy_table_name'];
|
|
$legacyCreated = $config['legacy_created'];
|
|
$legacyUpdated = $config['legacy_updated'];
|
|
|
|
if ($isDryRun) {
|
|
// Nur zählen: wie viele Datensätze hätten falsche Timestamps?
|
|
return (int) DB::selectOne("
|
|
SELECT COUNT(*) as cnt
|
|
FROM {$newTable} n
|
|
JOIN legacy_import_map m
|
|
ON m.target_id = n.id
|
|
AND m.target_table = '{$newTable}'
|
|
AND m.legacy_portal = '{$portal}'
|
|
JOIN `{$legacyDb}`.`{$legacyTable}` lc ON lc.id = m.legacy_id
|
|
WHERE n.created_at != lc.{$legacyCreated}
|
|
OR n.updated_at != lc.{$legacyUpdated}
|
|
")->cnt ?? 0;
|
|
}
|
|
|
|
return DB::affectingStatement("
|
|
UPDATE {$newTable} n
|
|
JOIN legacy_import_map m
|
|
ON m.target_id = n.id
|
|
AND m.target_table = '{$newTable}'
|
|
AND m.legacy_portal = '{$portal}'
|
|
JOIN `{$legacyDb}`.`{$legacyTable}` lc ON lc.id = m.legacy_id
|
|
SET
|
|
n.created_at = lc.{$legacyCreated},
|
|
n.updated_at = lc.{$legacyUpdated}
|
|
");
|
|
}
|
|
}
|