Module: DatabaseCleaner::ConnectionAdapters::PostgreSQLAdapter

Defined in:
lib/database_cleaner/active_record/truncation.rb

Instance Method Summary collapse

Instance Method Details

#cascadeObject



141
142
143
# File 'lib/database_cleaner/active_record/truncation.rb', line 141

def cascade
  @cascade ||= db_version >=  80200 ? 'CASCADE' : ''
end

#database_cleaner_table_cacheObject



163
164
165
166
167
168
169
170
# File 'lib/database_cleaner/active_record/truncation.rb', line 163

def database_cleaner_table_cache
  # AR returns a list of tables without schema but then returns a
  # migrations table with the schema. There are other problems, too,
  # with using the base list. If a table exists in multiple schemas
  # within the search path, truncation without the schema name could
  # result in confusing, if not unexpected results.
  @database_cleaner_tables ||= tables_with_schema
end

#db_versionObject



137
138
139
# File 'lib/database_cleaner/active_record/truncation.rb', line 137

def db_version
  @db_version ||= postgresql_version
end

#pre_count_truncate_tables(tables, options = {:reset_ids => true}) ⇒ Object



158
159
160
161
# File 'lib/database_cleaner/active_record/truncation.rb', line 158

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_identityObject



145
146
147
# File 'lib/database_cleaner/active_record/truncation.rb', line 145

def restart_identity
  @restart_identity ||= db_version >=  80400 ? 'RESTART IDENTITY' : ''
end

#truncate_table(table_name) ⇒ Object



149
150
151
# File 'lib/database_cleaner/active_record/truncation.rb', line 149

def truncate_table(table_name)
  truncate_tables([table_name])
end

#truncate_tables(table_names) ⇒ Object



153
154
155
156
# File 'lib/database_cleaner/active_record/truncation.rb', line 153

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