Skip to content

Commit c16ac33

Browse files
authored
Add count option to migrate (#2360)
1 parent 2afc9e6 commit c16ac33

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

src/Phinx/Console/Command/Migrate.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ protected function configure(): void
3838
$this->setDescription('Migrate the database')
3939
->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to migrate to')
4040
->addOption('--date', '-d', InputOption::VALUE_REQUIRED, 'The date to migrate to')
41+
->addOption('--count', '-k', InputOption::VALUE_REQUIRED, 'The number of migrations to run')
4142
->addOption('--dry-run', '-x', InputOption::VALUE_NONE, 'Dump query to standard output instead of executing it')
4243
->addOption('--fake', null, InputOption::VALUE_NONE, "Mark any migrations selected as run, but don't actually execute them")
4344
->setHelp(
4445
<<<EOT
45-
The <info>migrate</info> command runs all available migrations, optionally up to a specific version
46+
The <info>migrate</info> command runs all available migrations, optionally up to a specific version, date, or count.
4647
4748
<info>phinx migrate -e development</info>
4849
<info>phinx migrate -e development -t 20110103081132</info>
4950
<info>phinx migrate -e development -d 20110103</info>
51+
<info>phinx migrate -e development -k 5</info>
5052
<info>phinx migrate -e development -v</info>
5153
5254
EOT,
@@ -68,6 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6870
/** @var string|null $environment */
6971
$environment = $input->getOption('environment');
7072
$date = $input->getOption('date');
73+
$count = $input->getOption('count') !== null ? (int)$input->getOption('count') : null;
7174
$fake = (bool)$input->getOption('fake');
7275

7376
$success = $this->writeInformationOutput($environment, $output);
@@ -85,7 +88,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8588
try {
8689
// run the migrations
8790
$start = microtime(true);
88-
if ($date !== null) {
91+
if ($count !== null) {
92+
$this->getManager()->migrateToCount($environment, $count, $fake);
93+
} elseif ($date !== null) {
8994
$this->getManager()->migrateToDateTime($environment, new DateTime($date), $fake);
9095
} else {
9196
$this->getManager()->migrate($environment, $version, $fake);

src/Phinx/Migration/Manager.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,30 @@ public function migrateToDateTime(string $environment, DateTime $dateTime, bool
301301
}
302302
}
303303

304+
/**
305+
* Migrate an environment to a specific number of migrations.
306+
*
307+
* @param string $environment Environment
308+
* @param int $count Number of migrations to apply
309+
* @param bool $fake flag that if true, we just record running the migration, but not actually do the migration
310+
* @return void
311+
*/
312+
public function migrateToCount(string $environment, int $count, bool $fake = false): void
313+
{
314+
$versions = array_keys($this->getMigrations($environment));
315+
$env = $this->getEnvironment($environment);
316+
$current = $env->getCurrentVersion();
317+
318+
if ($current === 0) {
319+
$version = $versions[$count - 1];
320+
} else {
321+
$currentIdx = array_search($current, $versions, true);
322+
$version = $versions[min($currentIdx + $count, count($versions) - 1)];
323+
}
324+
325+
$this->migrate($environment, $version, $fake);
326+
}
327+
304328
/**
305329
* Migrate an environment to the specified version.
306330
*

tests/Phinx/Console/Command/MigrateTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,38 @@ public function testExecuteWithDate(): void
332332
$this->assertSame(AbstractCommand::CODE_SUCCESS, $exitCode);
333333
}
334334

335+
public function testExecuteWithCount(): void
336+
{
337+
$application = new PhinxApplication();
338+
$application->add(new Migrate());
339+
340+
/** @var Migrate $command */
341+
$command = $application->find('migrate');
342+
343+
// mock the manager class
344+
/** @var Manager&MockObject $managerStub */
345+
$managerStub = $this->getMockBuilder(Manager::class)
346+
->setConstructorArgs([$this->config, $this->input, $this->output])
347+
->getMock();
348+
$managerStub->expects($this->never())
349+
->method('migrate');
350+
$managerStub->expects($this->once())
351+
->method('migrateToCount')
352+
->with('development', 5, false);
353+
354+
$command->setConfig($this->config);
355+
$command->setManager($managerStub);
356+
357+
$commandTester = new CommandTester($command);
358+
$exitCode = $commandTester->execute(
359+
['command' => $command->getName(), '--environment' => 'development', '--count' => 5],
360+
['decorated' => false],
361+
);
362+
363+
$this->assertStringContainsString('using environment development', $commandTester->getDisplay());
364+
$this->assertSame(AbstractCommand::CODE_SUCCESS, $exitCode);
365+
}
366+
335367
public function testExecuteWithError(): void
336368
{
337369
$exception = new RuntimeException('oops');

tests/Phinx/Migration/ManagerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,24 @@ public function testRollbackToVersion($availableRollbacks, $version, $expectedOu
13621362
}
13631363
}
13641364

1365+
public function testMigrationByCount(): void
1366+
{
1367+
$adapter = $this->prepareEnvironment([
1368+
'migrations' => $this->getCorrectedPath(__DIR__ . '/_files/reversiblemigrations'),
1369+
]);
1370+
1371+
$this->manager->migrateToCount('production', 2);
1372+
1373+
$this->assertTrue($adapter->hasTable('info'));
1374+
$this->assertFalse($adapter->hasTable('statuses'));
1375+
$this->assertTrue($adapter->hasTable('users'));
1376+
1377+
$this->manager->migrateToCount('production', 1);
1378+
$this->assertFalse($adapter->hasTable('info'));
1379+
$this->assertTrue($adapter->hasTable('statuses'));
1380+
$this->assertTrue($adapter->hasTable('users'));
1381+
}
1382+
13651383
/**
13661384
* Test that rollbacking to version chooses the correct
13671385
* migration (with namespace) to point to.

0 commit comments

Comments
 (0)