Skip to content

Commit abaa219

Browse files
authored
Support options batch for migrate:rollback. (#7531)
1 parent beffa3a commit abaa219

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-3
lines changed

src/Commands/Migrations/RollbackCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function handle()
4545
[
4646
'pretend' => $this->input->getOption('pretend'),
4747
'step' => (int) $this->input->getOption('step'),
48+
'batch' => (int) $this->input->getOption('batch'),
4849
]
4950
);
5051
}
@@ -63,6 +64,7 @@ protected function getOptions()
6364
['realpath', null, InputOption::VALUE_NONE, 'Indicate any provided migration file paths are pre-resolved absolute paths'],
6465
['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run'],
6566
['step', null, InputOption::VALUE_OPTIONAL, 'The number of migrations to be reverted'],
67+
['batch', null, InputOption::VALUE_OPTIONAL, 'The batch of migrations (identified by their batch number) to be reverted'],
6668
];
6769
}
6870
}

src/Migrations/DatabaseMigrationRepository.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ public function getMigrationBatches()
8888
->pluck('batch', 'migration')->all();
8989
}
9090

91+
/**
92+
* Get the list of the migrations by batch number.
93+
*
94+
* @param int $batch
95+
* @return array
96+
*/
97+
public function getMigrationsByBatch($batch)
98+
{
99+
return $this->table()
100+
->where('batch', $batch)
101+
->orderBy('migration', 'desc')
102+
->get()
103+
->all();
104+
}
105+
91106
/**
92107
* Log that a migration was run.
93108
*

src/Migrations/MigrationRepositoryInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public function getLast();
4343
*/
4444
public function getMigrationBatches();
4545

46+
/**
47+
* Get the list of the migrations by batch number.
48+
*
49+
* @param int $batch
50+
* @return array
51+
*/
52+
public function getMigrationsByBatch($batch);
53+
4654
/**
4755
* Log that a migration was run.
4856
*

src/Migrations/Migrator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ protected function getMigrationsForRollback(array $options): array
355355
return $this->repository->getMigrations($steps);
356356
}
357357

358+
if (($batch = $options['batch'] ?? 0) > 0) {
359+
return $this->repository->getMigrationsByBatch($batch);
360+
}
361+
358362
return $this->repository->getLast();
359363
}
360364

tests/DatabaseMigrationRepositoryTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,21 @@ public function testCreateRepositoryCreatesProperDatabaseTable()
139139
$repo->createRepository();
140140
}
141141

142+
public function testGetMigrationsByBatchReturnsCorrectMigrations()
143+
{
144+
$repo = $this->getRepository();
145+
$query = Mockery::mock(Builder::class);
146+
$connectionMock = Mockery::mock(Connection::class);
147+
$repo->getConnectionResolver()->shouldReceive('connection')->with(null)->andReturn($connectionMock);
148+
$repo->getConnection()->shouldReceive('table')->once()->with('migrations')->andReturn($query);
149+
$query->shouldReceive('where')->once()->with('batch', '5')->andReturn($query);
150+
$query->shouldReceive('orderBy')->once()->with('migration', 'desc')->andReturn($query);
151+
$query->shouldReceive('get')->once()->andReturn(new Collection(['migration2', 'migration1']));
152+
$query->shouldReceive('useWritePdo')->once()->andReturn($query);
153+
154+
$this->assertEquals(['migration2', 'migration1'], $repo->getMigrationsByBatch('5'));
155+
}
156+
142157
protected function getRepository()
143158
{
144159
return new DatabaseMigrationRepository(Mockery::mock(ConnectionResolverInterface::class), 'migrations');

tests/DatabaseMigrationRollbackCommandTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testRollbackCommandCallsMigratorWithProperArguments()
4949
$migrator->shouldReceive('paths')->once()->andReturn([]);
5050
$migrator->shouldReceive('setConnection')->once()->with('default');
5151
$migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
52-
$migrator->shouldReceive('rollback')->once()->with([BASE_PATH . DIRECTORY_SEPARATOR . 'migrations'], ['pretend' => false, 'step' => 0]);
52+
$migrator->shouldReceive('rollback')->once()->with([BASE_PATH . DIRECTORY_SEPARATOR . 'migrations'], ['pretend' => false, 'step' => 0, 'batch' => 0]);
5353

5454
$this->runCommand($command);
5555
}
@@ -60,11 +60,22 @@ public function testRollbackCommandCallsMigratorWithStepOption()
6060
$migrator->shouldReceive('paths')->once()->andReturn([]);
6161
$migrator->shouldReceive('setConnection')->once()->with('default');
6262
$migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
63-
$migrator->shouldReceive('rollback')->once()->with([BASE_PATH . DIRECTORY_SEPARATOR . 'migrations'], ['pretend' => false, 'step' => 2]);
63+
$migrator->shouldReceive('rollback')->once()->with([BASE_PATH . DIRECTORY_SEPARATOR . 'migrations'], ['pretend' => false, 'step' => 2, 'batch' => 0]);
6464

6565
$this->runCommand($command, ['--step' => 2]);
6666
}
6767

68+
public function testRollbackCommandCallsMigratorWithBatchOption()
69+
{
70+
$command = new RollbackCommand($migrator = Mockery::mock(Migrator::class));
71+
$migrator->shouldReceive('paths')->once()->andReturn([]);
72+
$migrator->shouldReceive('setConnection')->once()->with('default');
73+
$migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
74+
$migrator->shouldReceive('rollback')->once()->with([BASE_PATH . DIRECTORY_SEPARATOR . 'migrations'], ['pretend' => false, 'step' => 0, 'batch' => 2]);
75+
76+
$this->runCommand($command, ['--batch' => 2]);
77+
}
78+
6879
public function testRollbackCommandCanBePretended()
6980
{
7081
$command = new RollbackCommand($migrator = Mockery::mock(Migrator::class));
@@ -82,7 +93,7 @@ public function testRollbackCommandCanBePretendedWithStepOption()
8293
$migrator->shouldReceive('paths')->once()->andReturn([]);
8394
$migrator->shouldReceive('setConnection')->once()->with('foo');
8495
$migrator->shouldReceive('setOutput')->once()->andReturn($migrator);
85-
$migrator->shouldReceive('rollback')->once()->with([BASE_PATH . DIRECTORY_SEPARATOR . 'migrations'], ['pretend' => true, 'step' => 2]);
96+
$migrator->shouldReceive('rollback')->once()->with([BASE_PATH . DIRECTORY_SEPARATOR . 'migrations'], ['pretend' => true, 'step' => 2, 'batch' => 0]);
8697

8798
$this->runCommand($command, ['--pretend' => true, '--database' => 'foo', '--step' => 2]);
8899
}

0 commit comments

Comments
 (0)