@@ -78,6 +78,7 @@ class TransferSqliteData extends Command
|
||||
$this->transferTable($table, $key);
|
||||
}
|
||||
|
||||
$this->backfillWorkspaces();
|
||||
$this->resetAutoIncrements();
|
||||
} catch (Throwable $e) {
|
||||
$this->error('Transfer failed: '.$e->getMessage());
|
||||
@@ -117,12 +118,85 @@ class TransferSqliteData extends Command
|
||||
->table($table)
|
||||
->orderBy($key)
|
||||
->chunk(500, function ($rows) use ($table): void {
|
||||
DB::table($table)->insert($rows->map(fn ($row): array => (array) $row)->all());
|
||||
DB::table($table)->insert($this->prepareRows($table, $rows));
|
||||
});
|
||||
|
||||
$this->line(" transferred {$count} row(s) into {$table}.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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"
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bump each auto-increment counter past the highest transferred id so
|
||||
* newly created records never collide with the preserved ids.
|
||||
|
||||
Reference in New Issue
Block a user