We have a Rails app with a PostgreSQL database. We use git
for version control.
We're only two developers on the project, so we both have to do a little of everything, and when emergencies arise we often have to drop everything to address them.
We have a main branch (called staging
just to be difficult 🌚) which we only use directly for quick fixes, minor copy changes, etc. For bigger features, we work on independent feature branches.
When I work on a feature that requires changes to the database, I naturally have to create migrations that alter the schema. Let's say I'm working on feature-emoji
, and I create a migration 20150706101741_add_emoji_to_users.rb
. I run rake db:migrate
to get on with my work.
Later, I'm informed of some bug I need to address. I switch to staging
to start work on it; however, now my app will misbehave because the db schema does not match what the app expects. So before doing git checkout staging
, I have to remember to do rake db:rollback
. And then later when I switch back to feature-emoji
, I have to run rake db:migrate
again.
This whole flow is sort of okay-ish when dealing with just two branches, but when the git rebase
s and git merge
s happen, it gets complicated.
Is there no better way to handle versioning of code and db in parallel? Or am I doomed to have to run annoying rake
tasks every time I want to change branches?
There is no easy answer to this. You could perhaps set up something like a git hook to check for changes to schema.rb
, and fail the checkout if any are present; but there are lots of edge cases to check for in such a setup.
Ultimately, the responsibility lies with the human developer to restore untracked parts of their environment — e.g. the database — to a clean state before switching branches.