Plugins - Migratory Shortcuts

StarAdd to favorites

Migrations are truly a great addition to Rails. That said, if you have a boat load of tables in your project it can be pretty tedious to keep repeating the same values again and again. Thanks to some good feedback there is an even nice syntax now.

Updated example:

Somewhere in the middle of doing this…

  create_table :actors do |t|
    t.column "created_at", :datetime
    t.column "updated_at", :datetime
    t.column "deleted", :boolean, :null => false, :default => false
  end

  create_table :names do |t|
    t.column "name", :string, :null => false
    t.column "created_at", :datetime
    t.column "updated_at", :datetime
    t.column "created_by", :integer, :null => false
    t.column "updated_by", :integer, :null => false
    t.column "deleted", :boolean, :null => false, :default => false
  end

  create_table :actors_names do |t|
    t.column "actor_id", :integer, :null => false
    t.column "name_id", :integer, :null => false
    t.column "position", :integer, :null => false, :default => 0
    t.column "created_at", :datetime
    t.column "updated_at", :datetime
    t.column "created_by", :integer, :null => false
    t.column "updated_by", :integer, :null => false
    t.column "deleted", :boolean, :null => false, :default => false
  end

for the tenth time, you begin to wonder if there is a better way.

Introducing.. a better way.

The above becomes this…

  create_table :actors, :with => [:timestamps, :acts_as_paranoid]

  create_table :names, :with =>
 [:timestamps, :userstamps, :acts_as_paranoid] do |t|
    t.column "name", :string, :null => false
  end

and my favorite…

  create_link_table :actors, :names, :with =>
  [:timestamps, :userstamps, :acts_as_list, :acts_as_paranoid]

New addtion: You can now set defaults for your migrations!

So using defaults the above becomes:

  migratory_defaults [:userstamps, :timestamps, :acts_as_paranoid]

  create_table :actors
  create_table :names do |t|
    t.column "name", :string, :null => false
  end
  create_link_table :actors, :names, :with =>[:acts_as_list]

This makes dealing with migrations just a bit more managable, and you no longer need to remember all the "magic" column names. Just say "acts_as_list" and the "position" column is added for you.

Thus far the following options are supported:

    acts_as_list
    acts_as_tree
    acts_as_nested_set
    acts_as_paranoid
    timestamps
    userstamps

Enjoy!

Will Groppe

http://www.rubybrain.org/articles/4-DRYing-up-migrations-revisited

svn://rubyforge.org/var/svn/rubybrain/rails/plugins/migratory_shortcuts

PublicDomain

  • Currently 4.7/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Misc. Enhancements

Tags

Comments

Add a comment
Will groppe 13 Oct 2006

Oh thanks Wayne, that's a very interesting idea. I think I like that syntax even more. I'll have a new version out shortly.

Wayne 13 Oct 2006

Interesting. Would there be any value other than true for the options? How about this form:

createlinktable :actors, :names, :with=>[:timestamps, :userstamps, :actsas_list, :actsas_paranoid]

or

create_table :actors, :with=>:timestamps

ctran 13 Oct 2006

Neato! I have a different way to address this same concern but it's great to see a different approach.

Search Plugins

Query syntax

Plugins by Category

Sponsors

Rails Kits: Get Code. Get Moving.

Have a comment?