#RoR NoSQL Migrations: Part 2

Today we explain how to do the #nosql migration itself in the rake task we began building up before.

migrations = Dir["#{ENV["RAILS_ROOT"]}/db/nosql_migrate/*.rb"].map{|path|
  file_name = path[path.rindex("/")+1..(-1)]
  # [timestamp, path_to_file]
  [file_name[0..(file_name.index("_")-1)], path]
}.sort_by{|x| x[0]} # sort from earliest to latest.
migrations.each{|timestamp, path|
  if (timestamp > version)
    puts "Executing: system \"echo 'require \"#{path.to_s}\"' | rails c\""
    result = system "echo 'require \"#{path.to_s}\"' | rails c"
    if !result
      puts "FAIL"
      break
    else
      last_successful_version = timestamp
    end
  end
}

This code is iterating over all migrations with a newer timestamp than our current version, executing those migrations, and checking if it ran successfully or not. If not, we quit the migration where we failed, but otherwise we update the last successful version to the migration that just succeeded.

 

Advertisements

One thought on “#RoR NoSQL Migrations: Part 2

  1. […] migration for #elasticsearch might look using the rake task we built up previously in part 1 and part 2. In this case, we are creating a new mapping and saving all our SQL models which have been […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s