ruby-on-rails - Rails 迁移顺序和 Git

标签 ruby-on-rails git migration rails-migrations

由于使用 rails + git 进行迁移是一种痛苦,新的荆棘如雨后春笋般涌现..

在我对我的产品数据库造成任何伤害之前,以下情况会造成破坏吗?如果是这样,我将如何处理?

我正在一个单独的分支 (feature/long-term) 中开发长期功能。此功能是对很多组件的大修,需要一段时间才能完成。此功能有新的迁移,已迁移到本地主机数据库。

与此同时,我需要通过另一个分支 (feature/quick-fix) 修复/添加到 prod 系统的迁移。这有一个迁移文件,其日期晚于 feature/long-term 迁移。

quick-fixlong-term 的迁移彼此无关,它们不会冲突并在不同的表上工作。它们的运行顺序无关紧要。

如果我将 feature/quick-fix merge 到 master 和 db:migrate 并在几天/几周内 merge feature/long-term 迁移文件顺序将是 long-term 优先。

这会以某种方式影响数据库吗? (产品数据库很重要,所以我不想重置)

最佳答案

您所描述的是一个非常常见的开发工作流程(尤其是在成员较多的团队中)并且它对您的生产数据库来说是绝对安全的。

从 2.1 版开始,Rails 足够智能,可以保留所有运行过的迁移的列表,而不仅仅是运行的最新迁移版本。此信息存储在一个名为 schema_migrations 的单独表中。

因此,如果您今天推送一个新的迁移,比如 20140527_quick_fix.rb,一个月后您推送一个新的(但具有较旧的时间戳)20140101_long_term_feature.rb,Rails 仍会知道后者从未在您的生产环境中运行,因此在 rake db:migrate 期间它会处理它,正如您所期望的那样。最新的当然不会再次运行,因为 Rails 会知道它已经被处理过。

来自official documentation :

Rails versions 2.0 and prior used to create a table called schema_info when using migrations. This table contained the version of the schema as of the last applied migration.

Starting with Rails 2.1, the schema_info table is (automatically) replaced by the schema_migrations table, which contains the version numbers of all the migrations applied.

As a result, it is now possible to add migration files that are numbered lower than the current schema version: when migrating up, those never-applied “interleaved” migrations will be automatically applied, and when migrating down, never-applied “interleaved” migrations will be skipped.

关于ruby-on-rails - Rails 迁移顺序和 Git,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23886048/

相关文章:

Git Staged - 它有文件历史吗?

git - 未显示大型 github 提交差异

ruby-on-rails - 如何更改 Rails 应用程序数据?

ruby-on-rails - 使用 nested_form 获取对象值

javascript - 如何在数组中换行或打断?

ruby-on-rails - Sidekiq 不会在 ec2 服务器重启时启动

git - 由于正在创建 @tmp 路径,Jenkins 管道作业找不到脚本

android - 我在哪里可以找到默认的 Realm 数据库文件

android - 在没有 Samsung Cloud 帐户的情况下迁移 Samsung Health 数据

ruby-on-rails - 如何在 Heroku Postgres 数据库上启用 contrib 模块