Skip to content

A free open source IT asset/license management system

Fixed #19760 - fix calendar backfill for deleted sources

snipe60af8aca18d7
diff --git a/app/Console/Commands/ReconcileCalendarEvents.php b/app/Console/Commands/ReconcileCalendarEvents.php
index 4730eea2cd..03797ab3c4 100644
--- a/app/Console/Commands/ReconcileCalendarEvents.php
+++ b/app/Console/Commands/ReconcileCalendarEvents.php
@@ -87,12 +87,11 @@ class ReconcileCalendarEvents extends Command
// Sync path: walk every live source row and force-sync its
// calendar_events. force=true bypasses the observer's
// "did any declared field change" gate so drifted-in-place
- // date values get corrected.
+ // date values get corrected. Live rows only - trashed
+ // sources should not publish calendar_events (see issue
+ // #19760 and the matching gate in
+ // HasCalendarEvents::syncCalendarEvents).
$sourceClass::query()
- ->when(
- in_array(\Illuminate\Database\Eloquent\SoftDeletes::class, class_uses_recursive($sourceClass), true),
- fn ($q) => $q->withTrashed(),
- )
->chunkById(500, function ($rows) use (&$upserts, $dryRun) {
foreach ($rows as $row) {
if (! $dryRun) {
diff --git a/app/Http/Controllers/Api/CalendarEventsController.php b/app/Http/Controllers/Api/CalendarEventsController.php
index cef74008dc..2055fc01d5 100644
--- a/app/Http/Controllers/Api/CalendarEventsController.php
+++ b/app/Http/Controllers/Api/CalendarEventsController.php
@@ -246,7 +246,21 @@ class CalendarEventsController extends Controller
return $rows->filter(function (CalendarEvent $row) use ($sourcesByType, $viewer) {
$source = $sourcesByType[$row->source_type][$row->source_id] ?? null;
- return $source && $viewer?->can('view', $source);
+ if (! $source) {
+ return false;
+ }
+
+ // Belt and suspenders against issue #19760: hide events
+ // whose source has been soft-deleted. The display is the
+ // user-visible symptom. The sync-path gate in
+ // HasCalendarEvents::syncCalendarEvents prevents new ones
+ // from being written in the first place, and the cleanup
+ // migration soft-deletes any already written.
+ if (method_exists($source, 'trashed') && $source->trashed()) {
+ return false;
+ }
+
+ return $viewer?->can('view', $source);
})->values();
}
}
diff --git a/app/Models/Traits/HasCalendarEvents.php b/app/Models/Traits/HasCalendarEvents.php
index 158e8f3bc5..383ffc4b28 100644
--- a/app/Models/Traits/HasCalendarEvents.php
+++ b/app/Models/Traits/HasCalendarEvents.php
@@ -104,13 +104,19 @@ trait HasCalendarEvents
// additional model events fires here - the source's
// own save() during restore() already ran through the
// saved observer for anything date-related that
- // changed. Corner case where dates changed between
- // delete and restore is handled by the reconcile
- // command.
+ // changed.
CalendarEvent::withTrashed()
->where('source_type', $model::class)
->where('source_id', $model->getKey())
->restore();
+
+ // Also force a full re-sync so dates that drifted
+ // between delete and restore get corrected, and so
+ // the one-shot cleanup migration's force-deleted
+ // calendar_events (see issue #19760) get rebuilt
+ // from scratch on restore instead of coming back
+ // empty.
+ $model->forceSyncCalendarEvents();
});
}
}
@@ -134,6 +140,16 @@ trait HasCalendarEvents
return;
}
+ // Short-circuit for trashed sources so backfills, reconciles,
+ // and stray saves on soft-deleted rows never create or revive
+ // calendar_events. The deleted observer already soft-deleted
+ // the matching rows when the source was soft-deleted, and the
+ // restored observer re-syncs on restore, so leaving this
+ // branch dark on trashed() is safe. See issue #19760.
+ if (in_array(SoftDeletes::class, class_uses_recursive($this), true) && $this->trashed()) {
+ return;
+ }
+
if (! $force && ! $this->calendarEventFieldsChanged($definitions)) {
return;
}
diff --git a/database/migrations/2026_08_13_160100_backfill_calendar_events_from_existing_sources.php b/database/migrations/2026_08_13_160100_backfill_calendar_events_from_existing_sources.php
index 2f9a5735a4..b33fe3b626 100644
--- a/database/migrations/2026_08_13_160100_backfill_calendar_events_from_existing_sources.php
+++ b/database/migrations/2026_08_13_160100_backfill_calendar_events_from_existing_sources.php
@@ -1,7 +1,6 @@
<?php
use App\Models\CalendarEvent;
-use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Migrations\Migration;
/**
@@ -52,11 +51,10 @@ return new class extends Migration
$instance = new $sourceClass;
$keyName = $instance->getKeyName();
+ // Live rows only. Backfilling trashed sources created calendar
+ // events for soft-deleted records that users could neither
+ // open nor remove (see issue #19760).
$sourceClass::query()
- ->when(
- in_array(SoftDeletes::class, class_uses_recursive($sourceClass), true),
- fn ($q) => $q->withTrashed(),
- )
->orderByDesc($keyName)
->limit(2000)
->get()
diff --git a/database/migrations/2026_08_13_170000_backfill_calendar_events_for_new_sources.php b/database/migrations/2026_08_13_170000_backfill_calendar_events_for_new_sources.php
index 78972f7006..9c1f5b2c46 100644
--- a/database/migrations/2026_08_13_170000_backfill_calendar_events_for_new_sources.php
+++ b/database/migrations/2026_08_13_170000_backfill_calendar_events_for_new_sources.php
@@ -1,7 +1,6 @@
<?php
use App\Models\CalendarEvent;
-use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Migrations\Migration;
/**
@@ -33,11 +32,10 @@ return new class extends Migration
protected function backfillSource(string $sourceClass): void
{
+ // Live rows only. Backfilling trashed sources created calendar
+ // events for soft-deleted records that users could neither
+ // open nor remove (see issue #19760).
$sourceClass::query()
- ->when(
- in_array(SoftDeletes::class, class_uses_recursive($sourceClass), true),
- fn ($q) => $q->withTrashed(),
- )
->chunkById(500, function ($rows) {
foreach ($rows as $row) {
$row->forceSyncCalendarEvents();
diff --git a/database/migrations/2026_10_07_220000_soft_delete_calendar_events_for_trashed_sources.php b/database/migrations/2026_10_07_220000_soft_delete_calendar_events_for_trashed_sources.php
new file mode 100644
index 0000000000..a376cd47bd
--- /dev/null
+++ b/database/migrations/2026_10_07_220000_soft_delete_calendar_events_for_trashed_sources.php
@@ -0,0 +1,59 @@
+<?php
+
+use App\Models\CalendarEvent;
+use Illuminate\Database\Eloquent\SoftDeletes;
+use Illuminate\Database\Migrations\Migration;
+
+/**
+ * One-shot cleanup for installs that ran either of the earlier
+ * calendar backfill migrations before the trashed-source gate landed.
+ * Those backfills loaded sources withTrashed() and force-synced them,
+ * which published live calendar_events rows for records the admin had
+ * already soft-deleted. The user-visible symptom was deleted
+ * Maintenances showing up on the calendar with no way to open or
+ * remove them (see issue #19760).
+ *
+ * For every HasCalendarEvents source model, soft-delete the
+ * calendar_events rows whose source row is currently trashed. This
+ * matches the shape the deleted observer would have written if the
+ * source had been soft-deleted after the backfill, so if the admin
+ * ever restores the source, the restored observer's query-builder
+ * restore brings these back live.
+ */
+return new class extends Migration
+{
+ public function up(): void
+ {
+ foreach (CalendarEvent::sourceModels() as $sourceClass) {
+ if (! in_array(SoftDeletes::class, class_uses_recursive($sourceClass), true)) {
+ continue;
+ }
+
+ // IDs of trashed source rows. Deliberately not using
+ // onlyTrashed() so the query stays explicit about the
+ // intent and so chunked reads on large tables don't fight
+ // with the SoftDeletes global scope.
+ $trashedIds = $sourceClass::query()
+ ->onlyTrashed()
+ ->pluck((new $sourceClass)->getKeyName())
+ ->all();
+
+ if ($trashedIds === []) {
+ continue;
+ }
+
+ CalendarEvent::query()
+ ->where('source_type', $sourceClass)
+ ->whereIn('source_id', $trashedIds)
+ ->delete();
+ }
+ }
+
+ public function down(): void
+ {
+ // No-op. Restoring these would re-expose the symptom the
+ // forward migration exists to fix. Admins who restore an
+ // affected source get the live calendar_event back via the
+ // restored observer in HasCalendarEvents.
+ }
+};