Ruby on Rails
DBModelGem

The dbmodel gem accepts XML data model files from DBDesigner 4 , calls generate model or generate scaffold for tables with relationships, and inserts relationships (“has_many :beers”) into appropriate Model files. Insertion of relationships into Model files is non-destructive — existing files aren’t removed and current code is kept.

(full-size picture)

Installation

  1. Download DBDesigner 4 . (The successor MySQL Workbench will be released this year)
  2. Download the dbmodel gem and install with
    gem install dbmodel-0.1.0.gem --rdoc
  3. Patch the gem if you aren’t on Windows to handle paths properly. Brian Bugh provided the following patch:

91c91
<         modelfile = File.dirname(xmlfile) + File.join('', '..',
'app', 'models', table['name'].singularize + '.rb')
---
>         modelfile = File.dirname(xmlfile) + '\..\app\models\\' +
table['name'].singularize + '.rb'
265c265
<         require File.dirname(f) + File.join('', '..', 'config',
'environment') # Better way to do this?
---
>         require File.dirname(f) + '/../config/environment'  # Better
way to do this?
283c283
<       if ex.message =~ /config#{File::SEPARATOR}environment/
---
>       if ex.message =~ /config\/environment/

Usage

Use DBDesigner to construct tables and specify relationships between the tables. Label the relationships in Rails-compliant manner, like has_many :beers or has_and_belongs_to_many :things. As a time saver, you can use habtm :x instead of has_and_belongs_to_many :x. Note that you’ll never need to label a link belongs_to — relationships are defined from the source table viewpoint, so the belongs_to is implicit in the relationship.

Save your model into a DBDesigner XML file in your Rails app /db folder. Then use the command:

dbmodel mymodel.xml

This will generate model files for all tables with relations. You can choose scaffolding by embedding tags within your table comments.

Blog entry for dbmodel is here

NAME
     dbmodel - rails model generator from XML database model file

SYNOPSIS
     dbmodel [options] [xml_file_name(s) ...]

DESCRIPTION
     The dbmodel generator loads an XML file from a database design tool
     (MySQL DBDesigner 4), generates files you'd normally get by
     "generate model" or "generate scaffold", and inserts comments and
     relationships (e.g., "has_many :foos") into the model files.

     If no XML files are specified on the command line, dbmodel will
     check if the file "dbmodel.xml" is present in the current directory.

     While full roundtripping is possible in the future, this version is a
     simplistic proof-of-concept. It assumes you are using the free
     DBDesigner 4. Tables are created using Rails naming conventions and
     relationships are named in Rails fashion. Scaffolding commands can
     be embedded as comments in the tables. Scaffolding commands can
     be embedded as comments in the tables using the format:
     [SCAFFOLD]
     or optionally
     [SCAFFOLD controller-name action1 action2 ...]

     dbmodel is simplistic and requires convention to bypass configuration.
     Relationships should be labelled habtm, has_many, or has_one. Reciprocal
     relationships for has_many and has_one are auto-generated. The database
     model XML file is assumed to be in the /db directory of a Rails app.
     Creation of a model file or scaffolding only occurs if a model file is
     missing. Only tables with relationships drive creation of a model file.

EXAMPLE   
     Get DBDesigner 4 at <a href="http://www.fabforce.net/dbdesigner4/">http://www.fabforce.net/dbdesigner4/</a>
     Eventually, this will be replaced by <a href="http://wiki.rubyonrails.org/rails/pages/MySQL" class="existingWikiWord">MySQL</a> Workbench.

     Using DBDesigner 4, create two tables: banks and accounts. Add a 1:n
     relationship between the "banks" table and the "accounts" table. Name
     the relationship "has_many :accounts". Open the "accounts" table and
     add the following comment: "[SCAFFOLD]These are the bank accounts." 
     Comments can have newlines (/n) in them; dbmodel will break the comments
     into separate lines.

     DBDesigner can synchronize a model with your database in two ways:
     1) Output a SQL create script (a DDL file).
     2) Use the "synchronize" feature which updates your database. Note that
        DBDesigner is an old program that will be re-released as <a href="http://wiki.rubyonrails.org/rails/pages/MySQL" class="existingWikiWord">MySQL</a>
        Workbench. For now, though, in order to use the built-in synchronize
        feature, you'll have to establish a <a href="http://wiki.rubyonrails.org/rails/pages/MySQL" class="existingWikiWord">MySQL</a> account with an OLD PASSWORD
        (pre-4.1) -- see <a href="http://www.billkatz.com/rails_dbmodel">http://www.billkatz.com/rails_dbmodel</a>

     Save the database model as "bankmodel". By default, the model is saved
     as an XML file from DBDesigner. Place the XML file in the /db directory 
     of your Rails app.

     To update the Rails model files, simply execute the following:

     ruby script/generate dbmodel bankmodel.xml

     The generator will create Bank and Account model files by calling
     the model or scaffold generator, depending on whether a table has
     the [SCAFFOLD] tag in the table comments. If a model file already
     exists, it is NOT overwritten; the file is scanned for the existence
     of the proper relationships, and if they aren't there, they're added.
     Table comments are written only when a file is newly created.

LICENSE
     dbmodel is available under an MIT-style license.