Ruby on rails Can';t Rails 3在MySQL MyISAM模式下运行,没有InnoDB?
我有一个MySQL服务器运行时禁用了InnoDB(出于性能原因),在这种设置下,我似乎无法使用Rails 3(使用Ruby on rails Can';t Rails 3在MySQL MyISAM模式下运行,没有InnoDB?,ruby-on-rails,rails-migrations,Ruby On Rails,Rails Migrations,我有一个MySQL服务器运行时禁用了InnoDB(出于性能原因),在这种设置下,我似乎无法使用Rails 3(使用mysql2适配器) 以下是我的测试迁移: class CreateTxts < ActiveRecord::Migration def change create_table(:txts, :options => 'ENGINE=MyISAM') do |t| t.timestamps end end end 尝试了上述解决方法,但
mysql2
适配器)
以下是我的测试迁移:
class CreateTxts < ActiveRecord::Migration
def change
create_table(:txts, :options => 'ENGINE=MyISAM') do |t|
t.timestamps
end
end
end
尝试了上述解决方法,但似乎也不起作用(我确实将MysqlAdapter修改为Mysql2Adapter以匹配我的设置)
对不起,我是Rails的新手。任何帮助都将不胜感激:o错误来自创建
schema\u migrations
表(rails使用该表跟踪已运行的迁移),而不是您的表。您可以自己创建该表(使用一个名为version
的varchar(255)列,该列上有一个索引)
如果您最终覆盖了create\u table
方法,则需要保留该方法的签名-您忽略了它产生的块。我想试试这样的东西
def create_table(name, options={})
super(name, options.merge(...)) {|t| yield t}
end
尝试创建表,但不指定正在使用的引擎类型,如下所示
class CreateTxts < ActiveRecord::Migration
def change
create_table(:txts) do |t|
t.timestamps
end
end
end
希望它能帮助我回答我自己的问题。下面是我最后使用的
environment.rb
补丁,它与本机mysql驱动程序以及JRuby/JDBC mysql一起工作:
# Load the rails application
require File.expand_path('../application', __FILE__)
# Patch Mysql adapter to default to MyISAM instead of InnoDB
require 'active_record/connection_adapters/mysql_adapter'
module ActiveRecord
module ConnectionAdapters
class MysqlAdapter
def create_table(table_name, options = {}) #:nodoc:
super(table_name, options.reverse_merge(:options => "ENGINE=MyISAM"))
end
end
end
end
# Initialize the rails application
.....
rakedb:migrate
现在成功并创建所有表,包括TYPE=MyISAM的schema\u迁移
注意:对于mysql2适配器,将mysql_适配器重命名为mysql2_适配器,将MysqlAdapter重命名为Mysql2Adapter。@rustyx nice one&这是我的一个稍微调整过的monkey补丁,因此可以从/config/database.yml
配置中设置引擎:
&我不是在/config/environment.rb
中,而是将代码放入一个初始值设定项中,例如:/config/initializers/mysql2adapter\u default\u engine.rb
为了简洁起见:
require 'active_record/connection_adapters/abstract_mysql_adapter'
class ActiveRecord::ConnectionAdapters::Mysql2Adapter < ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
def create_table(table_name, options = {}) #:nodoc:
super(table_name, @config[:engine].nil? ? options : options.reverse_merge(:options => "ENGINE="+@config[:engine]))
end
end
如果未指定engine
,则mysql2
适配器将使用默认值(InnoDB
或其他任何值) 我没有帮助,因为InnoDB在我的MySQL服务器中被禁用。不,在最初的MysqlAdapter的create_表中没有yield块:Oops,对super的行为感到困惑。您是否尝试自己创建架构迁移表?您可以将模块ActiveRecord模块ConnectionAdapters类MysqlAdapter
替换为类ActiveRecord::ConnectionAdapters::MysqlAdapter
# Load the rails application
require File.expand_path('../application', __FILE__)
# Patch Mysql adapter to default to MyISAM instead of InnoDB
require 'active_record/connection_adapters/mysql_adapter'
module ActiveRecord
module ConnectionAdapters
class MysqlAdapter
def create_table(table_name, options = {}) #:nodoc:
super(table_name, options.reverse_merge(:options => "ENGINE=MyISAM"))
end
end
end
end
# Initialize the rails application
.....
require 'active_record/connection_adapters/abstract_mysql_adapter'
class ActiveRecord::ConnectionAdapters::Mysql2Adapter < ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
def create_table(table_name, options = {}) #:nodoc:
super(table_name, @config[:engine].nil? ? options : options.reverse_merge(:options => "ENGINE="+@config[:engine]))
end
end
require 'active_record/connection_adapters/abstract_mysql_adapter'
module ActiveRecord
module ConnectionAdapters
class Mysql2Adapter < AbstractMysqlAdapter
def create_table(table_name, options = {}) #:nodoc:
super(table_name, @config[:engine].nil? ? options : options.reverse_merge(:options => "ENGINE="+@config[:engine]))
end
end
end
end
production:
adapter: mysql2
encoding: utf8
database: ooook
username: ooook
password: ooook
host: ooook.tld
port: 3306
pool: 5
timeout: 5000
engine: MyISAM