Module: DatabaseCleaner::ActiveRecord::SelectiveTruncation

Included in:
Deletion
Defined in:
lib/database_cleaner/active_record/deletion.rb

Instance Method Summary collapse

Instance Method Details

#information_schema_exists?(connection) ⇒ Boolean

Returns:

  • (Boolean)

61
62
63
64
65
66
67
68
69
# File 'lib/database_cleaner/active_record/deletion.rb', line 61

def information_schema_exists? connection
  @information_schema_exists ||=
    begin
      connection.execute("SELECT * FROM information_schema.tables")
      true
    rescue
      false
    end
end

#tables_to_truncate(connection) ⇒ Object


47
48
49
50
51
52
53
# File 'lib/database_cleaner/active_record/deletion.rb', line 47

def tables_to_truncate(connection)
  if information_schema_exists?(connection)
    (@only || tables_with_new_rows(connection)) - @tables_to_exclude
  else
    super
  end
end

#tables_with_new_rows(connection) ⇒ Object


55
56
57
58
59
# File 'lib/database_cleaner/active_record/deletion.rb', line 55

def tables_with_new_rows(connection)
  @db_name ||= connection.instance_variable_get('@config')[:database]
  result = connection.execute("SELECT table_name FROM information_schema.tables WHERE table_schema = '#{@db_name}' AND table_rows > 0")
  result.map{ |row| row[0] } - ['schema_migrations']
end