Module: DatabaseCleaner::ActiveRecord::SelectiveTruncation
- Included in:
- Deletion
- Defined in:
- lib/database_cleaner/active_record/deletion.rb
Instance Method Summary collapse
- #build_table_stats_query(connection) ⇒ Object
- #information_schema_exists?(connection) ⇒ Boolean
- #table_stats_query(connection) ⇒ Object
- #tables_to_truncate(connection) ⇒ Object
- #tables_with_new_rows(connection) ⇒ Object
Instance Method Details
#build_table_stats_query(connection) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/database_cleaner/active_record/deletion.rb', line 69 def build_table_stats_query(connection) tables = connection.select_values(<<-SQL) SELECT table_name FROM information_schema.tables WHERE table_schema = database() AND #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('table_name')}; SQL queries = tables.map do |table| "(SELECT #{connection.quote(table)} FROM #{connection.quote_table_name(table)} LIMIT 1)" end queries.join(' UNION ALL ') end |
#information_schema_exists?(connection) ⇒ Boolean
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/database_cleaner/active_record/deletion.rb', line 82 def information_schema_exists? connection return false unless connection.is_a? ActiveRecord::ConnectionAdapters::Mysql2Adapter @information_schema_exists ||= begin connection.execute("SELECT 1 FROM information_schema.tables") true rescue false end end |
#table_stats_query(connection) ⇒ Object
63 64 65 66 67 |
# File 'lib/database_cleaner/active_record/deletion.rb', line 63 def table_stats_query(connection) @table_stats_query ||= build_table_stats_query(connection) ensure @table_stats_query = nil unless @cache_tables end |
#tables_to_truncate(connection) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/database_cleaner/active_record/deletion.rb', line 46 def tables_to_truncate(connection) if information_schema_exists?(connection) (@only || tables_with_new_rows(connection)) - @tables_to_exclude else super end end |
#tables_with_new_rows(connection) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/database_cleaner/active_record/deletion.rb', line 54 def tables_with_new_rows(connection) stats = table_stats_query(connection) if stats != '' connection.select_values(stats) else [] end end |