1 | <?php |
||
2 | |||
3 | |||
4 | namespace Quantick\DeployMigration\Commands; |
||
5 | |||
6 | |||
7 | use Illuminate\Database\Connection; |
||
8 | use Illuminate\Database\Schema\Builder; |
||
9 | use Illuminate\Filesystem\Filesystem; |
||
10 | use Quantick\DeployMigration\Lib\DeployMigration; |
||
11 | use Quantick\DeployMigration\Lib\Service\Instantiator; |
||
12 | use Quantick\DeployMigration\Lib\Service\Migrator; |
||
13 | |||
14 | class RunDeployMigrationsCommand extends BaseDeployMigrationCommand |
||
15 | { |
||
16 | protected $signature = 'deploy:migrate'; |
||
17 | |||
18 | protected $description = 'Run migrate commands in deploy time'; |
||
19 | /** |
||
20 | * @var Connection |
||
21 | */ |
||
22 | private $connection; |
||
23 | /** |
||
24 | * @var Filesystem |
||
25 | */ |
||
26 | private $filesystem; |
||
27 | /** |
||
28 | * @var Builder |
||
29 | */ |
||
30 | private $schemaBuilder; |
||
31 | /** |
||
32 | * @var Migrator |
||
33 | */ |
||
34 | private $migrator; |
||
35 | |||
36 | /** |
||
37 | * DeployMigrateCommand constructor. |
||
38 | * @param Connection $connection |
||
39 | * @param Filesystem $filesystem |
||
40 | * @param Migrator $migrator |
||
41 | */ |
||
42 | public function __construct( |
||
43 | Connection $connection, |
||
44 | Filesystem $filesystem, |
||
45 | Migrator $migrator |
||
46 | ) |
||
47 | { |
||
48 | parent::__construct(); |
||
49 | $this->connection = $connection; |
||
50 | $this->schemaBuilder = $connection->getSchemaBuilder(); |
||
51 | $this->filesystem = $filesystem; |
||
52 | $this->migrator = $migrator; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @throws \ReflectionException |
||
57 | * @throws \Exception |
||
58 | * @throws \Throwable |
||
59 | */ |
||
60 | public function handle() |
||
61 | { |
||
62 | $this->prepareConsoleStyles(); |
||
63 | if ($this->hasMigrationTable() === false) { |
||
64 | throw new \RuntimeException('Table `deploy_migrations` not found. You probably should execute migrationFiles'); |
||
65 | } |
||
66 | |||
67 | $migrationsPath = $this->getMigrationsPath(); |
||
68 | $migrationFiles = $this->filesystem->files($migrationsPath); |
||
69 | |||
70 | $instantiator = Instantiator::create($migrationFiles); |
||
71 | |||
72 | $migrationCollection = $instantiator->run(); |
||
73 | |||
74 | $migrations = $migrationCollection->filter(function (DeployMigration $migration) { |
||
75 | $tableQuery = $this->getTableQuery(); |
||
76 | $alreadyExecuted = $tableQuery->where('migration', '=', get_class($migration))->count() > 0; |
||
77 | return $alreadyExecuted === false; |
||
78 | }); |
||
79 | |||
80 | $migrationsCount = count($migrations); |
||
81 | if ($migrationsCount === 0) { |
||
82 | $this->output->writeln('<info>Nothing to migrate...</info>'); |
||
83 | return; |
||
84 | } |
||
85 | |||
86 | $this->output->progressStart(count($migrations)); |
||
87 | |||
88 | try { |
||
89 | $this->migrator->run($migrations, $this->output); |
||
90 | } catch (\Throwable $e) { |
||
91 | throw $e; |
||
92 | } |
||
93 | |||
94 | $this->output->progressFinish(); |
||
95 | $this->output->writeln('<info>Deploy migrations finished</info>'); |
||
96 | } |
||
97 | |||
98 | private function hasMigrationTable(): bool |
||
99 | { |
||
100 | return $this->schemaBuilder->hasTable('deploy_migrations'); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @return string |
||
105 | */ |
||
106 | private function getMigrationsPath(): string |
||
107 | { |
||
108 | return config('deploy-migration.migration_path'); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @return \Illuminate\Database\Query\Builder |
||
113 | */ |
||
114 | private function getTableQuery(): \Illuminate\Database\Query\Builder |
||
115 | { |
||
116 | return $this->connection->table('deploy_migrations'); |
||
117 | } |
||
118 | } |