Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MatTheCat/1bf31a300eacbfa8f953e701f8c6f078 to your computer and use it in GitHub Desktop.
Save MatTheCat/1bf31a300eacbfa8f953e701f8c6f078 to your computer and use it in GitHub Desktop.
Symfony command checking if SQLite foreign keys are enabled for a given connection
<?php
namespace App;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Console\Attribute\AsCommand;
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 Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand('sqlite:check-foreign-key-support')]
class CheckSQLiteForeignKeySupportCommand extends Command
{
public function __construct(
private readonly ManagerRegistry $registry
) {
parent::__construct();
}
public function configure(): void
{
$this->addOption('connection', 'c', InputOption::VALUE_REQUIRED, 'Name of the connection to check');
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$style = new SymfonyStyle($input, $output);
if ($connectionName = $input->getOption('connection')) {
$connections = $this->registry->getConnectionNames();
if (!isset($connections[$connectionName])) {
$error = sprintf('“%s” connection does not exist. ', $connectionName);
$error .= match (count($connections)) {
0 => 'None has been configured.',
1 => 'You can remove the option to use the default one.',
default => sprintf('Try one of “%s”.', implode('”, “', array_keys($connections))),
};
$style->error($error);
return Command::FAILURE;
}
}
$connection = $this->registry->getConnection($connectionName);
assert($connection instanceof Connection);
$platformClass = $connection->getDatabasePlatform()::class;
if (SqlitePlatform::class !== $platformClass) {
$style->warning(sprintf(
'Got %s instead of SqlitePlatform, aborting.',
substr(strrchr($platformClass, '\\'), 1)
));
return Command::FAILURE;
}
match ($connection->executeQuery('PRAGMA foreign_keys')->fetchOne()) {
false => $style->error('Foreign keys are not supported.'),
0 => $style->warning('Foreign keys support is disabled.'),
1 => $style->success('Foreign keys support is enabled.'),
};
return Command::SUCCESS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment