Skip to content
austinmills edited this page Mar 18, 2011 · 6 revisions

Migrations

Octopus allow you to send migrations to specific shards. If you don’t specify what shard to send the migration or Octopus isn’t configured to run in that environment, Octopus will send the migration to master database specified in database.yml.

Code Examples:

To send a migration to specific shard:

# This will send the migration to the shard named "canada"
class CreateUsersOnCanada < ActiveRecord::Migration
  using(:canada)

  def self.up
    User.create!(:name => "Sharding")
  end

  def self.down
    User.delete_all()
  end
end

Octopus allows you to use nested blocks inside migrations. This means that the following code works:

class CreateUsersUsingBlockAndUsing < ActiveRecord::Migration
  using(:brazil)

  def self.up
    using(:canada) do
      User.create!(:name => "Canada")
    end

    User.create!(:name => "Brazil")
  end

  def self.down
    User.delete_all()
  end
end

This code will create a user named Canada in the canada shard, and a user named Brazil in the brazil shard.

To send a migration to multiple shards:

# This will send the migration to the shards named "canada" and "brazil"
class CreateUsersOnBothShards < ActiveRecord::Migration
  using(:brazil, :canada)

  def self.up
    User.create!(:name => "Both")
  end

  def self.down
    User.delete_all()
  end
end

To send a migration to a group of shards:

# This will send the migration to the all shards inside "country_shards" group
class CreateUsersOnShardsOfAGroup < ActiveRecord::Migration
  using_group(:country_shards)

  def self.up
    User.create!(:name => "Group")
  end

  def self.down
    User.delete_all()
  end
end

To send a migration to a multiple groups of shards:

# This will send the migration to all shards inside "country_shards" 
# and "history_shards" groups
class CreateUsersOnMultiplesGroups < ActiveRecord::Migration
  using_group(:country_shards, :history_shards)

  def self.up
    User.create!(:name => "MultipleGroup")
  end

  def self.down
    User.delete_all()
  end
end
Clone this wiki locally