Skip to content

Instantly share code, notes, and snippets.

@sandervanhooft
Created October 15, 2020 13:03
Show Gist options
  • Save sandervanhooft/d49f4cc4c7c991772e76a26a91939953 to your computer and use it in GitHub Desktop.
Save sandervanhooft/d49f4cc4c7c991772e76a26a91939953 to your computer and use it in GitHub Desktop.
<?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