Schema Generator v1.0.2 by Scott Laird, [email protected] scottstuff.net

This is a SQL schema generator for Rails. It slurps up all of the migration files in db/migrate and turns them into a single SQL schema file for each supported database type. The schema files contains table definitions, indexes, and any seed data created by Model.create() calls in the migrations.

Supported databases:

* MySQL
* PostgreSQL
* SQLite
* SQLServer

Usage


To use, run ‘./scripts/generate schema’ on any Rails project. This will produce five files:

* db/schema.mysql.sql
* db/schema.postgresql.sql
* db/schema.sqlite.sql
* db/schema.sqlserver.sql
* db/schema.rb

If these files already exist, then Rails will prompt you before overwriting them.

If your Rails project already has migrations to build its entire schema from scratch, then this will work right off the bat. If not, you may wish to create a db/migrate/0_initial_schema.rb migration and use it to create your initial schema using the usual migration create_table methods.

You can see an example in Typo (typo.leetsoft.com). This generator was written to reduce the pain involved in creating new migrations for Typo, because each migration required an equivalent change to 3 different hand-edited static schema files, which was usually an error-prone process, because no one user tests all three DB types. Now we can just use ‘./script/generate schema’ and be done with it.

Supported Migration Actions


This generator currently supports create_table, add_column, drop_column, change_column, rename_column, add_index, drop_index, and model .create and .update_all calls. Any model find calls will immediately return an empty array; this allows data migration blocks in migration files to be safely ignored.

Debugging and Troubleshooting


This generator reaches deep into the guts of Rails; it has to make several changes to the DB drivers and ActiveRecord::Base in order to work. This means that it may have issues with future Rails releases. It has been tested with 1.0.0, and previous versions broke twice on different 1.0RC releases. All of these changes are made on the fly using open classes, so installing this generator will not break any outside code–the modifications are only active while the generator is running.

If you have any other problems, please mail me at [email protected] or post a comment on scottstuff.net/.