Files
rkm-conectasus-ci-sandbox/api/app/Console/Commands/PublishCommand.php
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

75 lines
1.8 KiB
PHP

<?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]
));
}
}