current state
This commit is contained in:
137
app/Console/Commands/ImportLegacyFavourites.php
Normal file
137
app/Console/Commands/ImportLegacyFavourites.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ImportLegacyFavourites extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'import:legacy-favourites
|
||||
{--connection=legacy : Legacy DB connection name}
|
||||
{--table=favourites : Legacy favourites table name}
|
||||
{--id-column=id : ID column to use for chunking}
|
||||
{--map-user=user_id : Column name for user id}
|
||||
{--map-artwork=artwork_id : Column name for artwork id}
|
||||
{--map-created=datum : Column name for created timestamp}
|
||||
{--chunk=500 : Chunk size}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Copy legacy favourites (from another DB connection) into user_favorites';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$connection = $this->option('connection');
|
||||
$table = $this->option('table');
|
||||
$idColumn = $this->option('id-column');
|
||||
$mapUser = $this->option('map-user');
|
||||
$mapArtwork = $this->option('map-artwork');
|
||||
$mapCreated = $this->option('map-created');
|
||||
$chunk = (int) $this->option('chunk');
|
||||
|
||||
$this->info("Using connection='{$connection}', table='{$table}', idColumn='{$idColumn}'");
|
||||
|
||||
try {
|
||||
$legacy = DB::connection($connection);
|
||||
} catch (\Throwable $e) {
|
||||
$this->error('Cannot connect to legacy connection: '.$e->getMessage());
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
$schema = $legacy->getSchemaBuilder();
|
||||
} catch (\Throwable $e) {
|
||||
$this->error('Failed to get schema builder for legacy connection: '.$e->getMessage());
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (! $schema->hasTable($table)) {
|
||||
$this->error("Table '{$table}' does not exist on connection '{$connection}'");
|
||||
return 1;
|
||||
}
|
||||
|
||||
$this->info('Starting import...');
|
||||
|
||||
$attempted = 0;
|
||||
$inserted = 0;
|
||||
|
||||
// Try chunkById for efficient processing; fallback to cursor if id column missing
|
||||
try {
|
||||
$legacy->table($table)
|
||||
->select([$idColumn, $mapUser, $mapArtwork, $mapCreated])
|
||||
->orderBy($idColumn)
|
||||
->chunkById($chunk, function ($rows) use (&$attempted, &$inserted, $mapUser, $mapArtwork, $mapCreated) {
|
||||
$batch = [];
|
||||
foreach ($rows as $r) {
|
||||
$attempted++;
|
||||
$batch[] = [
|
||||
'user_id' => $r->{$mapUser},
|
||||
'artwork_id' => $r->{$mapArtwork},
|
||||
'created_at' => $r->{$mapCreated} ?? now(),
|
||||
];
|
||||
}
|
||||
|
||||
if (count($batch) > 0) {
|
||||
$res = DB::table('user_favorites')->insertOrIgnore($batch);
|
||||
// insertOrIgnore may return number inserted on some drivers; approximate otherwise
|
||||
if (is_int($res)) {
|
||||
$inserted += $res;
|
||||
} else {
|
||||
$inserted += count($batch);
|
||||
}
|
||||
}
|
||||
|
||||
$this->info("Processed {$attempted} rows, approx inserted {$inserted}");
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
$this->warn('chunkById failed, falling back to cursor: '.$e->getMessage());
|
||||
|
||||
$cursor = $legacy->table($table)
|
||||
->select([$mapUser, $mapArtwork, $mapCreated])
|
||||
->orderBy($mapCreated)
|
||||
->cursor();
|
||||
|
||||
$batch = [];
|
||||
foreach ($cursor as $r) {
|
||||
$attempted++;
|
||||
$batch[] = [
|
||||
'user_id' => $r->{$mapUser},
|
||||
'artwork_id' => $r->{$mapArtwork},
|
||||
'created_at' => $r->{$mapCreated} ?? now(),
|
||||
];
|
||||
|
||||
if (count($batch) >= $chunk) {
|
||||
$res = DB::table('user_favorites')->insertOrIgnore($batch);
|
||||
if (is_int($res)) {
|
||||
$inserted += $res;
|
||||
} else {
|
||||
$inserted += count($batch);
|
||||
}
|
||||
$this->info("Processed {$attempted} rows, approx inserted {$inserted}");
|
||||
$batch = [];
|
||||
}
|
||||
}
|
||||
|
||||
if (count($batch) > 0) {
|
||||
$res = DB::table('user_favorites')->insertOrIgnore($batch);
|
||||
if (is_int($res)) {
|
||||
$inserted += $res;
|
||||
} else {
|
||||
$inserted += count($batch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->info("Import complete. Attempted {$attempted}, approx inserted {$inserted}.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -25,17 +25,15 @@ class ImportLegacyUsers extends Command
|
||||
$imported = 0;
|
||||
$skipped = 0;
|
||||
|
||||
if (! DB::connection('legacy')->getPdo()) {
|
||||
if (! DB::getPdo()) {
|
||||
$this->error('Legacy DB connection "legacy" is not configured or reachable.');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
DB::connection('legacy')
|
||||
->table('users')
|
||||
DB::table('users')
|
||||
->chunkById($chunk, function ($rows) use (&$imported, &$skipped) {
|
||||
$ids = $rows->pluck('user_id')->all();
|
||||
$stats = DB::connection('legacy')
|
||||
->table('users_statistics')
|
||||
$stats = DB::table('users_statistics')
|
||||
->whereIn('user_id', $ids)
|
||||
->get()
|
||||
->keyBy('user_id');
|
||||
|
||||
92
app/Console/Commands/ImportWallzCategories.php
Normal file
92
app/Console/Commands/ImportWallzCategories.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class ImportWallzCategories extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* --connection: database connection name for legacy DB (default: legacy)
|
||||
* --table: legacy table name (default: wallz)
|
||||
* --chunk: rows per chunk (default: 500)
|
||||
*/
|
||||
protected $signature = 'import:wallz-categories {--connection=legacy} {--table=wallz} {--chunk=500}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Import artwork -> category mappings from legacy wallz table into artwork_category';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$conn = $this->option('connection');
|
||||
$table = $this->option('table');
|
||||
$chunk = (int) $this->option('chunk');
|
||||
|
||||
$this->info("Importing from connection=[{$conn}] table=[{$table}]");
|
||||
|
||||
// Validate connection exists in config
|
||||
if (! config()->has('database.connections.' . $conn)) {
|
||||
$this->error("Database connection '{$conn}' not configured. Available connections: " . implode(', ', array_keys(config('database.connections'))));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check legacy table exists
|
||||
try {
|
||||
if (! Schema::connection($conn)->hasTable($table)) {
|
||||
$this->error("Table '{$table}' does not exist on connection '{$conn}'.");
|
||||
return 1;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->error('Error checking legacy table: ' . $e->getMessage());
|
||||
return 1;
|
||||
}
|
||||
|
||||
$total = DB::connection($conn)->table($table)->count();
|
||||
$this->info("Found {$total} rows in legacy table.");
|
||||
|
||||
$bar = $this->output->createProgressBar($total ?: 0);
|
||||
$bar->start();
|
||||
|
||||
try {
|
||||
DB::connection($conn)->table($table)
|
||||
->select('id', 'category')
|
||||
->orderBy('id')
|
||||
->chunk($chunk, function ($rows) use ($bar) {
|
||||
$inserts = [];
|
||||
foreach ($rows as $r) {
|
||||
// Skip empty category
|
||||
if (empty($r->category)) continue;
|
||||
$inserts[] = [
|
||||
'artwork_id' => (int) $r->id,
|
||||
'category_id' => (int) $r->category,
|
||||
];
|
||||
}
|
||||
|
||||
if (! empty($inserts)) {
|
||||
// Use insertOrIgnore to avoid duplicates
|
||||
DB::table('artwork_category')->insertOrIgnore($inserts);
|
||||
}
|
||||
|
||||
$bar->advance(count($rows));
|
||||
});
|
||||
|
||||
$bar->finish();
|
||||
$this->newLine(2);
|
||||
$this->info('Import complete.');
|
||||
return 0;
|
||||
} catch (\Throwable $e) {
|
||||
$bar->finish();
|
||||
$this->newLine(2);
|
||||
$this->error('Import failed: ' . $e->getMessage());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user