ActiveRecord :: Schema.define info:schema.rb中的版本 - >任何需要顺序?

时间:2011-11-27 23:20:51

标签: ruby-on-rails rails-migrations

在rails db creation script schema.rb中,顶部有这一行:

ActiveRecord::Schema.define(:version => 20111127090505) do

文档(http://api.rubyonrails.org/classes/ActiveRecord/Schema.html)说 info hash param是可选的

  • 什么是:用于的版本?
  • 这个号码是否需要在登记时增加?即,当天晚些时候登记会使版本号下降......
  • 如果存在时间戳大于给定的迁移,它们是否会运行只是因为它们不在迁移表中,而类文件是否存在?
  • 迁移文件是否按顺序运行?

2 个答案:

答案 0 :(得分:4)

该版本用于确定最后运行的迁移。这只会随着时间的推移而增加。您在此处运行的迁移是在2011年11月27日上午09:05:05在UTC时间创建(而非运行)。这就是数字:时间戳。

每次运行新创建的迁移时,这个数字都会增加,这样Rails就会知道哪一个最后运行,哪一个运行下一个。下一个要运行的迁移将是第一个具有比该数字更大的数字的迁移。

是的,迁移文件按创建顺序运行。

答案 1 :(得分:0)

从基于分支的开发(使用 git 和 github PR)的角度来看,我也想知道这个问题。

如下所示,rails 确实处理架构版本中没有变化的情况,并且会正确地注意到尚未应用较旧的迁移。


鉴于功能分支“F”的以下变基:

o
| \
|  F  # migration on Feature branch
A     # other migration merged to main first

功能分支的变基会导致版本的 schema.rb 发生冲突。按照 Managing conflict in schema.rb created by Git operation 使用 migrate+dump 解决:

$ git checkout main
$ rails db:migrate      # apply new migration from commit A on main branch
$ git checkout feature
$ git rebase main       # stops on conflict
$ rails db:migrate && rails db:schema:dump  # overwrite conflicted schema file
$ git add db/schema.rb  # mark conflict as resolved
$ git rebase --continue
o
|
|
A     # other migration merged to main first
F'    # rebased B, no longer has diff on schema

然后检查迁移状态,表明 rails 确实 了解在不依赖 schema.rb 中的版本信息的情况下,“较旧的”迁移之一尚未运行。

$ rails db:migrate:status | tail -n 5
   up     20210609094438  Add access booleans to token
  down    20210610090328  Add uplift to early career teacher profile
   up     20210611133458  Change default api token type
   up     20210611141524  Update e and l tokens with private access

生成的架构补丁,注意版本没有差异:

diff --git a/db/schema.rb b/db/schema.rb
index 4523b11b..c7bbd6e0 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -153,0 +154 @@ ActiveRecord::Schema.define(version: 2021_06_11_141524) do
+    t.boolean "uplift", default: false, null: false

Rails 6.1.3.2

上测试