Skip to content
Closed
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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-08-24 - Eloquent Memory Exhaustion Anti-pattern
**Learning:** Found multiple instances where the codebase fetches entire tables into memory using `Model::all()` before filtering or grabbing the first record (e.g., `Model::all()->first()` or `Team::all()->filter(...)`). This creates severe memory bottlenecks and N+1 query issues on large tables.
**Action:** Always scan for `::all()->` usages and push the constraints down to the database level using `::first()`, `::where()`, or relationship queries like `::doesntHave()`.
9 changes: 5 additions & 4 deletions backend/app/Console/Commands/CleanupStuckedResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function handle()
private function cleanup_stucked_resources()
{
try {
$teams = Team::all()->filter(function ($team) {
return $team->members()->count() === 0 && $team->servers()->count() === 0;
});
// Bolt: Optimize finding stuck teams by executing a single database query
// instead of fetching all teams and running N+1 queries for members and servers.
$teams = Team::doesntHave('members')->doesntHave('servers')->get();
foreach ($teams as $team) {
$team->delete();
}
Expand Down Expand Up @@ -263,7 +263,8 @@ private function cleanup_stucked_resources()
echo "Error in application: {$e->getMessage()}\n";
}
try {
$postgresqls = StandalonePostgresql::all()->where('id', '!=', 0);
// Bolt: Filter id != 0 at the database level instead of fetching everything into memory
$postgresqls = StandalonePostgresql::where('id', '!=', 0)->get();
foreach ($postgresqls as $postgresql) {
if (! data_get($postgresql, 'environment')) {
echo 'Postgresql without environment: '.$postgresql->name.'\n';
Expand Down
21 changes: 13 additions & 8 deletions backend/app/Console/Commands/Emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,15 @@ public function handle()
}
break;
case 'application-deployment-success':
$application = Application::all()->first();
// Bolt: Optimize fetching the first record directly from DB instead of fetching all records into memory
$application = Application::first();
$this->mail = (new DeploymentSuccess($application, 'test'))->toMail();
$this->sendEmail();
break;
case 'application-deployment-failed':
$application = Application::all()->first();
$preview = ApplicationPreview::all()->first();
// Bolt: Optimize fetching the first record directly from DB instead of fetching all records into memory
$application = Application::first();
$preview = ApplicationPreview::first();
if (! $preview) {
$preview = ApplicationPreview::create([
'application_id' => $application->id,
Expand All @@ -149,13 +151,15 @@ public function handle()
$this->sendEmail();
break;
case 'application-status-changed':
$application = Application::all()->first();
// Bolt: Optimize fetching the first record directly from DB instead of fetching all records into memory
$application = Application::first();
$this->mail = (new StatusChanged($application))->toMail();
$this->sendEmail();
break;
case 'backup-failed':
$backup = ScheduledDatabaseBackup::all()->first();
$db = StandalonePostgresql::all()->first();
// Bolt: Optimize fetching the first record directly from DB instead of fetching all records into memory
$backup = ScheduledDatabaseBackup::first();
$db = StandalonePostgresql::first();
if (! $backup) {
$backup = ScheduledDatabaseBackup::create([
'enabled' => true,
Expand All @@ -171,8 +175,9 @@ public function handle()
$this->sendEmail();
break;
case 'backup-success':
$backup = ScheduledDatabaseBackup::all()->first();
$db = StandalonePostgresql::all()->first();
// Bolt: Optimize fetching the first record directly from DB instead of fetching all records into memory
$backup = ScheduledDatabaseBackup::first();
$db = StandalonePostgresql::first();
if (! $backup) {
$backup = ScheduledDatabaseBackup::create([
'enabled' => true,
Expand Down