upgrade auto_migration to rails edge

Rails 升级到 edge 以后出了自己的代码要重构,很多 plugins 也会出问题,今天就碰上一个: auto_migration,问题发现是 rake db:auto:migrate 之后出错提示 schema_info_table_name 是 undefined method,问题定位在auto_migrations.rb 的 update_schema_version() 中。

查 Rails 文档发现原来数据库中记录 migration 版本的 schema_info 表变成了 schema_migraions,看看源代码 activerecord/lib/active_record/migration.rb 中,ActiveRecord::Migrator 中也没有了 schema_info_table_name,而变成了 schema_migraions_table_name,找到问题所在!

修改原来的 plugin 很简单,此时又 Google 到别人也遇到这问题,并且在 Github 上 fork 出一个解决方案,对 update_schema_version() 的处理比较完美:

def update_schema_version(version)
      schema_info_table_name = ActiveRecord::Migrator.schema_info_table_name rescue ActiveRecord::Migrator.schema_migrations_table_name
      ActiveRecord::Base.connection.update("UPDATE #{schema_info_table_name} SET version = #{version}")
end

相比之下,原作者的解决太直接了:

def update_schema_version(version)
      ActiveRecord::Base.connection.update(”INSERT INTO schema_migrations VALUES (’#{version}’)”)
end

在老版本的 Rails 下又会出问题吧?

这个问题从发现到彻底弄明白,大概30分钟。在 Rails 升级的过程中这样的问题可能会遇到很多,刚接触 Rails 的 dev 面对这样的问题八成会比较棘手,看来 Rails 光提供一个框架还不够,对于 Web 开发中很多的常见问题如果能整理出一套解决方案,我相信一定会有更多的同学加入 Rails 社区。

Tags: , , ,

One Response to “upgrade auto_migration to rails edge”

  1. Suave’s Blog » » Ruby ecosystem in China Says:

    [...] Rails 的升级不断升级,这个问题,问题是你用的 plugin 随着 Rails 升级也会出现不兼容,这些问题不会有人主动告诉你,需要去 Google [...]

Leave a Reply