Skip to content

Commit

Permalink
Merge pull request #534 from cakephp/improve-messages
Browse files Browse the repository at this point in the history
Improve debugging messages
  • Loading branch information
markstory authored Mar 14, 2022
2 parents c161295 + 6ca43c5 commit 6b03a44
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
54 changes: 42 additions & 12 deletions src/TestSuite/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function runMany(

if (!$migrations->migrate($migrationSet)) {
throw new RuntimeException(
sprintf('Unable to migrate fixtures for `%s`.', $migrationSet['connection'])
"Unable to migrate fixtures for `{$migrationSet['connection']}`."
);
}
}
Expand Down Expand Up @@ -158,26 +158,56 @@ protected function shouldDropTables(Migrations $migrations, array $options): boo
{
Log::write('debug', "Reading migrations status for {$options['connection']}...");

$messages = [
'up' => [],
'down' => [],
'missing' => [],
];
foreach ($migrations->status($options) as $migration) {
if ($migration['status'] === 'up') {
Log::write('debug', 'One or more additional migrations detected.');

return true;
$messages['up'][] = "Unapplied migration source={$migration['name']} id={$migration['id']}";
}
if ($migration['missing'] ?? false) {
Log::write('debug', 'One or more missing migrations detected.');

return true;
$messages['missing'][] = 'Applied but, missing Migration ' .
"source={$migration['name']} id={$migration['id']}";
}
if ($migration['status'] === 'down') {
Log::write('debug', 'New migration(s) found.');

return true;
$messages['down'][] = "Migration to reverse. source={$migration['name']} id={$migration['id']}";
}
}
Log::write('debug', 'No migration changes detected');
$output = [];
$hasProblems = false;
$itemize = function ($item) {
return '- ' . $item;
};
if (!empty($messages['up'])) {
$hasProblems = true;
$output[] = 'Unapplied migrations:';
$output = array_merge($output, array_map($itemize, $messages['up']));
$output[] = '';
}
if (!empty($messages['down'])) {
$hasProblems = true;
$output[] = 'Migrations needing to be reversed:';
$output = array_merge($output, array_map($itemize, $messages['down']));
$output[] = '';
}
if (!empty($messages['missing'])) {
$hasProblems = true;
$output[] = 'Applied but missing migrations:';
$output = array_merge($output, array_map($itemize, $messages['down']));
$output[] = '';
}
if ($output) {
$output = array_merge(
['Your migration status some differences with the expected state.', ''],
$output,
['Going to drop all tables in this source, and re-apply migrations.']
);
Log::write('debug', implode("\n", $output));
}

return false;
return $hasProblems;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/TestSuite/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

class MigratorTest extends TestCase
{
protected $dropDatabase = null;

public function setUp(): void
{
parent::setUp();
Expand All @@ -36,6 +34,8 @@ public function tearDown(): void
{
parent::tearDown();
$GLOBALS['__PHPUNIT_BOOTSTRAP'] = $this->restore;

(new ConnectionHelper())->dropTables('test');
}

public function testMigrateDropTruncate(): void
Expand Down
3 changes: 0 additions & 3 deletions tests/TestCase/View/Helper/MigrationHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
namespace Migrations\Test\TestCase\View\Helper;

use Cake\Cache\Cache;
use Cake\Database\Schema\Collection;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -78,8 +77,6 @@ public function setUp(): void
$this->helper = new MigrationHelper($this->view, [
'collection' => $this->collection,
]);
Cache::clear('_cake_model_');
Cache::enable();

$this->types = [
'timestamp' => 'timestamp',
Expand Down
10 changes: 8 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,16 @@
}
putenv('DB=' . $db);
}
ConnectionManager::setConfig('test', ['url' => getenv('DB_URL')]);
ConnectionManager::setConfig('test', [
'cacheMetadata' => false,
'url' => getenv('DB_URL'),
]);

if (getenv('DB_URL_COMPARE') !== false) {
ConnectionManager::setConfig('test_comparisons', ['url' => getenv('DB_URL_COMPARE')]);
ConnectionManager::setConfig('test_comparisons', [
'cacheMetadata' => false,
'url' => getenv('DB_URL_COMPARE'),
]);
}

Plugin::getCollection()->add(new \Migrations\Plugin());
Expand Down

0 comments on commit 6b03a44

Please sign in to comment.