Skip to content

Commit

Permalink
Adds "database:upgrade" artisan command (#125)
Browse files Browse the repository at this point in the history
* Adds "database:upgrade" artisan command

* Update DatabaseUpgradeCommand.php

* Update DatabaseUpgradeCommand.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
nunomaduro and taylorotwell authored May 19, 2021
1 parent 17d4670 commit 6502aaa
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
111 changes: 111 additions & 0 deletions src/Commands/DatabaseUpgradeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Laravel\VaporCli\Commands;

use Illuminate\Support\Arr;
use Laravel\VaporCli\Helpers;
use Symfony\Component\Console\Input\InputArgument;

class DatabaseUpgradeCommand extends Command
{
/**
* Existing database types.
*
* @var array
*/
protected $databaseTypes = [
'rds' => 'Fixed Size MySQL Instance 8.0 (Free Tier Eligible)',
'rds-mysql-5.7' => 'Fixed Size MySQL Instance 5.7 (Free Tier Eligible)',
'aurora-serverless' => 'Serverless MySQL Aurora Cluster',
'rds-pgsql-11.10' => 'Fixed Size PostgreSQL Instance 11.10',
'rds-pgsql' => 'Fixed Size PostgreSQL Instance 10.7',
'aurora-serverless-pgsql' => 'Serverless PostgreSQL Aurora Cluster',
];

/**
* Existing database possible upgrades types.
*
* @var array
*/
protected $possibleUpgrades = [
'rds-mysql-5.7' => ['rds'],
'rds-pgsql' => ['rds-pgsql-11.10'],
];

/**
* Configure the command options.
*
* @return void
*/
protected function configure()
{
$this
->setName('database:upgrade')
->addArgument('from', InputArgument::REQUIRED, 'The name / ID of the existing database')
->addArgument('to', InputArgument::REQUIRED, 'The name of the new database')
->setDescription('Create a new database of the selected type containing the contents of an existing database.');
}

/**
* Execute the command.
*
* @return void
*/
public function handle()
{
Helpers::ensure_api_token_is_available();

if (! is_numeric($databaseId = $this->argument('from'))) {
$databaseId = $this->findIdByName($this->vapor->databases(), $databaseId);
}

if (is_null($databaseId)) {
Helpers::abort('Unable to find a database with that name / ID.');
}

$databaseType = $this->determineDatabaseType($databaseId);

if (is_null($databaseType)) {
Helpers::danger('No possible upgrades were found for the given database');

return 1;
}

if (! Helpers::confirm('Create a new database ['
.$this->argument('to').'] that contains the contents of ['
.$this->argument('from').'] and is of the type ('.$this->databaseTypes[$databaseType].')', false)) {
Helpers::abort('Action cancelled.');
}

$this->vapor->upgradeDatabase(
$databaseId,
$this->argument('to'),
$databaseType,
);

Helpers::info('Database upgrade initiated successfully.');
Helpers::line();
Helpers::line('Databases may take several minutes to finish provisioning.');
}

/**
* Determine the database type.
*
* @param int $databaseId
*
* @return string|null
*/
protected function determineDatabaseType($databaseId)
{
$databaseType = $this->vapor->database($databaseId)['type'];
$possibleUpgrades = Arr::get($this->possibleUpgrades, $databaseType, []);

if (! empty($possibleUpgrades)) {
return $this->menu('Which type of database would you like to create?', collect($this->databaseTypes)
->filter(function ($label, $type) use ($possibleUpgrades) {
return in_array($type, $possibleUpgrades);
})->all(),
);
}
}
}
17 changes: 17 additions & 0 deletions src/ConsoleVaporClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,23 @@ public function restoreDatabase($databaseId, $name, $restoreTo)
]);
}

/**
* Upgrade the given database to new database with the given type.
*
* @param string $databaseId
* @param string $name
* @param string $type
*
* @return array
*/
public function upgradeDatabase($databaseId, $name, $type)
{
return $this->requestWithErrorHandling('post', '/api/databases/'.$databaseId.'/upgrades', [
'name' => $name,
'type' => $type,
]);
}

/**
* Get the password for the given database user.
*
Expand Down
1 change: 1 addition & 0 deletions vapor
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ $app->add(new Commands\DatabaseProxyCommand);
$app->add(new Commands\DatabaseDeleteProxyCommand);
$app->add(new Commands\DatabaseRestoreCommand);
$app->add(new Commands\DatabaseMetricsCommand);
$app->add(new Commands\DatabaseUpgradeCommand);
$app->add(new Commands\DatabaseUsersCommand);
$app->add(new Commands\DatabaseUserCommand);
$app->add(new Commands\DatabaseDropUserCommand);
Expand Down

0 comments on commit 6502aaa

Please sign in to comment.