version: Rails 4.1
# db/migrate/20100513121110_add_flag_to_product.rb class AddFlagToProduct < ActiveRecord::Migration
def change
add_column :products , :flag , :boolean
reversible do |dir|
dir.up { Product.update_all flag: false }
end
end
end |
她同样地也给 Product的一个新的列 增加了一个验证:
# app/models/product.rb class Product < ActiveRecord::Base
validates :flag , inclusion: { in : [ true , false ] }
end |
Alice 增加第二个 migration,用来在 products 数据表中增加另一个列,然后再初始化它:
# db/migrate/20100515121110_add_fuzz_to_product.rb class AddFuzzToProduct < ActiveRecord::Migration
def change
add_column :products , :fuzz , :string
reversible do |dir|
dir.up { Product.update_all fuzz: 'fuzzy' }
end
end
end |
她同样地也对于 Product 模型的一个新的列增加了一个验证:
# app/models/product.rb class Product < ActiveRecord::Base
validates :flag , inclusion: { in : [ true , false ] }
validates :fuzz , presence: true
end |
两个 migrations 都是为 Alice 工作的。
Bob 从假期中回来了:
- 更新了代码—— 这其中包含了两个 migrations 和最近版本的 Product 模型。
- 运行未被执行过的 migrations 用 rake db:migrate,包括那个 Product 模型的更新。
migration 有冲突了,因为当模型想要保存的时候,尝试着去验证第二个新增的列,当第一个 migration 运行的时候,这一列是不存在与数据库中的:
rake aborted! An error has occurred, this and all later migrations canceled: undefined method `fuzz' for #<Product:0x000001049b14a0> |
一个解决的办法就是,在一个 migration 中创建一个本地模型。这使得让 Rails 持续地执行验证,因此 migrations 可以完整的运行了。
当使用一个本地模型时,比较好的做法是调用 Product.reset_column_information 来刷新 Active Record 的缓存,为了 Product 模型在数据库中优先更新数据。
如果 Alice 已经这样做了,那就不会存在问题了:
# db/migrate/20100513121110_add_flag_to_product.rb class AddFlagToProduct < ActiveRecord::Migration
class Product < ActiveRecord::Base
end
def change
add_column :products , :flag , :boolean
Product.reset_column_information
reversible do |dir|
dir.up { Product.update_all flag: false }
end
end
end |
# db/migrate/20100515121110_add_fuzz_to_product.rb class AddFuzzToProduct < ActiveRecord::Migration
class Product < ActiveRecord::Base
end
def change
add_column :products , :fuzz , :string
Product.reset_column_information
reversible do |dir|
dir.up { Product.update_all fuzz: 'fuzzy' }
end
end
end |
在上面的例子中,可能还有其他的错误。
例如,想象一下,Alice 创建了一个 migration ,有选择地更新某些 products 的 description 字段。她运行 migration,提交代码,然后开始工作于下一个任务,在 products 数据表中增加一个新的列。
对于这个新的任务,她创建了两个 migrations,一方面是增加了新的列,另一方面是有选择地更新 fuzz 列,基于 product 的其他特性。
这些 migrations 可以很好地执行,但当 Bob 从他的假期中归来,调用 rake db:migrate 去运行所有未执行过的 migrations,他得到了一个不可思议的 bug :description 字段中有默认值,fuzz 列是显示的,但 所有的products中 fuzz 的值是nil。
相关推荐
mongoid_rails_migrations, Mongoid的数据迁移 发行说明最新版本,1.1. x, 目标 Mongoid> = 4.0.0和 Rails> = 4.2.0.对于 Rails> = 3.2.0和 Mongoid> = 3.0.0,使用版本 1.0.0.
Mongoid的数据迁移。 宝石文件: gem "mongoid_rails_migrations" 如何使用 创建迁移 $ rails generate mongoid:migration <your> 运行迁移: $ rails db:migrate $ rails db:migrate:down VERSION= $ rails db:...
适用于Nova的Rails迁移扩展提供命令以轻松访问Rails项目中的迁移。特征快速打开最新的Rails迁移从您的迁移列表中选择一个特定的迁移安装下载最新。 解压缩档案。 双击RailsMigrations.novaextension将扩展安装在Nova...
Rails版本使用此宝石的版本Rails 4.x,4.1.x,4.2 v1.0.1(2019年6月1日发布) Rails 5.0 v2.0.1(2019年6月1日发布) Rails 5.1 v3.0.1(2019年6月1日发布) Rails 5.2 v4.0.2(2019年6月2日发布) Rails 6.0 v...
good_migrations 警告:good_migrations当前不适用于zeitwerk,这是Rails中的当前自动加载器实现。 参见 ... 这意味着,如果您的迁移引用了您在app/models定义的ActiveRecord模型对象,则旧的迁移可能
目前,持久性框架使用两种方法中的一种:映射或包装 。要有效地管理涉及持久性域模型的应用程序变化,必须协调数据、模式和模型的变化。Ruby on Rails是不断发展的Web开发框架,它实现了一些先进的想法,例如通过...
Get your front end working with Webpack, use Postgres' features from migrations, and write unit tests for all of it. All of this within Rails 5. You'll gain the confidence to work at every level of ...
Rails is a full-stack, open source web framework powered by Ruby. Now in version 4, Rails is mature and powerful, and to use it effectively you need more than a few Google searches. You'll find no ...
“PHP 数据库迁移”使您能够拥有不同版本的数据库模式。 每个迁移文件都会改变架构,这个概念来自 Ruby on Rails。 可以在所有数据库服务器上执行迁移,确保一致性和可靠性。
Detection of critical defects in rails using ultrasonic surface wave
在6.x版本中,由于增加了对Rails 6迁移的支持。 在5.x版本中,我们转向使用Rails 5迁移,而不是维护我们自己的迁移相关代码。 使用Rails 5迁移几乎可以做的任何事情,现在也可以使用! 贡献 依赖于开源社区的贡献!...
Rails 3 in Action 2011年9月新鲜出炉,针对最新的Rails 3.1进行说明
8 Setting up a Todo application in Rails 8 Analyzing folder structure of a Rails application 10 Creating views 12 Styling views using CSS 14 Redesigning the Todo application 16 Challenges in styling ...
You’ll learn how to create something visible with Rails’ view layer before diving into the more difficult inner layers: the database models and controller code. All you need to begin your Rails ...
迁移 sql 文件名必须包含时间戳和类似20150118140555_DatabaseStructure.sql的描述该脚本创建一个名为migrations的数据库表,并将已处理的迁移文件时间戳存储到此表中。 下次运行时,它不会运行基于migrations表的...
允许您在非Rails项目中使用ActiveRecord迁移。 安装 将此行添加到应用程序的Gemfile中(如果没有,请运行bundle init ): gem 'active_record_migrations' gem 'sqlite3' # or 'pg', 'mysql2', ... 然后执行: ...
Rails 4 in Action, Second Edition.pdf
没有Rails的ActiveRecord 只是在没有Rails的情况下使用ActiveRecord迁移的简单示例您可以执行的任务: rake db:create rake db:migrate rake db:dropRails 5+的注意事项请注意,即使使用Rails 5,您也需要rake db:...
As a Rails developer, you care about user experience and performance, but you also want simple and maintainable code. Achieve all that by embracing the full stack of web development, from styling with...