8

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.

I want the migration to also include the stored procedures, functions and events present in the database.

I would specifically like to know how to create laravel migrations for stored procedures, events and functions from an existing database.

Moppo
  • 18,797
  • 5
  • 65
  • 64
jai
  • 391
  • 2
  • 6
  • 14

2 Answers2

17

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

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');
}
Moppo
  • 18,797
  • 5
  • 65
  • 64
  • I understand that. But I already have my db. I found some gist code here http://stackoverflow.com/questions/23514679/create-migration-from-existing-database-in-laravel-4 which tells me how to generate a migration script for all the tables in an already existing mysql db in one shot. But this code does not tell me how to include the stored procedures, function or events present in the db which is what I need. – jai Nov 16 '15 at 09:51
  • 1
    After some looking around and consulting I have found there is no way to do what I want. We need to manually create the the above code copy/ pasting the stored procedures and the parameters in the code above. – jai Nov 18 '15 at 04:39
  • 1
    Thanks for this, exactly what I needed. – Banago Sep 02 '16 at 08:39
0

Works with DB::raw('DELIMITER $$ CREATE FUNCTION....') for me.

Jean Freitas
  • 209
  • 1
  • 5