1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Console\Commands;
7
8use Illuminate\Console\Command;
9
10class MigrateFreshOrRunCommand extends Command
11{
12 protected $signature = 'migrate:fresh-or-run';
13
14 protected $description = 'Run a migrate:fresh on empty database or just run plain migrate otherwise';
15
16 protected $migrator;
17
18 public function __construct()
19 {
20 $this->migrator = app('migration.repository');
21
22 parent::__construct();
23 }
24
25 /**
26 * Execute the console command.
27 *
28 * @return void
29 */
30 public function handle()
31 {
32 if (!$this->migrator->repositoryExists() || $this->migrator->getLastBatchNumber() === null) {
33 $this->fresh();
34 } else {
35 $this->migrate();
36 }
37 }
38
39 private function fresh()
40 {
41 $this->info('Database is empty. Calling migrate:fresh to initalise database and elasticsearch.');
42 $this->call('migrate:fresh', ['--no-interaction' => true]);
43 }
44
45 private function migrate()
46 {
47 $this->info('Running pending migrations...');
48 $this->call('migrate', ['--step' => true]);
49 }
50}