Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Migration to change all MySQL table engines to InnoDB (See related posts)

I have a database with an eclectic mix of table types/engines - almost random between MyISAM and InnoDB. I wanted to get transactional, so I wrote this migration to bring everything in line.

class ChangeTableTypes < ActiveRecord::Migration
  
  def self.up
    ActiveRecord::Migration::say 'Setting all tables to InnoDB engine (excluding schema_info table)...'
    result = ActiveRecord::Migration::execute 'show tables'
    while table = result.fetch_row
      execute("ALTER TABLE #{table.to_s} TYPE = InnoDB") unless table.to_s == 'schema_info'
    end
  end

  def self.down
    raise ActiveRecord::IrreversibleMigration
  end
end


Barry Hess

Comments on this post

jerome posts on Nov 16, 2007 at 05:00
ActiveRecord::Base.connection.tables do |table|
...
jerome posts on Nov 16, 2007 at 05:04
mysql back quotes are also missing:

ActiveRecord::Base.connection.tables.reject { |t| t == "schema_info" } do |table|
  execute("ALTER TABLE `#{table}` TYPE = InnoDB")
end

You need to create an account or log in to post comments to this site.


Click here to browse all 4861 code snippets

Related Posts