-
Notifications
You must be signed in to change notification settings - Fork 122
Issue #487 Create the test schema with migrations (Draft) #488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
| * | ||
| * Licensed under The MIT License | ||
| * Redistributions of files must retain the above copyright notice. | ||
| * | ||
| * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
| * @link http://cakephp.org CakePHP(tm) Project | ||
| * @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
| */ | ||
| namespace Migrations\TestSuite; | ||
|
|
||
| use Cake\Datasource\ConnectionManager; | ||
|
|
||
| class ConfigReader | ||
| { | ||
| /** | ||
| * @var array | ||
| */ | ||
| private $config = []; | ||
|
|
||
| /** | ||
| * Read in the Datasources the 'migrations' key for each | ||
| * active connection | ||
| * | ||
| * @return $this | ||
| */ | ||
| public function readMigrationsInDatasources(): self | ||
| { | ||
| foreach ($this->getActiveConnections() as $connectionName) { | ||
| $connection = ConnectionManager::getConfig($connectionName); | ||
| $config = []; | ||
|
|
||
| if (isset($connection['migrations'])) { | ||
| if ($connection['migrations'] === true) { | ||
| $config = ['connection' => $connectionName ]; | ||
| $this->normalizeArray($config); | ||
| } elseif (is_array($connection['migrations'])) { | ||
| $config = $connection['migrations']; | ||
| $this->normalizeArray($config); | ||
| foreach ($config as $k => $v) { | ||
| $config[$k]['connection'] = $config[$k]['connection'] ?? $connectionName; | ||
| } | ||
|
|
||
| } | ||
| $this->config = array_merge($this->config, $config); | ||
| } | ||
| } | ||
|
|
||
| $this->processConfig(); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * @param string[]|array[] $config An array of migration configs | ||
| * @return $this | ||
| */ | ||
| public function readConfig(array $config = []): self | ||
| { | ||
| if (!empty($config)) { | ||
| $this->normalizeArray($config); | ||
| $this->config = $config; | ||
| } | ||
|
|
||
| $this->processConfig(); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| public function processConfig(): void | ||
| { | ||
| foreach ($this->config as $k => $config) { | ||
| $this->config[$k]['connection'] = $this->config[$k]['connection'] ?? 'test'; | ||
| } | ||
| if (empty($this->config)) { | ||
| $this->config = [['connection' => 'test']]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Initialize all connections used by the manager | ||
| * | ||
| * @return array | ||
| */ | ||
| public function getActiveConnections(): array | ||
| { | ||
| $connections = ConnectionManager::configured(); | ||
| foreach ($connections as $i => $connectionName) { | ||
| if ($this->skipConnection($connectionName)) { | ||
| unset($connections[$i]); | ||
| } | ||
| } | ||
|
|
||
| return $connections; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $connectionName Connection name | ||
| * | ||
| * @return bool | ||
| */ | ||
| public function skipConnection(string $connectionName): bool | ||
| { | ||
| // CakePHP 4 solves a DebugKit issue by creating an Sqlite connection | ||
| // in tests/bootstrap.php. This connection should be ignored. | ||
| if ($connectionName === 'test_debug_kit') { | ||
| return true; | ||
| } | ||
|
|
||
| if ($connectionName === 'test' || strpos($connectionName, 'test_') === 0) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Make array an array of arrays | ||
| * | ||
| * @param array $array | ||
| * @return void | ||
| */ | ||
| public function normalizeArray(array &$array): void | ||
| { | ||
| if (!empty($array) && !isset($array[0])) { | ||
| $array = [$array]; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| public function getConfig(): array | ||
| { | ||
| return $this->config; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,185 @@ | ||||||
| <?php | ||||||
| declare(strict_types=1); | ||||||
|
|
||||||
| /** | ||||||
| * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||||||
| * | ||||||
| * Licensed under The MIT License | ||||||
| * Redistributions of files must retain the above copyright notice. | ||||||
| * | ||||||
| * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||||||
| * @link http://cakephp.org CakePHP(tm) Project | ||||||
| * @license http://www.opensource.org/licenses/mit-license.php MIT License | ||||||
| */ | ||||||
| namespace Migrations\TestSuite; | ||||||
|
|
||||||
| use Cake\Console\ConsoleIo; | ||||||
| use Cake\Datasource\ConnectionManager; | ||||||
| use Cake\TestSuite\Schema\SchemaCleaner; | ||||||
| use Cake\TestSuite\Schema\SchemaManager; | ||||||
| use Cake\TestSuite\TestConnectionManager; | ||||||
| use Migrations\Migrations; | ||||||
|
|
||||||
| class Migrator extends SchemaManager | ||||||
| { | ||||||
| /** | ||||||
| * @var ConsoleIo | ||||||
| */ | ||||||
| protected $io; | ||||||
|
|
||||||
| /** | ||||||
| * General command to run before your tests run | ||||||
| * E.g. in tests/bootstrap.php | ||||||
| * | ||||||
| * @param array $config | ||||||
| * @param bool $verbose Set to true to display messages | ||||||
| * @return Migrator | ||||||
| */ | ||||||
| public static function migrate(array $config = [], $verbose = false): Migrator | ||||||
| { | ||||||
| $migrator = new static($verbose); | ||||||
|
|
||||||
| // Ensures that the connections are aliased, in case | ||||||
| // the migrations invoke the table registry. | ||||||
| TestConnectionManager::aliasConnections(); | ||||||
|
|
||||||
| $configReader = new ConfigReader(); | ||||||
| $configReader->readMigrationsInDatasources(); | ||||||
| $configReader->readConfig($config); | ||||||
| $migrator->handleMigrationsStatus($configReader->getConfig()); | ||||||
|
|
||||||
| return $migrator; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Run migrations for all configured migrations. | ||||||
| * | ||||||
| * @param string[] $config Migration configuration. | ||||||
| * @return void | ||||||
| */ | ||||||
| protected function runMigrations(array $config): void | ||||||
| { | ||||||
| $migrations = new Migrations(); | ||||||
| $result = $migrations->migrate($config); | ||||||
|
|
||||||
| $msg = 'Migrations for ' . $this->stringifyConfig($config); | ||||||
|
|
||||||
|
|
||||||
| if ($result === true) { | ||||||
| $this->io->success($msg . ' successfully run.'); | ||||||
| } else { | ||||||
| $this->io->error( $msg . ' failed.'); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * If a migration is missing or down, all tables of the considered connection are dropped. | ||||||
| * | ||||||
| * @param array $configs Array of migration configurations to handle. | ||||||
| * @return $this | ||||||
| * @throws \Exception | ||||||
| */ | ||||||
| protected function handleMigrationsStatus(array $configs): self | ||||||
| { | ||||||
| $connectionsToDrop = []; | ||||||
| foreach ($configs as &$config) { | ||||||
| $connectionName = $config['connection'] = $config['connection'] ?? 'test'; | ||||||
| $this->io->info("Reading migrations status for {$this->stringifyConfig($config)}..."); | ||||||
| $migrations = new Migrations($config); | ||||||
| if ($this->isStatusChanged($migrations)) { | ||||||
| if (!in_array($connectionName, $connectionsToDrop)) { | ||||||
| $connectionsToDrop[] = $connectionName; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (empty($connectionsToDrop)) { | ||||||
| $this->io->success("No migration changes detected."); | ||||||
|
|
||||||
| return $this; | ||||||
| } | ||||||
|
|
||||||
| $schemaCleaner = new SchemaCleaner($this->io); | ||||||
| foreach ($connectionsToDrop as $connectionName) { | ||||||
| $schemaCleaner->dropTables($connectionName); | ||||||
| } | ||||||
|
|
||||||
| foreach ($configs as $migration) { | ||||||
| $this->runMigrations($migration); | ||||||
| } | ||||||
|
|
||||||
| // Truncate all created tables, except migration tables | ||||||
| foreach ($connectionsToDrop as $connectionName) { | ||||||
| $schema = ConnectionManager::get($connectionName)->getSchemaCollection(); | ||||||
|
pabloelcolombiano marked this conversation as resolved.
|
||||||
| $allTables = $schema->listTables(); | ||||||
| $tablesToTruncate = $this->unsetMigrationTables($allTables); | ||||||
| $schemaCleaner->truncateTables($connectionName, $tablesToTruncate); | ||||||
| } | ||||||
|
|
||||||
| return $this; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Unset the phinx migration tables from an array of tables. | ||||||
| * | ||||||
| * @param string[] $tables | ||||||
| * @return array | ||||||
| */ | ||||||
| protected function unsetMigrationTables(array $tables): array | ||||||
| { | ||||||
| $endsWithPhinxlog = function (string $string) { | ||||||
| $needle = 'phinxlog'; | ||||||
| return substr($string, -strlen($needle)) === $needle; | ||||||
| }; | ||||||
|
|
||||||
| foreach ($tables as $i => $table) { | ||||||
| if ($endsWithPhinxlog($table)) { | ||||||
| unset($tables[$i]); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return array_values($tables); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Checks if any migrations are up but missing. | ||||||
| * | ||||||
| * @param Migrations $migrations | ||||||
| * @return bool | ||||||
| */ | ||||||
| protected function isStatusChanged(Migrations $migrations): bool | ||||||
| { | ||||||
| foreach ($migrations->status() as $migration) { | ||||||
| if ($migration['status'] === 'up' && ($migration['missing'] ?? false)) { | ||||||
| $this->io->info('Missing migration(s) detected.'); | ||||||
| return true; | ||||||
| } | ||||||
| if ($migration['status'] === 'down') { | ||||||
| $this->io->info('New migration(s) found.'); | ||||||
| return true; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Stringify the migration parameters. | ||||||
| * This is used to display readable messages | ||||||
| * on the command line. | ||||||
| * | ||||||
| * @param string[] $config Config array | ||||||
| * @return string | ||||||
| */ | ||||||
| protected function stringifyConfig(array $config): string | ||||||
| { | ||||||
| $options = []; | ||||||
| foreach (['connection', 'plugin', 'source', 'target'] as $option) { | ||||||
| if (isset($config[$option])) { | ||||||
| $options[] = $option . ' "'.$config[$option].'"'; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Our phpcs rules will get grumpy about the operator spacing. |
||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return implode(', ', $options); | ||||||
| } | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
| * | ||
| * Licensed under The MIT License | ||
| * Redistributions of files must retain the above copyright notice. | ||
| * | ||
| * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
| * @link http://cakephp.org CakePHP(tm) Project | ||
| * @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
| */ | ||
|
|
||
| namespace Migrations\Test; | ||
|
|
||
| use Cake\Datasource\ConnectionManager; | ||
|
|
||
| trait MigratorTestTrait | ||
| { | ||
| public function setDummyConnections(): void | ||
| { | ||
| $testConfig = ['url' => getenv('db_dsn')]; | ||
| $testConfig['migrations'] = [ | ||
| ['source' => 'FooSource'], | ||
| ['plugin' => 'FooPlugin'], | ||
| ]; | ||
|
|
||
| $this->setConfigIfNotDefined('test_migrator', $testConfig); | ||
|
|
||
| $testConfig['migrations'] = ['plugin' => 'BarPlugin']; | ||
| $this->setConfigIfNotDefined('test_migrator_2', $testConfig); | ||
|
|
||
| $testConfig['migrations'] = true; | ||
| $this->setConfigIfNotDefined('test_migrator_3', $testConfig); | ||
| } | ||
|
|
||
| public function setConfigIfNotDefined(string $name, array $config): void | ||
| { | ||
| if (ConnectionManager::getConfig($name) === null) { | ||
| ConnectionManager::setConfig($name, $config); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need a mutable reference to the configuration data?