Skip to content

Commit e24d370

Browse files
committed
Finished draft of MigrateCleanCommand.php
Also updated messages in Migrator classes that command can be run to remove stale migrations.
1 parent 9ffe94c commit e24d370

3 files changed

Lines changed: 32 additions & 25 deletions

File tree

app/sprinkles/core/src/Bakery/MigrateCleanCommand.php

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
use Symfony\Component\Console\Input\InputInterface;
1515
use Symfony\Component\Console\Input\InputOption;
1616
use Symfony\Component\Console\Output\OutputInterface;
17+
use UserFrosting\Sprinkle\Core\Database\Migrator\Migrator;
1718

1819
/**
19-
* migrate:rollback Bakery Command
20-
* Rollback the last migrations ran against the database.
20+
* migrate:clean Bakery Command
21+
* Remove stale migrations from the database.
2122
*
22-
* @author Louis Charette
23+
* @author Amos Folz
2324
*/
2425
class MigrateCleanCommand extends MigrateCommand
2526
{
@@ -29,12 +30,8 @@ class MigrateCleanCommand extends MigrateCommand
2930
protected function configure()
3031
{
3132
$this->setName('migrate:clean')
32-
->setDescription('Clean stale records from migrations database')
33-
->addOption('pretend', 'p', InputOption::VALUE_NONE, 'Run migrations in "dry run" mode.')
34-
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force the operation to run when in production.')
35-
->addOption('database', 'd', InputOption::VALUE_REQUIRED, 'The database connection to use.')
36-
->addOption('migration', 'm', InputOption::VALUE_REQUIRED, 'The specific migration to rollback.')
37-
->addOption('steps', 's', InputOption::VALUE_REQUIRED, 'Number of batch to rollback.', 1);
33+
->setDescription('Remove stale migrations from the database.')
34+
->addOption('database', 'd', InputOption::VALUE_REQUIRED, 'The database connection to use.');
3835
}
3936

4037
/**
@@ -61,26 +58,36 @@ protected function execute(InputInterface $input, OutputInterface $output)
6158
$available = $migrator->getAvailableMigrations();
6259

6360
$stale = $this->getStaleRecords($ran, $available);
64-
// print_r($stale);
6561

66-
$this->cleanStaleRecords($stale, $migrator);
62+
if ($stale->count() > 0) {
63+
$this->io->section('Stale migrations');
64+
$this->io->listing($stale->toArray());
6765

68-
// Display ran migrations
69-
$this->io->section('Cleaned migrations');
70-
if ($ran->count() > 0) {
66+
if (!$this->io->confirm('Continue and remove stale migrations?', false)) {
67+
exit;
68+
}
69+
$this->io->section('Cleaned migrations');
70+
$this->cleanStaleRecords($stale, $migrator);
71+
$this->io->listing($stale->toArray());
7172
} else {
72-
$this->io->note('No installed migrations');
73+
$this->io->note('No stale migrations');
7374
}
7475
}
7576

76-
protected function cleanStaleRecords(array $stale, $migrator)
77+
/**
78+
* Delete stale migrations from the database.
79+
*
80+
* @param Collection $stale Collection of stale migartion classes.
81+
* @param Migrator $migrator Migrator object
82+
*/
83+
protected function cleanStaleRecords(Collection $stale, Migrator $migrator)
7784
{
78-
// print_r($stale);
79-
foreach ($stale as $staleFile) {
80-
print_r($staleFile);
81-
print_r($staleFile['migration']);
82-
$migrator->$repository->delete($staleFile['migration']);
83-
}
85+
$migrationRepository = $migrator->getRepository();
86+
87+
//Delete the stale migration classes from the database.
88+
$stale->each(function ($class) use ($migrationRepository) {
89+
$migrationRepository->delete($class);
90+
});
8491
}
8592

8693
/**
@@ -96,6 +103,6 @@ protected function getStaleRecords(Collection $ran, array $available)
96103
{
97104
return $filtered = collect($ran)->filter(function ($migration) use ($available) {
98105
return !in_array($migration->migration, $available);
99-
})->toArray();
106+
})->pluck('migration');
100107
}
101108
}

app/sprinkles/core/src/Database/Migrator/MigrationDependencyAnalyser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ protected function getMigrationDependencies($migration)
210210

211211
// Make sure class exists
212212
if (!class_exists($migration)) {
213-
throw new BadClassNameException("Unable to find the migration class '$migration'.");
213+
throw new BadClassNameException("Unable to find the migration class '$migration'. Run 'php bakery migrate:clean' to remove stale migrations.");
214214
}
215215

216216
// If the `dependencies` property exist and is static, use this one.

app/sprinkles/core/src/Database/Migrator/Migrator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ protected function getQueries(MigrationInterface $migration, $method)
453453
public function resolve($migrationClassName)
454454
{
455455
if (!class_exists($migrationClassName)) {
456-
throw new BadClassNameException("Unable to find the migration class '$migrationClassName'.");
456+
throw new BadClassNameException("Unable to find the migration class '$migrationClassName'. Run 'php bakery migrate:clean' to remove stale migrations.");
457457
}
458458

459459
$migration = new $migrationClassName($this->getSchemaBuilder());

0 commit comments

Comments
 (0)