所以我做了一个这样的迁移
class AddDatetimeAttrToUsers < ActiveRecord::Migration
def change
change_column :users, :oauth_expires_at, :datetime
end
end在我的本地环境中,它运行得很好,但是当我尝试的时候
heroku运行rake db:迁移,我得到一个错误
ERROR: column "oauth_expires_at" cannot be cast automatically to type timestamp without time zone
HINT: Specify a USING expression to perform the conversion.当我搜索它时,我创建了这样一个新的迁移,作为使用change更改属性的最佳实践。
class PutDatetimeFieldToUsersExpireAtColumn < ActiveRecord::Migration
def change
remove_column :users, :oauth_expires_at
add_column :users, :oauth_expires_at, :datetime
end
end因此,我尝试使用rake :rollback删除最后一次迁移,并添加此迁移,告诉我最后一次迁移是不可逆转的。
我的问题是,是否有办法真正回滚不可逆转的迁移,还是应该使用上面的新迁移进行迁移?
发布于 2018-06-27 11:28:03
在您的示例中,您有以下迁移文件:
class AddDatetimeAttrToUsers < ActiveRecord::Migration
def change
change_column :users, :oauth_expires_at, :datetime
end
end您已经在开发环境中成功地运行了rake db:migrate。您正在本地运行sqlite3,在heroku上运行PG,因此您的change_column在本地工作,但是它在PG上失败了,因为PG需要一个“使用”语句。
修复这个问题,步骤1是编辑您的迁移文件,以添加上面Yohann建议的上下迁移。是的,即使您已经对此迁移进行了扫描,也应该这样做。
class AddDatetimeAttrToUsers < ActiveRecord::Migration
def up
change_column :users, :oauth_expires_at, :datetime
end
def down
change_column :users, :oauth_expires_at, :time (or whatever type existed before datetime)
end
end现在您可以运行rake db:rollback并避免不可逆转的迁移错误,但前提是您没有添加其他迁移。如果添加了其他迁移,则需要指定使用rake db:down VERSION=2018xxxxxxx或rake db:rollback STEP=X可追溯到多远。
现在,编辑迁移,这样它就可以很好地处理pg和sqlite3:
class AddDatetimeAttrToUsers < ActiveRecord::Migration
def up
change_column :users, :oauth_expires_at, :datetime, using: 'oauth_expires_at::datetime'
end
def down
change_column :users, :oauth_expires_at, :time
end现在,您应该能够对db:migrate,push到heroku,以及heroku运行rake :migrate并继续前进。
最后,您应该让pg在本地工作,以与您的生产环境相匹配。
https://stackoverflow.com/questions/31867927
复制相似问题