0

When I return the following MySQL migration with the symfony command php bin/console doctrine:migrations:migrate

<?php                                                                                                 
                                                   
declare(strict_types=1);        
                                                   
namespace DoctrineMigrations;
                                                   
use Doctrine\DBAL\Schema\Schema;                                                                      
use Doctrine\Migrations\AbstractMigration;                                                            
                                                   
/**                         
 * Auto-generated Migration: Please modify to your needs!
 */                                                                                                   
final class Version20211112124807 extends AbstractMigration
{                                             
    public function getDescription() : string
    {                                     
        return '';                
    }                     
                                                   
    public function up(Schema $schema) : void
    {         
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql("DROP FUNCTION IF EXISTS wordcount;");
        $this->addSql("
            DELIMITER $$
            CREATE FUNCTION wordcount(str LONGTEXT)
                   RETURNS INT
                   DETERMINISTIC
                   SQL SECURITY INVOKER
                   NO SQL
              BEGIN
                DECLARE wordCnt, idx, maxIdx INT DEFAULT 0;
                DECLARE currChar, prevChar BOOL DEFAULT 0;
                SET maxIdx=char_length(str);
                SET idx = 1;
                WHILE idx <= maxIdx DO
                    SET currChar=SUBSTRING(str, idx, 1) RLIKE '[[:alnum:]]';
                    IF NOT prevChar AND currChar THEN
                        SET wordCnt=wordCnt+1;
                    END IF;
                    SET prevChar=currChar;
                    SET idx=idx+1;
                END WHILE;
                RETURN wordCnt;
              END
            $$
            DELIMITER ;
        ");

        $this->addSql('ALTER TABLE article ADD words_amount INT DEFAULT 0 NOT NULL');

        $this->addSql("UPDATE article SET article.words_amount = wordcount( article.content )");
    }

    public function down(Schema $schema) : void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->addSql('ALTER TABLE article DROP words_amount');
    }
}

I get the following error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$  CREATE FUNCTION wordcount(str LONGTEXT)

But when I paste the wordcount function declaration directly into my mysql console, everything is ok (I have checked with show function status).

Do you know or have you an idea why it is not working? Or maybe there is another way which I do not know.

For your information, I am using MySQL 5.7

Kaharon
  • 365
  • 4
  • 16
  • https://stackoverflow.com/a/10259528/2600812 ?? – craigh Nov 13 '21 at 23:34
  • So if I understand it well, I just cannot use this migration because doctrine does not support delimiters. Thank you for your help, by understanding better my issue, I have search internet with different keywords, and found this https://stackoverflow.com/questions/19340046/problems-with-mysql-create-procedure-from-doctrine-raw-sql so if can define a function in the doctrine migration and I must not use the delimiters. – Kaharon Nov 14 '21 at 09:01

0 Answers0