Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails migration, change table to MyISAM

How does one create a Rails migration properly so that a table gets changed to MyISAM in MySQL? It is currently InnoDB. Running a raw execute statement will change the table, but it won't update db/schema.rb, so when the table is recreated in a testing environment, it goes back to InnoDB and my fulltext searches fail.

How do I go about changing/adding a migration so that the existing table gets modified to MyISAM and schema.rb gets updated so my db and respective test db get updated accordingly?

like image 632
randombits Avatar asked Oct 13 '10 13:10

randombits


2 Answers

I didn't find a great way to do this. You could change your schema.rb like someone suggested and then run: rake db:schema:load, however, this will overwrite your data.

The way I did it was (assuming you are trying to convert a table called books):

  1. Save the existing data from the CLI: CREATE TABLE tmp SELECT * FROM books;

  2. In your new migration file, drop the books table and recreate it with :options => "ENGINE=MyISAM" like someone said in the comment

  3. Copy the contents back: INSERT INTO books SELECT * FROM tmp

like image 164
Dex Avatar answered Sep 22 '22 14:09

Dex


i think that if you change your schema format (config.active_record.schema_format) from :ruby to :sql, all sql will be saved there.

i'd do some tests on a fresh app first if i were you, see how it works.

like image 25
Elad Meidar Avatar answered Sep 21 '22 14:09

Elad Meidar