Files
mytodos/app/Console/Commands/TransferSqliteData.php
T

215 lines
6.8 KiB
PHP
Raw Normal View History

2026-09-24 21:13:19 -07:00
<?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);
}
2026-10-15 20:59:32 -07:00
$this->backfillWorkspaces();
2026-09-24 21:13:19 -07:00
$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 {
2026-10-15 20:59:32 -07:00
DB::table($table)->insert($this->prepareRows($table, $rows));
2026-09-24 21:13:19 -07:00
});
$this->line(" transferred {$count} row(s) into {$table}.");
}
2026-10-15 20:59:32 -07:00
/**
* Folders and todos cannot be inserted without a workspace, but the source
* database predates workspaces. Folders inherit the current organization of
* their owner, and todos inherit the organization of their folder. Rows
* that already carry the value are left untouched.
*
* @param iterable<object> $rows
* @return list<array<string, mixed>>
*/
private function prepareRows(string $table, iterable $rows): array
{
$records = collect($rows)->map(fn ($row): array => (array) $row)->all();
if ($table === 'folders') {
$userIds = collect($records)->pluck('user_id')->all();
$organizationByUser = DB::table('users')
->whereIn('id', $userIds)
->pluck('current_organization_id', 'id')
->all();
foreach ($records as &$record) {
$record['organization_id'] ??= $organizationByUser[$record['user_id']] ?? null;
}
unset($record);
return $records;
}
if ($table === 'todos') {
$folderIds = collect($records)->pluck('folder_id')->all();
$organizationByFolder = DB::table('folders')
->whereIn('id', $folderIds)
->pluck('organization_id', 'id')
->all();
foreach ($records as &$record) {
$record['organization_id'] ??= $organizationByFolder[$record['folder_id']] ?? null;
}
unset($record);
return $records;
}
return $records;
}
/**
* The source database predates workspaces, so folders and todos arrive
* without an organization id and folders without a visibility. Fold them
* into the workspace their owner had active, exactly as the workspace
* migration does for existing rows.
*/
private function backfillWorkspaces(): void
{
DB::table('folders')
->join('users', 'users.id', '=', 'folders.user_id')
->whereNull('folders.organization_id')
->update(['folders.organization_id' => DB::raw('users.current_organization_id')]);
DB::table('todos')
->join('folders', 'folders.id', '=', 'todos.folder_id')
->whereNull('todos.organization_id')
->update(['todos.organization_id' => DB::raw('folders.organization_id')]);
DB::table('folders')
->join('organizations', 'organizations.id', '=', 'folders.organization_id')
->update([
'folders.visibility' => DB::raw(
"CASE WHEN organizations.personal_workspace = 1 THEN 'private' ELSE 'workspace' END"
),
]);
}
2026-09-24 21:13:19 -07:00
/**
* 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));
}
}
}
}