Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with Git branches and Rails migrations

I am working on a rails app with quite a few git branches and many of them include db migrations. We try to be careful but occasionally some piece of code in master asks for a column that got removed/renamed in another branch.

  1. What would be a nice solution to "couple" git branches with DB states?

  2. What would these "states" actually be?

    We can't just duplicate a database if it's a few GBs in size.

  3. And what should happen with merges?

  4. Would the solution translate to noSQL databases as well?

    We currently use MySQL, mongodb and redis


EDIT: Looks like I forgot to mention a very important point, I am only interested in the development environment but with large databases (a few GBs in size).

like image 938
Kostas Avatar asked Oct 06 '22 01:10

Kostas


People also ask

How does migration work in Rails?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.

How do I run a specific rails migration?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.

How do you run migration in Ruby on Rails?

Active Record tracks which migrations have already been run so all you have to do is update your source and run rake db:migrate. Active Record will work out which migrations should be run. It will also update your db/schema. rb file to match the structure of your database.

Why do we need migration in Rails?

Migrations are a convenient way to alter your database schema over time in a consistent way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent. You can think of each migration as being a new 'version' of the database.


2 Answers

When you add a new migration in any branch, run rake db:migrate and commit both the migration and db/schema.rb

If you do this, in development, you'll be able to switch to another branch that has a different set of migrations and simply run rake db:schema:load.

Note that this will recreate the entire database, and existing data will be lost.

You'll probably only want to run production off of one branch which you're very careful with, so these steps don't apply there (just run rake db:migrate as usual there). But in development, it should be no big deal to recreate the database from the schema, which is what rake db:schema:load will do.

like image 70
Andy Lindeman Avatar answered Oct 08 '22 13:10

Andy Lindeman


If you have a large database that you can't readily reproduce, then I'd recommend using the normal migration tools. If you want a simple process, this is what I'd recommend:

  • Before switching branches, rollback (rake db:rollback) to the state before the branch point. Then, after switching branches, run db:migrate. This is mathematically correct, and as long as you write down scripts, it will work.
  • If you forget to do this before switching branches, in general you can safely switch back, rollback, and switch again, so I think as a workflow, it's feasible.
  • If you have dependencies between migrations in different branches... well, you'll have to think hard.
like image 24
ndp Avatar answered Oct 08 '22 13:10

ndp