Module: DatabaseCleaner::ConnectionAdapters::PostgreSQLAdapter

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

Instance Method Summary collapse

Instance Method Details

#cascadeObject



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

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

#database_cleaner_table_cacheObject



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

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



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

def db_version
  @db_version ||= postgresql_version
end

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



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

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



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

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

#truncate_table(table_name) ⇒ Object



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

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

#truncate_tables(table_names) ⇒ Object



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

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