diff --git a/README.md b/README.md index 7fa5fafa..facc8775 100644 --- a/README.md +++ b/README.md @@ -235,6 +235,8 @@ op read "op://vault/stripe/api-key" | cloud secret:create --name=STRIPE_KEY -n | `cloud database-cluster:create` | Create a database cluster | | `cloud database-cluster:update` | Update a database cluster | | `cloud database-cluster:delete` | Delete a database cluster | +| `cloud database-cluster:start` | Start a stopped database cluster | +| `cloud database-cluster:stop` | Stop a database cluster | | `cloud database-cluster:metrics` | View cluster metrics | | `cloud database-cluster:types` | List database types and versions | | `cloud database:list` | List databases (schemas) in a cluster | @@ -253,6 +255,8 @@ op read "op://vault/stripe/api-key" | cloud secret:create --name=STRIPE_KEY -n | `cloud database-user:delete` | Delete a database user | | `cloud database-restore:create` | Create a restore from snapshot or PITR | +A stopped database cluster keeps its data, but environments cannot connect to it until it is started again, so `database-cluster:stop` asks for confirmation (or `--force`). A Serverless Postgres cluster gets a new hostname when it starts, so redeploy connected environments once it is running. + ### Cache | Command | Description | diff --git a/app/Client/README.md b/app/Client/README.md index 919d5670..86283bdb 100644 --- a/app/Client/README.md +++ b/app/Client/README.md @@ -93,6 +93,8 @@ $data = $response->json(); - `CreateDatabaseClusterRequest` - Create a new database cluster - `UpdateDatabaseClusterRequest` - Update a database cluster - `DeleteDatabaseClusterRequest` - Delete a database cluster +- `StartDatabaseClusterRequest` - Start a stopped database cluster +- `StopDatabaseClusterRequest` - Stop a database cluster - `ListDatabaseTypesRequest` - List available database types #### Databases (within clusters) diff --git a/app/Client/Resources/DatabaseClusters/StartDatabaseClusterRequest.php b/app/Client/Resources/DatabaseClusters/StartDatabaseClusterRequest.php new file mode 100644 index 00000000..6ac815dc --- /dev/null +++ b/app/Client/Resources/DatabaseClusters/StartDatabaseClusterRequest.php @@ -0,0 +1,29 @@ +clusterId}/start"; + } + + public function createDtoFromResponse(Response $response): DatabaseCluster + { + return DatabaseCluster::createFromResponse($response->json()); + } +} diff --git a/app/Client/Resources/DatabaseClusters/StopDatabaseClusterRequest.php b/app/Client/Resources/DatabaseClusters/StopDatabaseClusterRequest.php new file mode 100644 index 00000000..f7774c00 --- /dev/null +++ b/app/Client/Resources/DatabaseClusters/StopDatabaseClusterRequest.php @@ -0,0 +1,29 @@ +clusterId}/stop"; + } + + public function createDtoFromResponse(Response $response): DatabaseCluster + { + return DatabaseCluster::createFromResponse($response->json()); + } +} diff --git a/app/Client/Resources/DatabaseClustersResource.php b/app/Client/Resources/DatabaseClustersResource.php index 8033f0ef..2ec39fd5 100644 --- a/app/Client/Resources/DatabaseClustersResource.php +++ b/app/Client/Resources/DatabaseClustersResource.php @@ -11,6 +11,8 @@ use App\Client\Resources\DatabaseClusters\ListDatabaseClustersRequest; use App\Client\Resources\DatabaseClusters\ListDatabasePresetsRequest; use App\Client\Resources\DatabaseClusters\ListDatabaseTypesRequest; +use App\Client\Resources\DatabaseClusters\StartDatabaseClusterRequest; +use App\Client\Resources\DatabaseClusters\StopDatabaseClusterRequest; use App\Client\Resources\DatabaseClusters\UpdateDatabaseClusterRequest; use App\Dto\DatabaseCluster; use App\Dto\DatabaseClusterMetrics; @@ -58,6 +60,22 @@ public function update(UpdateDatabaseClusterRequestData $data): DatabaseCluster return $request->createDtoFromResponse($response); } + public function start(string $clusterId): DatabaseCluster + { + $request = new StartDatabaseClusterRequest($clusterId); + $response = $this->send($request); + + return $request->createDtoFromResponse($response); + } + + public function stop(string $clusterId): DatabaseCluster + { + $request = new StopDatabaseClusterRequest($clusterId); + $response = $this->send($request); + + return $request->createDtoFromResponse($response); + } + public function delete(string $clusterId): void { $this->send(new DeleteDatabaseClusterRequest($clusterId)); diff --git a/app/Commands/DatabaseClusterStart.php b/app/Commands/DatabaseClusterStart.php new file mode 100644 index 00000000..06de13f6 --- /dev/null +++ b/app/Commands/DatabaseClusterStart.php @@ -0,0 +1,45 @@ +ensureClient(); + + intro('Starting Database Cluster'); + + $cluster = $this->resolvers()->databaseCluster()->from($this->argument('cluster')); + + $started = spin( + fn () => $this->client->databaseClusters()->start($cluster->id), + 'Starting database cluster...', + ); + + $this->outputJsonIfWanted($started); + + success("Database cluster '{$started->name}' started."); + + // Serverless Postgres comes back on a new hostname, so environments keep + // the old one in their connection settings until they are redeployed. + if ($started->type === DatabaseClusterPreset::NeonServerlessPostgres->value) { + warning('Serverless Postgres clusters get a new hostname when they start. Redeploy connected environments once the cluster is running.'); + } + } +} diff --git a/app/Commands/DatabaseClusterStop.php b/app/Commands/DatabaseClusterStop.php new file mode 100644 index 00000000..6fd874bb --- /dev/null +++ b/app/Commands/DatabaseClusterStop.php @@ -0,0 +1,41 @@ +ensureClient(); + + intro('Stopping Database Cluster'); + + $cluster = $this->resolvers()->databaseCluster()->from($this->argument('cluster')); + + $this->confirmDestructive("Stop database cluster '{$cluster->name}'?"); + + $stopped = spin( + fn () => $this->client->databaseClusters()->stop($cluster->id), + 'Stopping database cluster...', + ); + + $this->outputJsonIfWanted($stopped); + + success("Database cluster '{$stopped->name}' stopped."); + } +} diff --git a/tests/Feature/DatabaseClusterStartStopTest.php b/tests/Feature/DatabaseClusterStartStopTest.php new file mode 100644 index 00000000..882baf5b --- /dev/null +++ b/tests/Feature/DatabaseClusterStartStopTest.php @@ -0,0 +1,133 @@ +shouldReceive('apiTokens')->andReturn(collect(['test-api-token'])); + $this->app->instance(ConfigRepository::class, $config); + + $this->suppressOutput = Renderer::$suppressOutput; +}); + +afterEach(function () { + MockClient::destroyGlobal(); + + Renderer::$suppressOutput = $this->suppressOutput; +}); + +function setupDatabaseClusterStartStopMocks(string $type = 'laravel_mysql'): void +{ + $cluster = fn (string $status) => databaseClusterResponse([ + 'attributes' => ['name' => 'storefront-db', 'type' => $type, 'status' => $status], + ]); + + MockClient::global([ + GetDatabaseClusterRequest::class => MockResponse::make($cluster('available'), 200), + StartDatabaseClusterRequest::class => MockResponse::make($cluster('available'), 200), + StopDatabaseClusterRequest::class => MockResponse::make($cluster('stopped'), 200), + ]); +} + +it('starts a database cluster', function () { + setupDatabaseClusterStartStopMocks(); + + $this->artisan('database-cluster:start', ['cluster' => 'db-123', '--no-interaction' => true]) + ->assertSuccessful(); + + MockClient::global()->assertSent(fn ($request) => $request instanceof StartDatabaseClusterRequest + && $request->getMethod()->value === 'POST' + && $request->resolveEndpoint() === '/databases/clusters/db-123/start'); + MockClient::global()->assertNotSent(StopDatabaseClusterRequest::class); +}); + +it('outputs the started database cluster as json', function () { + setupDatabaseClusterStartStopMocks(); + + Artisan::call('database-cluster:start', ['cluster' => 'db-123', '--json' => true]); + + $output = json_decode(Artisan::output(), true); + + expect($output)->toMatchArray([ + 'id' => 'db-123', + 'name' => 'storefront-db', + 'type' => 'laravel_mysql', + 'status' => 'available', + ]); + expect($output['schemas'])->toHaveCount(1); +}); + +it('tells a person to redeploy after starting a serverless postgres cluster', function () { + Artisan::registerCommand(new InteractiveDatabaseClusterStartCommand); + setupDatabaseClusterStartStopMocks('neon_serverless_postgres'); + + expect(Artisan::call('test:interactive-database-cluster-start', ['cluster' => 'db-123']))->toBe(0); + + expect(Artisan::output()) + ->toContain("Database cluster 'storefront-db' started.") + ->toContain('Redeploy connected environments'); +}); + +it('does not ask for a redeploy after starting a mysql cluster', function () { + Artisan::registerCommand(new InteractiveDatabaseClusterStartCommand); + setupDatabaseClusterStartStopMocks('laravel_mysql'); + + expect(Artisan::call('test:interactive-database-cluster-start', ['cluster' => 'db-123']))->toBe(0); + + expect(Artisan::output()) + ->toContain("Database cluster 'storefront-db' started.") + ->not->toContain('Redeploy connected environments'); +}); + +it('keeps the redeploy hint out of json output', function () { + setupDatabaseClusterStartStopMocks('neon_serverless_postgres'); + + Artisan::call('database-cluster:start', ['cluster' => 'db-123', '--json' => true]); + + $output = Artisan::output(); + + expect($output)->not->toContain('Redeploy') + ->and(json_decode($output, true))->toMatchArray(['type' => 'neon_serverless_postgres']); +}); + +it('stops a database cluster with --force', function () { + setupDatabaseClusterStartStopMocks(); + + $this->artisan('database-cluster:stop', ['cluster' => 'db-123', '--force' => true, '--no-interaction' => true]) + ->assertSuccessful(); + + MockClient::global()->assertSent(fn ($request) => $request instanceof StopDatabaseClusterRequest + && $request->getMethod()->value === 'POST' + && $request->resolveEndpoint() === '/databases/clusters/db-123/stop'); +}); + +it('outputs the stopped database cluster as json', function () { + setupDatabaseClusterStartStopMocks(); + + Artisan::call('database-cluster:stop', ['cluster' => 'db-123', '--force' => true, '--json' => true]); + + $output = json_decode(Artisan::output(), true); + + expect($output)->toMatchArray([ + 'id' => 'db-123', + 'name' => 'storefront-db', + 'status' => 'stopped', + ]); +}); + +it('requires --force to stop a database cluster non-interactively', function () { + setupDatabaseClusterStartStopMocks(); + + $this->artisan('database-cluster:stop', ['cluster' => 'db-123', '--no-interaction' => true]) + ->assertFailed(); + + MockClient::global()->assertNotSent(StopDatabaseClusterRequest::class); +}); diff --git a/tests/Fixtures/InteractiveDatabaseClusterStartCommand.php b/tests/Fixtures/InteractiveDatabaseClusterStartCommand.php new file mode 100644 index 00000000..9e8b54e8 --- /dev/null +++ b/tests/Fixtures/InteractiveDatabaseClusterStartCommand.php @@ -0,0 +1,37 @@ +signature = str_replace('database-cluster:start', 'test:interactive-database-cluster-start', $this->signature); + $this->aliases = []; + + parent::__construct(); + } + + protected function isInteractive(): bool + { + return true; + } + + protected function configurePrompts(InputInterface $input): void + { + Renderer::$suppressOutput = false; + + parent::configurePrompts($input); + + Prompt::interactive(true); + } +}