module DatabaseCleaner::ConnectionAdapters::PostgreSQLAdapter
Public Instance Methods
cascade()
click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 137 def cascade @cascade ||= db_version >= 80200 ? 'CASCADE' : '' end
database_cleaner_table_cache()
click to toggle source
# 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_version()
click to toggle source
# 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})
click to toggle source
# 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_identity()
click to toggle source
# 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)
click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 145 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 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
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 174 def has_been_used?(table) return has_rows?(table) unless has_sequence?(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 185 def has_rows?(table) select_value("SELECT true FROM #{table} LIMIT 1;") end
has_sequence?(table)
click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 181 def has_sequence?(table) select_value("SELECT true FROM pg_class WHERE relname = '#{table}_id_seq';") end
tables_with_schema()
click to toggle source
# File lib/database_cleaner/active_record/truncation.rb, line 189 def tables_with_schema rows = select_rows <<-_SQL SELECT schemaname || '.' || tablename FROM pg_tables WHERE tablename !~ '_prt_' AND #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('tablename')} AND schemaname = ANY (current_schemas(false)) _SQL rows.collect { |result| result.first } end