This commit is contained in:
74
api/app/Console/Commands/PublishCommand.php
Normal file
74
api/app/Console/Commands/PublishCommand.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PublishCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'publish';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Run migrations and seeders';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->getCustomers()->each(function ($customer, $i) {
|
||||
|
||||
if ($i > 0) {
|
||||
$this->getOutput()->writeln("\n");
|
||||
}
|
||||
|
||||
$this->getOutput()->writeln("<question>{$customer}</question>");
|
||||
|
||||
$this->call('migrate', ['--database' => $customer, '--force' => true]);
|
||||
$this->call('db:seed', ['--database' => $customer, '--force' => true]);
|
||||
});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function getCustomers()
|
||||
{
|
||||
return collect(json_decode(file_get_contents(base_path('connections.json')), true))
|
||||
->filter(fn ($config) => isset($config['database']) && Str::startsWith($config['database'], 'conectasus'))
|
||||
->reject(fn ($config) => !$this->databaseExists($config['database']))
|
||||
->map(fn ($config, $customer) => $customer)
|
||||
->sort()
|
||||
->values();
|
||||
}
|
||||
|
||||
private function databaseExists($databaseName)
|
||||
{
|
||||
return !empty(DB::connection('schema')->select(
|
||||
"SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?",
|
||||
[$databaseName]
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user