如何跨git分支管理对数据库的更改?

时间:2022-03-05 20:32:50

So, I have an app that I am experimenting with.

所以,我有一个我正在尝试的应用程序。

The current state is good on my develop branch. I want to install spree, but that comes with many migrations and changes to the db.

我的开发分支目前状态良好。我想安装spree,但随之而来的是db的许多迁移和更改。

So I made a new branch, just for Spree, based on my develop branch.

所以我根据我的开发分支为Spree创建了一个新的分支。

I installed the gem, ran the migrations, and all that stuff.

我安装了gem,运行了迁移,以及所有这些东西。

However, I messed up a few stuff, so I wanted to revert to my develop branch and deleted the spree branch.

但是,我搞砸了一些东西,所以我想恢复到我的开发分支并删除了spree分支。

I assumed that my schema.rb would return to normal and so would my DB.

我假设我的schema.rb会恢复正常,我的数据库也会恢复正常。

But then I look in my Schema.rb and I see a lot of Spree tables.

但后来我查看了我的Schema.rb,我看到很多Spree表。

Not only that, but I also see other tables in my Schema.rb that I know I had created a migration for and 'dropped' in the spree branch.

不仅如此,我还在Schema.rb中看到了其他表,我知道我已经在spree分支中创建了一个迁移并“删除”了。

So, for instance, I have an orders table in my Schema.rb that I know should have been dropped. A bit confused, I decide to create a new migration to drop the orders table along with the other tables that I dropped before installing spree and I get a PG error saying that the table doesn't exist.

所以,例如,我在Schema.rb中有一个订单表,我知道应该删除它。有点困惑,我决定创建一个新的迁移来删除订单表以及我在安装spree之前删除的其他表,并且我得到PG错误,表示该表不存在。

So....it seems that right now, my schema.rb and DB don't sync up.

所以....现在看来,我的schema.rb和DB不同步。

And to make matters worse, my schema.rb and my db/migrate folder doesn't sync up.

更糟糕的是,我的schema.rb和我的db / migrate文件夹没有同步。

I was going to manually delete the create_table statements in my schema.rb for the tables that shouldn't be there - but then it's all a mess.

我打算在schema.rb中手动删除不应该存在的表中的create_table语句 - 但是这一切都很乱。

How do I dig myself out of this hole and how do I prevent it in the future?

我如何从这个洞中挖掘自己,以后如何防止它?

In Summary

DB

D B

Pre-Spree State

前狂欢状态

  • Table A
  • 表A.
  • Table B
  • 表B.
  • Table C
  • 表C.

Spree State

狂欢状态

  • Table A (removed)
  • 表A(已删除)
  • Table B (removed)
  • 表B(已删除)
  • Table C
  • 表C.
  • Spree_Table_A
  • Spree_Table_A
  • Spree_Table_B
  • Spree_Table_B

Post-Spree State

后疯狂国家

  • Table A (removed)
  • 表A(已删除)
  • Table B (removed)
  • 表B(已删除)
  • Table C
  • 表C.
  • Spree_Table_A
  • Spree_Table_A
  • Spree_Table_B
  • Spree_Table_B

Basically identical to the Spree State

基本上与狂欢状态相同

DB/Migrate Folder

DB / Migrate文件夹

Pre-Spree State

前狂欢状态

  • Migration for Table A
  • 表A的迁移
  • Migration for Table B
  • 表B的迁移
  • Migration for Table C
  • 表C的迁移

Spree State

狂欢状态

  • Migration to Remove Table A
  • 迁移到删除表A.
  • Migration to Remove Table B
  • 迁移到删除表B.
  • Migration for Table C
  • 表C的迁移
  • Migration for Spree_Table_A
  • 迁移Spree_Table_A
  • Migration for Spree_Table_B
  • 迁移Spree_Table_B

Post-Spree State

后疯狂国家

  • Migration for Table A
  • 表A的迁移
  • Migration for Table B
  • 表B的迁移
  • Migration for Table C
  • 表C的迁移

Note that the migrations for the 'Post-Spree State' don't sync up with the state of the DB of the 'Post-Spree State'.

请注意,“Post-Spree State”的迁移不会与“Post-Spree State”的DB状态同步。

2 个解决方案

#1


1  

Converted comments to answer since it was getting quite lengthy:

将评论转换为答案,因为它变得非常冗长:

It's safer to have a separate database for any new branch, even if it's just a snapshot of the development database. You will need to restore the migrates (if possible) from your spree branch and run the down on them to get your database back to where you had it.

为任何新分支建立一个单独的数据库更安全,即使它只是开发数据库的快照。您需要从狂欢分支恢复迁移(如果可能)并向下运行以使数据库返回到您拥有它的位置。

rake db:migrate VERSION=xxx 

Where xxx is equal to the number of the last migrate before spree

其中xxx等于狂欢前的最后一次迁移的数量

I'm not a git expert, but if your branch was deleted through git then this thread may help you restore it. If you only deleted it locally then you should be able to just check it out again.

我不是git专家,但是如果你的分支是通过git删除的话,那么这个帖子可以帮助你恢复它。如果您只在本地删除它,那么您应该可以再次检查它。

Git: Recover deleted (remote) branch

Git:恢复已删除(远程)分支

#2


1  

When you switch back to your develop branch, git should check out your original schema.rb (if this isn't happening, I think you should recheck the git command you're using). However, it won't automatically bring your DB back into line with where it was when you created spree.

当你切换回你的开发分支时,git应该检查你的原始schema.rb(如果没有发生这种情况,我认为你应该重新检查你正在使用的git命令)。但是,它不会自动使您的数据库与创建狂欢时的位置保持一致。

If you no longer have the required migration files to get your DB state back to one that corresponds to the commit you're checking out of git, you should use rake db:reset to get them to match up. This will dump the current state of the DB and rebuild it using schema.rb (then apply the appropriate seed file).

如果您不再拥有所需的迁移文件以将数据库状态恢复为与您正在检出git的提交相对应的状态,则应使用rake db:reset来使它们匹配。这将转储DB的当前状态并使用schema.rb重建它(然后应用适当的种子文件)。

#1


1  

Converted comments to answer since it was getting quite lengthy:

将评论转换为答案,因为它变得非常冗长:

It's safer to have a separate database for any new branch, even if it's just a snapshot of the development database. You will need to restore the migrates (if possible) from your spree branch and run the down on them to get your database back to where you had it.

为任何新分支建立一个单独的数据库更安全,即使它只是开发数据库的快照。您需要从狂欢分支恢复迁移(如果可能)并向下运行以使数据库返回到您拥有它的位置。

rake db:migrate VERSION=xxx 

Where xxx is equal to the number of the last migrate before spree

其中xxx等于狂欢前的最后一次迁移的数量

I'm not a git expert, but if your branch was deleted through git then this thread may help you restore it. If you only deleted it locally then you should be able to just check it out again.

我不是git专家,但是如果你的分支是通过git删除的话,那么这个帖子可以帮助你恢复它。如果您只在本地删除它,那么您应该可以再次检查它。

Git: Recover deleted (remote) branch

Git:恢复已删除(远程)分支

#2


1  

When you switch back to your develop branch, git should check out your original schema.rb (if this isn't happening, I think you should recheck the git command you're using). However, it won't automatically bring your DB back into line with where it was when you created spree.

当你切换回你的开发分支时,git应该检查你的原始schema.rb(如果没有发生这种情况,我认为你应该重新检查你正在使用的git命令)。但是,它不会自动使您的数据库与创建狂欢时的位置保持一致。

If you no longer have the required migration files to get your DB state back to one that corresponds to the commit you're checking out of git, you should use rake db:reset to get them to match up. This will dump the current state of the DB and rebuild it using schema.rb (then apply the appropriate seed file).

如果您不再拥有所需的迁移文件以将数据库状态恢复为与您正在检出git的提交相对应的状态,则应使用rake db:reset来使它们匹配。这将转储DB的当前状态并使用schema.rb重建它(然后应用适当的种子文件)。