|
14 | 14 |
|
15 | 15 | class ExecuteCommand extends Command { |
16 | 16 | public function run(?ArgumentValueList $arguments = null):void { |
17 | | - $forced = $arguments->contains("force"); |
18 | | - |
19 | 17 | $repoBasePath = getcwd(); |
20 | 18 | $defaultPath = $this->getDefaultPath($repoBasePath); |
21 | | - |
22 | 19 | $config = $this->getConfig($repoBasePath, $defaultPath); |
23 | 20 |
|
24 | | - $settings = new Settings( |
| 21 | + $settings = $this->buildSettingsFromConfig($config, $repoBasePath); |
| 22 | + [$migrationPath, $migrationTable] = $this->getMigrationLocation($config, $repoBasePath); |
| 23 | + |
| 24 | + $migrator = new Migrator($settings, $migrationPath, $migrationTable); |
| 25 | + $migrator->setOutput( |
| 26 | + $this->stream->getOutStream(), |
| 27 | + $this->stream->getErrorStream() |
| 28 | + ); |
| 29 | + |
| 30 | + if($this->isForced($arguments)) { |
| 31 | + $migrator->deleteAndRecreateSchema(); |
| 32 | + } |
| 33 | + |
| 34 | + $migrator->selectSchema(); |
| 35 | + $migrator->createMigrationTable(); |
| 36 | + $migrationCount = $migrator->getMigrationCount(); |
| 37 | + $migrationFileList = $migrator->getMigrationFileList(); |
| 38 | + |
| 39 | + $runFrom = $this->calculateResetNumber($arguments, $migrationFileList, $migrator, $migrationCount); |
| 40 | + |
| 41 | + $this->executeMigrations($migrator, $migrationFileList, $runFrom); |
| 42 | + } |
| 43 | + |
| 44 | + /** Determine whether the --force flag was provided. */ |
| 45 | + private function isForced(?ArgumentValueList $arguments):bool { |
| 46 | + return $arguments?->contains("force") ?? false; |
| 47 | + } |
| 48 | + |
| 49 | + /** Build Settings from config for the current repository. */ |
| 50 | + private function buildSettingsFromConfig(\Gt\Config\Config $config, string $repoBasePath): Settings { |
| 51 | + return new Settings( |
25 | 52 | implode(DIRECTORY_SEPARATOR, [ |
26 | 53 | $repoBasePath, |
27 | 54 | $config->get("database.query_path") |
28 | 55 | ]), |
29 | | - |
30 | 56 | $config->get("database.driver") ?? 'mysql', |
31 | 57 | $config->get("database.schema"), |
32 | 58 | $config->get("database.host") ?? "localhost", |
33 | 59 | (int)($config->get("database.port") ?? "3306"), |
34 | 60 | $config->get("database.username"), |
35 | 61 | $config->get("database.password") |
36 | 62 | ); |
| 63 | + } |
37 | 64 |
|
| 65 | + /** |
| 66 | + * Return [migrationPath, migrationTable] derived from config. |
| 67 | + * |
| 68 | + * @return list<string> |
| 69 | + */ |
| 70 | + private function getMigrationLocation(\Gt\Config\Config $config, string $repoBasePath): array { |
38 | 71 | $migrationPath = implode(DIRECTORY_SEPARATOR, [ |
39 | 72 | $repoBasePath, |
40 | 73 | $config->get("database.query_path") ?? "query", |
41 | 74 | $config->get("database.migration_path") ?? "_migration", |
42 | 75 | ]); |
43 | 76 | $migrationTable = $config->get("database.migration_table") ?? "_migration"; |
| 77 | + return [$migrationPath, $migrationTable]; |
| 78 | + } |
44 | 79 |
|
45 | | - $migrator = new Migrator($settings, $migrationPath, $migrationTable); |
46 | | - $migrator->setOutput( |
47 | | - $this->stream->getOutStream(), |
48 | | - $this->stream->getErrorStream() |
49 | | - ); |
50 | | - |
51 | | - if($forced) { |
52 | | - $migrator->deleteAndRecreateSchema(); |
53 | | - } |
54 | | - |
55 | | - $migrator->selectSchema(); |
56 | | - $migrator->createMigrationTable(); |
57 | | - $migrationCount = $migrator->getMigrationCount(); |
58 | | - $migrationFileList = $migrator->getMigrationFileList(); |
59 | | - |
60 | | -// TODO: Expected functionality is to provide a number to reset like --reset=15, OR leave it blank like --reset |
61 | | -// If number is provided, ignore everything before this number. |
62 | | -// If number is not provided, ignore everything other than the latest file in the directory. |
| 80 | + /** |
| 81 | + * Calculate the migration start point from --reset or current migration count. |
| 82 | + * |
| 83 | + * @param list<string> $migrationFileList |
| 84 | + */ |
| 85 | + private function calculateResetNumber( |
| 86 | + ?ArgumentValueList $arguments, |
| 87 | + array $migrationFileList, |
| 88 | + Migrator $migrator, |
| 89 | + int $migrationCount |
| 90 | + ): int { |
63 | 91 | $resetNumber = null; |
64 | | - if($arguments->contains("reset")) { |
| 92 | + if($arguments?->contains("reset")) { |
65 | 93 | $resetNumber = $arguments->get("reset")->get(); |
66 | 94 | if(!$resetNumber) { |
67 | 95 | $lastKey = array_key_last($migrationFileList); |
68 | 96 | $lastNumber = $migrator->extractNumberFromFilename($migrationFileList[$lastKey]); |
69 | | - // When no number provided, execute only the latest migration by |
70 | | - // setting the reset point to one less than the latest number. |
71 | 97 | $resetNumber = max(0, $lastNumber - 1); |
72 | 98 | } |
73 | 99 | $resetNumber = (int)$resetNumber; |
74 | 100 | } |
| 101 | + return $resetNumber ?? $migrationCount; |
| 102 | + } |
75 | 103 |
|
| 104 | + /** |
| 105 | + * Wrap integrity check and perform migration with error handling. |
| 106 | + * |
| 107 | + * @param list<string> $migrationFileList |
| 108 | + */ |
| 109 | + private function executeMigrations(Migrator $migrator, array $migrationFileList, int $runFrom): void { |
76 | 110 | try { |
77 | | - $migrator->checkIntegrity($migrationFileList, $resetNumber ?? $migrationCount); |
78 | | - $migrator->performMigration($migrationFileList, $resetNumber ?? $migrationCount); |
| 111 | + $migrator->checkIntegrity($migrationFileList, $runFrom); |
| 112 | + $migrator->performMigration($migrationFileList, $runFrom); |
79 | 113 | } |
80 | 114 | catch(MigrationIntegrityException $exception) { |
81 | 115 | $this->writeLine( |
|
0 commit comments