43 lines
1.7 KiB
PHP
43 lines
1.7 KiB
PHP
|
|
<?php
|
||
|
|
use Symfony\Component\Console\Command\Command;
|
||
|
|
use Symfony\Component\Console\Input\InputInterface;
|
||
|
|
use Symfony\Component\Console\Input\InputOption;
|
||
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
||
|
|
use App\Services\Migrator;
|
||
|
|
|
||
|
|
class MigrateCommand extends Command
|
||
|
|
{
|
||
|
|
protected function configure()
|
||
|
|
{
|
||
|
|
$this->setName('db:migrate')
|
||
|
|
->setDescription('Apply pending SQL migrations from db/migrations/')
|
||
|
|
->addOption('status', null, InputOption::VALUE_NONE, 'Show applied/pending without applying');
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||
|
|
{
|
||
|
|
$base = dirname(__DIR__); // api/
|
||
|
|
if (!is_file($base . '/config.php')) {
|
||
|
|
$output->writeln('<error>config.php missing — run app:install first.</error>');
|
||
|
|
return Command::FAILURE;
|
||
|
|
}
|
||
|
|
require_once $base . '/config.php'; // defines DB_* constants
|
||
|
|
$pdo = new PDO(
|
||
|
|
'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=utf8mb4',
|
||
|
|
DB_USER, DB_PASS,
|
||
|
|
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
|
||
|
|
);
|
||
|
|
$migrator = new Migrator($pdo, $base . '/db/migrations');
|
||
|
|
$migrator->ensureTable();
|
||
|
|
|
||
|
|
if ($input->getOption('status')) {
|
||
|
|
$output->writeln('Applied: ' . (implode(', ', $migrator->applied()) ?: '(none)'));
|
||
|
|
$output->writeln('Pending: ' . (implode(', ', array_map('basename', $migrator->pending())) ?: '(none)'));
|
||
|
|
return Command::SUCCESS;
|
||
|
|
}
|
||
|
|
$done = $migrator->migrate();
|
||
|
|
$output->writeln($done ? 'Applied: ' . implode(', ', $done) : 'Nothing to migrate.');
|
||
|
|
return Command::SUCCESS;
|
||
|
|
}
|
||
|
|
}
|