Sha256: d39b95f9ceb591021a44681c1d92480c89b68bb11b6fa7eb585233b21f83599c

Contents?: true

Size: 1.55 KB

Versions: 2

Compression:

Stored size: 1.55 KB

Contents

module OctopusHelper
  def self.clean_all_shards(shards)
    if shards.nil?
      shards = BlankModel.using(:master).connection.instance_variable_get(:@shards).keys
    end

    shards.each do |shard_symbol|
      ['schema_migrations', 'users', 'clients', 'cats', 'items', 'keyboards', 'computers', 'permissions_roles', 'roles', 'permissions', 'assignments', 'projects', 'programmers', "yummy", 'adverts'].each do |tables|
        BlankModel.using(shard_symbol).connection.execute("DELETE FROM #{tables}")
      end
    end
  end

  def self.clean_connection_proxy()
    Thread.current["octopus.current_model"] = nil
    Thread.current["octopus.current_shard"] = nil
    Thread.current["octopus.current_group"] = nil
    Thread.current["octopus.block"] = nil
    Thread.current["octopus.last_current_shard"] = nil

    ActiveRecord::Base.send(:class_variable_set, '@@connection_proxy', nil)
  end

  def self.migrating_to_version(version, &block)
    migrations_root = File.expand_path(File.join(File.dirname(__FILE__), '..', 'migrations'))

    begin
      ActiveRecord::Migrator.run(:up, migrations_root, version)
      yield
    ensure
      ActiveRecord::Migrator.run(:down, migrations_root, version)
    end
  end

  def self.using_environment(environment, &block)
    begin
      set_octopus_env(environment.to_s)
      clean_connection_proxy()
      yield
    ensure
      set_octopus_env('octopus')
      clean_connection_proxy()
    end
  end

  def self.set_octopus_env(env)
    Octopus.instance_variable_set(:@config, nil)
    Octopus.stub!(:env).and_return(env)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ar-octopus-0.6.0 spec/support/octopus_helper.rb
ar-octopus-0.5.0 spec/support/octopus_helper.rb