当我使用自定义sql进行迁移在开发环境中工作正常并且生产失败时,我只是遇到了问题。
我的迁移看起来像这样:
class DbPatch < ActiveRecord::Migration
def up
execute("START TRANSACTION")
execute("update users set status = 1 where status = 3")
execute("COMMIT")
end
def down
execute("START TRANSACTION")
execute("update users set status = 3 where status = 1")
execute("COMMIT")
end
end
生产环境迁移失败,出现下一条错误消息:
== 20140627155848 DbPatch: migrating =================================
-- execute("START TRANSACTION")
-> 0.0001s
-- execute("update users set status = 1 where status = 3")
-> 0.0003s
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
undefined method `-@' for nil:NilClass/var/www/app/releases/20140628083752/db/migrate/20140627155848_db_patch.rb:4:in `up'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:598:in `exec_migration'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:578:in `block in migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:577:in `migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:752:in `migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:994:in `block in execute_migration_in_transaction'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:1042:in `ddl_transaction'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:993:in `execute_migration_in_transaction'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:954:in `block in migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:950:in `each'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:950:in `migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:808:in `up'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:785:in `migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
答案 0 :(得分:0)
在ActiveRecord资源中进行了半小时的研究后,我发现问题出现在我的习惯中,即“开始交易”。
看起来rails会自动执行事务中生产环境的所有迁移。
所以下一个代码的工作正确:
class DbPatch < ActiveRecord::Migration
def up
execute("update users set status = 1 where status = 3")
end
def down
execute("update users set status = 3 where status = 1")
end
end
我在这里发布这个主题是为了不仅在脑子里保持知识:)