database_cleaner-active_record 1.99.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: db0629196991321d7ca040c181f5e10feffb27a307c0db0f3a0aeaced412eaaa
4
- data.tar.gz: 1b298eeb459d1903cba80e1031b5bbd24ea146fe6d4ad8ed2d4eeb93eb6cf254
3
+ metadata.gz: e2c9c7514dc1bea223af68a8a3822b1fb07e227911897166432f6018ab5b23a9
4
+ data.tar.gz: b3217f7ad1128617b17be1872207df59317204aa7912bca0a00e0aab77490d15
5
5
  SHA512:
6
- metadata.gz: 69e20a7c75b34cadff9745bafae326a3d160cf285d9f2c37f4078363ceb9e88976cd22a3cc8f8789815f79843aa42c7b70b0549236447829377adc3af28e7e67
7
- data.tar.gz: d7a27c45270537e4d72a09e8a1c23f95931f6bd1aec979aa9bba1318eab9a64e4395fc36398bdd8a77b0173b8d7c5cace9e42d68f351cd1e9249c5c330545f79
6
+ metadata.gz: 74e5baa1c2eb6de6b39213d9d192dcb649ff069d8ac5a9ad3d9347beb40b68a15c29a16149e2338098d246db3fdb6a7a22baef9911f4be5fb1954560d87e2b81
7
+ data.tar.gz: 991af4358084057df8efd4a05d7fa0eb75756ee8f940a7f2bd2e2da2a65a64a1bd96f668c355948e16b04c33b407276d2076f5bec5b47631e6682dd0a5221408
data/.gitignore CHANGED
@@ -8,6 +8,11 @@
8
8
  /spec/support/config.yml
9
9
  /tmp/
10
10
  !/tmp/.keep
11
+ /.ruby-version
12
+ /.byebug_history
11
13
 
12
14
  # rspec failure tracking
13
15
  .rspec_status
16
+
17
+ /Gemfile.lock
18
+ /gemfiles/*.lock
data/.travis.yml CHANGED
@@ -1,4 +1,17 @@
1
1
  language: ruby
2
+ services:
3
+ - mysql
4
+ - postgresql
2
5
  rvm:
3
- - 2.2.9
4
- before_install: gem install bundler -v 1.17.2
6
+ - 2.5
7
+ - 2.6
8
+ - 2.7
9
+ gemfile:
10
+ - gemfiles/rails_5.1.gemfile
11
+ - gemfiles/rails_5.2.gemfile
12
+ - gemfiles/rails_6.0.gemfile
13
+ - gemfiles/rails_6.1.gemfile
14
+ before_install:
15
+ - bin/setup
16
+ cache:
17
+ bundler: true
data/Appraisals ADDED
@@ -0,0 +1,15 @@
1
+ appraise "rails-5.1" do
2
+ gem "rails", "~>5.1.0"
3
+ end
4
+
5
+ appraise "rails-5.2" do
6
+ gem "rails", "~>5.2.0"
7
+ end
8
+
9
+ appraise "rails-6.0" do
10
+ gem "rails", "~>6.0.0"
11
+ end
12
+
13
+ appraise "rails-6.1" do
14
+ gem "rails", "~>6.1.0"
15
+ end
data/Gemfile CHANGED
@@ -1,8 +1,13 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in database_cleaner-active_record.gemspec
6
3
  gemspec
7
4
 
8
- gem "database_cleaner", path: "../.."
5
+ gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
6
+
7
+ gem "rails", "~>5.2"
8
+ gem "byebug"
9
+
10
+ group :test do
11
+ gem "simplecov", require: false
12
+ gem "codecov", require: false
13
+ end
data/README.md CHANGED
@@ -2,11 +2,14 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner-active_record.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/database_cleaner-active_record)
4
4
  [![Code Climate](https://codeclimate.com/github/DatabaseCleaner/database_cleaner-active_record/badges/gpa.svg)](https://codeclimate.com/github/DatabaseCleaner/database_cleaner-active_record)
5
+ [![codecov](https://codecov.io/gh/DatabaseCleaner/database_cleaner-active_record/branch/master/graph/badge.svg)](https://codecov.io/gh/DatabaseCleaner/database_cleaner-active_record)
5
6
 
6
7
  Clean your ActiveRecord databases with Database Cleaner.
7
8
 
8
9
  See https://github.com/DatabaseCleaner/database_cleaner for more information.
9
10
 
11
+ For support or to discuss development please use the [Google Group](https://groups.google.com/group/database_cleaner).
12
+
10
13
  ## Installation
11
14
 
12
15
  ```ruby
@@ -18,26 +21,11 @@ end
18
21
 
19
22
  ## Supported Strategies
20
23
 
21
- Here is an overview of the supported strategies:
22
-
23
- <table>
24
- <tbody>
25
- <tr>
26
- <th>Truncation</th>
27
- <th>Transaction</th>
28
- <th>Deletion</th>
29
- </tr>
30
- <tr>
31
- <td> Yes</td>
32
- <td> <b>Yes</b></td>
33
- <td> Yes</td>
34
- </tr>
35
- </tbody>
36
- </table>
37
-
38
- (Default strategy is denoted in bold)
24
+ Three strategies are supported:
39
25
 
40
- For support or to discuss development please use the [Google Group](https://groups.google.com/group/database_cleaner).
26
+ * Transaction (default)
27
+ * Truncation
28
+ * Deletion
41
29
 
42
30
  ## What strategy is fastest?
43
31
 
@@ -53,36 +41,41 @@ So what is fastest out of `:deletion` and `:truncation`? Well, it depends on you
53
41
 
54
42
  Some people report much faster speeds with `:deletion` while others say `:truncation` is faster for them. The best approach therefore is it try all options on your test suite and see what is faster.
55
43
 
56
- If you are using ActiveRecord then take a look at the [additional options](#additional-activerecord-options-for-truncation) available for `:truncation`.
44
+ ## Strategy configuration options
57
45
 
58
- ## Configuration options
46
+ The transaction strategy accepts no options.
59
47
 
60
- <table>
61
- <tbody>
62
- <tr>
63
- <th>ORM</th>
64
- <th>How to access</th>
65
- <th>Notes</th>
66
- </tr>
67
- <tr>
68
- <td> Active Record </td>
69
- <td> <code>DatabaseCleaner[:active_record]</code></td>
70
- <td> Connection specified as <code>:symbol</code> keys, loaded from <code>config/database.yml</code>. You may also pass in the ActiveRecord model under the <code>:model</code> key.</td>
71
- </tr>
72
- </tbody>
73
- </table>
48
+ The truncation and deletion strategies may accept the following options:
74
49
 
75
- ### Additional ActiveRecord options for Truncation
50
+ * `:only` and `:except` may take a list of table names:
76
51
 
77
- The following options are available for ActiveRecord's `:truncation` strategy _only_ for MySQL and Postgres.
52
+ ```ruby
53
+ # Only truncate the "users" table.
54
+ DatabaseCleaner[:active_record].strategy = :truncation, { only: ["users"] }
78
55
 
79
- * `:pre_count` - When set to `true` this will check each table for existing rows before truncating it. This can speed up test suites when many of the tables to be truncated are never populated. Defaults to `:false`. (Also, see the section on [What strategy is fastest?](#what-strategy-is-fastest))
80
- * `:reset_ids` - This only matters when `:pre_count` is used, and it will make sure that a tables auto-incrementing id is reset even if there are no rows in the table (e.g. records were created in the test but also removed before DatabaseCleaner gets to it). Defaults to `true`.
56
+ # Delete all tables except the "users" table.
57
+ DatabaseCleaner[:active_record].strategy = :deletion, { except: ["users"] }
58
+ ```
81
59
 
82
- The following option is available for ActiveRecord's `:truncation` and `:deletion` strategy for any DB.
60
+ * `:pre_count` - When set to `true` this will check each table for existing rows before truncating or deleting it. This can speed up test suites when many of the tables are never populated. Defaults to `false`. (Also, see the section on [What strategy is fastest?](#what-strategy-is-fastest))
83
61
 
84
62
  * `:cache_tables` - When set to `true` the list of tables to truncate or delete from will only be read from the DB once, otherwise it will be read before each cleanup run. Set this to `false` if (1) you create and drop tables in your tests, or (2) you change Postgres schemas (`ActiveRecord::Base.connection.schema_search_path`) in your tests (for example, in a multitenancy setup with each tenant in a different Postgres schema). Defaults to `true`.
85
63
 
64
+ ## Adapter configuration options
65
+
66
+ `#db` defaults to the default ActiveRecord database, but can be specified manually in a few ways:
67
+
68
+ ```ruby
69
+ # ActiveRecord connection key
70
+ DatabaseCleaner[:active_record].db = :logs
71
+
72
+ # Back to default:
73
+ DatabaseCleaner[:active_record].db = :default
74
+
75
+ # Multiple databases can be specified:
76
+ DatabaseCleaner[:active_record, db: :default]
77
+ DatabaseCleaner[:active_record, db: :logs]
78
+ ```
86
79
 
87
80
  ## Common Errors
88
81
 
@@ -92,11 +85,11 @@ If you are using Postgres and have foreign key constraints, the truncation strat
92
85
 
93
86
  To silence these warnings set the following log level in your `postgresql.conf` file:
94
87
 
95
- ```ruby
88
+ ```
96
89
  client_min_messages = warning
97
90
  ```
98
91
 
99
- For ActiveRecord, you add the following parameter in your database.yml file:
92
+ You can also add this parameter to your database.yml file:
100
93
 
101
94
  <pre>
102
95
  test:
@@ -107,4 +100,4 @@ test:
107
100
 
108
101
  ## COPYRIGHT
109
102
 
110
- See [LICENSE] for details.
103
+ See [LICENSE](LICENSE) for details.
data/bin/setup CHANGED
@@ -5,4 +5,3 @@ set -vx
5
5
 
6
6
  bundle check || bundle install
7
7
  cp spec/support/sample.config.yml spec/support/config.yml
8
-
@@ -1,12 +1,9 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "database_cleaner/active_record/version"
1
+ require_relative "./lib/database_cleaner/active_record/version"
5
2
 
6
3
  Gem::Specification.new do |spec|
7
4
  spec.name = "database_cleaner-active_record"
8
5
  spec.version = DatabaseCleaner::ActiveRecord::VERSION
9
- spec.authors = ["Ernesto Tagwerker"]
6
+ spec.authors = ["Ernesto Tagwerker", "Micah Geisel"]
10
7
  spec.email = ["ernesto@ombulabs.com"]
11
8
 
12
9
  spec.summary = "Strategies for cleaning databases using ActiveRecord. Can be used to ensure a clean state for testing."
@@ -21,20 +18,14 @@ Gem::Specification.new do |spec|
21
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
19
  spec.require_paths = ["lib"]
23
20
 
24
- spec.add_dependency "database_cleaner", "~> 1.99.0"
25
- spec.add_dependency "activerecord"
26
-
27
- spec.add_development_dependency "rake", "~> 10.0"
28
- spec.add_development_dependency "bundler", "~> 1.16"
29
- spec.add_development_dependency "rspec", "~> 3.0"
21
+ spec.add_dependency "database_cleaner-core", "~>2.0.0"
22
+ spec.add_dependency "activerecord", ">= 5.a"
30
23
 
31
- unless RUBY_PLATFORM =~ /java/
32
- spec.add_development_dependency 'mysql', '~> 2.9.1'
33
- spec.add_development_dependency 'mysql2'
34
- spec.add_development_dependency "activerecord-mysql2-adapter"
35
- spec.add_development_dependency 'pg'
36
- spec.add_development_dependency "sqlite3"
37
- else
38
- spec.add_development_dependency "activerecord-jdbc-adapter"
39
- end
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "appraisal"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "mysql2"
29
+ spec.add_development_dependency "pg"
30
+ spec.add_development_dependency "sqlite3"
40
31
  end
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_RETRY: "1"
@@ -0,0 +1,14 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
6
+ gem "rails", "~>5.1.0"
7
+ gem "byebug"
8
+
9
+ group :test do
10
+ gem "simplecov", require: false
11
+ gem "codecov", require: false
12
+ end
13
+
14
+ gemspec path: "../"
@@ -0,0 +1,14 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
6
+ gem "rails", "~>5.2.0"
7
+ gem "byebug"
8
+
9
+ group :test do
10
+ gem "simplecov", require: false
11
+ gem "codecov", require: false
12
+ end
13
+
14
+ gemspec path: "../"
@@ -0,0 +1,14 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
6
+ gem "rails", "~>6.0.0"
7
+ gem "byebug"
8
+
9
+ group :test do
10
+ gem "simplecov", require: false
11
+ gem "codecov", require: false
12
+ end
13
+
14
+ gemspec path: "../"
@@ -0,0 +1,14 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
6
+ gem "rails", "~>6.1.0"
7
+ gem "byebug"
8
+
9
+ group :test do
10
+ gem "simplecov", require: false
11
+ gem "codecov", require: false
12
+ end
13
+
14
+ gemspec path: "../"
@@ -1,94 +1,76 @@
1
1
  require 'active_record'
2
- require 'database_cleaner/generic/base'
2
+ require 'database_cleaner/strategy'
3
3
  require 'erb'
4
4
  require 'yaml'
5
5
 
6
6
  module DatabaseCleaner
7
7
  module ActiveRecord
8
- def self.available_strategies
9
- %w[truncation transaction deletion]
10
- end
11
-
12
- def self.default_strategy
13
- :transaction
14
- end
15
-
16
8
  def self.config_file_location=(path)
17
9
  @config_file_location = path
18
10
  end
19
11
 
20
12
  def self.config_file_location
21
- @config_file_location ||= begin
22
- # Has DC.app_root been set? Check in this intrusive way to avoid triggering deprecation warnings if it hasn't.
23
- app_root = DatabaseCleaner.send(:configuration).instance_variable_get(:@app_root)
24
- root = app_root || Dir.pwd
25
- "#{root}/config/database.yml"
26
- end
13
+ @config_file_location ||= "#{Dir.pwd}/config/database.yml"
27
14
  end
28
15
 
29
- module Base
30
- include ::DatabaseCleaner::Generic::Base
31
-
32
- attr_accessor :connection_hash
33
-
34
- def db=(desired_db)
35
- @db = desired_db
36
- load_config
16
+ class Base < DatabaseCleaner::Strategy
17
+ def self.migration_table_name
18
+ ::ActiveRecord::SchemaMigration.table_name
37
19
  end
38
20
 
39
- def db
40
- @db ||= super
21
+ def self.exclusion_condition(column_name)
22
+ <<~SQL
23
+ #{column_name} <> '#{DatabaseCleaner::ActiveRecord::Base.migration_table_name}'
24
+ AND #{column_name} <> '#{::ActiveRecord::Base.internal_metadata_table_name}'
25
+ SQL
41
26
  end
42
27
 
43
- def load_config
44
- if self.db != :default && self.db.is_a?(Symbol) && File.file?(ActiveRecord.config_file_location)
45
- connection_details = YAML::load(ERB.new(IO.read(ActiveRecord.config_file_location)).result)
46
- @connection_hash = valid_config(connection_details)[self.db.to_s]
47
- end
28
+ def db=(*)
29
+ super
30
+ load_config
48
31
  end
49
32
 
50
- def valid_config(connection_file)
51
- if !::ActiveRecord::Base.configurations.nil? && !::ActiveRecord::Base.configurations.empty?
52
- if connection_file != ::ActiveRecord::Base.configurations
53
- return ::ActiveRecord::Base.configurations
54
- end
55
- end
56
- connection_file
57
- end
33
+ attr_accessor :connection_hash
58
34
 
59
35
  def connection_class
60
36
  @connection_class ||= if db && !db.is_a?(Symbol)
61
37
  db
62
38
  elsif connection_hash
63
- lookup_from_connection_pool || establish_connection
39
+ (lookup_from_connection_pool rescue nil) || establish_connection
64
40
  else
65
41
  ::ActiveRecord::Base
66
42
  end
67
43
  end
68
44
 
69
- def self.migration_table_name
70
- if ::ActiveRecord::VERSION::MAJOR < 5
71
- ::ActiveRecord::Migrator.schema_migrations_table_name
72
- else
73
- ::ActiveRecord::SchemaMigration.table_name
45
+ private
46
+
47
+ def load_config
48
+ if db != :default && db.is_a?(Symbol) && File.file?(DatabaseCleaner::ActiveRecord.config_file_location)
49
+ connection_details = YAML::load(ERB.new(IO.read(DatabaseCleaner::ActiveRecord.config_file_location)).result)
50
+ @connection_hash = valid_config(connection_details, db.to_s)
74
51
  end
75
52
  end
76
53
 
77
- def self.exclusion_condition(column_name)
78
- result = " #{column_name} <> '#{::DatabaseCleaner::ActiveRecord::Base.migration_table_name}' "
79
- if ::ActiveRecord::VERSION::MAJOR >= 5
80
- result += " AND #{column_name} <> '#{::ActiveRecord::Base.internal_metadata_table_name}' "
81
- end
82
- result
54
+ def valid_config(connection_file, db)
55
+ return connection_file[db] unless (active_record_config_hash = active_record_config_hash_for(db))
56
+
57
+ active_record_config_hash
83
58
  end
84
59
 
85
- private
60
+ def active_record_config_hash_for(db)
61
+ if ::ActiveRecord.version >= Gem::Version.new('6.1')
62
+ ::ActiveRecord::Base.configurations&.configs_for(name: db)&.configuration_hash
63
+ else
64
+ ::ActiveRecord::Base.configurations[db]
65
+ end
66
+ end
86
67
 
87
68
  def lookup_from_connection_pool
88
- if ::ActiveRecord::Base.respond_to?(:descendants)
89
- database_name = connection_hash["database"] || connection_hash[:database]
90
- models = ::ActiveRecord::Base.descendants
91
- models.select(&:connection_pool).detect { |m| m.connection_pool.spec.config[:database] == database_name }
69
+ return unless ::ActiveRecord::Base.respond_to?(:descendants)
70
+
71
+ database_name = connection_hash['database'] || connection_hash[:database]
72
+ ::ActiveRecord::Base.descendants.select(&:connection_pool).detect do |model|
73
+ database_for(model) == database_name
92
74
  end
93
75
  end
94
76
 
@@ -96,6 +78,14 @@ module DatabaseCleaner
96
78
  ::ActiveRecord::Base.establish_connection(connection_hash)
97
79
  ::ActiveRecord::Base
98
80
  end
81
+
82
+ def database_for(model)
83
+ if model.connection_pool.respond_to?(:db_config) # ActiveRecord >= 6.1
84
+ model.connection_pool.db_config.configuration_hash[:database]
85
+ else
86
+ model.connection_pool.spec.config[:database]
87
+ end
88
+ end
99
89
  end
100
90
  end
101
91
  end
@@ -1,107 +1,70 @@
1
1
  require 'active_record'
2
- require 'active_record/connection_adapters/abstract_adapter'
3
- require "database_cleaner/generic/truncation"
4
2
  require 'database_cleaner/active_record/truncation'
5
3
 
6
4
  module DatabaseCleaner
7
- module ConnectionAdapters
8
- module AbstractDeleteAdapter
9
- def delete_table(table_name)
10
- raise NotImplementedError
5
+ module ActiveRecord
6
+ class Deletion < Truncation
7
+ def clean
8
+ connection.disable_referential_integrity do
9
+ if pre_count? && connection.respond_to?(:pre_count_tables)
10
+ delete_tables(connection, connection.pre_count_tables(tables_to_truncate(connection)))
11
+ else
12
+ delete_tables(connection, tables_to_truncate(connection))
13
+ end
14
+ end
11
15
  end
12
- end
13
16
 
14
- module GenericDeleteAdapter
15
- def delete_table(table_name)
16
- execute("DELETE FROM #{quote_table_name(table_name)};")
17
- end
18
- end
17
+ private
19
18
 
20
- module OracleDeleteAdapter
21
- def delete_table(table_name)
22
- execute("DELETE FROM #{quote_table_name(table_name)}")
19
+ def delete_tables(connection, table_names)
20
+ table_names.each do |table_name|
21
+ delete_table(connection, table_name)
22
+ end
23
23
  end
24
- end
25
- end
26
- end
27
-
28
- module ActiveRecord
29
- module ConnectionAdapters
30
- AbstractAdapter.class_eval { include DatabaseCleaner::ConnectionAdapters::AbstractDeleteAdapter }
31
-
32
- JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(JdbcAdapter)
33
- AbstractMysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(AbstractMysqlAdapter)
34
- Mysql2Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(Mysql2Adapter)
35
- SQLiteAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLiteAdapter)
36
- SQLite3Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLite3Adapter)
37
- PostgreSQLAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(PostgreSQLAdapter)
38
- IBM_DBAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(IBM_DBAdapter)
39
- SQLServerAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLServerAdapter)
40
- OracleEnhancedAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleDeleteAdapter } if defined?(OracleEnhancedAdapter)
41
- end
42
- end
43
24
 
44
- module DatabaseCleaner::ActiveRecord
45
- module SelectiveTruncation
46
- def tables_to_truncate(connection)
47
- if information_schema_exists?(connection)
48
- (@only || tables_with_new_rows(connection)) - @tables_to_exclude
49
- else
50
- super
25
+ def delete_table connection, table_name
26
+ connection.execute("DELETE FROM #{connection.quote_table_name(table_name)}")
51
27
  end
52
- end
53
28
 
54
- def tables_with_new_rows(connection)
55
- stats = table_stats_query(connection)
56
- if stats != ''
57
- connection.select_values(stats)
58
- else
59
- []
29
+ def tables_to_truncate(connection)
30
+ if information_schema_exists?(connection)
31
+ @except += connection.database_cleaner_view_cache + migration_storage_names
32
+ (@only.any? ? @only : tables_with_new_rows(connection)) - @except
33
+ else
34
+ super
35
+ end
60
36
  end
61
- end
62
37
 
63
- def table_stats_query(connection)
64
- @table_stats_query ||= build_table_stats_query(connection)
65
- ensure
66
- @table_stats_query = nil unless @cache_tables
67
- end
38
+ def tables_with_new_rows(connection)
39
+ stats = table_stats_query(connection)
40
+ if stats != ''
41
+ connection.select_values(stats)
42
+ else
43
+ []
44
+ end
45
+ end
68
46
 
69
- def build_table_stats_query(connection)
70
- tables = connection.select_values(<<-SQL)
71
- SELECT table_name
72
- FROM information_schema.tables
73
- WHERE table_schema = database()
74
- AND #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('table_name')};
75
- SQL
76
- queries = tables.map do |table|
77
- "(SELECT #{connection.quote(table)} FROM #{connection.quote_table_name(table)} LIMIT 1)"
47
+ def table_stats_query(connection)
48
+ @table_stats_query ||= build_table_stats_query(connection)
49
+ ensure
50
+ @table_stats_query = nil unless @cache_tables
78
51
  end
79
- queries.join(' UNION ALL ')
80
- end
81
52
 
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
53
+ def build_table_stats_query(connection)
54
+ tables = connection.select_values(<<-SQL)
55
+ SELECT table_name
56
+ FROM information_schema.tables
57
+ WHERE table_schema = database()
58
+ AND #{self.class.exclusion_condition('table_name')};
59
+ SQL
60
+ queries = tables.map do |table|
61
+ "(SELECT #{connection.quote(table)} FROM #{connection.quote_table_name(table)} LIMIT 1)"
90
62
  end
91
- end
92
- end
93
-
94
- class Deletion < Truncation
95
- if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
96
- include SelectiveTruncation
97
- end
63
+ queries.join(' UNION ALL ')
64
+ end
98
65
 
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
66
+ def information_schema_exists? connection
67
+ connection.adapter_name == "Mysql2"
105
68
  end
106
69
  end
107
70
  end