first commit
This commit is contained in:
37
src/Shared/Database/Provisioner.php
Normal file
37
src/Shared/Database/Provisioner.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Shared\Database;
|
||||
|
||||
use PDO;
|
||||
|
||||
/**
|
||||
* Orchestration du provisionnement de la base de données.
|
||||
*
|
||||
* Exécute les migrations puis le seeding éventuel sous verrou fichier
|
||||
* afin d'éviter les exécutions concurrentes au démarrage ou en CLI.
|
||||
*/
|
||||
final class Provisioner
|
||||
{
|
||||
public static function run(PDO $db): void
|
||||
{
|
||||
$lockPath = __DIR__ . '/../../../database/.provision.lock';
|
||||
$handle = fopen($lockPath, 'c+');
|
||||
|
||||
if ($handle === false) {
|
||||
throw new \RuntimeException('Impossible d\'ouvrir le verrou de provisionnement');
|
||||
}
|
||||
|
||||
try {
|
||||
if (!flock($handle, LOCK_EX)) {
|
||||
throw new \RuntimeException('Impossible d\'obtenir le verrou de provisionnement');
|
||||
}
|
||||
|
||||
Migrator::run($db);
|
||||
Seeder::seed($db);
|
||||
} finally {
|
||||
flock($handle, LOCK_UN);
|
||||
fclose($handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user