Migration Data Dumper plugin

Plugin details

This plugin adds the methods save_table_to_fixture and restore_table_from_fixture to ActiveRecord::Migration, which dump and restore the data from the specified table.

Websitehttp://blog.tammersaleh.com/pages/migration_data_dumper Repositorysvn://rubyforge.org/var/svn/mdd Author Tammer Saleh Tags migration LicenseUnknown

Documentation

Install the plugin:
ruby script/plugin install svn://rubyforge.org/var/svn/mdd

Example
==============

Let's say we create a posts table...

db/migration/001_posts_table.rb:

  class PostsTable < ActiveRecord::Migration
    def self.up
      create_table "posts", :force => true do |t|
        t.column "title", :string, :default => "", :null => false
        t.column "text", :text, :default => "", :null => false
      end

      restore_table_from_fixture("posts")
    end

    def self.down
      save_table_to_fixture("posts")
      drop_table "posts"
    end
  end


When you migrate down (via rake migrate VERSION=0), the data for the posts table is saved in db/data/development/posts.yml, and that data is pushed back into the table when you migrate back up (via rake migrate).

This works great until you have to edit an existing table. Consider adding a column to the posts
table:

db/migration/002_draft_flag_for_posts.rb:

  class DraftFlagForPosts < ActiveRecord::Migration
    def self.up
      add_column "posts", "draft", :integer, :limit => 4, :default => 1, :null => false  
      Post.reset_column_information
      Post.find(:all).each { |p| p.draft == 0 }
      restore_table_from_fixture("posts")
    end

    def self.down
      save_table_to_fixture("posts")
      remove_column "posts", "draft"
    end
  end


With these two migrations, if you run rake migrate VERSION=0, the following things will happen:

1. db/data/development/posts.yml will be created (by migration 002) with the latest version of the table (including the draft column).
2. db/data/development/posts.yml will be recreated (by migration 001) with the latest version of the table (including the draft column).

This is an error, but doesn't cause any real problems just yet. The problem arrises when you attempt to migrate back up (via rake migrate). The 001 migration attempts to restore the yaml file, but cannot, since the specified draft column doesn't exist in the table.

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: hardway, 9 months ago