Skip to content

Instantly share code, notes, and snippets.

@fdabek1
Created March 11, 2022 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdabek1/a3790ea24f7581782e3a377df7758e09 to your computer and use it in GitHub Desktop.
Save fdabek1/a3790ea24f7581782e3a377df7758e09 to your computer and use it in GitHub Desktop.
Drop all tenants databases in multi-db version of stancl/tenancy package
<?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