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 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.

Simple Example

Get DBDesigner 4 at http://www.fabforce.net/dbdesigner4/
Eventually, this will be replaced by MySQL 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 MySQL
   Workbench. For now, though, in order to use the built-in synchronize
   feature, you'll have to establish a MySQL account with an OLD PASSWORD
   (pre-4.1) -- see http://www.billkatz.com/rails_dbmodel
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.

:include: MIT-LICENSE