How do I manage changes to my db across git branches?

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.

  • Hook to create/add a database dump file to repository on git pull
  • Forking (postgre)SQL database structure
  • Put MySQL database under version control?
  • Collaboration on wordpress site git. How to share database?
  • What's a good way (or tool) to version control a SQLite database (schema only)?
  • Is there a version control system for database structure changes?
  • So I made a new branch, just for Spree, based on my develop branch.

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

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

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

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

    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.

    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.

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

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

    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.

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

    In Summary

    DB

    Pre-Spree State

    • Table A
    • Table B
    • Table C

    Spree State

    • Table A (removed)
    • Table B (removed)
    • Table C
    • Spree_Table_A
    • Spree_Table_B

    Post-Spree State

    • Table A (removed)
    • Table B (removed)
    • Table C
    • Spree_Table_A
    • Spree_Table_B

    Basically identical to the Spree State

    DB/Migrate Folder

    Pre-Spree State

    • Migration for Table A
    • Migration for Table B
    • Migration for Table C

    Spree State

    • Migration to Remove Table A
    • Migration to Remove Table B
    • Migration for Table C
    • Migration for Spree_Table_A
    • Migration for Spree_Table_B

    Post-Spree State

    • Migration for Table A
    • Migration for Table B
    • Migration for Table 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’.

  • Git: How to make *git rebase upstream/master* work around when there is removed files in the working branch?
  • What is the difference between 'git pull' and 'git fetch'?
  • How to use git metadata strategies compared to ClearCase ones?
  • How to push the forked repository which has submodule?
  • Replace remote tag with Git
  • How to override unmerged git checkout with upstream version
  • 2 Solutions collect form web for “How do I manage changes to my db across git branches?”

    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

    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: Recover deleted (remote) branch

    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.

    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 Baby is a git and github fan, let's start git clone.