Created
October 15, 2020 13:03
-
-
Save sandervanhooft/d49f4cc4c7c991772e76a26a91939953 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
class EnsureDatabaseIs extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'ensure:database:is {name}'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Assert that the database matches the provided name'; | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
$this->info('Ensuring the right database is being used'); | |
$configured = config('database.connections.mysql.database'); | |
$expected = $this->argument('name'); | |
if ($configured === $expected) { | |
return 0; // OK | |
} | |
$message = 'Configured database "' . $configured | |
. '" does not match expected value "' . $expected . '"'; | |
$this->error($message); | |
return 1; // Not OK | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment