Skip to content

A free open source IT asset/license management system

Clean up sync adapter rows on soft-delete and purge

snipeb5562585e35c
diff --git a/app/Console/Commands/Purge.php b/app/Console/Commands/Purge.php
index 5d6420dc57..e57ea68870 100644
--- a/app/Console/Commands/Purge.php
+++ b/app/Console/Commands/Purge.php
@@ -226,6 +226,12 @@ class Purge extends Command
Asset::class => [
'maintenances' => ['item_id', 'item_type', Asset::class],
'checkout_requests' => ['requestable_id', 'requestable_type', Asset::class],
+ // Sync-adapter identity rows tying this asset to a
+ // vendor host. Orphaned once the asset is purged, and
+ // an orphan would cause the next sync to skip creating
+ // a fresh asset for that vendor host because
+ // provisionAsset() finds the dead link first.
+ 'asset_external_sources' => 'asset_id',
],
License::class => [
'license_seats' => 'license_id',
diff --git a/app/Observers/AssetObserver.php b/app/Observers/AssetObserver.php
index 7119dcdef9..5a7b18b500 100644
--- a/app/Observers/AssetObserver.php
+++ b/app/Observers/AssetObserver.php
@@ -8,6 +8,7 @@ use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Setting;
use Carbon\Carbon;
+use Illuminate\Support\Facades\DB;
class AssetObserver
{
@@ -274,6 +275,13 @@ class AssetObserver
$logAction->action_date = date('Y-m-d H:i:s');
$logAction->created_by = auth()->id();
$logAction->logaction('delete');
+
+ // Sync-adapter identity rows would otherwise become orphans
+ // that violate the unique(source, external_id) constraint on
+ // the next sync run when the vendor keeps reporting the same
+ // host. If the admin later restores the asset the link is gone,
+ // and the next sync creates a fresh identity for it.
+ DB::table('asset_external_sources')->where('asset_id', $asset->id)->delete();
}
/**
diff --git a/tests/Feature/Assets/AssetObserverSyncAdapterCleanupTest.php b/tests/Feature/Assets/AssetObserverSyncAdapterCleanupTest.php
new file mode 100644
index 0000000000..ef44d3741a
--- /dev/null
+++ b/tests/Feature/Assets/AssetObserverSyncAdapterCleanupTest.php
@@ -0,0 +1,61 @@
+<?php
+
+namespace Tests\Feature\Assets;
+
+use App\Models\Asset;
+use Illuminate\Support\Facades\DB;
+use Tests\TestCase;
+
+/**
+ * AssetObserver::deleting cascades a hard delete to
+ * asset_external_sources so a soft-deleted asset does not leave the
+ * sync loop crashing on the next run. Without this, the still-
+ * present identity row keeps the vendor's (source, external_id)
+ * pair locked, and provisionAsset's fresh-asset path violates the
+ * unique constraint on that pair.
+ */
+class AssetObserverSyncAdapterCleanupTest extends TestCase
+{
+ public function test_soft_deleting_an_asset_removes_its_sync_adapter_identity_rows(): void
+ {
+ $asset = Asset::factory()->create();
+
+ DB::table('asset_external_sources')->insert([
+ 'asset_id' => $asset->id,
+ 'source' => 'fleet',
+ 'external_id' => 'ext-42',
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+
+ $asset->delete();
+
+ // Soft-delete leaves the asset row in place with a deleted_at
+ // timestamp, but the identity row is gone so the next sync
+ // does not trip the unique(source, external_id) constraint.
+ $this->assertDatabaseHas('assets', ['id' => $asset->id]);
+ $this->assertNotNull($asset->fresh()->deleted_at);
+ $this->assertDatabaseMissing('asset_external_sources', ['asset_id' => $asset->id]);
+ }
+
+ public function test_deleting_an_asset_with_multiple_source_rows_removes_all_of_them(): void
+ {
+ // An asset synced from more than one vendor (Fleet + Landscape,
+ // say) has one identity row per source. All get cleaned up.
+ $asset = Asset::factory()->create();
+
+ foreach (['fleet' => 'ext-1', 'landscape' => 'lscape-99'] as $source => $externalId) {
+ DB::table('asset_external_sources')->insert([
+ 'asset_id' => $asset->id,
+ 'source' => $source,
+ 'external_id' => $externalId,
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
+ }
+
+ $asset->delete();
+
+ $this->assertSame(0, DB::table('asset_external_sources')->where('asset_id', $asset->id)->count());
+ }
+}
diff --git a/tests/Feature/Console/Commands/PurgeTest.php b/tests/Feature/Console/Commands/PurgeTest.php
index 835808a743..7b097cb5f8 100644
--- a/tests/Feature/Console/Commands/PurgeTest.php
+++ b/tests/Feature/Console/Commands/PurgeTest.php
@@ -35,12 +35,23 @@ class PurgeTest extends TestCase
'action_type' => 'checkout',
]);
$trashedMaintenance = Maintenance::factory()->create(['asset_id' => $trashed->id]);
+ // Sync-adapter identity row that would otherwise become an
+ // orphan and cause a unique-constraint violation on the next
+ // sync when the vendor re-reports the same host.
+ DB::table('asset_external_sources')->insert([
+ 'asset_id' => $trashed->id,
+ 'source' => 'fleet',
+ 'external_id' => 'ext-99',
+ 'created_at' => now(),
+ 'updated_at' => now(),
+ ]);
$this->artisan('snipeit:purge', ['--force' => 'true'])->assertExitCode(0);
$this->assertDatabaseMissing('assets', ['id' => $trashed->id]);
$this->assertDatabaseMissing('action_logs', ['id' => $trashedLog->id]);
$this->assertDatabaseMissing('maintenances', ['id' => $trashedMaintenance->id]);
+ $this->assertDatabaseMissing('asset_external_sources', ['asset_id' => $trashed->id]);
}
public function test_live_asset_and_its_children_are_not_purged(): void