1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gendoria\CommandQueueDoctrineDriverBundle\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Doctrine\DBAL\Schema\Schema; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Create database schema command. |
13
|
|
|
* |
14
|
|
|
* @author Tomasz Struczyński <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class CreateSchemaCommand extends ContainerAwareCommand |
17
|
|
|
{ |
18
|
|
|
|
19
|
2 |
|
protected function configure() |
20
|
|
|
{ |
21
|
2 |
|
$this->setName('cmq:doctrine:schema-create') |
22
|
2 |
|
->setDescription("Create database schema for doctrine command queue") |
23
|
2 |
|
->setHelp(<<<EOT |
24
|
|
|
The <info>%command.name%</info> command creates tables for pools defined in your configuration: |
25
|
|
|
|
26
|
|
|
<info>php %command.full_name%</info> |
27
|
|
|
|
28
|
|
|
Only non existing tables will be created. Existing tables ill not be modified. |
29
|
|
|
EOT |
30
|
2 |
|
) |
31
|
|
|
; |
32
|
2 |
|
} |
33
|
|
|
|
34
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
35
|
|
|
{ |
36
|
2 |
|
if (!$this->getContainer()->hasParameter('gendoria_command_queue_doctrine_driver.drivers')) { |
37
|
1 |
|
$output->writeln("<error>Bundle configuration not present.</error>"); |
38
|
1 |
|
return 1; |
39
|
|
|
} |
40
|
1 |
|
$connections = $this->prepareConnectionsAndTables(); |
41
|
1 |
|
foreach ($connections as $connection) { |
42
|
1 |
|
foreach ($connection['tables'] as $tableName) { |
43
|
1 |
|
$this->createTable($connection['driver'], $tableName); |
44
|
1 |
|
} |
45
|
1 |
|
} |
46
|
1 |
|
} |
47
|
|
|
|
48
|
1 |
|
private function prepareConnectionsAndTables() |
49
|
|
|
{ |
50
|
1 |
|
$connections = array(); |
51
|
1 |
|
$container = $this->getContainer(); |
52
|
1 |
|
$drivers = $container->getParameter('gendoria_command_queue_doctrine_driver.drivers'); |
53
|
1 |
|
foreach ($drivers as $driver) { |
54
|
1 |
|
$connectionService = sprintf('doctrine.dbal.%s_connection', $driver['connection']); |
55
|
1 |
|
if (empty($connections[$connectionService])) { |
56
|
1 |
|
$connections[$connectionService] = array( |
57
|
1 |
|
'driver' => $container->get($connectionService), |
58
|
1 |
|
'tables' => array(), |
59
|
|
|
); |
60
|
1 |
|
} |
61
|
1 |
|
$connections[$connectionService]['tables'][$driver['table_name']] = $driver['table_name']; |
62
|
1 |
|
} |
63
|
1 |
|
return $connections; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
private function createTable(Connection $connection, $tableName) |
67
|
|
|
{ |
68
|
1 |
|
$schemaManager = $connection->getSchemaManager(); |
69
|
1 |
|
if ($schemaManager->tablesExist(array($tableName))) { |
70
|
1 |
|
return; |
71
|
|
|
} |
72
|
|
|
/* @var $schema Schema */ |
73
|
1 |
|
$schema = $schemaManager->createSchema(); |
74
|
1 |
|
$table = $schema->createTable($tableName); |
75
|
1 |
|
$table->addColumn('id', 'bigint', array('unsigned' => true, 'autoincrement' => true)); |
76
|
1 |
|
$table->addColumn('command_class', 'string', array('length' => 255)); |
77
|
1 |
|
$table->addColumn('command', 'blob'); |
78
|
1 |
|
$table->addColumn('pool', 'string', array('length' => 100)); |
79
|
1 |
|
$table->addColumn('failed_no', 'smallint', array('default' => 0)); |
80
|
1 |
|
$table->addColumn('processed', 'boolean', array('default' => 0)); |
81
|
1 |
|
$table->addColumn('process_after', 'datetime'); |
82
|
1 |
|
$table->setPrimaryKey(array('id')); |
83
|
1 |
|
$table->addIndex(array('pool', 'processed', 'process_after')); |
84
|
|
|
|
85
|
1 |
|
$schemaManager->createTable($table); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|