Skip to content

Commit e6d55f3

Browse files
Merge pull request #1468 from liberu-genealogy/copilot/debug-import-gedcom-job
[WIP] Fix ImportGedcom job not dispatching on file upload
2 parents 2fdbee8 + 6107425 commit e6d55f3

File tree

3 files changed

+95
-27
lines changed

3 files changed

+95
-27
lines changed

app/Filament/App/Resources/GedcomResource/Pages/CreateGedcom.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,58 @@
33
namespace App\Filament\App\Resources\GedcomResource\Pages;
44

55
use App\Filament\App\Resources\GedcomResource;
6-
use Filament\Resources\Pages\CreateRecord;
76
use App\Jobs\ImportGedcom;
87
use App\Jobs\ImportGrampsXml;
9-
use Illuminate\Support\Facades\Log;
8+
use Filament\Resources\Pages\CreateRecord;
109
use Illuminate\Support\Facades\Auth;
10+
use Illuminate\Support\Facades\Log;
1111
use Illuminate\Support\Facades\Storage;
12+
use Throwable;
1213

1314
class CreateGedcom extends CreateRecord
1415
{
1516
protected static string $resource = GedcomResource::class;
1617

1718
protected function afterCreate(): void
1819
{
19-
$path = $this->getRecord()->filename;
20+
$record = $this->getRecord();
21+
$path = $record->filename;
22+
23+
Log::info('CreateGedcom::afterCreate called', [
24+
'gedcom_id' => $record->getKey(),
25+
'filename' => $path,
26+
]);
2027

2128
if (! $path) {
29+
Log::warning('CreateGedcom::afterCreate: filename is empty, skipping dispatch', [
30+
'gedcom_id' => $record->getKey(),
31+
]);
32+
2233
return;
2334
}
2435

2536
$fullPath = Storage::disk('private')->path($path);
2637
$extension = strtolower(pathinfo($fullPath, PATHINFO_EXTENSION));
2738

28-
// Dispatch appropriate import job based on file extension
29-
if (in_array($extension, ['gramps', 'xml'])) {
30-
ImportGrampsXml::dispatch(Auth::user(), $fullPath);
31-
Log::info("Dispatched GrampsXML import for: {$path}");
32-
} else {
33-
ImportGedcom::dispatch(Auth::user(), $fullPath);
34-
Log::info("Dispatched GEDCOM import for: {$path}");
39+
try {
40+
// Dispatch appropriate import job based on file extension
41+
if (in_array($extension, ['gramps', 'xml'])) {
42+
ImportGrampsXml::dispatch(Auth::user(), $fullPath);
43+
Log::info('Dispatched GrampsXML import', ['path' => $path, 'full_path' => $fullPath]);
44+
} else {
45+
ImportGedcom::dispatch(Auth::user(), $fullPath);
46+
Log::info('Dispatched GEDCOM import', ['path' => $path, 'full_path' => $fullPath]);
47+
}
48+
} catch (Throwable $e) {
49+
Log::error('Failed to dispatch GEDCOM import job', [
50+
'gedcom_id' => $record->getKey(),
51+
'path' => $path,
52+
'full_path' => $fullPath,
53+
'error' => $e->getMessage(),
54+
'trace' => $e->getTraceAsString(),
55+
]);
56+
57+
throw $e;
3558
}
3659
}
3760
}

app/Jobs/ImportGedcom.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
namespace App\Jobs;
44

55
use Artisan;
6-
use Throwable;
76
use Exception;
7+
use Throwable;
88
use App\Models\ImportJob;
99
use App\Models\User;
10-
use App\Tenant\Manager;
1110
use FamilyTree365\LaravelGedcom\Utils\GedcomParser;
1211
use Illuminate\Bus\Queueable;
1312
use Illuminate\Contracts\Queue\ShouldQueue;
1413
use Illuminate\Foundation\Bus\Dispatchable;
1514
use Illuminate\Queue\InteractsWithQueue;
1615
use Illuminate\Queue\SerializesModels;
1716
use Illuminate\Support\Facades\File;
17+
use Illuminate\Support\Facades\Log;
1818
use Illuminate\Support\Str;
1919

2020
class ImportGedcom implements ShouldQueue
@@ -33,32 +33,44 @@ public function __construct(protected User $user, protected string $filePath, pr
3333

3434
public function handle(): int
3535
{
36+
Log::info('ImportGedcom job started', [
37+
'file_path' => $this->filePath,
38+
'user_id' => $this->user->getKey(),
39+
]);
40+
3641
throw_unless(File::isFile($this->filePath), Exception::class, "{$this->filePath} does not exist.");
3742

38-
// $tenant = Manager::fromModel($this->user->company(), $this->user);
39-
// if (!$tenant->databaseExists()) {
40-
// //$tenant->dropDatabase();
41-
// $tenant->createDatabase();
42-
// $tenant->connect();
43-
// $tenant->migrateDatabase();
44-
// }
45-
// $tenant->connect();
4643
$slug = $this->slug ?? Str::uuid();
4744

4845
$job = ImportJob::create([
4946
'user_id' => $this->user->getKey(),
5047
'status' => 'queue',
5148
'slug' => $slug,
5249
]);
53-
$parser = new GedcomParser();
54-
$team_id = $this->user->currentTeam?->id;
55-
$parser->parse(config('database.default'), $this->filePath, $slug, true, $team_id);
56-
// with(new GedcomParser())->parse($tenant->connectionName(), $this->filePath, $slug, true);
5750

58-
// File::delete($this->filePath);
51+
try {
52+
$parser = new GedcomParser();
53+
$team_id = $this->user->currentTeam?->id;
54+
$parser->parse(config('database.default'), $this->filePath, $slug, true, $team_id);
55+
} catch (Throwable $e) {
56+
$job->update(['status' => 'failed']);
57+
Log::error('ImportGedcom parser failed', [
58+
'file_path' => $this->filePath,
59+
'user_id' => $this->user->getKey(),
60+
'error' => $e->getMessage(),
61+
'trace' => $e->getTraceAsString(),
62+
]);
63+
throw $e;
64+
}
5965

6066
$job->update(['status' => 'complete']);
6167

68+
Log::info('ImportGedcom job completed', [
69+
'file_path' => $this->filePath,
70+
'user_id' => $this->user->getKey(),
71+
'slug' => $slug,
72+
]);
73+
6274
// Clear application caches so new records are visible immediately
6375
try {
6476
Artisan::call('cache:clear');
@@ -68,8 +80,6 @@ public function handle(): int
6880
// swallow cache clear errors
6981
}
7082

71-
// $tenant->disconnect();
72-
7383
return 0;
7484
}
7585
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
/**
8+
* The original create_families_table migration used the incorrect pattern
9+
* `->constrained()->nullable()` which does NOT propagate nullable() to the
10+
* column (constrained() returns a ForeignKeyDefinition, not a ColumnDefinition).
11+
* This resulted in husband_id, wife_id, and type_id being NOT NULL in the
12+
* database despite the developer's intent, causing integrity constraint
13+
* violations when creating families with null spouses (which is valid in
14+
* GEDCOM files and in the FamilyFactory).
15+
*/
16+
return new class extends Migration
17+
{
18+
public function up(): void
19+
{
20+
Schema::table('families', function (Blueprint $table) {
21+
$table->unsignedBigInteger('type_id')->nullable()->change();
22+
$table->unsignedBigInteger('husband_id')->nullable()->change();
23+
$table->unsignedBigInteger('wife_id')->nullable()->change();
24+
});
25+
}
26+
27+
public function down(): void
28+
{
29+
Schema::table('families', function (Blueprint $table) {
30+
$table->unsignedBigInteger('type_id')->nullable(false)->change();
31+
$table->unsignedBigInteger('husband_id')->nullable(false)->change();
32+
$table->unsignedBigInteger('wife_id')->nullable(false)->change();
33+
});
34+
}
35+
};

0 commit comments

Comments
 (0)