Sha256: 6088ec0f60c43b04f98d61856af16315d9eaaaafd45b80d328c7b3fc1747ffdd

Contents?: true

Size: 1.34 KB

Versions: 25

Compression:

Stored size: 1.34 KB

Contents

# Fixes Capybara database connection issues
# Found http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite/

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection

# fast truncation of all tables that need truncations (select is 10x faster then truncate)
# http://grosser.it/2012/07/03/rubyactiverecord-fastest-way-to-truncate-test-database/
def truncate_all_tables
  config = ActiveRecord::Base.configurations[::Rails.env]
  connection = ActiveRecord::Base.connection
  connection.disable_referential_integrity do
    connection.tables.each do |table_name|
      next if connection.select_value("SELECT count(*) FROM #{table_name}") == 0
      case config["adapter"]
        when "mysql2", "postgresql"
          connection.execute("TRUNCATE #{table_name}")
        when "sqlite3"
          connection.execute("DELETE FROM #{table_name}")
          connection.execute("DELETE FROM sqlite_sequence where name='#{table_name}'")
      end
    end
    connection.execute("VACUUM") if config["adapter"] == "sqlite3"
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
alchemy_cms-2.6.0.rc5 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.5.3.1 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.5.3 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.5.2.2 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.5.2.1 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.5.2 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.5.1 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.5.0 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.5.0.rc3 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.5.0.b9 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.3.2 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.2.4 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.4.1 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.5.0.b5 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.5.0.b2 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.4.0 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.4.rc4 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.4.rc2 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.4.rc1 spec/support/alchemy/test_tweaks.rb
alchemy_cms-2.3.1 spec/support/alchemy/test_tweaks.rb