Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/Jobs/VerifyUpcomingPostConnections.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,9 @@ private function atRiskPostPlatforms(): Collection
// socialAccount.workspace is eager-loaded even though this job
// never reads it directly — SocialAccountObserver::notifyOnboarding()
// (fired by the ->update() calls below via markAsTokenExpired())
// accesses $account->workspace, and lazy loading is disabled
// app-wide. Dropping this eager load throws LazyLoadingViolationException
// the moment a second account in the same run gets updated (see #255).
// reads $account->workspace. The observer self-heals with
// loadMissing() (see #255), but without this eager load every
// account in the batch triggers its own extra query there.
->with(['socialAccount.workspace', 'post'])
->get();
}
Expand Down
2 changes: 2 additions & 0 deletions app/Observers/SocialAccountObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ private function syncUsageAndOnboarding(SocialAccount $socialAccount): void
*/
private function notifyOnboarding(SocialAccount $socialAccount): void
{
$socialAccount->loadMissing('workspace.account');

$account = $socialAccount->workspace?->account;

if (! $account?->isOnboardingOpen()) {
Expand Down
16 changes: 16 additions & 0 deletions tests/Feature/Observers/SocialAccountObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use App\Enums\SocialAccount\Status;
use App\Jobs\PostHog\SyncAccountUsage;
use App\Models\Account;
use App\Models\SocialAccount;
Expand Down Expand Up @@ -64,3 +65,18 @@

Bus::assertNotDispatched(SyncAccountUsage::class);
});

test('updating status on multiple batch-hydrated social accounts does not throw a lazy loading violation', function () {
$accounts = SocialAccount::factory()->count(2)->create([
'workspace_id' => $this->workspace->id,
'status' => Status::Connected,
]);

$batch = SocialAccount::query()
->whereIn('id', $accounts->pluck('id'))
->get();

foreach ($batch as $socialAccount) {
$socialAccount->update(['status' => Status::Disconnected]);
}
})->throwsNoExceptions();