|
| 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 | +} |
0 commit comments