gpt4 book ai didi

ruby-on-rails - 在 db :schema:load 之后运行 future 的 Rails 迁移

转载 作者:行者123 更新时间:2023-12-05 06:42:39 26 4
gpt4 key购买 nike

我觉得这一定是一个过去已经回答过的问题,但我用谷歌找不到任何东西。

根据 Rails 指南:

There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire migration history. It is much simpler and faster to just load into the database a description of the current schema.

但是,当您通过架构文件加载数据库时,schema_migrations 表中将没有数据,因此如果不先运行整个迁移历史。

我是不是漏掉了什么?如果我从模式文件创建 Rails DB 的新实例,我如何才能针对它运行任何 future 的迁移?

最佳答案

最好设置一个带有模式加载的新数据库而不是运行旧的迁移,并且应该期望在运行 rake db:schema:load 时正确更新 schema_migrations 表。 .当你运行 rake db:schema:load应该根据 version 的值填充 schema_migrations 表传递给 ::define 的 key 对db/schema.rb 文件中的方法。

例如:

ActiveRecord::Schema.define(version: 20161208214643) do
end

应该将 schema_migrations 表更新到版本中列出的迁移。版本值应该在运行 rake db:schema:dump 后自动填充到 db/schema.rb 文件中。或 rake db:migrate (调用 db:schema:dump)。这允许任何 future 的迁移正常运行,正如预期的那样。

schema_migrations 表中的插入由 assume_migrated_upto_version 处理方法,如以下文件所示:

https://github.com/rails/rails/blob/5-0-stable/activerecord/lib/active_record/schema.rb#L52 https://github.com/rails/rails/blob/5-0-stable/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb#L1021

关于ruby-on-rails - 在 db :schema:load 之后运行 future 的 Rails 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36313924/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com