Data Plan

Data Plan migration generator - computes migration code automatically. Works with Sinatra/ActiveRecord and with Rails too.

Usage

Create db/plan.rb file once and write desired db schema in it according to Rails’s schema.rb syntax. Tip: in rails, just copy your schema.rb to plan.rb. Of course, you can update plan.rb anytime later.

When you create a migration, this generator will find and put all differencies between plan.rb and your current database schema!

That’s it, you get migration code automatically. You can migrate immediately, or add some more tweaks to your migration, or something else.

Why is it great?

  • You have the one place (plan.rb) where you write db schema and

comments on it.

  • You don’t need to remember all that syntax like

change_column, add_column, etc. You just write the schema in plan.rb file and get migrations code for free.

Nice bonus: migrations are automatically named (of course you may override name suggested). So you just call ‘rake db:create_migration’ without NAME=… argument and get the migration name for free too.

Example

./script/generate migration

This will look at plan.rb and at current db. If for example plan.rb contains more columns/table/indexes than in db, it will generate migration with corresponding add_column/add_table/add_index commands in it.

How to install

Before use with Sinatra, install ‘sinatra-activerecord’ gem. It extends Sinatra with extension methods and Rake tasks for dealing with

an SQL database using the ActiveRecord ORM. Follow it’s instructions and create a Rakefile with migration commands. After that you can invoke ‘rake db:create_migration’ and get migrations.

  • Install the gem:

gem install data_plan

  • Add to Rakefile (after sinatra-activerecord rake commands):

require ‘data_plan/generators/migration/sinatra’

How to use with Rails

Yes, you can use DataPlan in Rails too. But it better to use not like a gem, and instead directly copy to your application.

Copy from the gem the folder

DATAPLANGEMROOT/lib/data_plan/generators/migration/*

to the Rails project as follows:

YOURAPPROOT/lib/generators/migration/*

Thats it, Data Plan now in your rails app, and you can invoke ‘script/generate migration’ and get migrations auto-generated as described above.

Ruby versions

Because Sinatra-ActiveRecord works only with Ruby 1.9, DataPlan works with 1.9 for Sinatra. For Rails, it should work both for 1.8 and 1.9. Only (Sinatra-ActiveRecord 1.1.2 + ruby 1.9) and (Rails 2.3 + ruby 1.8) were tested.

Todo

Indices are partially supported at the moment. But seems they work ;-)

Changelog

Workaround on views. Thanks to Cyril Boswell.

Found a bug? Fix it and email pull request to me: [email protected] Want to add a feature? Implement it and email the patch!

Pavel Vasev [ [email protected] ]

Released under the MIT license (included)

p.s. Schema-Definition plugin is hugely based on Auto-Migrations by PJ Hyett