-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMigrationRunner.php
More file actions
45 lines (33 loc) · 1.09 KB
/
Copy pathMigrationRunner.php
File metadata and controls
45 lines (33 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
declare(strict_types=1);
namespace Micilini\PhpSockets\Database;
use Micilini\PhpSockets\Exceptions\StorageException;
use PDO;
final readonly class MigrationRunner
{
public function __construct(
private PDO $pdo,
private ?string $schemaPath = null,
) {
}
public function run(string $driver): void
{
$this->pdo->exec($this->schemaSql($driver));
}
private function schemaSql(string $driver): string
{
$driver = strtolower(trim($driver));
if (!in_array($driver, ['sqlite', 'mysql', 'pgsql'], true)) {
throw new StorageException("Unsupported migration driver: {$driver}");
}
$path = $this->schemaPath ?? dirname(__DIR__) . '/Database/Schema/' . $driver . '.sql';
if (!is_file($path)) {
throw new StorageException("Migration schema file not found: {$path}");
}
$sql = file_get_contents($path);
if (!is_string($sql) || trim($sql) === '') {
throw new StorageException("Migration schema file is empty: {$path}");
}
return $sql;
}
}