module DatabaseCleaner::ActiveRecord::PostgreSQLAdapter

Public Instance Methods

cascade() click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 126
def cascade
  @cascade ||= db_version >=  80200 ? 'CASCADE' : ''
end
db_version() click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 122
def db_version
  @db_version ||= postgresql_version
end
pre_count_truncate_tables(tables, options = {:reset_ids => true}) click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 143
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
restart_identity() click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 130
def restart_identity
  @restart_identity ||= db_version >=  80400 ? 'RESTART IDENTITY' : ''
end
truncate_table(table_name) click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 134
def truncate_table(table_name)
  truncate_tables([table_name])
end
truncate_tables(table_names) click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 138
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

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 154
def has_been_used?(table)
  cur_val = select_value("SELECT currval('#{table}_id_seq');").to_i rescue 0
  cur_val > 0
end
has_rows?(table) click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 159
def has_rows?(table)
  select_value("SELECT true FROM #{table} LIMIT 1;")
end