crmpicco
crmpicco

Reputation: 17181

Prevent "Migration executed but did not result in any SQL statements" message when running custom SQL in Doctrine migration

I am executing manual prepared statements in my Doctrine migration. I am doing this as I require the last inserted ID to build further related queries. Using the standard addSql method does not allow for this due to the nature of it "lining up" the queries and then executing them at the end.

Is there a way I can prevent the following error message from displaying?

Migration 20151102112832 was executed but did not result in any SQL statements.

There are many SQL statements executed, however they are execute when calling this method of mine:

/**
 * @param string $sql
 *
 * @throws DBALException
 */
private function runSql($sql)
{
    $this->write($sql);
    $stmt = $this->connection->prepare($sql);
    $stmt->execute();
}

Upvotes: 6

Views: 3654

Answers (3)

Sebastian Viereck
Sebastian Viereck

Reputation: 5877

doctrine migration version 3 was throwing an error with MySQL5.7 when I used:

public function up(Schema $schema)
{
    $this->addSql('SELECT 1');
}

Error Message:

[error] Migration DoctrineMigrations\Version20190823142208 failed during Post-Checks. Error: "An exception occurred while executing 'SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'':

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll().  Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute."

This error can be fixed when using comments:

public function up(Schema $schema)
{
    $this->addSql('#prevent warning' ); //Prevent “Migration executed but did not result in any SQL statements”
}

Upvotes: 0

Andriyun
Andriyun

Reputation: 546

I think the best option here to show what your migration is doing. You can do this and suppress warning message by sending sql request with comment string. For example:

public function up(Schema $schema)
{
    // Do not show warning on migrations.
    $this->addSql('# Updating entities.');
}

Upvotes: 5

Marek
Marek

Reputation: 7433

You can create a "do nothing" sql:

public function up(Schema $schema)
{
    $this->addSql('SELECT 1');
}

Upvotes: 6

Related Questions