An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
refactor: replace queries with cached versions for performance improvements
Andras Bacsai5e8d11f7325c
diff --git a/app/Http/Controllers/MagicController.php b/app/Http/Controllers/MagicController.phpdeleted file mode 100644index 59c9b8b94e..0000000000--- a/app/Http/Controllers/MagicController.php+++ /dev/null@@ -1,84 +0,0 @@-<?php--namespace App\Http\Controllers;--use App\Models\Environment;-use App\Models\Project;-use App\Models\Server;-use App\Models\Team;--class MagicController extends Controller-{- public function servers()- {- return response()->json([- 'servers' => Server::isUsable()->get(),- ]);- }-- public function destinations()- {- return response()->json([- 'destinations' => Server::destinationsByServer(request()->query('server_id'))->sortBy('name'),- ]);- }-- public function projects()- {- return response()->json([- 'projects' => Project::ownedByCurrentTeam()->get(),- ]);- }-- public function environments()- {- $project = Project::ownedByCurrentTeam()->whereUuid(request()->query('project_uuid'))->first();- if (! $project) {- return response()->json([- 'environments' => [],- ]);- }-- return response()->json([- 'environments' => $project->environments,- ]);- }-- public function newProject()- {- $project = Project::firstOrCreate(- ['name' => request()->query('name') ?? generate_random_name()],- ['team_id' => currentTeam()->id]- );-- return response()->json([- 'project_uuid' => $project->uuid,- ]);- }-- public function newEnvironment()- {- $environment = Environment::firstOrCreate(- ['name' => request()->query('name') ?? generate_random_name()],- ['project_id' => Project::ownedByCurrentTeam()->whereUuid(request()->query('project_uuid'))->firstOrFail()->id]- );-- return response()->json([- 'environment_name' => $environment->name,- ]);- }-- public function newTeam()- {- $team = Team::create(- [- 'name' => request()->query('name') ?? generate_random_name(),- 'personal_team' => false,- ],- );- auth()->user()->teams()->attach($team, ['role' => 'admin']);- refreshSession();-- return redirect(request()->header('Referer'));- }-}diff --git a/app/Livewire/Dashboard.php b/app/Livewire/Dashboard.phpindex 57ecaa8a25..8c2be9ab6b 100644--- a/app/Livewire/Dashboard.php+++ b/app/Livewire/Dashboard.php@@ -18,9 +18,9 @@ class Dashboard extends Componentpublic function mount(){- $this->privateKeys = PrivateKey::ownedByCurrentTeam()->get();- $this->servers = Server::ownedByCurrentTeam()->get();- $this->projects = Project::ownedByCurrentTeam()->get();+ $this->privateKeys = PrivateKey::ownedByCurrentTeamCached();+ $this->servers = Server::ownedByCurrentTeamCached();+ $this->projects = Project::ownedByCurrentTeam()->with('environments')->get();}public function render()diff --git a/app/Livewire/DeploymentsIndicator.php b/app/Livewire/DeploymentsIndicator.phpindex ac9cfd1c2d..268aed1529 100644--- a/app/Livewire/DeploymentsIndicator.php+++ b/app/Livewire/DeploymentsIndicator.php@@ -14,7 +14,7 @@ class DeploymentsIndicator extends Component#[Computed]public function deployments(){- $servers = Server::ownedByCurrentTeam()->get();+ $servers = Server::ownedByCurrentTeamCached();return ApplicationDeploymentQueue::with(['application.environment.project'])->whereIn('status', ['in_progress', 'queued'])diff --git a/app/Livewire/Project/Index.php b/app/Livewire/Project/Index.phpindex 0e4f15a5c5..7aa8dfc49f 100644--- a/app/Livewire/Project/Index.php+++ b/app/Livewire/Project/Index.php@@ -17,9 +17,9 @@ class Index extends Componentpublic function mount(){- $this->private_keys = PrivateKey::ownedByCurrentTeam()->get();- $this->projects = Project::ownedByCurrentTeam()->get();- $this->servers = Server::ownedByCurrentTeam()->count();+ $this->private_keys = PrivateKey::ownedByCurrentTeamCached();+ $this->projects = Project::ownedByCurrentTeamCached();+ $this->servers = Server::ownedByCurrentTeamCached();}public function render()diff --git a/app/Livewire/Project/Shared/ResourceOperations.php b/app/Livewire/Project/Shared/ResourceOperations.phpindex 47b3534a2c..4ba961dfd5 100644--- a/app/Livewire/Project/Shared/ResourceOperations.php+++ b/app/Livewire/Project/Shared/ResourceOperations.php@@ -36,7 +36,7 @@ class ResourceOperations extends Component$parameters = get_route_parameters();$this->projectUuid = data_get($parameters, 'project_uuid');$this->environmentUuid = data_get($parameters, 'environment_uuid');- $this->projects = Project::ownedByCurrentTeam()->get();+ $this->projects = Project::ownedByCurrentTeamCached();$this->servers = currentTeam()->servers->filter(fn ($server) => ! $server->isBuildServer());}diff --git a/app/Livewire/Project/Shared/ScheduledTask/Show.php b/app/Livewire/Project/Shared/ScheduledTask/Show.phpindex f7947951be..b1b34dd719 100644--- a/app/Livewire/Project/Shared/ScheduledTask/Show.php+++ b/app/Livewire/Project/Shared/ScheduledTask/Show.php@@ -72,7 +72,7 @@ class Show extends Component} elseif ($service_uuid) {$this->type = 'service';$this->service_uuid = $service_uuid;- $this->resource = Service::ownedByCurrentTeam()->where('uuid', $service_uuid)->firstOrFail();+ $this->resource = Service::ownedByCurrentTeamCached()->where('uuid', $service_uuid)->firstOrFail();}$this->parameters = ['environment_uuid' => $environment_uuid,diff --git a/app/Livewire/Server/Create.php b/app/Livewire/Server/Create.phpindex cf77664fec..5fd2ea4f7d 100644--- a/app/Livewire/Server/Create.php+++ b/app/Livewire/Server/Create.php@@ -17,7 +17,7 @@ class Create extends Componentpublic function mount(){- $this->private_keys = PrivateKey::ownedByCurrentTeam()->get();+ $this->private_keys = PrivateKey::ownedByCurrentTeamCached();if (! isCloud()) {$this->limit_reached = false;diff --git a/app/Livewire/Server/Index.php b/app/Livewire/Server/Index.phpindex 74764960ae..eb832d72f7 100644--- a/app/Livewire/Server/Index.php+++ b/app/Livewire/Server/Index.php@@ -12,7 +12,7 @@ class Index extends Componentpublic function mount(){- $this->servers = Server::ownedByCurrentTeam()->get();+ $this->servers = Server::ownedByCurrentTeamCached();}public function render()diff --git a/app/Livewire/SharedVariables/Environment/Index.php b/app/Livewire/SharedVariables/Environment/Index.phpindex 3673a3882c..6685c5c99d 100644--- a/app/Livewire/SharedVariables/Environment/Index.php+++ b/app/Livewire/SharedVariables/Environment/Index.php@@ -12,7 +12,7 @@ class Index extends Componentpublic function mount(){- $this->projects = Project::ownedByCurrentTeam()->get();+ $this->projects = Project::ownedByCurrentTeamCached();}public function render()diff --git a/app/Livewire/SharedVariables/Project/Index.php b/app/Livewire/SharedVariables/Project/Index.phpindex 570da74d3c..58929bade7 100644--- a/app/Livewire/SharedVariables/Project/Index.php+++ b/app/Livewire/SharedVariables/Project/Index.php@@ -12,7 +12,7 @@ class Index extends Componentpublic function mount(){- $this->projects = Project::ownedByCurrentTeam()->get();+ $this->projects = Project::ownedByCurrentTeamCached();}public function render()diff --git a/app/Livewire/Source/Github/Change.php b/app/Livewire/Source/Github/Change.phpindex 4bd0b798ac..0a38e6088a 100644--- a/app/Livewire/Source/Github/Change.php+++ b/app/Livewire/Source/Github/Change.php@@ -196,7 +196,7 @@ class Change extends Component$github_app_uuid = request()->github_app_uuid;$this->github_app = GithubApp::ownedByCurrentTeam()->whereUuid($github_app_uuid)->firstOrFail();$this->github_app->makeVisible(['client_secret', 'webhook_secret']);- $this->privateKeys = PrivateKey::ownedByCurrentTeam()->get();+ $this->privateKeys = PrivateKey::ownedByCurrentTeamCached();$this->applications = $this->github_app->applications;$settings = instanceSettings();diff --git a/app/Models/Application.php b/app/Models/Application.phpindex 118245546d..5006d0ff83 100644--- a/app/Models/Application.php+++ b/app/Models/Application.php@@ -338,11 +338,25 @@ class Application extends BaseModelreturn Application::whereRelation('environment.project.team', 'id', $teamId)->orderBy('name');}+ /**+ * Get query builder for applications owned by current team.+ * If you need all applications without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return Application::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');}+ /**+ * Get all applications owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return Application::ownedByCurrentTeam()->get();+ });+ }+public function getContainersToStop(Server $server, bool $previewDeployments = false): array{$containers = $previewDeploymentsdiff --git a/app/Models/PrivateKey.php b/app/Models/PrivateKey.phpindex 46531ed349..bb76d5ed67 100644--- a/app/Models/PrivateKey.php+++ b/app/Models/PrivateKey.php@@ -80,6 +80,10 @@ class PrivateKey extends BaseModelreturn self::extractPublicKeyFromPrivate($this->private_key) ?? 'Error loading private key';}+ /**+ * Get query builder for private keys owned by current team.+ * If you need all private keys without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(array $select = ['*']){$teamId = currentTeam()->id;@@ -88,6 +92,16 @@ class PrivateKey extends BaseModelreturn self::whereTeamId($teamId)->select($selectArray->all());}+ /**+ * Get all private keys owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return PrivateKey::ownedByCurrentTeam()->get();+ });+ }+public static function ownedAndOnlySShKeys(array $select = ['*']){$teamId = currentTeam()->id;diff --git a/app/Models/Project.php b/app/Models/Project.phpindex a9bf768036..8b26672f0e 100644--- a/app/Models/Project.php+++ b/app/Models/Project.php@@ -30,11 +30,25 @@ class Project extends BaseModelprotected $guarded = [];+ /**+ * Get query builder for projects owned by current team.+ * If you need all projects without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return Project::whereTeamId(currentTeam()->id)->orderByRaw('LOWER(name)');}+ /**+ * Get all projects owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return Project::ownedByCurrentTeam()->get();+ });+ }+protected static function booted(){static::created(function ($project) {diff --git a/app/Models/Server.php b/app/Models/Server.phpindex 8b153c8ace..82ee6721d8 100644--- a/app/Models/Server.php+++ b/app/Models/Server.php@@ -242,6 +242,10 @@ class Server extends BaseModelreturn Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true);}+ /**+ * Get query builder for servers owned by current team.+ * If you need all servers without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(array $select = ['*']){$teamId = currentTeam()->id;@@ -250,6 +254,16 @@ class Server extends BaseModelreturn Server::whereTeamId($teamId)->with('settings', 'swarmDockers', 'standaloneDockers')->select($selectArray->all())->orderBy('name');}+ /**+ * Get all servers owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return Server::ownedByCurrentTeam()->get();+ });+ }+public static function isUsable(){return Server::ownedByCurrentTeam()->whereRelation('settings', 'is_reachable', true)->whereRelation('settings', 'is_usable', true)->whereRelation('settings', 'is_swarm_worker', false)->whereRelation('settings', 'is_build_server', false)->whereRelation('settings', 'force_disabled', false);diff --git a/app/Models/Service.php b/app/Models/Service.phpindex 2cea4c805f..2daf9c39d9 100644--- a/app/Models/Service.php+++ b/app/Models/Service.php@@ -153,11 +153,25 @@ class Service extends BaseModelreturn $this->morphToMany(Tag::class, 'taggable');}+ /**+ * Get query builder for services owned by current team.+ * If you need all services without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return Service::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');}+ /**+ * Get all services owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return Service::ownedByCurrentTeam()->get();+ });+ }+public function deleteConfigurations(){$server = data_get($this, 'destination.server');diff --git a/app/Models/ServiceApplication.php b/app/Models/ServiceApplication.phpindex aef74b402b..7b8b46812d 100644--- a/app/Models/ServiceApplication.php+++ b/app/Models/ServiceApplication.php@@ -37,11 +37,25 @@ class ServiceApplication extends BaseModelreturn ServiceApplication::whereRelation('service.environment.project.team', 'id', $teamId)->orderBy('name');}+ /**+ * Get query builder for service applications owned by current team.+ * If you need all service applications without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return ServiceApplication::whereRelation('service.environment.project.team', 'id', currentTeam()->id)->orderBy('name');}+ /**+ * Get all service applications owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return ServiceApplication::ownedByCurrentTeam()->get();+ });+ }+public function isRunning(){return str($this->status)->contains('running');diff --git a/app/Models/ServiceDatabase.php b/app/Models/ServiceDatabase.phpindex 3a249059c2..f6a39cfe48 100644--- a/app/Models/ServiceDatabase.php+++ b/app/Models/ServiceDatabase.php@@ -30,11 +30,25 @@ class ServiceDatabase extends BaseModelreturn ServiceDatabase::whereRelation('service.environment.project.team', 'id', $teamId)->orderBy('name');}+ /**+ * Get query builder for service databases owned by current team.+ * If you need all service databases without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return ServiceDatabase::whereRelation('service.environment.project.team', 'id', currentTeam()->id)->orderBy('name');}+ /**+ * Get all service databases owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return ServiceDatabase::ownedByCurrentTeam()->get();+ });+ }+public function restart(){$container_id = $this->name.'-'.$this->service->uuid;diff --git a/app/Models/StandaloneClickhouse.php b/app/Models/StandaloneClickhouse.phpindex 6ac6856189..f598ef2ea3 100644--- a/app/Models/StandaloneClickhouse.php+++ b/app/Models/StandaloneClickhouse.php@@ -44,11 +44,25 @@ class StandaloneClickhouse extends BaseModel});}+ /**+ * Get query builder for ClickHouse databases owned by current team.+ * If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return StandaloneClickhouse::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');}+ /**+ * Get all ClickHouse databases owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return StandaloneClickhouse::ownedByCurrentTeam()->get();+ });+ }+protected function serverStatus(): Attribute{return Attribute::make(diff --git a/app/Models/StandaloneDragonfly.php b/app/Models/StandaloneDragonfly.phpindex 2d004246cf..47170056fe 100644--- a/app/Models/StandaloneDragonfly.php+++ b/app/Models/StandaloneDragonfly.php@@ -44,11 +44,25 @@ class StandaloneDragonfly extends BaseModel});}+ /**+ * Get query builder for Dragonfly databases owned by current team.+ * If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return StandaloneDragonfly::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');}+ /**+ * Get all Dragonfly databases owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return StandaloneDragonfly::ownedByCurrentTeam()->get();+ });+ }+protected function serverStatus(): Attribute{return Attribute::make(diff --git a/app/Models/StandaloneKeydb.php b/app/Models/StandaloneKeydb.phpindex 131e5bb3fa..266110d0a2 100644--- a/app/Models/StandaloneKeydb.php+++ b/app/Models/StandaloneKeydb.php@@ -44,11 +44,25 @@ class StandaloneKeydb extends BaseModel});}+ /**+ * Get query builder for KeyDB databases owned by current team.+ * If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return StandaloneKeydb::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');}+ /**+ * Get all KeyDB databases owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return StandaloneKeydb::ownedByCurrentTeam()->get();+ });+ }+protected function serverStatus(): Attribute{return Attribute::make(diff --git a/app/Models/StandaloneMariadb.php b/app/Models/StandaloneMariadb.phpindex 675c7987f9..aa7f2d31a1 100644--- a/app/Models/StandaloneMariadb.php+++ b/app/Models/StandaloneMariadb.php@@ -45,11 +45,25 @@ class StandaloneMariadb extends BaseModel});}+ /**+ * Get query builder for MariaDB databases owned by current team.+ * If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return StandaloneMariadb::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');}+ /**+ * Get all MariaDB databases owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return StandaloneMariadb::ownedByCurrentTeam()->get();+ });+ }+protected function serverStatus(): Attribute{return Attribute::make(diff --git a/app/Models/StandaloneMongodb.php b/app/Models/StandaloneMongodb.phpindex 7b70988f6e..9046ab013a 100644--- a/app/Models/StandaloneMongodb.php+++ b/app/Models/StandaloneMongodb.php@@ -47,11 +47,25 @@ class StandaloneMongodb extends BaseModel});}+ /**+ * Get query builder for MongoDB databases owned by current team.+ * If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return StandaloneMongodb::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');}+ /**+ * Get all MongoDB databases owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return StandaloneMongodb::ownedByCurrentTeam()->get();+ });+ }+protected function serverStatus(): Attribute{return Attribute::make(diff --git a/app/Models/StandaloneMysql.php b/app/Models/StandaloneMysql.phpindex 6f79241af2..719387b364 100644--- a/app/Models/StandaloneMysql.php+++ b/app/Models/StandaloneMysql.php@@ -45,11 +45,25 @@ class StandaloneMysql extends BaseModel});}+ /**+ * Get query builder for MySQL databases owned by current team.+ * If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return StandaloneMysql::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');}+ /**+ * Get all MySQL databases owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return StandaloneMysql::ownedByCurrentTeam()->get();+ });+ }+protected function serverStatus(): Attribute{return Attribute::make(diff --git a/app/Models/StandalonePostgresql.php b/app/Models/StandalonePostgresql.phpindex 2dc5616a29..03080fd3df 100644--- a/app/Models/StandalonePostgresql.php+++ b/app/Models/StandalonePostgresql.php@@ -45,11 +45,25 @@ class StandalonePostgresql extends BaseModel});}+ /**+ * Get query builder for PostgreSQL databases owned by current team.+ * If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return StandalonePostgresql::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');}+ /**+ * Get all PostgreSQL databases owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return StandalonePostgresql::ownedByCurrentTeam()->get();+ });+ }+public function workdir(){return database_configuration_dir()."/{$this->uuid}";diff --git a/app/Models/StandaloneRedis.php b/app/Models/StandaloneRedis.phpindex c0223304a0..6aca8af9ad 100644--- a/app/Models/StandaloneRedis.php+++ b/app/Models/StandaloneRedis.php@@ -46,11 +46,25 @@ class StandaloneRedis extends BaseModel});}+ /**+ * Get query builder for Redis databases owned by current team.+ * If you need all databases without further query chaining, use ownedByCurrentTeamCached() instead.+ */public static function ownedByCurrentTeam(){return StandaloneRedis::whereRelation('environment.project.team', 'id', currentTeam()->id)->orderBy('name');}+ /**+ * Get all Redis databases owned by current team (cached for request duration).+ */+ public static function ownedByCurrentTeamCached()+ {+ return once(function () {+ return StandaloneRedis::ownedByCurrentTeam()->get();+ });+ }+protected function serverStatus(): Attribute{return Attribute::make(diff --git a/bootstrap/helpers/subscriptions.php b/bootstrap/helpers/subscriptions.phpindex 48c3a62c32..1a0ae0fbd8 100644--- a/bootstrap/helpers/subscriptions.php+++ b/bootstrap/helpers/subscriptions.php@@ -5,39 +5,44 @@ use Stripe\Stripe;function isSubscriptionActive(){- if (! isCloud()) {- return false;- }- $team = currentTeam();- if (! $team) {- return false;- }- $subscription = $team?->subscription;+ return once(function () {+ if (! isCloud()) {+ return false;+ }+ $team = currentTeam();+ if (! $team) {+ return false;+ }+ $subscription = $team?->subscription;- if (is_null($subscription)) {- return false;- }- if (isStripe()) {- return $subscription->stripe_invoice_paid === true;- }+ if (is_null($subscription)) {+ return false;+ }+ if (isStripe()) {+ return $subscription->stripe_invoice_paid === true;+ }- return false;+ return false;+ });}+function isSubscriptionOnGracePeriod(){- $team = currentTeam();- if (! $team) {- return false;- }- $subscription = $team?->subscription;- if (! $subscription) {- return false;- }- if (isStripe()) {- return $subscription->stripe_cancel_at_period_end;- }+ return once(function () {+ $team = currentTeam();+ if (! $team) {+ return false;+ }+ $subscription = $team?->subscription;+ if (! $subscription) {+ return false;+ }+ if (isStripe()) {+ return $subscription->stripe_cancel_at_period_end;+ }- return false;+ return false;+ });}function subscriptionProvider(){diff --git a/database/migrations/2025_12_08_000000_add_performance_indexes.php b/database/migrations/2025_12_08_000000_add_performance_indexes.phpnew file mode 100644index 0000000000..680c4b4f72--- /dev/null+++ b/database/migrations/2025_12_08_000000_add_performance_indexes.php@@ -0,0 +1,49 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Support\Facades\DB;++return new class extends Migration+{+ /**+ * Index definitions: [table, columns, index_name]+ */+ private array $indexes = [+ ['servers', ['team_id'], 'idx_servers_team_id'],+ ['private_keys', ['team_id'], 'idx_private_keys_team_id'],+ ['projects', ['team_id'], 'idx_projects_team_id'],+ ['subscriptions', ['team_id'], 'idx_subscriptions_team_id'],+ ['cloud_init_scripts', ['team_id'], 'idx_cloud_init_scripts_team_id'],+ ['cloud_provider_tokens', ['team_id'], 'idx_cloud_provider_tokens_team_id'],+ ['application_deployment_queues', ['status', 'server_id'], 'idx_deployment_queues_status_server'],+ ['application_deployment_queues', ['application_id', 'status', 'pull_request_id', 'created_at'], 'idx_deployment_queues_app_status_pr_created'],+ ['environments', ['project_id'], 'idx_environments_project_id'],+ ];++ public function up(): void+ {+ foreach ($this->indexes as [$table, $columns, $indexName]) {+ if (! $this->indexExists($indexName)) {+ $columnList = implode(', ', array_map(fn ($col) => "\"$col\"", $columns));+ DB::statement("CREATE INDEX \"{$indexName}\" ON \"{$table}\" ({$columnList})");+ }+ }+ }++ public function down(): void+ {+ foreach ($this->indexes as [, , $indexName]) {+ DB::statement("DROP INDEX IF EXISTS \"{$indexName}\"");+ }+ }++ private function indexExists(string $indexName): bool+ {+ $result = DB::selectOne(+ 'SELECT 1 FROM pg_indexes WHERE indexname = ?',+ [$indexName]+ );++ return $result !== null;+ }+};