Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line foward engineering using a .mwb file?

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?

like image 715
balteo Avatar asked Mar 04 '12 15:03

balteo


People also ask

How do I convert MWB to SQL?

In the menu click "Database" and then "Forward engineering" (shortcut CTRL+G). You can omit "yourDatabase" when you create it with your script. @VikasGupta Edited my answer. Ask if you have more questions.

How do I import a MWB file into Workbench?

To import a file, open Workbench and click on + next to the MySQL connections option. Fill in the fields with the connection information. Once connected to the database go to Data Import/Restore. Choose the option Import from Self-Contained File and select the file.


3 Answers

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.

like image 164
Sergio Avatar answered Oct 26 '22 13:10

Sergio


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.

like image 30
madhead - StandWithUkraine Avatar answered Oct 26 '22 13:10

madhead - StandWithUkraine


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
like image 3
Artur_Indio Avatar answered Oct 26 '22 11:10

Artur_Indio