Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] [Doctrine] Cannot generate migration, if no entity nor mapping infos available #39928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
OskarStark opened this issue Jan 21, 2021 · 13 comments
Milestone

Comments

@OskarStark
Copy link
Contributor

OskarStark commented Jan 21, 2021

Symfony version(s) affected: 5.1.10

PHP version:

PHP 8.0.1 (cli) (built: Jan  8 2021 02:39:34) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.1, Copyright (c), by Zend Technologies

Installed packages:

composer/package-versions-deprecated 1.11.99.1 Composer plugin that provides efficient querying for installed package versions (no runtime IO)
composer/semver                      3.2.4     Semver library that offers utilities, version constraint parsing and validation.
composer/xdebug-handler              1.4.5     Restarts a process without Xdebug.
dama/doctrine-test-bundle            v6.5.0    Symfony bundle to isolate doctrine database tests and improve test performance
datana-gmbh/php-cs-fixer-config      1.1.0     Provides a configuration factory and multiple rule sets for friendsofphp/php-cs-fixer.
doctrine/annotations                 1.11.1    Docblock Annotations Parser
doctrine/cache                       1.10.2    PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and ...
doctrine/collections                 1.6.7     PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.
doctrine/common                      3.1.0     PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better reflec...
doctrine/dbal                        2.12.1    Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
doctrine/doctrine-bundle             2.2.2     Symfony DoctrineBundle
doctrine/doctrine-migrations-bundle  3.0.2     Symfony DoctrineMigrationsBundle
doctrine/event-manager               1.1.1     The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.
doctrine/inflector                   2.0.3     PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowercase and singular/plural forms of ...
doctrine/instantiator                1.4.0     A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                       1.2.1     PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.
doctrine/migrations                  3.0.2     PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database ...
doctrine/orm                         2.8.1     Object-Relational-Mapper for PHP
doctrine/persistence                 2.1.0     The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share.
doctrine/sql-formatter               1.1.1     a PHP SQL highlighting library
egulias/email-validator              2.1.25    A library for validating emails against several RFCs
ergebnis/classy                      1.1.0     Provides a finder for classy constructs (classes, interfaces, and traits).
ergebnis/clock                       2.2.0     Provides a simple abstraction of a clock.
ergebnis/composer-normalize          2.13.2    Provides a composer plugin for normalizing composer.json.
ergebnis/factory-bot                 0.4.0     Provides a fixture factory for doctrine/orm entities.
ergebnis/json-normalizer             1.0.2     Provides generic and vendor-specific normalizers for normalizing JSON documents.
ergebnis/json-printer                3.1.1     Provides a JSON printer, allowing for flexible indentation.
ergebnis/test-util                   1.4.0     Provides a helper trait and generic data providers for tests.
fakerphp/faker                       v1.13.0   Faker is a PHP library that generates fake data for you.
friendsofphp/php-cs-fixer            v2.18.0   A tool to automatically fix PHP code style
friendsofphp/proxy-manager-lts       v1.0.2    Adding support for a wider range of PHP versions to ocramius/proxy-manager
justinrainbow/json-schema            5.2.10    A library to validate a json schema.
laminas/laminas-code                 4.0.0     Extensions to the PHP Reflection API, static code scanning, and code generation
laminas/laminas-eventmanager         3.3.0     Trigger and listen to events within a PHP application
laminas/laminas-zendframework-bridge 1.1.1     Alias legacy ZF class names to Laminas Project equivalents.
localheinz/diff                      1.1.1     Fork of sebastian/diff for use with ergebnis/composer-normalize
monolog/monolog                      2.2.0     Sends your logs to files, sockets, inboxes, databases and various web services
myclabs/deep-copy                    1.10.2    Create deep copies (clones) of your objects
nikic/php-parser                     v4.10.4   A PHP parser written in PHP
phar-io/manifest                     2.0.1     Component for reading phar.io manifest information from a PHP Archive (PHAR)
phar-io/version                      3.0.4     Library for handling version information and constraints
php-cs-fixer/diff                    v1.3.1    sebastian/diff v2 backport support for PHP5.6
phpdocumentor/reflection-common      2.2.0     Common reflection classes used by phpdocumentor to reflect the code structure
phpdocumentor/reflection-docblock    5.2.2     With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a Doc...
phpdocumentor/type-resolver          1.4.0     A PSR-5 based resolver of Class names, Types and Structural Element Names
...
psr/cache                            1.0.1     Common interface for caching libraries
psr/container                        1.0.0     Common Container Interface (PHP FIG PSR-11)
psr/event-dispatcher                 1.0.0     Standard interfaces for event handling.
psr/link                             1.0.0     Common interfaces for HTTP links
psr/log                              1.1.3     Common interface for logging libraries
...
sensio/framework-extra-bundle        v5.6.1    This bundle provides a way to configure your controllers with annotations
symfony/amqp-messenger               v5.2.1    Symfony AMQP extension Messenger Bridge
symfony/asset                        v5.2.1    Symfony Asset Component
symfony/browser-kit                  v5.2.1    Symfony BrowserKit Component
symfony/cache                        v5.2.1    Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts              v2.2.0    Generic abstractions related to caching
symfony/config                       v5.2.1    Symfony Config Component
symfony/console                      v5.2.1    Symfony Console Component
symfony/css-selector                 v5.2.1    Symfony CssSelector Component
symfony/debug-bundle                 v5.2.1    Symfony DebugBundle
symfony/dependency-injection         v5.2.1    Symfony DependencyInjection Component
symfony/deprecation-contracts        v2.2.0    A generic function and convention to trigger deprecation notices
symfony/doctrine-bridge              v5.2.1    Symfony Doctrine Bridge
symfony/doctrine-messenger           v5.2.1    Symfony Doctrine Messenger Bridge
symfony/dom-crawler                  v5.2.1    Symfony DomCrawler Component
symfony/dotenv                       v5.2.1    Registers environment variables from a .env file
symfony/error-handler                v5.2.1    Symfony ErrorHandler Component
symfony/event-dispatcher             v5.2.1    Symfony EventDispatcher Component
symfony/event-dispatcher-contracts   v2.2.0    Generic abstractions related to dispatching event
symfony/expression-language          v5.2.1    Symfony ExpressionLanguage Component
symfony/filesystem                   v5.2.1    Symfony Filesystem Component
symfony/finder                       v5.2.1    Symfony Finder Component
symfony/flex                         v1.11.0   Composer plugin for Symfony
symfony/form                         v5.2.1    Symfony Form Component
symfony/framework-bundle             v5.2.1    Symfony FrameworkBundle
symfony/http-client                  v5.2.1    Symfony HttpClient component
symfony/http-client-contracts        v2.3.1    Generic abstractions related to HTTP clients
symfony/http-foundation              v5.2.1    Symfony HttpFoundation Component
symfony/http-kernel                  v5.2.1    Symfony HttpKernel Component
symfony/intl                         v5.2.1    A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/mailer                       v5.2.1    Symfony Mailer Component
symfony/maker-bundle                 v1.28.0   Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/messenger                    v5.2.1    Symfony Messenger Component
symfony/mime                         v5.2.1    A library to manipulate MIME messages
symfony/monolog-bridge               v5.2.1    Symfony Monolog Bridge
symfony/monolog-bundle               v3.6.0    Symfony MonologBundle
symfony/notifier                     v5.2.1    A library to notify messages
symfony/options-resolver             v5.2.1    Symfony OptionsResolver Component
symfony/phpunit-bridge               v5.2.1    Symfony PHPUnit Bridge
symfony/polyfill-intl-grapheme       v1.22.0   Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu            v1.20.0   Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn            v1.22.0   Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer     v1.22.0   Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring            v1.22.0   Symfony polyfill for the Mbstring extension
symfony/polyfill-php70               v1.20.0   Symfony polyfill backporting some PHP 7.0+ features to lower PHP versions
symfony/polyfill-php73               v1.22.0   Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-php80               v1.22.0   Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/process                      v5.2.1    Symfony Process Component
symfony/property-access              v5.2.1    Symfony PropertyAccess Component
symfony/property-info                v5.2.1    Symfony Property Info Component
symfony/proxy-manager-bridge         v5.2.1    Symfony ProxyManager Bridge
symfony/redis-messenger              v5.2.1    Symfony Redis extension Messenger Bridge
symfony/routing                      v5.2.1    Symfony Routing Component
symfony/security-bundle              v5.2.1    Symfony SecurityBundle
symfony/security-core                v5.2.1    Symfony Security Component - Core Library
symfony/security-csrf                v5.2.1    Symfony Security Component - CSRF Library
symfony/security-guard               v5.2.1    Symfony Security Component - Guard
symfony/security-http                v5.2.1    Symfony Security Component - HTTP Integration
symfony/serializer                   v5.2.1    Symfony Serializer Component
symfony/service-contracts            v2.2.0    Generic abstractions related to writing services
symfony/stopwatch                    v5.2.1    Symfony Stopwatch Component
symfony/string                       v5.2.1    Symfony String component
symfony/translation                  v5.2.1    Symfony Translation Component
symfony/translation-contracts        v2.3.0    Generic abstractions related to translation
symfony/twig-bridge                  v5.2.1    Symfony Twig Bridge
symfony/twig-bundle                  v5.2.1    Symfony TwigBundle
symfony/validator                    v5.2.1    Symfony Validator Component
symfony/var-dumper                   v5.2.1    Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter                 v5.2.1    A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-link                     v5.2.1    Symfony WebLink Component
symfony/web-profiler-bundle          v5.2.1    Symfony WebProfilerBundle
symfony/yaml                         v5.2.1    Symfony Yaml Component
...
theseer/tokenizer                    1.2.0     A small library for converting tokenized PHP source code into XML and potentially other formats
twig/extra-bundle                    v3.2.1    A Symfony bundle for extra Twig extensions
twig/twig                            v3.2.1    Twig, the flexible, fast, and secure template language for PHP
webmozart/assert                     1.9.1     Assertions to validate method input/output with nice error messages.

Description
I have a new project and required symfony/doctrine-messenger package. Afterwards I want to setup the migration via make:migration. The result is an error:
CleanShot 2021-01-21 at 21 55 25

The reason is, that I don't have any entites yet, so no mapping info available.

I created a new Test entity for testing purpose and run the command again... it works 🎉
CleanShot 2021-01-21 at 21 56 33

The result:

<?php

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20210121205631 extends AbstractMigration
{
    public function up(Schema $schema) : void
    {
        $this->addSql('CREATE SEQUENCE test_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
        $this->addSql('CREATE TABLE test (id INT NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE TABLE messenger_messages (id BIGSERIAL NOT NULL, body TEXT NOT NULL, headers TEXT NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, available_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, delivered_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE INDEX IDX_75EA56E0FB7336F0 ON messenger_messages (queue_name)');
        $this->addSql('CREATE INDEX IDX_75EA56E0E3BD61CE ON messenger_messages (available_at)');
        $this->addSql('CREATE INDEX IDX_75EA56E016BA31DB ON messenger_messages (delivered_at)');
        $this->addSql('BEGIN;');
        $this->addSql('LOCK TABLE messenger_messages;');
        $this->addSql('CREATE OR REPLACE FUNCTION notify_messenger_messages() RETURNS TRIGGER AS $$
            BEGIN
                PERFORM pg_notify(\'messenger_messages\', NEW.queue_name::text);
                RETURN NEW;
            END;
        $$ LANGUAGE plpgsql;');
        $this->addSql('DROP TRIGGER IF EXISTS notify_trigger ON messenger_messages;');
        $this->addSql('CREATE TRIGGER notify_trigger AFTER INSERT ON messenger_messages FOR EACH ROW EXECUTE PROCEDURE notify_messenger_messages();');
        $this->addSql('COMMIT;');
    }

    public function down(Schema $schema) : void
    {
        $this->addSql('CREATE SCHEMA public');
        $this->addSql('DROP SEQUENCE test_id_seq CASCADE');
        $this->addSql('DROP TABLE test');
        $this->addSql('DROP TABLE messenger_messages');
    }
}

How to reproduce
I created a reproducer: https://github.com/OskarStark/reproducer-doctrine-messenger

Check the README 👀

Possible Solution

Additional context
It works as long you have any mapping info available .... 🤔

EDIT
I also tried to add auto_setup=false to the DSN, same result...

@OskarStark OskarStark added this to the 5.1 milestone Jan 21, 2021
@OskarStark
Copy link
Contributor Author

If I look at the stack trace I can't see any "Symfony-Doctrine" related code:

Exception trace:
  at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/doctrine/migrations/lib/Doctrine/Migrations/Provider/Exception/NoMappingFound.php:13
 Doctrine\Migrations\Provider\Exception\NoMappingFound::new() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/doctrine/migrations/lib/Doctrine/Migrations/Provider/OrmSchemaProvider.php:41
 Doctrine\Migrations\Provider\OrmSchemaProvider->createSchema() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/doctrine/migrations/lib/Doctrine/Migrations/Generator/DiffGenerator.php:132
 Doctrine\Migrations\Generator\DiffGenerator->createToSchema() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/doctrine/migrations/lib/Doctrine/Migrations/Generator/DiffGenerator.php:93
 Doctrine\Migrations\Generator\DiffGenerator->generate() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/doctrine/migrations/lib/Doctrine/Migrations/Tools/Console/Command/DiffCommand.php:154
 Doctrine\Migrations\Tools\Console\Command\DiffCommand->execute() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/symfony/console/Command/Command.php:258
 Symfony\Component\Console\Command\Command->run() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/symfony/maker-bundle/src/Maker/MakeMigration.php:93
 Symfony\Bundle\MakerBundle\Maker\MakeMigration->generate() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/symfony/maker-bundle/src/Command/MakerCommand.php:104
 Symfony\Bundle\MakerBundle\Command\MakerCommand->execute() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/symfony/console/Command/Command.php:258
 Symfony\Component\Console\Command\Command->run() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/symfony/console/Application.php:938
 Symfony\Component\Console\Application->doRunCommand() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/symfony/framework-bundle/Console/Application.php:96
 Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/symfony/console/Application.php:266
 Symfony\Component\Console\Application->doRun() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/symfony/framework-bundle/Console/Application.php:82
 Symfony\Bundle\FrameworkBundle\Console\Application->doRun() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/vendor/symfony/console/Application.php:142
 Symfony\Component\Console\Application->run() at /Users/oskar.stark/dev/reproducer/reproducer-doctrine-messenger/bin/console:43

So I think the MessengerTransportDoctrineSchemaSubscriber will never run... 🤔

@OskarStark
Copy link
Contributor Author

OskarStark commented Jan 22, 2021

So lets say I use the workaround using the Test entity, I end up with the migration above and delete the Test entity related parts:

<?php

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20210121205631 extends AbstractMigration
{
    public function up(Schema $schema) : void
    {
-       $this->addSql('CREATE SEQUENCE test_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
-       $this->addSql('CREATE TABLE test (id INT NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE TABLE messenger_messages (id BIGSERIAL NOT NULL, body TEXT NOT NULL, headers TEXT NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, available_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, delivered_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE INDEX IDX_75EA56E0FB7336F0 ON messenger_messages (queue_name)');
        $this->addSql('CREATE INDEX IDX_75EA56E0E3BD61CE ON messenger_messages (available_at)');
        $this->addSql('CREATE INDEX IDX_75EA56E016BA31DB ON messenger_messages (delivered_at)');
        $this->addSql('BEGIN;');
        $this->addSql('LOCK TABLE messenger_messages;');
        $this->addSql('CREATE OR REPLACE FUNCTION notify_messenger_messages() RETURNS TRIGGER AS $$
            BEGIN
                PERFORM pg_notify(\'messenger_messages\', NEW.queue_name::text);
                RETURN NEW;
            END;
        $$ LANGUAGE plpgsql;');
        $this->addSql('DROP TRIGGER IF EXISTS notify_trigger ON messenger_messages;');
        $this->addSql('CREATE TRIGGER notify_trigger AFTER INSERT ON messenger_messages FOR EACH ROW EXECUTE PROCEDURE notify_messenger_messages();');
        $this->addSql('COMMIT;');
    }

    public function down(Schema $schema) : void
    {
        $this->addSql('CREATE SCHEMA public');
-       $this->addSql('DROP SEQUENCE test_id_seq CASCADE');
-       $this->addSql('DROP TABLE test');
        $this->addSql('DROP TABLE messenger_messages');
    }
}

and execute this migration I get another error "There is no active transaction":
CleanShot 2021-01-22 at 11 50 43

@OskarStark
Copy link
Contributor Author

I accidentally tried to migrate a second time and it works, but not at the first time 👀

I can reproduce by drop and create the database, migrate -> ERROR -> migrate again -> SUCCESS 🤷‍♂️ :
CleanShot 2021-01-22 at 11 59 33

@OskarStark
Copy link
Contributor Author

OskarStark commented Jan 22, 2021

Weird, it looks like the migration is valid and marked as "migrated"/"current":
CleanShot 2021-01-22 at 12 06 23

But something is kicking in an throw an exception 🤔
CleanShot 2021-01-22 at 12 07 11

@OskarStark
Copy link
Contributor Author

I found 2 ways to fix this "There is no active transaction" error:

Solution 1: remove the final COMMIT;

<?php

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20210121205631 extends AbstractMigration
{
    public function up(Schema $schema) : void
    {
        $this->addSql('CREATE TABLE messenger_messages (id BIGSERIAL NOT NULL, body TEXT NOT NULL, headers TEXT NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, available_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, delivered_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE INDEX IDX_75EA56E0FB7336F0 ON messenger_messages (queue_name)');
        $this->addSql('CREATE INDEX IDX_75EA56E0E3BD61CE ON messenger_messages (available_at)');
        $this->addSql('CREATE INDEX IDX_75EA56E016BA31DB ON messenger_messages (delivered_at)');
        $this->addSql('BEGIN;');
        $this->addSql('LOCK TABLE messenger_messages;');
        $this->addSql('CREATE OR REPLACE FUNCTION notify_messenger_messages() RETURNS TRIGGER AS $$
            BEGIN
                PERFORM pg_notify(\'messenger_messages\', NEW.queue_name::text);
                RETURN NEW;
            END;
        $$ LANGUAGE plpgsql;');
        $this->addSql('DROP TRIGGER IF EXISTS notify_trigger ON messenger_messages;');
        $this->addSql('CREATE TRIGGER notify_trigger AFTER INSERT ON messenger_messages FOR EACH ROW EXECUTE PROCEDURE notify_messenger_messages();');
-       $this->addSql('COMMIT;');
    }

    public function down(Schema $schema) : void
    {
        $this->addSql('CREATE SCHEMA public');
        $this->addSql('DROP TABLE messenger_messages');
    }
}

Solution 2: start a new transaction by adding BEGIN;

<?php

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20210121205631 extends AbstractMigration
{
    public function up(Schema $schema) : void
    {
        $this->addSql('CREATE TABLE messenger_messages (id BIGSERIAL NOT NULL, body TEXT NOT NULL, headers TEXT NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, available_at TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, delivered_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE INDEX IDX_75EA56E0FB7336F0 ON messenger_messages (queue_name)');
        $this->addSql('CREATE INDEX IDX_75EA56E0E3BD61CE ON messenger_messages (available_at)');
        $this->addSql('CREATE INDEX IDX_75EA56E016BA31DB ON messenger_messages (delivered_at)');
        $this->addSql('BEGIN;');
        $this->addSql('LOCK TABLE messenger_messages;');
        $this->addSql('CREATE OR REPLACE FUNCTION notify_messenger_messages() RETURNS TRIGGER AS $$
            BEGIN
                PERFORM pg_notify(\'messenger_messages\', NEW.queue_name::text);
                RETURN NEW;
            END;
        $$ LANGUAGE plpgsql;');
        $this->addSql('DROP TRIGGER IF EXISTS notify_trigger ON messenger_messages;');
        $this->addSql('CREATE TRIGGER notify_trigger AFTER INSERT ON messenger_messages FOR EACH ROW EXECUTE PROCEDURE notify_messenger_messages();');
        $this->addSql('COMMIT;');
+       $this->addSql('BEGIN;');
    }

    public function down(Schema $schema) : void
    {
        $this->addSql('CREATE SCHEMA public');
        $this->addSql('DROP TABLE messenger_messages');
    }
}

Output:
CleanShot 2021-01-22 at 12 09 55

@stof
Copy link
Member

stof commented Jan 25, 2021

inserting a transaction inside the generated migration looks weird to me, as doctrine/migrations already manages a transaction (which might be the issue here)

@OskarStark
Copy link
Contributor Author

cc @weaverryan

@nicolas-grekas nicolas-grekas removed this from the 5.1 milestone Jan 27, 2021
@fabpot fabpot added this to the 5.2 milestone Feb 1, 2021
@fabpot
Copy link
Member

fabpot commented Feb 1, 2021

@OskarStark See #40055 for a fix.

fabpot added a commit that referenced this issue Feb 1, 2021
…pot)

This PR was merged into the 5.2 branch.

Discussion
----------

[Messenger] Fix Doctrine setup when using a migration

| Q             | A
| ------------- | ---
| Branch?       | 5.2 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #39928 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License       | MIT
| Doc PR        | n/a

#38136 fixed running `messenger:setup-transports` (issue reported in #37179), but it breaks usage with `make:migration` (reported in #39928) as code is already executed in a transaction.

This PR fixes both use cases.

Commits
-------

42eeb44 [Messenger] Fix Doctrine setup when using a migration
@fabpot fabpot closed this as completed Feb 1, 2021
@akondas
Copy link
Contributor

akondas commented Feb 4, 2021

#40055 introduce regression (which had already been repaired in #38136):

> bin/console messenger:setup-transports
In AbstractPostgreSQLDriver.php line 102:
                                                                               
  An exception occurred while executing 'LOCK TABLE messenger_messages;':      
                                                                               
  SQLSTATE[25P01]: No active sql transaction: 7 ERROR:  LOCK TABLE can only b  
  e used in transaction blocks     

@baiiko
Copy link

baiiko commented Feb 11, 2021

The problem for me here is generating a migration for this table, there is no need for it.
The commande messenger:setup-transports already does all the work.
It's a same principe of table migrations.

With the last fix, my app work it. https://github.com/symfony/symfony/pull/38136/files

⚠️ 5.x should be edited for this fix no ? not only 5.2

@xabbuh
Copy link
Member

xabbuh commented Feb 11, 2021

@GuigZ- Branches are merged up regularly. So fixes in 4.4 and 5.2 will end up in 5.x eventually when that's done.

@nicolas-grekas
Copy link
Member

#38136 has been reverted and replaced in #40055

@baiiko
Copy link

baiiko commented Feb 11, 2021

#38136 has been reverted and replaced in #40055

Yes, but does not work in auto setup mode :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants