-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMigrateCommand.php
More file actions
65 lines (48 loc) · 1.96 KB
/
Copy pathMigrateCommand.php
File metadata and controls
65 lines (48 loc) · 1.96 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace Micilini\PhpSockets\Laravel\Commands;
use Illuminate\Console\Command;
use Micilini\PhpSockets\Database\MigrationRunner;
use Micilini\PhpSockets\Laravel\PhpSocketsManager;
use Throwable;
final class MigrateCommand extends Command
{
protected $signature = 'phpsockets:migrate
{--driver= : Storage driver: sqlite, mysql or pgsql}
{--database= : SQLite database path override}
{--force : Run without confirmation in production}';
protected $description = 'Run PHPSockets database migrations.';
public function handle(PhpSocketsManager $manager): int
{
$driver = $this->option('driver');
$driver = is_string($driver) && $driver !== ''
? strtolower(trim($driver))
: $manager->storageDriver();
if ($driver === 'memory') {
$this->components->warn('The memory storage driver does not need migrations.');
return self::SUCCESS;
}
if (!in_array($driver, ['sqlite', 'mysql', 'pgsql'], true)) {
$this->components->error("Unsupported migration driver: {$driver}");
return self::FAILURE;
}
if ($this->laravel->environment('production') && !$this->option('force')) {
if (!$this->confirm('You are running PHPSockets migrations in production. Continue?')) {
return self::FAILURE;
}
}
$database = $this->option('database');
try {
$pdo = $manager->pdo(
driver: $driver,
databaseOverride: is_string($database) && $database !== '' ? $database : null,
);
(new MigrationRunner($pdo))->run($driver);
$this->components->info("PHPSockets {$driver} migrations completed.");
return self::SUCCESS;
} catch (Throwable $exception) {
$this->components->error($exception->getMessage());
return self::FAILURE;
}
}
}