Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I automatically execute a MySQL script or routine after forward engineering from model

I've built a EER Model in MySQL Workbench that I forward engineer to create the database. The forward engineering works perfectly, and the database is created from the diagram as expected.

Apart from tables, there are also some Stored Procedures (aka Routines) that I've included in the model. These routines are designed to only be run once, as soon as the database has been set up. They automatically insert necessary data into the tables.

My question is, how can I get the forward engineering process to automatically call/execute one of these routines once the tables have been created.

At the moment, I have to forward engineer the database, and then manually call the stored procedures?

like image 965
sparkyspider Avatar asked Apr 30 '12 13:04

sparkyspider


People also ask

How do I run a script in MySQL Workbench?

To run SQL script in MySQL, use the MySQL workbench. First, you need to open MySQL workbench. Now, File -> Open SQL Script to open the SQL script. Note − Press OK button twice to connect with MySQL.

What is forward and reverse engineering in MySQL Workbench?

9.4.1 Forward Engineering 9.4.2 Reverse Engineering. MySQL Workbench provides capabilities to forward engineering physical database designs. A visual data model can be transformed into a physical database on a target MySQL Server by executing the forward engineering wizard.


1 Answers

In your EER diagram on the workbench right click on a table and select edit table. This will open a pane at the bottom with a couple of tabs. The tabs are table, columns, indexes, foreign key, etc. There is a tab called insert. This tab allows you to insert records into the Model database.

When you click on the insert tab it will show a grid. Add the records you want to insert onto this grid. Make sure you commit these records. See screenshot for example.

Workbench Data Insert

Now when you forward engineer the database on the very first screen there is an option to Generate insert statements. Tick the option forward engineer and the data you want inserted will be scripted when you create the model. Save the script so you can run it over and over without going into the MySQL workbench.

Tick Insert option

I have not found options to update, delete or do other data manipulation in the workbench but I think this is what you are looking for.

NOTE : To directly import the records, you won't be able to do that via the workbench; there is no option. You can save the records to file. However to import/create them you would need to add them one at a time (from the modeller). You could however make a backup of the MySQL database with the records in already. Then copy those INSERT Statements from the MYSQL backup script into your setup script.

Steps would be:

  1. Create database.

  2. Import the files with the setup/config records into newly created database

  3. Backup database

  4. Open backup file, then copy and paste the INSERT statements you are looking for into the setup script created by the MySQL Workbench

UPDATE:

I did some experimenting when you get to the review script to be executed step in the forward engineering you can also at the end call the stored procedures (as you mentioned) by editing the script. Once done save the script to file and test.

Hope that helps!

like image 67
Namphibian Avatar answered Sep 24 '22 17:09

Namphibian