Sha256: 1d3dac8a0dd496ce468d876929e50a7e11f5d6bde9aa9a529c89c2d925437921

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 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|
      %w(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

      if shard_symbol == 'alone_shard'
        %w(mmorpg_players weapons skills).each do |table|
          BlankModel.using(shard_symbol).connection.execute("DELETE FROM #{table}")
        end
      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.current_slave_group'] = nil
    Thread.current['octopus.block'] = nil
    Thread.current['octopus.last_current_shard'] = nil

    ActiveRecord::Base.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)
    self.octopus_env = environment.to_s
    clean_connection_proxy
    yield
  ensure
    self.octopus_env = 'octopus'
    clean_connection_proxy
  end

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

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
ar-octopus-0.9.0 spec/support/octopus_helper.rb
ar-octopus-0.8.6 spec/support/octopus_helper.rb
misha-ar-octopus-0.8.7 spec/support/octopus_helper.rb
misha-ar-octopus-0.8.5 spec/support/octopus_helper.rb
ar-octopus-0.8.5 spec/support/octopus_helper.rb
ar-octopus-0.8.4 spec/support/octopus_helper.rb
ar-octopus-0.8.3 spec/support/octopus_helper.rb