|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @var CLI $cli |
| 5 | + */ |
| 6 | +global $cli; |
| 7 | + |
| 8 | +use Utopia\CLI\CLI; |
| 9 | +use Utopia\Console; |
| 10 | +use Utopia\Database\Database; |
| 11 | +use Utopia\Database\Migration\Generator; |
| 12 | +use Utopia\Database\Migration\Migration; |
| 13 | +use Utopia\Database\Migration\Runner; |
| 14 | +use Utopia\Validator\Boolean; |
| 15 | +use Utopia\Validator\Integer; |
| 16 | +use Utopia\Validator\Text; |
| 17 | + |
| 18 | +/** |
| 19 | + * @Example |
| 20 | + * docker compose exec tests bin/cli migrate --adapter=mysql --name=testing --path=migrations |
| 21 | + * docker compose exec tests bin/cli migrate:rollback --adapter=mysql --name=testing --path=migrations --steps=1 |
| 22 | + * docker compose exec tests bin/cli migrate:status --adapter=mysql --name=testing --path=migrations |
| 23 | + * docker compose exec tests bin/cli migrate:fresh --adapter=mysql --name=testing --path=migrations |
| 24 | + * docker compose exec tests bin/cli migrate:generate --name=add_users_table |
| 25 | + */ |
| 26 | + |
| 27 | +$cli |
| 28 | + ->task('migrate') |
| 29 | + ->desc('Run pending database migrations') |
| 30 | + ->param('path', 'migrations', new Text(0), 'Path to migration files', true) |
| 31 | + ->param('adapter', '', new Text(0), 'Database adapter') |
| 32 | + ->param('name', '', new Text(0), 'Database name') |
| 33 | + ->param('namespace', '_ns', new Text(0), 'Database namespace', true) |
| 34 | + ->param('sharedTables', false, new Boolean(true), 'Whether to use shared tables', true) |
| 35 | + ->inject('database') |
| 36 | + ->action(function (string $path, string $adapter, string $name, string $namespace, bool $sharedTables, callable $database) { |
| 37 | + $migrations = loadMigrations($path); |
| 38 | + |
| 39 | + if ($migrations === []) { |
| 40 | + Console::warning('No migration files found in: ' . $path); |
| 41 | + |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + Console::info('Running migrations...'); |
| 46 | + |
| 47 | + $db = $database($adapter, $name, $namespace, $sharedTables); |
| 48 | + if (! $db instanceof Database) { |
| 49 | + throw new \RuntimeException('The database resource must return a Database instance.'); |
| 50 | + } |
| 51 | + $runner = new Runner($db); |
| 52 | + $count = $runner->migrate($migrations); |
| 53 | + |
| 54 | + Console::success("Ran {$count} migration(s)."); |
| 55 | + }); |
| 56 | + |
| 57 | +$cli |
| 58 | + ->task('migrate:rollback') |
| 59 | + ->desc('Rollback the last batch of migrations') |
| 60 | + ->param('path', 'migrations', new Text(0), 'Path to migration files', true) |
| 61 | + ->param('steps', 1, new Integer(true), 'Number of batches to rollback', true) |
| 62 | + ->param('adapter', '', new Text(0), 'Database adapter') |
| 63 | + ->param('name', '', new Text(0), 'Database name') |
| 64 | + ->param('namespace', '_ns', new Text(0), 'Database namespace', true) |
| 65 | + ->param('sharedTables', false, new Boolean(true), 'Whether to use shared tables', true) |
| 66 | + ->inject('database') |
| 67 | + ->action(function (string $path, int $steps, string $adapter, string $name, string $namespace, bool $sharedTables, callable $database) { |
| 68 | + $migrations = loadMigrations($path); |
| 69 | + $db = $database($adapter, $name, $namespace, $sharedTables); |
| 70 | + if (! $db instanceof Database) { |
| 71 | + throw new \RuntimeException('The database resource must return a Database instance.'); |
| 72 | + } |
| 73 | + $runner = new Runner($db); |
| 74 | + $count = $runner->rollback($migrations, $steps); |
| 75 | + |
| 76 | + Console::success("Rolled back {$count} migration(s)."); |
| 77 | + }); |
| 78 | + |
| 79 | +$cli |
| 80 | + ->task('migrate:status') |
| 81 | + ->desc('Show the status of all migrations') |
| 82 | + ->param('path', 'migrations', new Text(0), 'Path to migration files', true) |
| 83 | + ->param('adapter', '', new Text(0), 'Database adapter') |
| 84 | + ->param('name', '', new Text(0), 'Database name') |
| 85 | + ->param('namespace', '_ns', new Text(0), 'Database namespace', true) |
| 86 | + ->param('sharedTables', false, new Boolean(true), 'Whether to use shared tables', true) |
| 87 | + ->inject('database') |
| 88 | + ->action(function (string $path, string $adapter, string $name, string $namespace, bool $sharedTables, callable $database) { |
| 89 | + $migrations = loadMigrations($path); |
| 90 | + $db = $database($adapter, $name, $namespace, $sharedTables); |
| 91 | + if (! $db instanceof Database) { |
| 92 | + throw new \RuntimeException('The database resource must return a Database instance.'); |
| 93 | + } |
| 94 | + $runner = new Runner($db); |
| 95 | + $status = $runner->status($migrations); |
| 96 | + |
| 97 | + Console::info(\str_pad('Version', 20) . \str_pad('Name', 40) . 'Applied'); |
| 98 | + Console::info(\str_repeat('-', 70)); |
| 99 | + |
| 100 | + foreach ($status as $entry) { |
| 101 | + $applied = $entry['applied'] ? 'Yes' : 'No'; |
| 102 | + Console::log(\str_pad($entry['version'], 20) . \str_pad($entry['name'], 40) . $applied); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | +$cli |
| 107 | + ->task('migrate:fresh') |
| 108 | + ->desc('Drop all collections and re-run all migrations') |
| 109 | + ->param('path', 'migrations', new Text(0), 'Path to migration files', true) |
| 110 | + ->param('adapter', '', new Text(0), 'Database adapter') |
| 111 | + ->param('name', '', new Text(0), 'Database name') |
| 112 | + ->param('namespace', '_ns', new Text(0), 'Database namespace', true) |
| 113 | + ->param('sharedTables', false, new Boolean(true), 'Whether to use shared tables', true) |
| 114 | + ->inject('database') |
| 115 | + ->action(function (string $path, string $adapter, string $name, string $namespace, bool $sharedTables, callable $database) { |
| 116 | + $migrations = loadMigrations($path); |
| 117 | + $db = $database($adapter, $name, $namespace, $sharedTables); |
| 118 | + if (! $db instanceof Database) { |
| 119 | + throw new \RuntimeException('The database resource must return a Database instance.'); |
| 120 | + } |
| 121 | + $runner = new Runner($db); |
| 122 | + |
| 123 | + Console::warning('Dropping all collections and re-migrating...'); |
| 124 | + $count = $runner->fresh($migrations); |
| 125 | + |
| 126 | + Console::success("Fresh migration complete. Ran {$count} migration(s)."); |
| 127 | + }); |
| 128 | + |
| 129 | +$cli |
| 130 | + ->task('migrate:generate') |
| 131 | + ->desc('Generate an empty migration file') |
| 132 | + ->param('name', '', new Text(0), 'Migration name (e.g. add_users_table)') |
| 133 | + ->param('path', 'migrations', new Text(0), 'Output directory', true) |
| 134 | + ->action(function (string $name, string $path) { |
| 135 | + $timestamp = \date('YmdHis'); |
| 136 | + $className = 'V' . $timestamp . '_' . \str_replace(' ', '', \ucwords(\str_replace('_', ' ', $name))); |
| 137 | + |
| 138 | + $generator = new Generator(); |
| 139 | + $content = $generator->generateEmpty($className); |
| 140 | + |
| 141 | + if (! \is_dir($path)) { |
| 142 | + \mkdir($path, 0755, true); |
| 143 | + } |
| 144 | + |
| 145 | + $filePath = $path . '/' . $className . '.php'; |
| 146 | + \file_put_contents($filePath, $content); |
| 147 | + |
| 148 | + Console::success("Created migration: {$filePath}"); |
| 149 | + }); |
| 150 | + |
| 151 | +/** |
| 152 | + * @return array<Migration> |
| 153 | + */ |
| 154 | +function loadMigrations(string $path): array |
| 155 | +{ |
| 156 | + if (! \is_dir($path)) { |
| 157 | + return []; |
| 158 | + } |
| 159 | + |
| 160 | + $migrations = []; |
| 161 | + $files = \glob($path . '/*.php'); |
| 162 | + |
| 163 | + if ($files === false) { |
| 164 | + return []; |
| 165 | + } |
| 166 | + |
| 167 | + foreach ($files as $file) { |
| 168 | + $before = \get_declared_classes(); |
| 169 | + |
| 170 | + require_once $file; |
| 171 | + |
| 172 | + // migrate:generate writes the class under a namespace, so the file name |
| 173 | + // is not the class name and looking it up that way finds nothing -- |
| 174 | + // silently, leaving the run reporting success having skipped it. Take |
| 175 | + // whatever the file declared instead of guessing at it. |
| 176 | + foreach (\array_diff(\get_declared_classes(), $before) as $className) { |
| 177 | + if (\is_subclass_of($className, Migration::class)) { |
| 178 | + $migrations[] = new $className(); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return $migrations; |
| 184 | +} |
0 commit comments