|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Simple\Queue\Store; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
8
|
|
|
use Doctrine\DBAL\Types\Types; |
|
9
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class DoctrineDbalTableCreator |
|
13
|
|
|
* @package Simple\Queue\Store |
|
14
|
|
|
*/ |
|
15
|
|
|
class DoctrineDbalTableCreator |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Connection */ |
|
18
|
|
|
protected Connection $connection; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
protected static string $tableName = 'queue'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* QueueTableCreator constructor. |
|
25
|
|
|
* @param Connection $connection |
|
26
|
|
|
*/ |
|
27
|
3 |
|
public function __construct(Connection $connection) |
|
28
|
|
|
{ |
|
29
|
3 |
|
$this->connection = $connection; |
|
30
|
3 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $tableName |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public static function changeTableName(string $tableName): void |
|
36
|
|
|
{ |
|
37
|
1 |
|
self::$tableName = $tableName; |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
12 |
|
public static function getTableName(): string |
|
44
|
|
|
{ |
|
45
|
12 |
|
return self::$tableName; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Creating a queue table |
|
50
|
|
|
* |
|
51
|
|
|
* @throws \Doctrine\DBAL\Exception |
|
52
|
|
|
* @throws \Doctrine\DBAL\Schema\SchemaException |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public function createDataBaseTable(): void |
|
55
|
|
|
{ |
|
56
|
3 |
|
$schemaManager = $this->connection->getSchemaManager(); |
|
57
|
|
|
|
|
58
|
3 |
|
$tableExists = $schemaManager ? $schemaManager->tablesExist([self::getTableName()]) : false; |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
3 |
|
if ($schemaManager === null || $tableExists === true) { |
|
61
|
1 |
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
$table = new Table(self::getTableName()); |
|
65
|
|
|
|
|
66
|
2 |
|
$table->addColumn('id', Types::GUID, ['length' => 16, 'fixed' => true]); |
|
67
|
2 |
|
$table->addColumn('status', Types::STRING); |
|
68
|
2 |
|
$table->addColumn('attempts', Types::SMALLINT); |
|
69
|
2 |
|
$table->addColumn('queue', Types::STRING); |
|
70
|
2 |
|
$table->addColumn('event', Types::STRING, ['notnull' => false]); |
|
71
|
2 |
|
$table->addColumn('is_job', Types::BOOLEAN, ['default' => false]); |
|
72
|
2 |
|
$table->addColumn('body', Types::TEXT, ['notnull' => false]); |
|
73
|
2 |
|
$table->addColumn('priority', Types::SMALLINT, ['notnull' => false]); |
|
74
|
2 |
|
$table->addColumn('error', Types::TEXT, ['notnull' => false]); |
|
75
|
2 |
|
$table->addColumn('redelivered_at', Types::DATETIME_IMMUTABLE, ['notnull' => false]); |
|
76
|
2 |
|
$table->addColumn('created_at', Types::DATETIME_IMMUTABLE); |
|
77
|
2 |
|
$table->addColumn('exact_time', Types::BIGINT); |
|
78
|
|
|
|
|
79
|
2 |
|
$table->setPrimaryKey(['id']); |
|
80
|
2 |
|
$table->addIndex(['priority', 'created_at', 'queue', 'status', 'event', 'id']); |
|
81
|
|
|
|
|
82
|
2 |
|
$schemaManager->createTable($table); |
|
83
|
2 |
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|