Skip to content

Commit

Permalink
Merge pull request #176 from bowphp/5.x-fix-migration-rollback-error
Browse files Browse the repository at this point in the history
[5.x] Fixes migration errors
  • Loading branch information
papac authored Mar 27, 2023
2 parents 400a802 + acd5aa1 commit 5923ffe
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions src/Console/Command/MigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Bow\Console\Command;

use Bow\Console\Color;
use Bow\Console\ConsoleInformation;
use Bow\Console\Generator;
use Bow\Database\Database;
use Bow\Database\Migration\SQLGenerator;
Expand Down Expand Up @@ -68,14 +67,10 @@ private function factory(string $type)
// We create the migration database status
$this->createMigrationTable();

// We get current migration status
$current_migrations = $this->getMigrationTable()
->whereIn('migration', array_values($migrations))->get();

try {
$action = 'make' . strtoupper($type);

return $this->$action($current_migrations, $migrations);
return $this->$action($migrations);
} catch (\Exception $exception) {
throw $exception;
}
Expand All @@ -84,12 +79,15 @@ private function factory(string $type)
/**
* Up migration
*
* @param array $current_migration
* @param array $migrations
* @return void
*/
protected function makeUp(array $current_migrations, array $migrations): void
protected function makeUp(array $migrations): void
{
// We get migrations
$current_migrations = $this->getMigrationTable()
->whereIn('migration', array_values($migrations))->get();

if (count($current_migrations) == count($migrations)) {
echo Color::green('Nothing to migrate.');

Expand Down Expand Up @@ -126,23 +124,24 @@ protected function makeUp(array $current_migrations, array $migrations): void
/**
* Rollback migration
*
* @param array $current_migration
* @param array $migrations
* @return void
*/
protected function makeRollback(array $current_migrations, array $migrations): void
protected function makeRollback(array $migrations): void
{
// We get current migration status
$current_migrations = $this->getMigrationTable()
->whereIn('migration', array_values($migrations))
->orderBy("created_at", "desc")
->orderBy("migration", "desc")
->get();

if (count($current_migrations) == 0) {
echo Color::green('Nothing to rollback.');

return;
}

// We sort current migration by created date value
usort($current_migrations, function ($first, $second) {
return strtotime($first->created_at) < strtotime($second->created_at);
});

foreach ($current_migrations as $value) {
foreach ($migrations as $file => $migration) {
if (
Expand Down Expand Up @@ -185,27 +184,24 @@ protected function makeRollback(array $current_migrations, array $migrations): v
/**
* Reset migration
*
* @param array $current_migration
* @param array $migrations
* @return void
*/
protected function makeReset(array $current_migrations, array $migrations): void
protected function makeReset(array $migrations): void
{
// We get current migration status
$current_migrations = $this->getMigrationTable()
->whereIn('migration', array_values($migrations))
->orderBy("created_at", "desc")
->orderBy("migration", "desc")
->get();

if (count($current_migrations) == 0) {
echo Color::green('Nothing to reset.');

return;
}

// We sort current migration by batch or created date value
usort($current_migrations, function ($first, $second) {
if ($first->batch == $second->batch) {
return strtotime($first->created_at) < strtotime($second->created_at);
}

return $first->batch > $second->batch;
});

foreach ($current_migrations as $value) {
foreach ($migrations as $file => $migration) {
if ($value->migration != $migration) {
Expand Down Expand Up @@ -298,11 +294,9 @@ private function createMigrationTable()
* Create migration status
*
* @param string $migration
* @param int $batch
*
* @return void
* @return int
*/
private function createMigrationStatus($migration)
private function createMigrationStatus(string $migration): int
{
$table = $this->getMigrationTable();

Expand All @@ -318,10 +312,9 @@ private function createMigrationStatus($migration)
*
* @param string $migration
* @param int $batch
*
* @return void
* @return int
*/
private function updateMigrationStatus($migration, $batch)
private function updateMigrationStatus(string $migration, int $batch): int
{
$table = $this->getMigrationTable();

Expand All @@ -337,7 +330,7 @@ private function updateMigrationStatus($migration, $batch)
* @param string $migration
* @return bool
*/
private function checkIfMigrationExist($migration)
private function checkIfMigrationExist(string $migration): bool
{
$result = $this->getMigrationTable()
->where('migration', $migration)
Expand Down

0 comments on commit 5923ffe

Please sign in to comment.