50 lines
1.8 KiB
PHP
50 lines
1.8 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use Illuminate\Console\Attributes\Description;
|
||
use Illuminate\Console\Attributes\Signature;
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Facades\Artisan;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* Builds the migrated, empty SQLite template of the process tester's databases (storage/app/sandbox/template.sqlite).
|
||
* Run at every deployment after the migrations; a run copies it instead of migrating. Touches only that file.
|
||
*/
|
||
#[Signature('process:sandbox-template')]
|
||
#[Description('Build the empty, migrated SQLite template for process tester runs')]
|
||
class ProcessSandboxTemplate extends Command
|
||
{
|
||
public function handle(): int
|
||
{
|
||
if (! extension_loaded('pdo_sqlite')) {
|
||
$this->warn('pdo_sqlite fehlt – der Prozess-Tester bleibt gesperrt.');
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
$directory = storage_path('app/sandbox');
|
||
@mkdir($directory.'/runs', 0775, true);
|
||
$file = $directory.'/template.building.sqlite';
|
||
@unlink($file);
|
||
touch($file);
|
||
|
||
config(['database.connections.sandbox_template' => ['driver' => 'sqlite', 'database' => $file, 'prefix' => '', 'foreign_key_constraints' => true]]);
|
||
// `migrate --database` switches the default connection of this process and does not switch it back.
|
||
$default = DB::getDefaultConnection();
|
||
try {
|
||
Artisan::call('migrate', ['--database' => 'sandbox_template', '--force' => true, '--no-interaction' => true]);
|
||
} finally {
|
||
DB::setDefaultConnection($default);
|
||
DB::purge('sandbox_template');
|
||
config(['database.connections.sandbox_template' => null]);
|
||
}
|
||
|
||
rename($file, $directory.'/template.sqlite');
|
||
$this->info('Vorlage erstellt: '.$directory.'/template.sqlite');
|
||
|
||
return self::SUCCESS;
|
||
}
|
||
}
|