Rails Migrations 3.1
One of the things that has fascinated me about developing in Ruby on Rails is the ease of use that is also referred to in some circles as the 'Rails Way' :D
I would say that developing in rails if you follow the standards and conventions can be very fun. The problem is a lot of times developers have their way of doing things, so unless you buy into the 'Rails Way' of doing things maintaining your Rails app can become quit cumbersome. But that is for a different post.
A question that I have posed to myself but never really any other developer is why schema migrations are not a given in MVC frameworks in the PHP world, which is where I used to do most of my development until about 8 months ago when I got into Java and now its about a 50/50 split. I develop in Zend Framework and when thinking of writing my won migration framework/plug-in for Zend I of course did some poking and found this proposition.
Not surprisingly his references are from Rails.
I have been updating myself on Rails with the newest update 3.1 and of course the trend of making things easier is evident instead of two separate function for migrating up or down now you can just define one function called change ie.
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string "first_name", :limit => 25
t.string "last_name,:limit => 50
t.string "email",:limit => 100
t.string "password",:limit => 40
t.string "username",:limit => 4
t.timestamps
end
end
end
This helps raises an ActiveRecord::IrreversibleMigration exception if the down migration is irreversible. But this can still be avoided by defining up and down methods, which now I instance methods
class CreateUsers < ActiveRecord::Migration
def up
end
def up
end
end
as opposed to class methods the old ‘Rails Way’
class CreateUsers < ActiveRecord::Migration
def self.up
end
def self.up
end
end
I know migrations has it’s drawbacks, but as a developer we get paid to foresee some these, even though we miss them at times, and if we do foresee them at design time Rails has this cool feature for migrations which is function called execute()
class CreateUsers < ActiveRecord::Migration
def up
execute(“INSERT INTO users VALUES…..”)
end
def up
execute(“DELETE FORM users WHERE…..”)
end
end
This is my mind should be standard for any serious MVC. I love coding in Zend Framework, but this is a feature since introduced in my life I greatly miss especially engineers that don’t have a personal DBA for private projects.
Who knows maybe I will write something that I can use for Zend Framework. If I do I will share it right here :D