@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Throwable;
|
||||
|
||||
#[Signature(
|
||||
signature: 'db:transfer-sqlite {--source= : Path to the source SQLite database file} {--force : Run without confirmation}',
|
||||
)]
|
||||
#[Description('Transfer application data from a SQLite database into the current (MySQL) database')]
|
||||
class TransferSqliteData extends Command
|
||||
{
|
||||
/**
|
||||
* Domain tables to copy, keyed by their primary ordering column.
|
||||
* Framework and transient tables are intentionally excluded.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private const TABLES = [
|
||||
'users' => 'id',
|
||||
'folders' => 'id',
|
||||
'todos' => 'id',
|
||||
'passkeys' => 'id',
|
||||
'password_reset_tokens' => 'email',
|
||||
];
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
if ($this->option('source') !== null) {
|
||||
config([
|
||||
'database.connections.sqlite_source.database' => $this->option('source'),
|
||||
]);
|
||||
DB::purge('sqlite_source');
|
||||
}
|
||||
|
||||
$source = (string) config('database.connections.sqlite_source.database');
|
||||
|
||||
if (! is_file($source)) {
|
||||
$this->error("Source database not found: {$source}");
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
if (DB::connection()->getDriverName() !== 'mysql') {
|
||||
$this->error('The destination connection must be MySQL.');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
if (app()->environment('production') && ! $this->option('force')) {
|
||||
$this->error('Refusing to run in production without --force.');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
if (! $this->option('force') && ! $this->confirm(
|
||||
'This truncates and replaces the domain tables in the destination database. Continue?'
|
||||
)) {
|
||||
$this->info('Aborted.');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$this->info("Copying data from {$source} into the 'mysql' connection.");
|
||||
|
||||
try {
|
||||
Schema::disableForeignKeyConstraints();
|
||||
|
||||
foreach (self::TABLES as $table => $key) {
|
||||
$this->transferTable($table, $key);
|
||||
}
|
||||
|
||||
$this->resetAutoIncrements();
|
||||
} catch (Throwable $e) {
|
||||
$this->error('Transfer failed: '.$e->getMessage());
|
||||
|
||||
return self::FAILURE;
|
||||
} finally {
|
||||
Schema::enableForeignKeyConstraints();
|
||||
}
|
||||
|
||||
$this->info('Data transfer complete.');
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the contents of one table from the source into the destination.
|
||||
*/
|
||||
private function transferTable(string $table, string $key): void
|
||||
{
|
||||
if (! Schema::connection('sqlite_source')->hasTable($table)) {
|
||||
$this->line(" skipped {$table}: not present in the source database.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! Schema::hasTable($table)) {
|
||||
$this->line(" skipped {$table}: not present in the destination database.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$count = (int) DB::connection('sqlite_source')->table($table)->count();
|
||||
|
||||
DB::table($table)->truncate();
|
||||
|
||||
DB::connection('sqlite_source')
|
||||
->table($table)
|
||||
->orderBy($key)
|
||||
->chunk(500, function ($rows) use ($table): void {
|
||||
DB::table($table)->insert($rows->map(fn ($row): array => (array) $row)->all());
|
||||
});
|
||||
|
||||
$this->line(" transferred {$count} row(s) into {$table}.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Bump each auto-increment counter past the highest transferred id so
|
||||
* newly created records never collide with the preserved ids.
|
||||
*/
|
||||
private function resetAutoIncrements(): void
|
||||
{
|
||||
foreach (['users', 'folders', 'todos', 'passkeys'] as $table) {
|
||||
$max = DB::table($table)->max('id');
|
||||
|
||||
if ($max !== null) {
|
||||
DB::statement("ALTER TABLE `{$table}` AUTO_INCREMENT = ".((int) $max + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user