Passed
Push — master ( 0a3787...bc905b )
by Mihail
06:05
created

Apps/Console/MigrationUpCommand.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Apps\Console;
4
5
6
use Apps\ActiveRecord\Migration;
7
use Ffcms\Console\Command;
8
use Ffcms\Core\Helper\FileSystem\File;
9
use Ffcms\Core\Helper\Type\Any;
10
use Ffcms\Core\Helper\Type\Arr;
11
use Ffcms\Core\Helper\Type\Obj;
12
use Ffcms\Core\Helper\Type\Str;
13
use Ffcms\Core\Managers\MigrationsManager;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Class MigrationApplyCommand. Apply all migrations in db
20
 * @package Apps\Console
21
 */
22
class MigrationUpCommand extends Command
23
{
24
    /**
25
     * Set command
26
     */
27
    public function configure()
28
    {
29
        $this->setName('migration:up')
30
            ->setDescription('Apply all migrations to database')
31
            ->addArgument('name', InputArgument::OPTIONAL, 'Set search name or date (Y-m-d-H-i-s) for migration file');
32
    }
33
34
    /**
35
     * Apply all migrations into database without always apply'd
36
     * @param InputInterface $input
37
     * @param OutputInterface $output
38
     * @return void
39
     */
40
    public function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $name = $input->getArgument('name');
43
        // run migration manager - find migrations
44
        $manager = new MigrationsManager(null, $this->dbConnection);
45
        $search = $manager->search($name, false);
0 ignored issues
show
It seems like $name can also be of type string[]; however, parameter $query of Ffcms\Core\Managers\MigrationsManager::search() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $search = $manager->search(/** @scrutinizer ignore-type */ $name, false);
Loading history...
46
        if (!Any::isArray($search) || count($search) < 1) {
47
            $output->writeln('No migrations found');
48
            return;
49
        }
50
51
        // require confirmation from user each ever migration file
52
        $fired = false;
53
        foreach ($search as $migration) {
54
            if (!$this->confirm('Are you sure to apply migration: ' . $migration, true)) {
55
                continue;
56
            }
57
            $manager->makeUp($migration);
58
            $fired =  true;
59
        }
60
61
        if ($fired) {
62
            $output->writeln('All available migrations applied. Done.');
63
        } else {
64
            $output->writeln('No migrations executed.');
65
        }
66
    }
67
}