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

查看:19
本文介绍了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天全站免登陆