Skip to content
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

Fix migrations being run when nothing changes #551

Merged
merged 3 commits into from
May 10, 2022
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
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());
}
}