Created
March 11, 2022 06:48
-
-
Save fdabek1/a3790ea24f7581782e3a377df7758e09 to your computer and use it in GitHub Desktop.
Drop all tenants databases in multi-db version of stancl/tenancy package
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\Database\Console\WipeCommand as BaseWipeCommand; | |
class WipeCommand extends BaseWipeCommand | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function handle() | |
{ | |
$value = parent::handle(); | |
if ($value === 1) | |
return $value; | |
$database = $this->input->getOption('database'); | |
$connection = $this->laravel['db']->connection($database); | |
$prefix = config('tenancy.database.prefix'); | |
$tenant_dbs = $connection->select("SHOW DATABASES LIKE '{$prefix}%'"); | |
foreach ($tenant_dbs as $db) { | |
$name = array_values((array)$db)[0]; | |
$connection->select("DROP DATABASE `{$name}`;"); | |
} | |
$this->info('Dropped all tenant databases successfully.'); | |
return $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment