1

Perhaps a stupid question, and without code, so I'm not sure I'm on the right StackExchange site. If so, sorry and give me a comment please.

I begin programming in Sinatra (only intranet until now) and in the samples they almost always use migration with activerecord, the same with RoR. I have experience enough with activerecord itself and it is very helpfull but I'm not sure why migration is always used ? If I create a project I just create a SQL script or a Ruby activerecord script that does the creation of the DB for me and that's it..

If I need the site or script somewhere else I just need to execute that script and ready.

Obviously I'm missing here a lot, so who can me explain the other benefits or point me to a good explanation ?

2
  • 1
    what would you do if you need to update an existing server from version 1.2.3 to version 4.5.6? Or to downgrade it back to version 1.2.2 (before you inserted that db bug...)?
    – Uri Agassi
    Commented Aug 13, 2014 at 10:24
  • hello Uri, I don't understand.. what has the creation of tables,sequences and indexes to do with updating a server ? We use only Oracle at work and lucky me that has never has influenced my sql or other code, we are only a team of 2 dev's and even then we work individually most of the time
    – peter
    Commented Aug 13, 2014 at 18:20

2 Answers 2

2

From Rails docs:

Migrations are a convenient way to alter your database schema over time in a consistent and easy 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.

So, the main two benefits are:

  1. It's like Git for db schema, you won't know how that's useful until you are in a mid-size or big project with many contributors and someone makes a bobo :)

  2. You write ruby code to build your db schema, this way if you decide to move from mysql to pg for example, you don't need to open up pg manual and check code compatibility

Update

In the api docs of migrations, you will find many nice use cases (to be honest i didn't know about half of them) ... check it out (http://api.rubyonrails.org/classes/ActiveRecord/Migration.html)

2
  • I use code like ActiveRecord::Schema.define do create_table :vacation_details do |table| table.column :code, :string etc Is that also considered like Migration ? I do also use ActiveRecord::Migration.drop_table but that's about it what migration concerns..
    – peter
    Commented Aug 13, 2014 at 18:23
  • yes i guess that's the same idea of a migration, but then again when you inherit from ActiveRecord::Migration you have many powerful options .. i will update with a nice reference of some
    – Nimir
    Commented Aug 14, 2014 at 8:29
1

Building a creation script for your database is great, provided two things:

  1. All your database deployments are on new machines
  2. You get your database schema exactly right the first time

In our modern agile environment we not only don't believe it is possible for a project larger than a few lines of code, we also don't encourage aspiring to it.

Active Record's Migrations offer a way to describe your database incrementally. Each "migration" defines how to add its features to the database ("upgrade"), and how to remove them if necessary ("downgrade").

When running migrations, on top of actually running the scripts, Active Record also maintains its own table (schema_migrations), to remember which migrations have been applied to the database, and which are pending.

This enables you to build your database alongside the features as you develop them. It also facilitates working in teams, since each team member develops her own migrations, and Active Record "stitches" everything together, so you don't have to maintain a monolithic creation script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.