|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) 2023 CodeIgniter Foundation <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\PHPStan\Database; |
| 15 | + |
| 16 | +use CodeIgniter\Database\Migration; |
| 17 | +use CodeIgniter\Database\MigrationRunner; |
| 18 | +use CodeIgniter\Database\SQLite3\Connection; |
| 19 | +use Config\Database; |
| 20 | +use Config\Migrations; |
| 21 | +use ReflectionClass; |
| 22 | +use stdClass; |
| 23 | +use Throwable; |
| 24 | + |
| 25 | +/** |
| 26 | + * Runs the project's migrations into a schema connection and returns a fingerprint |
| 27 | + * of the migration set. |
| 28 | + * |
| 29 | + * Migrations are run one by one rather than through `MigrationRunner::latest()`: a migration |
| 30 | + * that cannot run on SQLite leaves its tables out of the inferred schema instead of aborting |
| 31 | + * the whole build, and migrations pinned to a specific database group are skipped. |
| 32 | + */ |
| 33 | +final class SchemaMigrator |
| 34 | +{ |
| 35 | + public function migrate(Connection $db, ?string $namespace = null): string |
| 36 | + { |
| 37 | + $runner = new MigrationRunner(config(Migrations::class), $db); |
| 38 | + |
| 39 | + if ($namespace !== null) { |
| 40 | + $runner->setNamespace($namespace); |
| 41 | + } |
| 42 | + |
| 43 | + $forge = Database::forge($db); |
| 44 | + |
| 45 | + $fingerprint = ''; |
| 46 | + |
| 47 | + foreach ($runner->findMigrations() as $migration) { |
| 48 | + if (! $migration instanceof stdClass) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + $path = $migration->path; |
| 53 | + $class = $migration->class; |
| 54 | + $uid = $migration->uid; |
| 55 | + |
| 56 | + if (! is_string($path) || ! is_string($class) || ! is_string($uid)) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + $fileHash = md5_file($path); |
| 61 | + $fingerprint .= $uid . ':' . ($fileHash === false ? '' : $fileHash) . "\n"; |
| 62 | + |
| 63 | + require_once $path; |
| 64 | + |
| 65 | + if (! class_exists($class, false) || $this->isPinnedToGroup($class)) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + $instance = new $class($forge); |
| 70 | + |
| 71 | + if (! $instance instanceof Migration) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + $instance->up(); |
| 77 | + } catch (Throwable) { |
| 78 | + // Degrade: the failed migration's tables are simply absent from the schema. |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return hash('sha256', $fingerprint); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Reads the migration's `$DBGroup` default without instantiating it, since instantiating a |
| 87 | + * pinned migration would open a connection to that group. |
| 88 | + * |
| 89 | + * @param class-string $class |
| 90 | + */ |
| 91 | + private function isPinnedToGroup(string $class): bool |
| 92 | + { |
| 93 | + return ((new ReflectionClass($class))->getDefaultProperties()['DBGroup'] ?? null) !== null; |
| 94 | + } |
| 95 | +} |
0 commit comments