jai jai - 3 months ago 24
MySQL Question

Creating migrations for stored procedures, functions and events from existing database in Laravel

I am working on laravel 4.1. I have a ready made mysql database created by other means. I have managed to create a migration from the existing database that would create all the tables in the database if run. But I want the migration to also include the stored procedures, functions and events present in the database. Any help would be appreciated. I would specifically like to know how to create laravel migrations for stored procedures, events and functions from an existing database.

Answer

To execute raw SQL commands in a migration (like the creation of a stored procedure) i do like this:

public function up() 
{            
    DB::unprepared('CREATE PROCEDURE my_procedure( IN param INT(10) )  BEGIN  /* here your SP code */ END');
}

public function down() 
{
    DB::unprepared('DROP PROCEDURE IF EXISTS my_procedure');
}