Skip to content

Commit

Permalink
Merge pull request #551 from pabloelcolombiano/3.x
Browse files Browse the repository at this point in the history
Fix migrations being run when nothing changes
  • Loading branch information
markstory authored May 10, 2022
2 parents 9b12dcd + 956c8e9 commit e1b03bf
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 11 deletions.
12 changes: 1 addition & 11 deletions src/TestSuite/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,11 @@ 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') {
$messages['up'][] = "Unapplied migration source={$migration['name']} id={$migration['id']}";
}
if ($migration['missing'] ?? false) {
if ($migration['status'] === 'up' && ($migration['missing'] ?? false)) {
$messages['missing'][] = 'Applied but, missing Migration ' .
"source={$migration['name']} id={$migration['id']}";
}
Expand All @@ -180,12 +176,6 @@ protected function shouldDropTables(Migrations $migrations, array $options): boo
$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:';
Expand Down
102 changes: 102 additions & 0 deletions tests/TestCase/TestSuite/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
*/
namespace Migrations\Test\TestCase\TestSuite;

use Cake\Chronos\ChronosInterface;
use Cake\Datasource\ConnectionManager;
use Cake\I18n\FrozenDate;
use Cake\TestSuite\ConnectionHelper;
use Cake\TestSuite\TestCase;
use Migrations\TestSuite\Migrator;
Expand Down Expand Up @@ -129,4 +131,104 @@ public function testTruncateAfterMigrations(): void
$connection = ConnectionManager::get('test');
$this->assertCount(0, $connection->query('SELECT * FROM migrator')->fetchAll());
}

private function setMigrationEndDateToYesterday()
{
ConnectionManager::get('test')->newQuery()
->update('migrator_phinxlog')
->set('end_time', FrozenDate::yesterday(), 'timestamp')
->execute();
}

private function fetchMigrationEndDate(): ChronosInterface
{
$endTime = ConnectionManager::get('test')->newQuery()
->select('end_time')
->from('migrator_phinxlog')
->execute()->fetchColumn(0);

return FrozenDate::parse($endTime);
}

public function testSkipMigrationDroppingIfOnlyUpMigrations(): void
{
// Run the migrator
$migrator = new Migrator();
$migrator->run(['plugin' => 'Migrator']);

// Update the end time in the migrator_phinxlog table
$this->setMigrationEndDateToYesterday();

// Re-run the migrator
$migrator->run(['plugin' => 'Migrator']);

// Ensure that the end time is unchanged, meaning that the phinx table was not dropped
// and the migrations were not re-run
$this->assertTrue($this->fetchMigrationEndDate()->isYesterday());
}

public function testSkipMigrationDroppingIfOnlyUpMigrationsWithTwoSetsOfMigrations(): void
{
// Run the migrator
$migrator = new Migrator();
$migrator->runMany([
['plugin' => 'Migrator',],
['source' => '../../Plugin/Migrator/config/Migrations2',],
], false);

// Update the end time in the migrator_phinxlog table
$this->setMigrationEndDateToYesterday();

// Re-run the migrator
$migrator->runMany([
['plugin' => 'Migrator',],
['source' => '../../Plugin/Migrator/config/Migrations2',],
], false);

// Ensure that the end time is unchanged, meaning that the phinx table was not dropped
// and the migrations were not re-run
$this->assertTrue($this->fetchMigrationEndDate()->isYesterday());
}

public function testDropMigrationsIfDownMigrations(): void
{
// Run the migrator
$migrator = new Migrator();
$migrator->run(['plugin' => 'Migrator']);

// Update the end time in the migrator_phinxlog table
$this->setMigrationEndDateToYesterday();

// Re-run the migrator with additional down migrations
$migrator->runMany([
['plugin' => 'Migrator',],
['plugin' => 'Migrator', 'source' => 'Migrations2',],
], false);

// Ensure that the end time is today, meaning that the phinx table was truncated
// and the migration were re-run
$this->assertTrue($this->fetchMigrationEndDate()->isToday());
}

public function testDropMigrationsIfMissingMigrations(): void
{
// Run the migrator
$migrator = new Migrator();
$migrator->runMany([
['plugin' => 'Migrator',],
['plugin' => 'Migrator', 'source' => 'Migrations2',],
]);

// Update the end time in the migrator_phinxlog table
$this->setMigrationEndDateToYesterday();

// Re-run the migrator with missing migrations
$migrator->runMany([
['plugin' => 'Migrator',],
], false);

// Ensure that the end time is today, meaning that the phinx table was truncated
// and the migration were re-run
$this->assertTrue($this->fetchMigrationEndDate()->isToday());
}
}

0 comments on commit e1b03bf

Please sign in to comment.