使用 MyISAM 表进行 Rails 单元测试 [英] Rails Unit Testing with MyISAM Tables

查看:55
本文介绍了使用 MyISAM 表进行 Rails 单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序需要在几个表上使用 MyISAM,但其余的是传统的 InnoDB 类型.应用程序本身不关心它应用于这些记录的事务,但性能是一个问题.

I've got an application that requires the use of MyISAM on a few tables, but the rest are the traditional InnoDB type. The application itself is not concerned with transactions where it applies to these records, but performance is a concern.

Rails 测试环境假设使用的引擎是事务性的,因此当测试数据库从 schema.rb 生成时,它会使用相同的引擎导入.是否有可能以简单的方式覆盖这种行为?

The Rails testing environment assumes the engine used is transactional, though, so when the test database is generated from the schema.rb it is imported with the same engine. Is it possible to over-ride this behaviour in a simple manner?

我已经采取了一个可怕的技巧,通过将其附加到 test_helper.rb 来确保表是正确的类型:

I've resorted to an awful hack to ensure the tables are the correct type by appending this to test_helper.rb:

(ActiveRecord::Base.connection.select_values("SHOW TABLES") - %w[ schema_info ]).each do |table_name|
  ActiveRecord::Base.connection.execute("ALTER TABLE `#{table_name}` ENGINE=InnoDB")
end

是否有更好的方法使 MyISAM 支持的模型可测试?

Is there a better way to make a MyISAM-backed model be testable?

推荐答案

您可以编辑您的 schema.rb 并修改 create_table 调用以包含以下标志,如下所示:

You can edit your schema.rb and modify the create_table call to include the following flag, like so:

create_table(:suppliers, :options => 'ENGINE=InnoDB DEFAULT CHARSET=utf8')

创建迁移时,请尝试将其添加到迁移中.我不知道当你运行 rake db:schema:dump 时这是否会坚持下去.鉴于您的经验,测试环境似乎没有正确复制开发环境,它可能不会:(

When you create your migrations, try adding that to the migrations. I don't know if this will stick when you run rake db:schema:dump. Given your experience that the test environment doesn't seem to be copying the development environment properly, it may not :(

此处有关 create_table 选项的更多信息:

More info about create_table options here:

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#M001901

这篇关于使用 MyISAM 表进行 Rails 单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆