Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"require-dev": {
"phpunit/phpunit": "~8.5.0",
"cakephp/cakephp": "^4.0.5",
"cakephp/cakephp": "dev-4.next as 4.3.0",
"cakephp/bake": "^2.1.0",
"cakephp/cakephp-codesniffer": "~4.1.0"
},
Expand Down
141 changes: 141 additions & 0 deletions src/TestSuite/ConfigReader.php
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;
}
}
185 changes: 185 additions & 0 deletions src/TestSuite/Migrator.php
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) {
Copy link
Copy Markdown
Member

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?

$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();
Comment thread
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].'"';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$options[] = $option . ' "'.$config[$option].'"';
$options[] = $option . ' "' . $config[$option] . '"';

Our phpcs rules will get grumpy about the operator spacing.

}
}

return implode(', ', $options);
}
}
44 changes: 44 additions & 0 deletions tests/MigratorTestTrait.php
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);
}
}
}
Loading