データベースをrakeで管理する場合、ストレージエンジンを指定してテーブルを作る時は、いちいちこんなのを書かなきゃいけない。
ちょっとした事なんだけど、非常に面倒くさい。
create_table :table_name, :options => "engine=MyISAM"
何かいい方法はないか調べたけど解決策はなさそう。
同じ所で困ってる方がいて、その方の記事が詳しかった。
migrateでストレージエンジンを指定 - よかろうもん!
http://d.hatena.ne.jp/interu/20080331/1206977231
記事はちょっと古いけど、現在我が家で使ってるActiveRecord 2.3.5もInnoDBをべた書きなのは変わってなかった。
def create_table(table_name, options = {}) #:nodoc: super(table_name, options.reverse_merge(:options => "ENGINE=InnoDB")) end
で、それを解消するプラグインを作った。
出来る事は、
・指定したストレージエンジンをデフォルトとして使う。
・指定したストレージエンジンを強制する。
の2つ。
RedmineとかオープンソースのものでMySQL Clusterを使いたい時に、ndbclusterを強制すればいい感じに動く。(かも知れない)
まだRuby on Rails始めてから数週間だけど、なんだか楽しくなってきた。
eth0jp's activerecord-engine_spec at master - GitHub
http://github.com/eth0jp/activerecord-engine_spec
インストール
確かこんな感じだった気がする。
sudo yum install git sudo gem install newgem ./script/plugin install git://github.com/eth0jp/activerecord-engine_spec.git
設定ファイルサンプル
config/initializersディレクトリの中にファイルを作る。
MyISAMをデフォルトのストレージエンジンとして指定する。
create_table呼び出し側でエンジンの指定があった場合は何もしない。
require 'active_record/engine_spec' ActiveRecord::EngineSpec.engine = :MyISAM ActiveRecord::EngineSpec.force = false
NDB Clusterをストレージエンジンとして使う事を強制する。
create_table呼び出し側でエンジンの指定があっても強制的に上書きする。
require 'active_record/engine_spec' ActiveRecord::EngineSpec.engine = :ndbcluster ActiveRecord::EngineSpec.force = true
migration実行結果
[root@localhost tmp]# rails testproj [root@localhost tmp]# cd testproj/ [root@localhost testproj]# ./script/plugin install git://github.com/eth0jp/activerecord-engine_spec.git [root@localhost testproj]# cat <<-'ENGINE_SPEC' > config/initializers/engine_spec.rb > require 'active_record/engine_spec' > ActiveRecord::EngineSpec.engine = :ndbcluster > ActiveRecord::EngineSpec.force = true > ENGINE_SPEC [root@localhost testproj]# vi config/database.yml [root@localhost testproj]# ./script/generate model test_model [root@localhost testproj]# rake db:migrate (in /tmp/testproj) == CreateTestModels: migrating =============================================== -- create_table(:test_models, {:options=>" engine=ndbcluster"}) -> 0.3325s == CreateTestModels: migrated (0.3327s) ====================================== [root@localhost testproj]# mysql -hxxx -uxxx -pxxx xxx mysql> show create table test_models; +-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | test_models | CREATE TABLE `test_models` ( `id` int(11) NOT NULL AUTO_INCREMENT, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=ndbcluster DEFAULT CHARSET=utf8 | +-------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)