# 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
# File lib/database_cleaner/active_record/truncation.rb, line 41 def truncate_table(table_name) execute("TRUNCATE TABLE #{quote_table_name(table_name)};") end
# File lib/database_cleaner/active_record/truncation.rb, line 45 def truncate_tables(tables) tables.each { |t| truncate_table(t) } end
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 65 def has_been_used?(table) if row_count(table) > 0 true else select_value(" SELECT Auto_increment FROM information_schema.tables WHERE table_name='#{table}'; ") > 1 # returns nil if not present end end
# File lib/database_cleaner/active_record/truncation.rb, line 77 def has_rows?(table) row_count(table) > 0 end
# File lib/database_cleaner/active_record/truncation.rb, line 57 def row_count(table) select_value("SELECT EXISTS (SELECT 1 FROM #{quote_table_name(table)} LIMIT 1)") end