Skip to content
This repository was archived by the owner on Nov 30, 2026. It is now read-only.
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand All @@ -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 |
Expand Down
2 changes: 2 additions & 0 deletions app/Client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Client\Resources\DatabaseClusters;

use App\Dto\DatabaseCluster;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

class StartDatabaseClusterRequest extends Request
{
protected Method $method = Method::POST;

public function __construct(
protected string $clusterId,
) {
//
}

public function resolveEndpoint(): string
{
return "/databases/clusters/{$this->clusterId}/start";
}

public function createDtoFromResponse(Response $response): DatabaseCluster
{
return DatabaseCluster::createFromResponse($response->json());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Client\Resources\DatabaseClusters;

use App\Dto\DatabaseCluster;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

class StopDatabaseClusterRequest extends Request
{
protected Method $method = Method::POST;

public function __construct(
protected string $clusterId,
) {
//
}

public function resolveEndpoint(): string
{
return "/databases/clusters/{$this->clusterId}/stop";
}

public function createDtoFromResponse(Response $response): DatabaseCluster
{
return DatabaseCluster::createFromResponse($response->json());
}
}
18 changes: 18 additions & 0 deletions app/Client/Resources/DatabaseClustersResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
45 changes: 45 additions & 0 deletions app/Commands/DatabaseClusterStart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Commands;

use App\Dto\DatabaseCluster;
use App\Enums\DatabaseClusterPreset;

use function Laravel\Prompts\intro;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\warning;

class DatabaseClusterStart extends BaseCommand
{
protected ?string $jsonDataClass = DatabaseCluster::class;

protected $signature = 'database-cluster:start {cluster? : The cluster ID or name}';

protected $description = 'Start a stopped database cluster';

protected $aliases = ['db-cluster:start'];

public function handle()
{
$this->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.');
}
}
}
41 changes: 41 additions & 0 deletions app/Commands/DatabaseClusterStop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Commands;

use App\Dto\DatabaseCluster;

use function Laravel\Prompts\intro;
use function Laravel\Prompts\spin;

class DatabaseClusterStop extends BaseCommand
{
protected ?string $jsonDataClass = DatabaseCluster::class;

protected $signature = 'database-cluster:stop
{cluster? : The cluster ID or name}
{--force : Skip confirmation}';

protected $description = 'Stop a database cluster';

protected $aliases = ['db-cluster:stop'];

public function handle()
{
$this->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.");
}
}
133 changes: 133 additions & 0 deletions tests/Feature/DatabaseClusterStartStopTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

use App\Client\Resources\DatabaseClusters\GetDatabaseClusterRequest;
use App\Client\Resources\DatabaseClusters\StartDatabaseClusterRequest;
use App\Client\Resources\DatabaseClusters\StopDatabaseClusterRequest;
use App\ConfigRepository;
use App\Prompts\Renderer;
use Illuminate\Support\Facades\Artisan;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use Tests\Fixtures\InteractiveDatabaseClusterStartCommand;

beforeEach(function () {
$config = Mockery::mock(ConfigRepository::class);
$config->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);
});
37 changes: 37 additions & 0 deletions tests/Fixtures/InteractiveDatabaseClusterStartCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Tests\Fixtures;

use App\Commands\DatabaseClusterStart;
use App\Prompts\Renderer;
use Laravel\Prompts\Prompt;
use Symfony\Component\Console\Input\InputInterface;

/**
* STDIN is not a TTY under the test runner and agent or CI env vars suppress output,
* so this forces the interactive, rendered path of database-cluster:start.
*/
class InteractiveDatabaseClusterStartCommand extends DatabaseClusterStart
{
public function __construct()
{
$this->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);
}
}