|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Messenger\Bridge\Doctrine\Transport; |
| 13 | + |
| 14 | +use Doctrine\DBAL\Connection; |
| 15 | +use Doctrine\DBAL\Schema\Schema; |
| 16 | +use Doctrine\DBAL\Types\Type; |
| 17 | +use Doctrine\DBAL\Types\Types; |
| 18 | + |
| 19 | +final class Migration |
| 20 | +{ |
| 21 | + public static function up(Schema $schema, Connection $connection, string $tableName = 'messenger_messages'): void |
| 22 | + { |
| 23 | + $useDeprecatedConstants = !class_exists(Types::class); |
| 24 | + $table = $schema->createTable($tableName); |
| 25 | + $table->addColumn('id', $useDeprecatedConstants ? Type::BIGINT : Types::BIGINT)->setAutoincrement(true)->setNotnull(true); |
| 26 | + $table->addColumn('body', $useDeprecatedConstants ? Type::TEXT : Types::TEXT)->setNotnull(true); |
| 27 | + $table->addColumn('headers', $useDeprecatedConstants ? Type::TEXT : Types::TEXT)->setNotnull(true); |
| 28 | + $table->addColumn('queue_name', $useDeprecatedConstants ? Type::STRING : Types::STRING)->setNotnull(true); |
| 29 | + $table->addColumn('created_at', $useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE)->setNotnull(true); |
| 30 | + $table->addColumn('available_at', $useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE)->setNotnull(true); |
| 31 | + $table->addColumn('delivered_at', $useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE)->setNotnull(false); |
| 32 | + $table->setPrimaryKey(['id']); |
| 33 | + $table->addIndex(['queue_name']); |
| 34 | + $table->addIndex(['available_at']); |
| 35 | + $table->addIndex(['delivered_at']); |
| 36 | + |
| 37 | + if ('postgresql' === $connection->getDatabasePlatform()->getName()) { |
| 38 | + $sql = sprintf(<<<'SQL' |
| 39 | +LOCK TABLE %1$s; |
| 40 | +-- create trigger function |
| 41 | +CREATE OR REPLACE FUNCTION notify_%1$s() RETURNS TRIGGER AS $$ |
| 42 | + BEGIN |
| 43 | + PERFORM pg_notify('%1$s', NEW.queue_name::text); |
| 44 | + RETURN NEW; |
| 45 | + END; |
| 46 | +$$ LANGUAGE plpgsql; |
| 47 | +
|
| 48 | +-- register trigger |
| 49 | +DROP TRIGGER IF EXISTS notify_trigger ON %1$s; |
| 50 | +
|
| 51 | +CREATE TRIGGER notify_trigger |
| 52 | +AFTER INSERT |
| 53 | +ON %1$s |
| 54 | +FOR EACH ROW EXECUTE PROCEDURE notify_%1$s(); |
| 55 | +SQL |
| 56 | + , $tableName); |
| 57 | + |
| 58 | + $connection->exec($sql); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments