spec/integration/integration_helper.rb in lhm-1.0.0.rc2 vs spec/integration/integration_helper.rb in lhm-1.0.0.rc3

- old
+ new

@@ -1,33 +1,38 @@ -# -# Copyright (c) 2011, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias -# Schmidt -# +# Copyright (c) 2011, SoundCloud Ltd., Rany Keddo, Tobias Bielohlawek, Tobias +# Schmidt require File.expand_path(File.dirname(__FILE__)) + "/../bootstrap" require 'active_record' require 'lhm/table' +require 'lhm/sql_helper' module IntegrationHelper + attr_accessor :connection # # Connectivity # - def connect! + def connect_master! + @connection = connect!(3306) + end + + def connect_slave! + @connection = connect!(3307) + end + + def connect!(port) ActiveRecord::Base.establish_connection( :adapter => 'mysql', + :host => '127.0.0.1', :database => 'lhm', :username => '', - :host => 'localhost' + :port => port ) - ActiveRecord::Migration.verbose = !!ENV["VERBOSE"] - end - - def connection ActiveRecord::Base.connection end def select_one(*args) connection.select_one(*args) @@ -39,10 +44,27 @@ def execute(*args) connection.execute(*args) end + def slave(&block) + if master_slave_mode? + connect_slave! + + # need to wait for the slave to catch up. a better method would be to + # check the master binlog position and wait for the slave to catch up + # to that position. + sleep 1 + end + + yield block + + if master_slave_mode? + connect_master! + end + end + # # Test Data # def fixture(name) @@ -75,11 +97,20 @@ def count_all(table) query = "select count(*) from #{ table }" select_value(query).to_i end - def key?(table, cols, type = :non_unique) + def key?(table_name, cols, type = :non_unique) non_unique = type == :non_unique ? 1 : 0 - query = "show indexes in #{ table.name } where key_name = '#{ table.idx_name(cols) }' and non_unique = #{ non_unique }" + key_name = Lhm::SqlHelper.idx_name(table_name, cols) + query = "show indexes in #{ table_name } where key_name = '#{ key_name }' and non_unique = #{ non_unique }" !!select_value(query) + end + + # + # Environment + # + + def master_slave_mode? + !!ENV["MASTER_SLAVE"] end end