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

Implemented migration rollback. #1932

Merged

Conversation

romanhavrylko
Copy link
Contributor

@romanhavrylko romanhavrylko commented Dec 27, 2022

  • Adjusted MigrationMigrateCommand::execute() with providing a new option migrate-to-version which allows defining the version to migrate database.
  • Adjusted MigrationStatusCommand::execute() with providing a new option last-version which allows receiving the last executed version of the migration.

@codecov-commenter
Copy link

codecov-commenter commented Dec 27, 2022

Codecov Report

Base: 88.28% // Head: 88.66% // Increases project coverage by +0.37% 🎉

Coverage data is based on head (74d175b) compared to base (49dc1ba).
Patch coverage: 90.95% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1932      +/-   ##
============================================
+ Coverage     88.28%   88.66%   +0.37%     
- Complexity     7951     7980      +29     
============================================
  Files           242      243       +1     
  Lines         24211    24313     +102     
============================================
+ Hits          21375    21557     +182     
+ Misses         2836     2756      -80     
Flag Coverage Δ
5-max 88.66% <90.95%> (+0.37%) ⬆️
7.4 88.66% <90.95%> (+0.37%) ⬆️
agnostic 67.31% <0.00%> (-0.29%) ⬇️
mysql 69.48% <90.95%> (+0.49%) ⬆️
pgsql 69.48% <90.95%> (+0.49%) ⬆️
sqlite 67.47% <90.42%> (+0.52%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...el/Generator/Command/Executor/RollbackExecutor.php 78.78% <78.78%> (ø)
.../Propel/Generator/Command/MigrationDownCommand.php 93.33% <90.90%> (+16.49%) ⬆️
...opel/Generator/Command/MigrationMigrateCommand.php 81.41% <94.44%> (+6.41%) ⬆️
src/Propel/Generator/Manager/MigrationManager.php 97.98% <98.85%> (+0.73%) ⬆️
...ropel/Generator/Command/MigrationStatusCommand.php 85.10% <100.00%> (+74.99%) ⬆️
...rc/Propel/Generator/Platform/PlatformInterface.php 0.00% <0.00%> (ø)
.../Propel/Runtime/Connection/ConnectionInterface.php 0.00% <0.00%> (ø)
...rc/Propel/Generator/Command/MigrationUpCommand.php 75.49% <0.00%> (+3.92%) ⬆️
... and 1 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@mringler mringler left a comment

Choose a reason for hiding this comment

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

Very nice!

Some smaller issues I think should be changed, mostly typos and some things I found hard to read Let me know what you think!

I'm not through all the way, need to take a break now.

src/Propel/Generator/Command/Executor/RollbackExecutor.php Outdated Show resolved Hide resolved
src/Propel/Generator/Command/Executor/RollbackExecutor.php Outdated Show resolved Hide resolved
src/Propel/Generator/Command/Executor/RollbackExecutor.php Outdated Show resolved Hide resolved
src/Propel/Generator/Command/MigrationMigrateCommand.php Outdated Show resolved Hide resolved
src/Propel/Generator/Manager/MigrationManager.php Outdated Show resolved Hide resolved
src/Propel/Generator/Manager/MigrationManager.php Outdated Show resolved Hide resolved
} catch (PDOException $e) {
$this->createMigrationTable($name);
$migrationTimestamps = [];
}
}

sort($migrationTimestamps);
usort($migrationTimestamps, function (array $a, array $b) {
if ($a[static::COL_EXECUTION_DATETIME] === $b[static::COL_EXECUTION_DATETIME]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

First off, nice, didn't know about the spaceship operator.

But why is this necessary? Is it even possible that ordering by execution date gives different results than ordering by migration timestamp?

Also, please don't repeat the execution date comparison. For me, this would be fine:

return $a[static::COL_EXECUTION_DATETIME] <=> $b[static::COL_EXECUTION_DATETIME]
    || $a[static::COL_VERSION] <=> $b[static::COL_VERSION];

Copy link
Contributor Author

Choose a reason for hiding this comment

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

According to the investigation that was made by @valerio8787, Propel migrate down command may roll back migration in the wrong order. E.g. we have two releases of the application, which contain the following migrations:

release 1 (migration applied 01.05.2022):
migration_001.php
migration_008.php
release 2 (migration applied 01.06.2022):
migration_005.php (migration created during development earlier than migration_008.php from release 1 and has an earlier version)

After the deployment of all versions, Propel migration table will have the following state:

version
--
001
008
005

Now if we run propel migrate:down command (that rollback only one migration) it will roll back migration with version 008 first, as Propel orders executed migrations by version and then rolls back a migration with max version value.

Speaking about sorting, we cannot use OR since we have to return -1, 0, 1, but in this case, it will return 0, 1.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for clearing that up, that makes sense.

Also, you are right, I got the logical operator behavior confused with JavaScript. This should work though:

return $a[static::COL_EXECUTION_DATETIME] <=> $b[static::COL_EXECUTION_DATETIME]
    ?: $a[static::COL_VERSION] <=> $b[static::COL_VERSION];

Or just a local variable:

$comparedDatetime = $a[static::COL_EXECUTION_DATETIME] <=> $b[static::COL_EXECUTION_DATETIME];
return ($comparedDatetime !== 0) ? $comparedDatetime : $a[static::COL_VERSION] <=> $b[static::COL_VERSION];

but it is probably fine the way it is.

src/Propel/Generator/Manager/MigrationManager.php Outdated Show resolved Hide resolved
src/Propel/Generator/Manager/MigrationManager.php Outdated Show resolved Hide resolved
src/Propel/Generator/Manager/MigrationManager.php Outdated Show resolved Hide resolved
Copy link
Contributor

@mringler mringler left a comment

Choose a reason for hiding this comment

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

Nice, thank you for the changes, I think it is great now.

} catch (PDOException $e) {
$this->createMigrationTable($name);
$migrationTimestamps = [];
}
}

sort($migrationTimestamps);
usort($migrationTimestamps, function (array $a, array $b) {
if ($a[static::COL_EXECUTION_DATETIME] === $b[static::COL_EXECUTION_DATETIME]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for clearing that up, that makes sense.

Also, you are right, I got the logical operator behavior confused with JavaScript. This should work though:

return $a[static::COL_EXECUTION_DATETIME] <=> $b[static::COL_EXECUTION_DATETIME]
    ?: $a[static::COL_VERSION] <=> $b[static::COL_VERSION];

Or just a local variable:

$comparedDatetime = $a[static::COL_EXECUTION_DATETIME] <=> $b[static::COL_EXECUTION_DATETIME];
return ($comparedDatetime !== 0) ? $comparedDatetime : $a[static::COL_VERSION] <=> $b[static::COL_VERSION];

but it is probably fine the way it is.

@dereuromark
Copy link
Contributor

The PHPStan issue seems unrelated.
👍

@dereuromark
Copy link
Contributor

@yaroslav-spryker @mringler Can you maybe approve again?

@dereuromark dereuromark added this to the 2.0.0 Beta milestone Jan 4, 2023
@dereuromark dereuromark merged commit c65858e into propelorm:master Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants