module DatabaseCleaner::ActiveRecord::MysqlAdapter
Public Instance Methods
pre_count_truncate_tables(tables, options = {:reset_ids => true})
click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 49 def pre_count_truncate_tables(tables, options = {:reset_ids => true}) filter = options[:reset_ids] ? method(:has_been_used?) : method(:has_rows?) truncate_tables(tables.select(&filter)) end
truncate_table(table_name)
click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 41 def truncate_table(table_name) execute("TRUNCATE TABLE #{quote_table_name(table_name)};") end
truncate_tables(tables)
click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 45 def truncate_tables(tables) tables.each { |t| truncate_table(t) } end
Private Instance Methods
has_been_used?(table)
click to toggle source
Returns a boolean indicating if the given table has an auto-inc number higher than 0. Note, this is different than an empty table since an table may populated, the index increased, but then the table is cleaned. In other words, this function tells us if the given table was ever inserted into.
# File lib/database_cleaner/active_record/truncation.rb, line 67 def has_been_used?(table) if row_count(table) > 0 true else # Patch for MysqlAdapter with ActiveRecord 3.2.7 later # select_value("SELECT 1") #=> "1" select_value(" SELECT Auto_increment FROM information_schema.tables WHERE table_name='#{table}'; ").to_i > 1 # returns nil if not present end end
has_rows?(table)
click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 81 def has_rows?(table) row_count(table) > 0 end
row_count(table)
click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 57 def row_count(table) # Patch for MysqlAdapter with ActiveRecord 3.2.7 later # select_value("SELECT 1") #=> "1" select_value("SELECT EXISTS (SELECT 1 FROM #{quote_table_name(table)} LIMIT 1)").to_i end