-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractDoctrineSchemaCommand.php
More file actions
60 lines (47 loc) · 1.48 KB
/
AbstractDoctrineSchemaCommand.php
File metadata and controls
60 lines (47 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
namespace Macpaw\PostgresSchemaBundle\Command\Doctrine;
use Doctrine\DBAL\Connection;
use Error;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
abstract class AbstractDoctrineSchemaCommand extends Command
{
public function __construct(
string $commandName,
protected readonly Connection $connection,
) {
parent::__construct($commandName);
}
protected function configure(): void
{
$this->addArgument(
'schema',
InputArgument::REQUIRED,
'The schema name.',
);
parent::configure();
}
protected function getSchemaFromInput(InputInterface $input): string
{
$schema = $input->getArgument('schema');
if (!is_string($schema) || $schema === '') {
throw new Error('Schema name must be a non-empty string');
}
return $schema;
}
protected function isSchemaExist(string $schema): bool
{
$exists = $this->connection->fetchOne(
'SELECT EXISTS (SELECT 1 FROM pg_namespace WHERE nspname = ?)',
[$schema],
);
return (bool) $exists;
}
protected function switchToSchema(string $schema): void
{
$quotedSchema = $this->connection->quoteIdentifier($schema);
$this->connection->executeStatement("SET search_path TO {$quotedSchema}");
}
}