Skip to content

Commit 9ffe94c

Browse files
committed
Initial commit for MigrateCleanCommand
1 parent c3ab9a6 commit 9ffe94c

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
/*
4+
* UserFrosting (http://www.userfrosting.com)
5+
*
6+
* @link https://github.com/userfrosting/UserFrosting
7+
* @copyright Copyright (c) 2019 Alexander Weissman
8+
* @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License)
9+
*/
10+
11+
namespace UserFrosting\Sprinkle\Core\Bakery;
12+
13+
use Illuminate\Support\Collection;
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Input\InputOption;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
18+
/**
19+
* migrate:rollback Bakery Command
20+
* Rollback the last migrations ran against the database.
21+
*
22+
* @author Louis Charette
23+
*/
24+
class MigrateCleanCommand extends MigrateCommand
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
protected function configure()
30+
{
31+
$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);
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
protected function execute(InputInterface $input, OutputInterface $output)
44+
{
45+
$this->io->title('Migration clean');
46+
47+
// Get migrator
48+
$migrator = $this->ci->migrator;
49+
50+
// Set connection to the selected database
51+
$migrator->setConnection($input->getOption('database'));
52+
53+
// Get ran migrations. If repository doesn't exist, there's no ran
54+
if (!$migrator->repositoryExists()) {
55+
$ran = collect();
56+
} else {
57+
$ran = $migrator->getRepository()->getMigrations();
58+
}
59+
60+
// Get available migrations
61+
$available = $migrator->getAvailableMigrations();
62+
63+
$stale = $this->getStaleRecords($ran, $available);
64+
// print_r($stale);
65+
66+
$this->cleanStaleRecords($stale, $migrator);
67+
68+
// Display ran migrations
69+
$this->io->section('Cleaned migrations');
70+
if ($ran->count() > 0) {
71+
} else {
72+
$this->io->note('No installed migrations');
73+
}
74+
}
75+
76+
protected function cleanStaleRecords(array $stale, $migrator)
77+
{
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+
}
84+
}
85+
86+
/**
87+
* Return an array of stale migrations.
88+
* A migration is stale if not found in the available stack (class is not in the Filesystem).
89+
*
90+
* @param Collection $ran The ran migrations
91+
* @param array $available The available migrations
92+
*
93+
* @return Collection Collection of stale migration classes.
94+
*/
95+
protected function getStaleRecords(Collection $ran, array $available)
96+
{
97+
return $filtered = collect($ran)->filter(function ($migration) use ($available) {
98+
return !in_array($migration->migration, $available);
99+
})->toArray();
100+
}
101+
}

0 commit comments

Comments
 (0)