Rails如何跟踪为数据库运行了哪些迁移? [英] How does Rails keep track of which migrations have run for a database?

查看:62
本文介绍了Rails如何跟踪为数据库运行了哪些迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Rails文档: http://guides.rubyonrails.org/migrations.html

According to Rails doc: http://guides.rubyonrails.org/migrations.html

Active Record跟踪已经运行了哪些迁移,因此您要做的就是更新源代码并运行rake db:migrate。

"Active Record tracks which migrations have already been run so all you have to do is update your source and run rake db:migrate."

ActiveRecord实际上是如何做到的? Active Record会将数据存储在哪里?

How does ActiveRecord actually do this? Where does Active Record store the data?

我怀疑这可能存储在数据库本身中?在某处的表中。

I suspect this might be stored in the database itself? In a table somewhere.

在开发计算机上,我运行了所有迁移。然后,我使用mysqldump复制了生产数据库。然后我运行 rake db:migrate:status,它正确显示了需要在生产数据库上运行的迁移。

On my development machine, I ran all the migrations. Then I copied the production database over using mysqldump. Then I ran "rake db:migrate:status", it shows correctly the migrations that need to run on the production database.

我以前认为ActiveRecord可以跟踪最后一次迁移使用时间戳运行。但是我认为这是不对的,因为ActiveRecord可以正确运行从另一个代码分支合并的较旧迁移。

I used to think that ActiveRecord keeps track of the last migration run using the timestamp. But I think this is not true because ActiveRecord correctly runs the "older" migrations merged in from another code branch.

有内部知识的人可以对此进行详细说明吗?
谢谢

Could someone with inside knowledge of this elaborate? Thanks

推荐答案

Rails在您的数据库中创建了一个名为 schema_migrations 跟踪运行了哪些迁移。

Rails creates a table in your database called schema_migrations to keep track of which migrations have run.

该表包含一个列,即 version 。 Rails进行迁移时,它将迁移文件名中的前导数字插入,并为该版本插入一行,表示已运行。如果回滚该迁移,Rails将从 schema_migrations 中删除​​相应的行。

The table contains a single column, version. When Rails runs a migration, it takes the leading digits in the migration's file name and inserts a row for that "version", indicating it has been run. If you roll back that migration, Rails will delete the corresponding row from schema_migrations.

例如,运行迁移名为 20120620193144_create_users.rb 的文件将在 schema_migrations中插入版本为 20120620193144 的新行。 表。

For example, running a migration file named 20120620193144_create_users.rb will insert a new row with a version of 20120620193144 into the schema_migrations table.

您随时可以使用较早的版本进行迁移。 Rails将始终运行 schema_migrations 中没有相应行的所有新迁移。前导数字不必是时间戳,您可以将迁移称为 001_blah.rb 。 Rails的早期版本使用这种格式,并对新生成的迁移使用顺序编号。更高版本已切换到时间戳,以帮助防止多个开发人员独立生成具有相同编号的迁移。

You are free at any point to introduce migrations with earlier versions. Rails will always run any new migrations for which there is not a corresponding row in schema_migrations. The leading digits don't have to be a timestamp, you could call your migration 001_blah.rb. Earlier versions of Rails used this format, and used sequential numbering for newly generated migrations. Later versions have switched to timestamps to help prevent multiple developers from independently generating migrations with the same number.

这篇关于Rails如何跟踪为数据库运行了哪些迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆