简体   繁体   English

如何将已签入到git的Rails迁移还原

[英]How to revert a Rails migration that has been checked in to git

On my development/feature branch, I have a commit with 20150818113106_add_team_id_to_account.rb (which adds a column to Account ) & the schema.rb that results from applying the migration. 在我的development/feature分支上,我提交了20150818113106_add_team_id_to_account.rb (将一列添加到Account )和由于应用迁移而产生的schema.rb This commit has been pushed, & also exists on origin/development/feature . 该提交已被推送,并且也存在于origin/development/feature

Now I want to revert this change- I do not want to add this column to Account after all. 现在,我要恢复此更改-毕竟我不想将此列添加到“ Account I can't work out if I should therefor revert the git commit, or run a db:migrate:down & commit the results of that, or both, or neither. 如果无法恢复git commit,或者运行db:migrate:down并提交结果,或者两者都执行,或者都不执行,则无法解决。

You can just create another migration file with the below command which removes the column. 您可以使用以下命令删除列,然后再创建另一个迁移文件。

rails g migration remove_team_id_from_accounts team_id:integer

Later do rake db:migrate commit it and push to git. 稍后做rake db:migrate提交并推送到git。

Solution (1) 解决方案(1)

You can run the following to rollback the migration: 您可以运行以下命令来回滚迁移:

rake db:migrate:down VERSION=2015081811310

Then delete the migration from the repository as the following: 然后,从存储库中删除迁移,如下所示:

git rm 20150818113106_add_team_id_to_account.rb

Then Push these changes, and make these steps again if you deployed on server. 然后推送这些更改,如果您已部署在服务器上,请再次执行这些步骤。

Solution (2) 解决方案(2)

Create another migration to remove this column as the following: 创建另一个迁移以删除此列,如下所示:

rails g migration remove_team_id_from_accounts team_id:integer 

Then rake db:migrate , then commit & Push 然后rake db:migrate ,然后提交并推送

You've run into a common problem here with rails: 您在这里遇到了Rails的常见问题:
The database state and the program code state are different things. 数据库状态和程序代码状态是不同的东西。

Try to separate the two concepts. 尝试将两个概念分开。

Normally with code you can just revert unneeded code. 通常,使用代码,您可以只还原不需要的代码。

Here however you need to update the database status as well. 但是,在这里您还需要更新数据库状态。

If the migration up has already been run there are two options: 如果已经运行向上迁移,则有两个选项:

  1. Use rake db:down to reverse the migration. 使用rake db:down撤消迁移。 This can be a good option if the commit is the most recent one made and the migration to add the column was just run. 如果提交是最近一次的提交,并且刚刚运行了添加列的迁移,那么这可能是一个不错的选择。

  2. Use another migration to remove the column. 使用另一个迁移来删除该列。 This can be the cleanest approach especially if the other migration is not the most recent. 这可能是最干净的方法,尤其是在其他迁移不是最近的迁移时。

If the migration had not been run on your database instance then you could just revert the code commit or in more complicated cases make a new commit to remove the code. 如果迁移尚未在数据库实例运行,那么你可以只恢复的代码提交或在更复杂的情况下,作出新的承诺删除代码。

As to which to choose that may depend on whether the migration had been run anywhere else (other than your local environment). 至于选择哪个选项,可能取决于迁移是否已在其他任何地方(本地环境除外)运行。 If the migration has been run in CI for staging, or pulled for other developers who have run it, then you may wish to choose the 'leave code and add a reversing migration' rather than running the down locally and then trying to revert the code commit. 如果迁移已在CI中运行以进行登台,或已为运行该迁移的其他开发人员拉动,则您可能希望选择“保留代码并添加一个反向迁移”,而不是在本地运行下来然后尝试恢复代码承诺。 Once the migration has been run elsewhere you're probably better off with original migration, plus another migration (I was going to call this a down migration but that is incorrect, these will be two up migrations) to remove the column with no reverts. 一旦在其他地方进行了迁移,您可能会更好地进行原始迁移,再加上另一次迁移(我将其称为向下迁移,但这是不正确的,这将是两次向上迁移),以删除没有还原的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM