# File lib/database_cleaner/active_record/truncation.rb, line 113 def cascade @cascade ||= db_version >= 80200 ? 'CASCADE' : '' end
# File lib/database_cleaner/active_record/truncation.rb, line 109 def db_version @db_version ||= postgresql_version end
# File lib/database_cleaner/active_record/truncation.rb, line 130 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 117 def restart_identity @restart_identity ||= db_version >= 80400 ? 'RESTART IDENTITY' : '' end
# File lib/database_cleaner/active_record/truncation.rb, line 121 def truncate_table(table_name) truncate_tables([table_name]) end
# File lib/database_cleaner/active_record/truncation.rb, line 125 def truncate_tables(table_names) return if table_names.nil? || table_names.empty? execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} #{restart_identity} #{cascade};") 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 141 def has_been_used?(table) cur_val = select_value("SELECT currval('#{table}_id_seq');").to_i rescue ActiveRecord::StatementInvalid cur_val && cur_val > 0 end
# File lib/database_cleaner/active_record/truncation.rb, line 146 def has_rows?(table) select_value("SELECT true FROM #{table} LIMIT 1;") end