Skip to content

Commit 6502aaa

Browse files
Adds "database:upgrade" artisan command (#125)
* Adds "database:upgrade" artisan command * Update DatabaseUpgradeCommand.php * Update DatabaseUpgradeCommand.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 17d4670 commit 6502aaa

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace Laravel\VaporCli\Commands;
4+
5+
use Illuminate\Support\Arr;
6+
use Laravel\VaporCli\Helpers;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
9+
class DatabaseUpgradeCommand extends Command
10+
{
11+
/**
12+
* Existing database types.
13+
*
14+
* @var array
15+
*/
16+
protected $databaseTypes = [
17+
'rds' => 'Fixed Size MySQL Instance 8.0 (Free Tier Eligible)',
18+
'rds-mysql-5.7' => 'Fixed Size MySQL Instance 5.7 (Free Tier Eligible)',
19+
'aurora-serverless' => 'Serverless MySQL Aurora Cluster',
20+
'rds-pgsql-11.10' => 'Fixed Size PostgreSQL Instance 11.10',
21+
'rds-pgsql' => 'Fixed Size PostgreSQL Instance 10.7',
22+
'aurora-serverless-pgsql' => 'Serverless PostgreSQL Aurora Cluster',
23+
];
24+
25+
/**
26+
* Existing database possible upgrades types.
27+
*
28+
* @var array
29+
*/
30+
protected $possibleUpgrades = [
31+
'rds-mysql-5.7' => ['rds'],
32+
'rds-pgsql' => ['rds-pgsql-11.10'],
33+
];
34+
35+
/**
36+
* Configure the command options.
37+
*
38+
* @return void
39+
*/
40+
protected function configure()
41+
{
42+
$this
43+
->setName('database:upgrade')
44+
->addArgument('from', InputArgument::REQUIRED, 'The name / ID of the existing database')
45+
->addArgument('to', InputArgument::REQUIRED, 'The name of the new database')
46+
->setDescription('Create a new database of the selected type containing the contents of an existing database.');
47+
}
48+
49+
/**
50+
* Execute the command.
51+
*
52+
* @return void
53+
*/
54+
public function handle()
55+
{
56+
Helpers::ensure_api_token_is_available();
57+
58+
if (! is_numeric($databaseId = $this->argument('from'))) {
59+
$databaseId = $this->findIdByName($this->vapor->databases(), $databaseId);
60+
}
61+
62+
if (is_null($databaseId)) {
63+
Helpers::abort('Unable to find a database with that name / ID.');
64+
}
65+
66+
$databaseType = $this->determineDatabaseType($databaseId);
67+
68+
if (is_null($databaseType)) {
69+
Helpers::danger('No possible upgrades were found for the given database');
70+
71+
return 1;
72+
}
73+
74+
if (! Helpers::confirm('Create a new database ['
75+
.$this->argument('to').'] that contains the contents of ['
76+
.$this->argument('from').'] and is of the type ('.$this->databaseTypes[$databaseType].')', false)) {
77+
Helpers::abort('Action cancelled.');
78+
}
79+
80+
$this->vapor->upgradeDatabase(
81+
$databaseId,
82+
$this->argument('to'),
83+
$databaseType,
84+
);
85+
86+
Helpers::info('Database upgrade initiated successfully.');
87+
Helpers::line();
88+
Helpers::line('Databases may take several minutes to finish provisioning.');
89+
}
90+
91+
/**
92+
* Determine the database type.
93+
*
94+
* @param int $databaseId
95+
*
96+
* @return string|null
97+
*/
98+
protected function determineDatabaseType($databaseId)
99+
{
100+
$databaseType = $this->vapor->database($databaseId)['type'];
101+
$possibleUpgrades = Arr::get($this->possibleUpgrades, $databaseType, []);
102+
103+
if (! empty($possibleUpgrades)) {
104+
return $this->menu('Which type of database would you like to create?', collect($this->databaseTypes)
105+
->filter(function ($label, $type) use ($possibleUpgrades) {
106+
return in_array($type, $possibleUpgrades);
107+
})->all(),
108+
);
109+
}
110+
}
111+
}

src/ConsoleVaporClient.php

+17
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,23 @@ public function restoreDatabase($databaseId, $name, $restoreTo)
542542
]);
543543
}
544544

545+
/**
546+
* Upgrade the given database to new database with the given type.
547+
*
548+
* @param string $databaseId
549+
* @param string $name
550+
* @param string $type
551+
*
552+
* @return array
553+
*/
554+
public function upgradeDatabase($databaseId, $name, $type)
555+
{
556+
return $this->requestWithErrorHandling('post', '/api/databases/'.$databaseId.'/upgrades', [
557+
'name' => $name,
558+
'type' => $type,
559+
]);
560+
}
561+
545562
/**
546563
* Get the password for the given database user.
547564
*

vapor

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ $app->add(new Commands\DatabaseProxyCommand);
126126
$app->add(new Commands\DatabaseDeleteProxyCommand);
127127
$app->add(new Commands\DatabaseRestoreCommand);
128128
$app->add(new Commands\DatabaseMetricsCommand);
129+
$app->add(new Commands\DatabaseUpgradeCommand);
129130
$app->add(new Commands\DatabaseUsersCommand);
130131
$app->add(new Commands\DatabaseUserCommand);
131132
$app->add(new Commands\DatabaseDropUserCommand);

0 commit comments

Comments
 (0)