Module: DatabaseCleaner::ConnectionAdapters::PostgreSQLAdapter

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

Instance Method Summary collapse

Instance Method Details

#cascadeObject



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

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

#database_cleaner_table_cacheObject



159
160
161
162
163
164
165
166
# File 'lib/database_cleaner/active_record/truncation.rb', line 159

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



133
134
135
# File 'lib/database_cleaner/active_record/truncation.rb', line 133

def db_version
  @db_version ||= postgresql_version
end

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



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

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



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

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

#truncate_table(table_name) ⇒ Object



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

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

#truncate_tables(table_names) ⇒ Object



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

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