Branch Specific Database YML
At reInteractive we use Git Flow for all our projects.
But with lots of branches comes a problem. What happens when you have migrations that are branch specific? If you have used branching a lot on an in development Rails application, you are sure to have run into the problem of changing branches and having an inconsistent database state against your code.
The solution we use at reInteractive (or at least the solution I use :) is to take advantage of the way Rails reads in YAML files and embed ERB to change the database name according to the current git branch.
So instead of a database.yml that looks like this:
development: adapter: postgresql database: appnamedevelopment timeout: 5000 host: /var/pgsqlsockettest: adapter: postgresql database: appnametest timeout: 5000 host: /var/pgsqlsocket
It looks like this:
development: adapter: postgresql <% ifgit symbolic-ref HEAD 2>/dev/null
.chomp.sub('refs/heads/', '').gsub(/\W/, '') =~ /hotfix|release/ %> database: appnamedevelopmentmaster <% else %> database: appnamedevelopment<%=git symbolic-ref HEAD 2>/dev/null
.chomp.sub('refs/heads/', '').gsub(/\W/, '')[0..20] %> <% end %> timeout: 5000 host: /var/pgsql_sockettest: adapter: postgresql <% if
git symbolic-ref HEAD 2>/dev/null
.chomp.sub('refs/heads/', '').gsub(/\W/, '') =~ /hotfix|release/ %> database: appnametestmaster <% else %> database: appnametest<%=git symbolic-ref HEAD 2>/dev/null
.chomp.sub('refs/heads/', '').gsub(/\W/, '')[0..20] %> <% end %> timeout: 5000 host: /var/pgsql_socket
This does two things. Firstly, every feature branch you have gets its own database, separate from all the other branch databases. Secondly all hotfix branches use the master branch's database. I use the same database for hotfix and master because hotfixes get merged into master very rapidly and you don't want to deal with this complexity when you are making a quick fix. Also, usually in development our master branch database has good test data, so we can test our hotfix changes rapidly.
Another point if you are doing this, is that it requires a couple of extra steps.
After you checkout your new feature branch, you need to do a rake db:setup
to setup the new database for the new branch. Also, you need to restart your development web server on each branch change to make sure the app is looking at the right database.
These could be put into git hooks, but I haven't bothered for my own system as it's trivial to do.
Popular Articles by Our Team
Our expert team of designers and developers love what the do and enjoy sharing their knowledge with the world.
-
The Benefits of Ruby on Rails
-
Heroku Forcing Password Resets as GitHub Investigation Continues
-
How to Maximise your ROI with a Custom Application Built in Sa...
We Hire Only the Best
reinteractive is Australia’s largest dedicated Ruby on Rails development company. We don’t cut corners and we know what we are doing.
We are an organisation made up of amazing individuals and we take pride in our team. We are 100% remote work enabling us to choose the best talent no matter which part of the country they live in. reinteractive is dedicated to making it a great place for any developer to work.
Free Community Workshops
We created the Ruby on Rails InstallFest and Ruby on Rails Development Hub to help introduce new people to software development and to help existing developers hone their skills. These workshops provide invaluable mentorship to train developers, addressing key skills shortages in the industry. Software development is a great career choice for all ages and these events help you get started and skilled up.