Module: ActiveRecord::UnloadAllFixtures
- Defined in:
- lib/activerecord_unload_all_fixtures.rb
Defined Under Namespace
Modules: MySQL
Class Attribute Summary collapse
-
.ordered_active_record_table_names ⇒ Object
Returns the value of attribute ordered_active_record_table_names.
-
.table_name_set ⇒ Object
Returns the value of attribute table_name_set.
Class Method Summary collapse
-
.activerecord_model_tables ⇒ Object
all tables associated with ActiveRecord models, not marked for skip_unload_fixtures.
- .delete_rows(table_names, truncate = false, shift_counter = table_names.size) ⇒ Object
-
.unload_all_fixtures ⇒ Object
iterate over all ActiveRecord models associated with db tables, deleting all rows if we’re inside a transaction, use delete, otherwise use truncate.
Class Attribute Details
.ordered_active_record_table_names ⇒ Object
Returns the value of attribute ordered_active_record_table_names.
7 8 9 |
# File 'lib/activerecord_unload_all_fixtures.rb', line 7 def ordered_active_record_table_names @ordered_active_record_table_names end |
.table_name_set ⇒ Object
Returns the value of attribute table_name_set.
8 9 10 |
# File 'lib/activerecord_unload_all_fixtures.rb', line 8 def table_name_set @table_name_set end |
Class Method Details
.activerecord_model_tables ⇒ Object
all tables associated with ActiveRecord models, not marked for skip_unload_fixtures
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/activerecord_unload_all_fixtures.rb', line 34 def activerecord_model_tables klasses = ActiveRecord::Base.send(:subclasses).reject{ |klass| klass.skip_unload_fixtures if klass.respond_to?(:skip_unload_fixtures) } klasses.map do |klass| if defined?(ActiveRecord::WormTable) && klass.ancestors.include?(ActiveRecord::WormTable) [klass.switch_table_name] + klass.table_version_names else klass.table_name end end.flatten.to_set.to_a end |
.delete_rows(table_names, truncate = false, shift_counter = table_names.size) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/activerecord_unload_all_fixtures.rb', line 45 def delete_rows(table_names, truncate=false, shift_counter=table_names.size) processed = [] table_names.each_with_index{ |table_name,i| begin if UnloadAllFixtures::table_name_set.include?(table_name) if truncate ActiveRecord::Base.connection.execute("truncate table #{table_name}") else ActiveRecord::Base.connection.execute("delete from #{table_name}") end end processed << table_name rescue Exception=>e $stderr << e. << "\n" $stderr << e.backtrace << "\n" remaining = table_names[i..-1] raise "can't remove all tables. tables remaining: #{remaining.join(', ')}" unless shift_counter>0 processed += delete_rows( remaining.unshift(remaining.pop), truncate, shift_counter-1 ) end } end |
.unload_all_fixtures ⇒ Object
iterate over all ActiveRecord models associated with db tables, deleting all rows if we’re inside a transaction, use delete, otherwise use truncate
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/activerecord_unload_all_fixtures.rb', line 12 def unload_all_fixtures() if UnloadAllFixtures::ordered_active_record_table_names.nil? || UnloadAllFixtures::ordered_active_record_table_names.empty? UnloadAllFixtures::ordered_active_record_table_names = activerecord_model_tables else # add newly defined models onto the end of the ordered list UnloadAllFixtures::ordered_active_record_table_names += activerecord_model_tables - UnloadAllFixtures::ordered_active_record_table_names end # get latest list of all tables UnloadAllFixtures::table_name_set = ActiveRecord::Base::connection.tables.to_set # start with the last successful delete ordering, only re-ordering if new foreign key dependencies are found ActiveRecord::Base::without_foreign_key_checks do UnloadAllFixtures::ordered_active_record_table_names = delete_rows( UnloadAllFixtures::ordered_active_record_table_names, ActiveRecord::Base.connection.open_transactions == 0 ) end true end |