I have a stored procedure in my database that I want to remove on rollback. Since laravel does not have direct support for stored procedures, I do this:
public function down()
{
DB::statement('DROP PROCEDURE IF EXISTS MyProcedure');
}
The statement fails to delete and I get the following error:
[Exception]
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. (SQL: DROP PROCEDURE IF EXISTS MyProcedure) (Bindings: array())
Then I try it like this:
public function down()
{
DB::connection()->getPdo()->exec('DROP PROCEDURE IF EXISTS MyProcedure');
}
And it works perfectly.
Check the source:
https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Connection.php#L308-L325
And the documentation:
http://php.net/manual/en/pdo.exec.php
http://php.net/manual/en/pdostatement.execute.php
Also, read the whole source code for Connection and you will find:
https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Connection.php#L351-L365
Okay, I understand now. My confusion comes from the API docs not mentioning DB::Statement('...')
it was a prepared query. It simply says
Execute an SQL statement and return the boolean result.
http://laravel.com/api/class-Illuminate.Database.Connection.html#_statement
Perhaps the docs should be improved?
Yeah, perhaps open up an issue on laravel/docs if you see an area that needs improvement. Thanks!
DB::raw('DROP PROCEDURE IF EXISTS MyProcedure');
Most helpful comment
DB::raw('DROP PROCEDURE IF EXISTS MyProcedure');