database_cleaner 1.7.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/lib/database_cleaner.rb +1 -12
  3. metadata +17 -417
  4. data/CONTRIBUTE.markdown +0 -28
  5. data/Gemfile.lock +0 -286
  6. data/History.rdoc +0 -453
  7. data/LICENSE +0 -20
  8. data/README.markdown +0 -541
  9. data/Rakefile +0 -40
  10. data/TODO +0 -3
  11. data/VERSION.yml +0 -4
  12. data/cucumber.yml +0 -1
  13. data/lib/database_cleaner/active_record/base.rb +0 -92
  14. data/lib/database_cleaner/active_record/deletion.rb +0 -108
  15. data/lib/database_cleaner/active_record/transaction.rb +0 -59
  16. data/lib/database_cleaner/active_record/truncation.rb +0 -275
  17. data/lib/database_cleaner/base.rb +0 -166
  18. data/lib/database_cleaner/configuration.rb +0 -131
  19. data/lib/database_cleaner/couch_potato/base.rb +0 -7
  20. data/lib/database_cleaner/couch_potato/truncation.rb +0 -28
  21. data/lib/database_cleaner/cucumber.rb +0 -3
  22. data/lib/database_cleaner/data_mapper/base.rb +0 -21
  23. data/lib/database_cleaner/data_mapper/transaction.rb +0 -28
  24. data/lib/database_cleaner/data_mapper/truncation.rb +0 -172
  25. data/lib/database_cleaner/generic/base.rb +0 -29
  26. data/lib/database_cleaner/generic/transaction.rb +0 -11
  27. data/lib/database_cleaner/generic/truncation.rb +0 -40
  28. data/lib/database_cleaner/mongo/base.rb +0 -16
  29. data/lib/database_cleaner/mongo/truncation.rb +0 -62
  30. data/lib/database_cleaner/mongo/truncation_mixin.rb +0 -26
  31. data/lib/database_cleaner/mongo2/base.rb +0 -16
  32. data/lib/database_cleaner/mongo2/truncation_mixin.rb +0 -39
  33. data/lib/database_cleaner/mongo_mapper/base.rb +0 -20
  34. data/lib/database_cleaner/mongo_mapper/truncation.rb +0 -19
  35. data/lib/database_cleaner/mongoid/base.rb +0 -20
  36. data/lib/database_cleaner/mongoid/truncation.rb +0 -49
  37. data/lib/database_cleaner/moped/base.rb +0 -39
  38. data/lib/database_cleaner/moped/truncation.rb +0 -9
  39. data/lib/database_cleaner/moped/truncation_base.rb +0 -40
  40. data/lib/database_cleaner/neo4j/base.rb +0 -62
  41. data/lib/database_cleaner/neo4j/deletion.rb +0 -16
  42. data/lib/database_cleaner/neo4j/transaction.rb +0 -35
  43. data/lib/database_cleaner/neo4j/truncation.rb +0 -9
  44. data/lib/database_cleaner/null_strategy.rb +0 -20
  45. data/lib/database_cleaner/ohm/truncation.rb +0 -15
  46. data/lib/database_cleaner/redis/base.rb +0 -37
  47. data/lib/database_cleaner/redis/truncation.rb +0 -26
  48. data/lib/database_cleaner/safeguard.rb +0 -72
  49. data/lib/database_cleaner/sequel/base.rb +0 -22
  50. data/lib/database_cleaner/sequel/deletion.rb +0 -47
  51. data/lib/database_cleaner/sequel/transaction.rb +0 -40
  52. data/lib/database_cleaner/sequel/truncation.rb +0 -78
data/Rakefile DELETED
@@ -1,40 +0,0 @@
1
- require "rubygems"
2
- require "bundler"
3
- Bundler.setup
4
-
5
- require 'rake'
6
- require 'rspec/core'
7
- require 'rspec/core/rake_task'
8
- RSpec::Core::RakeTask.new(:spec) do |spec|
9
-
10
- spec.pattern = FileList['spec/**/*_spec.rb']
11
- end
12
-
13
- RSpec::Core::RakeTask.new(:rcov) do |spec|
14
- spec.pattern = 'spec/**/*_spec.rb'
15
- spec.rcov = true
16
- end
17
-
18
- begin
19
- require 'cucumber/rake/task'
20
- Cucumber::Rake::Task.new(:features)
21
- rescue LoadError
22
- puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
23
- end
24
-
25
- task :default => [:spec, :features]
26
-
27
-
28
- desc "Cleans the project of any tmp file that should not be included in the gemspec."
29
- task :clean do
30
- ["examples/config/database.yml", "examples/db/activerecord_one.db", "examples/db/activerecord_two.db", "examples/db/datamapper_default.db",
31
- "examples/db/datamapper_one.db", "examples/db/datamapper_two.db"].each do |f|
32
- FileUtils.rm_f(f)
33
- end
34
- %w[*.sqlite3 *.log #* *.swp *.swo].each do |pattern|
35
- `find . -name "#{pattern}" -delete`
36
- end
37
- end
38
-
39
- desc "Cleans the dir and builds the gem"
40
- task :prep => [:clean, :gemspec, :build]
data/TODO DELETED
@@ -1,3 +0,0 @@
1
- Could be more Datamapper
2
- MongoMapper multiple db support
3
- CouchDB multiple db support
data/VERSION.yml DELETED
@@ -1,4 +0,0 @@
1
- ---
2
- :major: 1
3
- :minor: 7
4
- :patch: 0
data/cucumber.yml DELETED
@@ -1 +0,0 @@
1
- default: features
@@ -1,92 +0,0 @@
1
- require 'database_cleaner/generic/base'
2
- require 'active_record'
3
- require 'erb'
4
-
5
- module DatabaseCleaner
6
- module ActiveRecord
7
-
8
- def self.available_strategies
9
- %w[truncation transaction deletion]
10
- end
11
-
12
- def self.config_file_location=(path)
13
- @config_file_location = path
14
- end
15
-
16
- def self.config_file_location
17
- @config_file_location ||= "#{DatabaseCleaner.app_root}/config/database.yml"
18
- end
19
-
20
- module Base
21
- include ::DatabaseCleaner::Generic::Base
22
-
23
- attr_accessor :connection_hash
24
-
25
- def db=(desired_db)
26
- @db = desired_db
27
- load_config
28
- end
29
-
30
- def db
31
- @db ||= super
32
- end
33
-
34
- def load_config
35
- if self.db != :default && self.db.is_a?(Symbol) && File.file?(ActiveRecord.config_file_location)
36
- connection_details = YAML::load(ERB.new(IO.read(ActiveRecord.config_file_location)).result)
37
- @connection_hash = valid_config(connection_details)[self.db.to_s]
38
- end
39
- end
40
-
41
- def valid_config(connection_file)
42
- if !::ActiveRecord::Base.configurations.nil? && !::ActiveRecord::Base.configurations.empty?
43
- if connection_file != ::ActiveRecord::Base.configurations
44
- return ::ActiveRecord::Base.configurations
45
- end
46
- end
47
- connection_file
48
- end
49
-
50
- def connection_class
51
- @connection_class ||= if db && !db.is_a?(Symbol)
52
- db
53
- elsif connection_hash
54
- lookup_from_connection_pool || establish_connection
55
- else
56
- ::ActiveRecord::Base
57
- end
58
- end
59
-
60
- def self.migration_table_name
61
- if ::ActiveRecord::VERSION::MAJOR < 5
62
- ::ActiveRecord::Migrator.schema_migrations_table_name
63
- else
64
- ::ActiveRecord::SchemaMigration.table_name
65
- end
66
- end
67
-
68
- def self.exclusion_condition(column_name)
69
- result = " #{column_name} <> '#{::DatabaseCleaner::ActiveRecord::Base.migration_table_name}' "
70
- if ::ActiveRecord::VERSION::MAJOR >= 5
71
- result += " AND #{column_name} <> '#{::ActiveRecord::Base.internal_metadata_table_name}' "
72
- end
73
- result
74
- end
75
-
76
- private
77
-
78
- def lookup_from_connection_pool
79
- if ::ActiveRecord::Base.respond_to?(:descendants)
80
- database_name = connection_hash["database"] || connection_hash[:database]
81
- models = ::ActiveRecord::Base.descendants
82
- models.detect { |m| m.connection_pool.spec.config[:database] == database_name }
83
- end
84
- end
85
-
86
- def establish_connection
87
- ::ActiveRecord::Base.establish_connection(connection_hash)
88
- ::ActiveRecord::Base
89
- end
90
- end
91
- end
92
- end
@@ -1,108 +0,0 @@
1
- require 'active_record/base'
2
- require 'active_record/connection_adapters/abstract_adapter'
3
- require "database_cleaner/generic/truncation"
4
- require 'database_cleaner/active_record/base'
5
- require 'database_cleaner/active_record/truncation'
6
-
7
- module DatabaseCleaner
8
- module ConnectionAdapters
9
- module AbstractDeleteAdapter
10
- def delete_table(table_name)
11
- raise NotImplementedError
12
- end
13
- end
14
-
15
- module GenericDeleteAdapter
16
- def delete_table(table_name)
17
- execute("DELETE FROM #{quote_table_name(table_name)};")
18
- end
19
- end
20
-
21
- module OracleDeleteAdapter
22
- def delete_table(table_name)
23
- execute("DELETE FROM #{quote_table_name(table_name)}")
24
- end
25
- end
26
- end
27
- end
28
-
29
- module ActiveRecord
30
- module ConnectionAdapters
31
- AbstractAdapter.class_eval { include DatabaseCleaner::ConnectionAdapters::AbstractDeleteAdapter }
32
-
33
- JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(JdbcAdapter)
34
- AbstractMysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(AbstractMysqlAdapter)
35
- Mysql2Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(Mysql2Adapter)
36
- SQLiteAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLiteAdapter)
37
- SQLite3Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLite3Adapter)
38
- PostgreSQLAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(PostgreSQLAdapter)
39
- IBM_DBAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(IBM_DBAdapter)
40
- SQLServerAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLServerAdapter)
41
- OracleEnhancedAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleDeleteAdapter } if defined?(OracleEnhancedAdapter)
42
- end
43
- end
44
-
45
- module DatabaseCleaner::ActiveRecord
46
- module SelectiveTruncation
47
- def tables_to_truncate(connection)
48
- if information_schema_exists?(connection)
49
- (@only || tables_with_new_rows(connection)) - @tables_to_exclude
50
- else
51
- super
52
- end
53
- end
54
-
55
- def tables_with_new_rows(connection)
56
- @db_name ||= connection.instance_variable_get('@config')[:database]
57
- stats = table_stats_query(connection, @db_name)
58
- if stats != ''
59
- connection.exec_query(stats).inject([]) {|all, stat| all << stat['table_name'] if stat['exact_row_count'] > 0; all }
60
- else
61
- []
62
- end
63
- end
64
-
65
- def table_stats_query(connection, db_name)
66
- if @cache_tables && !@table_stats_query.nil?
67
- return @table_stats_query
68
- else
69
- tables = connection.select_values(<<-SQL)
70
- SELECT table_name
71
- FROM information_schema.tables
72
- WHERE table_schema = '#{db_name}'
73
- AND #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('table_name')};
74
- SQL
75
- queries = tables.map do |table|
76
- "SELECT #{connection.quote(table)} AS table_name, COUNT(*) AS exact_row_count FROM #{connection.quote_table_name(table)}"
77
- end
78
- @table_stats_query = queries.join(' UNION ')
79
- end
80
- end
81
-
82
- def information_schema_exists? connection
83
- return false unless connection.is_a? ActiveRecord::ConnectionAdapters::Mysql2Adapter
84
- @information_schema_exists ||=
85
- begin
86
- connection.execute("SELECT 1 FROM information_schema.tables")
87
- true
88
- rescue
89
- false
90
- end
91
- end
92
- end
93
-
94
- class Deletion < Truncation
95
- if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
96
- include SelectiveTruncation
97
- end
98
-
99
- def clean
100
- connection = connection_class.connection
101
- connection.disable_referential_integrity do
102
- tables_to_truncate(connection).each do |table_name|
103
- connection.delete_table table_name
104
- end
105
- end
106
- end
107
- end
108
- end
@@ -1,59 +0,0 @@
1
- require 'database_cleaner/active_record/base'
2
- require 'database_cleaner/generic/transaction'
3
-
4
- module DatabaseCleaner::ActiveRecord
5
- class Transaction
6
- include ::DatabaseCleaner::ActiveRecord::Base
7
- include ::DatabaseCleaner::Generic::Transaction
8
-
9
- def start
10
- # Hack to make sure that the connection is properly setup for
11
- # the clean code.
12
- connection_class.connection.transaction{ }
13
-
14
- if connection_maintains_transaction_count?
15
- if connection_class.connection.respond_to?(:increment_open_transactions)
16
- connection_class.connection.increment_open_transactions
17
- else
18
- connection_class.__send__(:increment_open_transactions)
19
- end
20
- end
21
- if connection_class.connection.respond_to?(:begin_transaction)
22
- connection_class.connection.begin_transaction :joinable => false
23
- else
24
- connection_class.connection.begin_db_transaction
25
- end
26
- end
27
-
28
-
29
- def clean
30
- connection_class.connection_pool.connections.each do |connection|
31
- next unless connection.open_transactions > 0
32
-
33
- if connection.respond_to?(:rollback_transaction)
34
- connection.rollback_transaction
35
- else
36
- connection.rollback_db_transaction
37
- end
38
-
39
- # The below is for handling after_commit hooks.. see https://github.com/bmabey/database_cleaner/issues/99
40
- if connection.respond_to?(:rollback_transaction_records, true)
41
- connection.send(:rollback_transaction_records, true)
42
- end
43
-
44
- if connection_maintains_transaction_count?
45
- if connection.respond_to?(:decrement_open_transactions)
46
- connection.decrement_open_transactions
47
- else
48
- connection_class.__send__(:decrement_open_transactions)
49
- end
50
- end
51
- end
52
- end
53
-
54
- def connection_maintains_transaction_count?
55
- ActiveRecord::VERSION::MAJOR < 4
56
- end
57
-
58
- end
59
- end
@@ -1,275 +0,0 @@
1
- require 'active_record/base'
2
-
3
- require 'active_record/connection_adapters/abstract_adapter'
4
-
5
- #Load available connection adapters
6
- %w(
7
- abstract_mysql_adapter postgresql_adapter sqlite3_adapter mysql_adapter mysql2_adapter
8
- ).each do |known_adapter|
9
- begin
10
- require "active_record/connection_adapters/#{known_adapter}"
11
- rescue LoadError
12
- end
13
- end
14
-
15
- require "database_cleaner/generic/truncation"
16
- require 'database_cleaner/active_record/base'
17
-
18
- module DatabaseCleaner
19
- module ConnectionAdapters
20
-
21
- module AbstractAdapter
22
- # used to be called views but that can clash with gems like schema_plus
23
- # this gem is not meant to be exposing such an extra interface any way
24
- def database_cleaner_view_cache
25
- @views ||= select_values("select table_name from information_schema.views where table_schema = '#{current_database}'") rescue []
26
- end
27
-
28
- def database_cleaner_table_cache
29
- # the adapters don't do caching (#130) but we make the assumption that the list stays the same in tests
30
- @database_cleaner_tables ||= ::ActiveRecord::VERSION::MAJOR >= 5 ? data_sources : tables
31
- end
32
-
33
- def truncate_table(table_name)
34
- raise NotImplementedError
35
- end
36
-
37
- def truncate_tables(tables)
38
- tables.each do |table_name|
39
- self.truncate_table(table_name)
40
- end
41
- end
42
- end
43
-
44
- module AbstractMysqlAdapter
45
- def truncate_table(table_name)
46
- execute("TRUNCATE TABLE #{quote_table_name(table_name)};")
47
- end
48
-
49
- def truncate_tables(tables)
50
- tables.each { |t| truncate_table(t) }
51
- end
52
-
53
- def pre_count_truncate_tables(tables, options = {:reset_ids => true})
54
- filter = options[:reset_ids] ? method(:has_been_used?) : method(:has_rows?)
55
- truncate_tables(tables.select(&filter))
56
- end
57
-
58
- private
59
-
60
- def row_count(table)
61
- # Patch for MysqlAdapter with ActiveRecord 3.2.7 later
62
- # select_value("SELECT 1") #=> "1"
63
- select_value("SELECT EXISTS (SELECT 1 FROM #{quote_table_name(table)} LIMIT 1)").to_i
64
- end
65
-
66
- # Returns a boolean indicating if the given table has an auto-inc number higher than 0.
67
- # Note, this is different than an empty table since an table may populated, the index increased,
68
- # but then the table is cleaned. In other words, this function tells us if the given table
69
- # was ever inserted into.
70
- def has_been_used?(table)
71
- if has_rows?(table)
72
- true
73
- else
74
- # Patch for MysqlAdapter with ActiveRecord 3.2.7 later
75
- # select_value("SELECT 1") #=> "1"
76
- select_value(<<-SQL).to_i > 1 # returns nil if not present
77
- SELECT Auto_increment
78
- FROM information_schema.tables
79
- WHERE table_name='#{table}';
80
- SQL
81
- end
82
- end
83
-
84
- def has_rows?(table)
85
- row_count(table) > 0
86
- end
87
- end
88
-
89
- module IBM_DBAdapter
90
- def truncate_table(table_name)
91
- execute("TRUNCATE #{quote_table_name(table_name)} IMMEDIATE")
92
- end
93
- end
94
-
95
- module SQLiteAdapter
96
- def delete_table(table_name)
97
- execute("DELETE FROM #{quote_table_name(table_name)};")
98
- if uses_sequence
99
- execute("DELETE FROM sqlite_sequence where name = '#{table_name}';")
100
- end
101
- end
102
- alias truncate_table delete_table
103
-
104
- def truncate_tables(tables)
105
- tables.each { |t| truncate_table(t) }
106
- end
107
-
108
- private
109
-
110
- # Returns a boolean indicating if the SQLite database is using the sqlite_sequence table.
111
- def uses_sequence
112
- select_value("SELECT name FROM sqlite_master WHERE type='table' AND name='sqlite_sequence';")
113
- end
114
- end
115
-
116
- module TruncateOrDelete
117
- def truncate_table(table_name)
118
- begin
119
- execute("TRUNCATE TABLE #{quote_table_name(table_name)};")
120
- rescue ::ActiveRecord::StatementInvalid
121
- execute("DELETE FROM #{quote_table_name(table_name)};")
122
- end
123
- end
124
- end
125
-
126
- module OracleAdapter
127
- def truncate_table(table_name)
128
- execute("TRUNCATE TABLE #{quote_table_name(table_name)}")
129
- end
130
- end
131
-
132
- module PostgreSQLAdapter
133
- def db_version
134
- @db_version ||= postgresql_version
135
- end
136
-
137
- def cascade
138
- @cascade ||= db_version >= 80200 ? 'CASCADE' : ''
139
- end
140
-
141
- def restart_identity
142
- @restart_identity ||= db_version >= 80400 ? 'RESTART IDENTITY' : ''
143
- end
144
-
145
- def truncate_table(table_name)
146
- truncate_tables([table_name])
147
- end
148
-
149
- def truncate_tables(table_names)
150
- return if table_names.nil? || table_names.empty?
151
- execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} #{restart_identity} #{cascade};")
152
- end
153
-
154
- def pre_count_truncate_tables(tables, options = {:reset_ids => true})
155
- filter = options[:reset_ids] ? method(:has_been_used?) : method(:has_rows?)
156
- truncate_tables(tables.select(&filter))
157
- end
158
-
159
- def database_cleaner_table_cache
160
- # AR returns a list of tables without schema but then returns a
161
- # migrations table with the schema. There are other problems, too,
162
- # with using the base list. If a table exists in multiple schemas
163
- # within the search path, truncation without the schema name could
164
- # result in confusing, if not unexpected results.
165
- @database_cleaner_tables ||= tables_with_schema
166
- end
167
-
168
- private
169
-
170
- # Returns a boolean indicating if the given table has an auto-inc number higher than 0.
171
- # Note, this is different than an empty table since an table may populated, the index increased,
172
- # but then the table is cleaned. In other words, this function tells us if the given table
173
- # was ever inserted into.
174
- def has_been_used?(table)
175
- return has_rows?(table) unless has_sequence?(table)
176
-
177
- cur_val = select_value("SELECT currval('#{table}_id_seq');").to_i rescue 0
178
- cur_val > 0
179
- end
180
-
181
- def has_sequence?(table)
182
- select_value("SELECT true FROM pg_class WHERE relname = '#{table}_id_seq';")
183
- end
184
-
185
- def has_rows?(table)
186
- select_value("SELECT true FROM #{table} LIMIT 1;")
187
- end
188
-
189
- def tables_with_schema
190
- rows = select_rows <<-_SQL
191
- SELECT schemaname || '.' || tablename
192
- FROM pg_tables
193
- WHERE
194
- tablename !~ '_prt_' AND
195
- #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('tablename')} AND
196
- schemaname = ANY (current_schemas(false))
197
- _SQL
198
- rows.collect { |result| result.first }
199
- end
200
- end
201
- end
202
- end
203
-
204
- module ActiveRecord
205
- module ConnectionAdapters
206
- #Apply adapter decoraters where applicable (adapter should be loaded)
207
- AbstractAdapter.class_eval { include DatabaseCleaner::ConnectionAdapters::AbstractAdapter }
208
-
209
- if defined?(JdbcAdapter)
210
- if defined?(OracleJdbcConnection)
211
- JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleAdapter }
212
- else
213
- JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::TruncateOrDelete }
214
- end
215
- end
216
- AbstractMysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::AbstractMysqlAdapter } if defined?(AbstractMysqlAdapter)
217
- Mysql2Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::AbstractMysqlAdapter } if defined?(Mysql2Adapter)
218
- MysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::AbstractMysqlAdapter } if defined?(MysqlAdapter)
219
- SQLiteAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::SQLiteAdapter } if defined?(SQLiteAdapter)
220
- SQLite3Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::SQLiteAdapter } if defined?(SQLite3Adapter)
221
- PostgreSQLAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::PostgreSQLAdapter } if defined?(PostgreSQLAdapter)
222
- IBM_DBAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::IBM_DBAdapter } if defined?(IBM_DBAdapter)
223
- SQLServerAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::TruncateOrDelete } if defined?(SQLServerAdapter)
224
- OracleEnhancedAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleAdapter } if defined?(OracleEnhancedAdapter)
225
- end
226
- end
227
-
228
- module DatabaseCleaner::ActiveRecord
229
- class Truncation
230
- include ::DatabaseCleaner::ActiveRecord::Base
231
- include ::DatabaseCleaner::Generic::Truncation
232
-
233
- def clean
234
- connection = connection_class.connection
235
- connection.disable_referential_integrity do
236
- if pre_count? && connection.respond_to?(:pre_count_truncate_tables)
237
- connection.pre_count_truncate_tables(tables_to_truncate(connection), {:reset_ids => reset_ids?})
238
- else
239
- connection.truncate_tables(tables_to_truncate(connection))
240
- end
241
- end
242
- end
243
-
244
- private
245
-
246
- def tables_to_truncate(connection)
247
- tables_in_db = cache_tables? ? connection.database_cleaner_table_cache : connection.tables
248
- to_reject = (@tables_to_exclude + connection.database_cleaner_view_cache)
249
- (@only || tables_in_db).reject do |table|
250
- if ( m = table.match(/([^.]+)$/) )
251
- to_reject.include?(m[1])
252
- else
253
- false
254
- end
255
- end
256
- end
257
-
258
- # overwritten
259
- def migration_storage_names
260
- [::DatabaseCleaner::ActiveRecord::Base.migration_table_name]
261
- end
262
-
263
- def cache_tables?
264
- !!@cache_tables
265
- end
266
-
267
- def pre_count?
268
- @pre_count == true
269
- end
270
-
271
- def reset_ids?
272
- @reset_ids != false
273
- end
274
- end
275
- end