#RoR NoSQL Migrations: Part 1

If you use #nosql dbs like #redis or #elasticsearch with Ruby on Rails, you might want to read this. We want to share some of our best practices with how we manage our schema changes for all of our NoSQL databases when using Ruby on Rails, so that you can simply say:

rake db:nosql_migrate

Today we will explain our versioning mechanism. We simply maintain a file called “nosql_version” in our db directory which stores the current version code which we store by timestamp, very similarly to the way rails maintains its SQL versions (e.g. 201404201200). We also have a rake task that only performs migrations newer than that code, and if the migration succeeded, we update the timecode. Here is an outline of how that logic looks:

namespace :db do
  desc "Update the NoSQL mappings."
  task :nosql_migrate => :environment do
    version = "0"
    File.open("#{ENV["RAILS_ROOT"]}/db/nosql_version", "r") do |infile|
      version = infile.gets
    end
    if version.nil? || version.empty?
      p "No version in file."
      version = "0"
    end
    last_successful_version = version
    # TODO The actual execution of the migration code should go here
    File.open("#{ENV["RAILS_ROOT"]}/db/nosql_version", "w") do |outfile| 
      # Write the timestamp for the latest migration performed. 
      outfile.write last_successful_version 
    end 
  end 
end

Stay tuned to see how the migration execution code looks over the coming days!

Advertisements

2 thoughts on “#RoR NoSQL Migrations: Part 1

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

  2. […] a #nosql 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 […]

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