7

I need to be able to perform a forward engineering from a model located in a .mwb file. All of this from the command line as I would like to automate the process.

Can anyone please let me know if this is possible and if so how?

balteo
  • 23,602
  • 63
  • 219
  • 412
  • Consider editing your post to include sample input, sample expected output, current code, current output. Good luck. – shellter Mar 04 '12 at 22:52
  • Hello Shellter. The input is a `.mwb` file; the output is a `.sql` DDL file. Current code: none as I am seeking directions. – balteo Mar 05 '12 at 07:56
  • Hi @balteo, were you ever able to get this working? I am currently facing the same problem. – Aistina Oct 18 '12 at 08:37
  • Hello Aistina. I don't know if you use Java but as far as I am concerned, I use a different approach now: I generate the database from the domain model using Hiberante/JPA (see: Hibernate and `hibernate.hbm2ddl.auto`). – balteo Oct 18 '12 at 08:58
  • Thanks for your reply. Unfortunately I am using PHP, so I will have to find a different way to achieve this. – Aistina Oct 18 '12 at 09:12
  • @balteo are you find a way to do that?I'm in the same issue. – Artur_Indio Jun 10 '15 at 23:59

3 Answers3

5

This is the output in the command line once you call WB with --help:

mysql-workbench [<options>] [<model file>]
Options:
  --force-sw-render      Force Xlib rendering
  --force-opengl-render  Force OpenGL rendering
  --query <connection>   Open a query tab to the named connection
  --admin <instance>     Open a administration tab to the named instance
  --model <model file>   Open the given EER model file
  --script <script file> Execute the given Python or Lua script file
  --run <script>         Execute the given code in default language for GRT shell
  --run-python <script>  Execute the given code in Python
  --run-lua <script>     Execute the given code in Lua
  --quit-when-done       Quit Workbench when the script is done
  --help, -h             Show command line options and exit
  --log-level=<level>    Valid levels are: error, warning, info, debug1, debug2, debug3
  --verbose              Enable diagnostics output
  --version              Show Workbench version number and exit

I guess you can load your model using the --model option and then create an script that will perform the forward engineering and run it using the --run option and then instruct WB to exit once it finishes with the --quit-when-done option.

You can consult WB help to learn more about creating scripts as well as this guide.

Sergio
  • 4,537
  • 4
  • 33
  • 41
3

You can actually automate this task with Python (or Lua) script - MySQL Workbench already has an interpreter under Scripting menu. Create a new script and use the stub:

# -*- coding: utf-8 -*-

import os
import grt
from grt.modules import DbMySQLFE

c = grt.root.wb.doc.physicalModels[0].catalog
DbMySQLFE.generateSQLCreateStatements(c, c.version, {
    'GenerateDrops' : 1,
    'GenerateSchemaDrops' : 1,
    'OmitSchemata' : 1,
    'GenerateUse' : 1
})
DbMySQLFE.generateSQLCreateStatements(c, c.version, {
DbMySQLFE.createScriptForCatalogObjects(os.path.dirname(grt.root.wb.docPath) + 'ddl.sql', c, {})

It does not actully run from command line, but I beleive you can run it with --run-script option.

madhead
  • 31,729
  • 16
  • 153
  • 201
  • It works fine from command line (I just needed a dummy X-server to run WB)... *but* I got a problem with my views. Some of my views use other views, and the script creations-ordering does note take in account dependencies. In other words, if I get two views vA and vB, and vB is defined with something like `select somefield FROM vA`, I may end-up with a sql script trying to generate view B before view A... which of course, results in an error : `Table 'vA' doesn't exist` Any idea to fix this ? – Balmipour Apr 27 '16 at 12:02
  • @Balmipour, sorry I'm out of the loop on this question now, cannot recall the details. Generally, if you have a new question it's better to ask it separately. Proper tagging can help others to give it attention and solve it. – madhead Apr 27 '16 at 12:31
3

This question is too old, but I found a project on github that make this, below the commands in cmd windows, and here the github repository and more linux sh file version.

Windows

@echo off
REM generate sql from mwb
REM usage: mwb2sql.bat {.mwb file} {output file}

SET WORKBENCH="C:\Program Files (x86)\MySQL\MySQL Workbench 6.0 CE\MySQLWorkbench.exe"
SET OUTPUT=%~f2
%WORKBENCH% ^
  -open %~f1 ^
  -run-python "import os;import grt;from grt.modules import DbMySQLFE as fe;c = grt.root.wb.doc.physicalModels[0].catalog;fe.generateSQLCreateStatements(c, c.version, {});fe.createScriptForCatalogObjects(os.getenv('OUTPUT'), c, {})" ^
  -quit-when-done
Artur_Indio
  • 736
  • 18
  • 35
  • From version 7 & 8, we have to specify double "-" for commande line argument : C:\Program Files\MySQL\MySQL Workbench 8.0 CE\MySQLWorkbench.exe --open "D:\MyFolder\MyFile.mwb" --run-python "import grt; from grt.modules import DbMySQLFE as fe; c = grt.root.wb.doc.physicalModels[0].catalog; fe.generateSQLCreateStatements(c, c.version, {}); fe.createScriptForCatalogObjects(r'C:\Windows\Temp\tempSqlFile.sql', c, {});" --quit-when-done – Picsonald Jun 24 '21 at 14:49