Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

fix: skip media of deleted workspaces in the owner backfill (#387)

Paulo Castellano7c956977928a
diff --git a/database/migrations/2026_10_01_000001_add_owners_to_medias_table.php b/database/migrations/2026_10_01_000001_add_owners_to_medias_table.php
index f13e8192b..1369f1be6 100644
--- a/database/migrations/2026_10_01_000001_add_owners_to_medias_table.php
+++ b/database/migrations/2026_10_01_000001_add_owners_to_medias_table.php
@@ -29,6 +29,7 @@ return new class extends Migration
DB::table('medias')
->where('mediable_type', 'workspace')
->whereNull('workspace_id')
+ ->whereIn('mediable_id', DB::table('workspaces')->select('id'))
->update(['workspace_id' => DB::raw('mediable_id')]);
}
diff --git a/tests/Feature/Media/MediaOwnershipModelTest.php b/tests/Feature/Media/MediaOwnershipModelTest.php
index 702a58cc0..3f097a6b6 100644
--- a/tests/Feature/Media/MediaOwnershipModelTest.php
+++ b/tests/Feature/Media/MediaOwnershipModelTest.php
@@ -87,7 +87,7 @@ test('a feed item owns its image row', function () {
->and($image->collection)->toBe(Media::COLLECTION_MEDIA);
});
-test('the migration backfills workspace_id for workspace-owned rows', function () {
+test('the migration backfills workspace_id for workspace-owned rows and skips rows of deleted workspaces', function () {
$workspace = Workspace::factory()->create();
$user = User::factory()->create();
$row = fn (string $type, string $id): array => [
@@ -104,12 +104,14 @@ test('the migration backfills workspace_id for workspace-owned rows', function (
];
$workspaceRow = $row($workspace->getMorphClass(), $workspace->id);
$avatarRow = $row($user->getMorphClass(), $user->id);
- DB::table('medias')->insert([$workspaceRow, $avatarRow]);
+ $deletedWorkspaceRow = $row($workspace->getMorphClass(), (string) Str::uuid());
+ DB::table('medias')->insert([$workspaceRow, $avatarRow, $deletedWorkspaceRow]);
(require database_path('migrations/2026_10_01_000001_add_owners_to_medias_table.php'))->backfillWorkspaceIds();
expect(DB::table('medias')->where('id', $workspaceRow['id'])->value('workspace_id'))->toBe($workspace->id)
- ->and(DB::table('medias')->where('id', $avatarRow['id'])->value('workspace_id'))->toBeNull();
+ ->and(DB::table('medias')->where('id', $avatarRow['id'])->value('workspace_id'))->toBeNull()
+ ->and(DB::table('medias')->where('id', $deletedWorkspaceRow['id'])->value('workspace_id'))->toBeNull();
});
test('workspace-owned rows get their workspace_id from the mediable', function () {