Total Complexity | 19 |
Total Lines | 146 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
17 | class Environment |
||
18 | { |
||
19 | public readonly Connection $conn; |
||
20 | public readonly string $driver; |
||
21 | public readonly bool $showStacktrace; |
||
22 | public readonly bool $convenience; |
||
23 | public readonly string $table; |
||
24 | public readonly string $columnMigration; |
||
25 | public readonly string $columnApplied; |
||
26 | public readonly Database $db; |
||
27 | |||
28 | /** @psalm-param array<non-empty-string, Connection> $connections */ |
||
29 | 39 | public function __construct( |
|
30 | array $connections, |
||
31 | public readonly array $options, |
||
32 | ) { |
||
33 | 39 | $opts = new Opts(); |
|
34 | |||
35 | try { |
||
36 | 39 | $key = $opts->get('--conn', 'default'); |
|
37 | assert(isset($connections[$key])); |
||
38 | 39 | $this->conn = $connections[$key]; |
|
1 ignored issue
–
show
|
|||
39 | 1 | } catch (Throwable) { |
|
40 | 1 | $key = $key ?? '<undefied>'; |
|
41 | |||
42 | 1 | throw new RuntimeException("Connection '{$key}' does not exist"); |
|
43 | } |
||
44 | |||
45 | 38 | $this->showStacktrace = $opts->has('--stacktrace'); |
|
1 ignored issue
–
show
|
|||
46 | 38 | $this->db = new Database($this->conn); |
|
1 ignored issue
–
show
|
|||
47 | 38 | $this->driver = $this->conn->driver; |
|
1 ignored issue
–
show
|
|||
48 | 38 | $this->convenience = in_array($this->driver, ['sqlite', 'mysql', 'pgsql']); |
|
1 ignored issue
–
show
|
|||
49 | 38 | $this->table = $this->conn->migrationsTable(); |
|
1 ignored issue
–
show
|
|||
50 | 38 | $this->columnMigration = $this->conn->migrationsColumnMigration(); |
|
1 ignored issue
–
show
|
|||
51 | 38 | $this->columnApplied = $this->conn->migrationsColumnApplied(); |
|
1 ignored issue
–
show
|
|||
52 | } |
||
53 | |||
54 | 22 | public function getMigrations(): array|false |
|
84 | } |
||
85 | |||
86 | 33 | public function checkIfMigrationsTableExists(Database $db): bool |
|
87 | { |
||
88 | 33 | $driver = $db->getPdoDriver(); |
|
89 | 33 | $table = $this->table; |
|
90 | |||
91 | 33 | if ($driver === 'pgsql' && strpos($table, '.') !== false) { |
|
92 | 9 | [$schema, $table] = explode('.', $table); |
|
93 | } else { |
||
94 | 24 | $schema = 'public'; |
|
95 | } |
||
96 | |||
97 | 33 | $query = match ($driver) { |
|
98 | 33 | 'sqlite' => " |
|
99 | SELECT count(*) AS available |
||
100 | FROM sqlite_master |
||
101 | WHERE type='table' |
||
102 | 33 | AND name='{$table}';", |
|
103 | |||
104 | 33 | 'mysql' => " |
|
105 | SELECT count(*) AS available |
||
106 | FROM information_schema.tables |
||
107 | 33 | WHERE table_name='{$table}';", |
|
108 | |||
109 | 33 | 'pgsql' => " |
|
110 | SELECT count(*) AS available |
||
111 | FROM pg_tables |
||
112 | 33 | WHERE schemaname = '{$schema}' |
|
113 | 33 | AND tablename = '{$table}';", |
|
114 | 33 | }; |
|
115 | |||
116 | 33 | if ($query && ($db->execute($query)->one(PDO::FETCH_ASSOC)['available'] ?? 0) === 1) { |
|
117 | 28 | return true; |
|
118 | } |
||
119 | |||
120 | 5 | return false; |
|
121 | } |
||
122 | |||
123 | 5 | public function getMigrationsTableDDL(): string|false |
|
163 | // @codeCoverageIgnoreEnd |
||
164 | } |
||
165 | } |
||
167 |