database_rewinder 0.9.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b151e420c4e1985e5674d1781dceae421a9e3d9e19c3486cf87529ac14399008
4
- data.tar.gz: e1ad8090d602a4db4d71626b765c6f932ac8690ae8dfeee40d429f8071465812
3
+ metadata.gz: f8f897592605b10d934e9c74fd24476a6ef52e44e3292f0aa39caec97d2a65ba
4
+ data.tar.gz: 8c670decda9d1d418f8cf81c27ed3e47eb90fd17ac15e1f7d715f63623712e39
5
5
  SHA512:
6
- metadata.gz: acca967b5d15e66623e5767fcc9304d56e0d19074f5c02619e77c50f6dacfb51bf0289e6ef64252884f2b2fb88ae198b6abde6b152d0beaf7c13e2c4df290552
7
- data.tar.gz: 9be584317f373acaa2797ff723b4e0a2e7e8121a0aecad1920ecb01b06307224e35238e10ede1a5ec43a3a4eb74511b760b132711c4cbf8dfb55d52700626d26
6
+ metadata.gz: 82d4794103447bebd78945eb398ec8ea37329827ac417a2b11a5bf39366cf4b71194b5c550f5d4dad36b54b3f491103660b7e261ec1ec9296fbf9f87b80c4a2b
7
+ data.tar.gz: 95e396cdc60bb54640890caf57f0c5c169544eb53171a8d060f23edde5837cec4cbd9f8c19892f01ec740a0f3d464188b14604b5043cf94371f771106a16a8d7
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # DatabaseRewinder
2
2
 
3
- [![Build Status](https://travis-ci.org/amatsuda/database_rewinder.svg)](http://travis-ci.org/amatsuda/database\_rewinder)
3
+ [![Build Status](https://github.com/amatsuda/database_rewinder/actions/workflows/main.yml/badge.svg)](https://github.com/amatsuda/database_rewinder/actions)
4
4
 
5
5
  database\_rewinder is a minimalist's tiny and ultra-fast database cleaner.
6
6
 
@@ -23,9 +23,9 @@ This strategy was originally devised and implemented by Shingo Morita (@eudoxa)
23
23
 
24
24
  ## Supported versions
25
25
 
26
- * ActiveRecord 4.0, 4.1, 4.2, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0 (edge)
26
+ * ActiveRecord 4.2, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, 7.1, 7.2 (edge)
27
27
 
28
- * Ruby 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 3.0, 3.1 (trunk)
28
+ * Ruby 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3 (trunk)
29
29
 
30
30
  ## Installation
31
31
 
@@ -2,29 +2,43 @@
2
2
 
3
3
  module DatabaseRewinder
4
4
  module InsertRecorder
5
- def execute(sql, *)
6
- DatabaseRewinder.record_inserted_table self, sql
7
- super
8
- end
5
+ # This method actually no longer has to be a `prepended` hook because InsertRecorder is a module without a direct method now, but still doing this just for compatibility
6
+ def self.prepended(mod)
7
+ [:execute, :exec_insert, :exec_query, :internal_exec_query].each do |method_name|
8
+ if mod.instance_methods.include?(method_name) && (meth = mod.instance_method(method_name))
9
+ method_body = if meth.parameters.any? {|type, _name| [:key, :keyreq, :keyrest].include? type }
10
+ <<-RUBY
11
+ def #{method_name}(sql, *, **)
12
+ DatabaseRewinder.record_inserted_table self, sql
13
+ super
14
+ end
15
+ RUBY
16
+ else
17
+ <<-RUBY
18
+ def #{method_name}(sql, *)
19
+ DatabaseRewinder.record_inserted_table self, sql
20
+ super
21
+ end
22
+ RUBY
23
+ end
9
24
 
10
- if ActiveRecord::VERSION::MAJOR < 5
11
- def exec_query(sql, *)
12
- DatabaseRewinder.record_inserted_table self, sql
13
- super
14
- end
15
- else
16
- def exec_query(sql, *, **)
17
- DatabaseRewinder.record_inserted_table self, sql
18
- super
25
+ mod.send :prepend, Module.new { class_eval method_body }
26
+ end
19
27
  end
20
28
  end
21
29
  end
22
30
  end
23
31
 
24
- ::ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :prepend, DatabaseRewinder::InsertRecorder if defined? ::ActiveRecord::ConnectionAdapters::SQLite3Adapter
25
- ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :prepend, DatabaseRewinder::InsertRecorder if defined? ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
26
- ::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.send :prepend, DatabaseRewinder::InsertRecorder if defined? ::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
27
32
 
33
+ # Already loaded adapters (SQLite3Adapter, PostgreSQLAdapter, AbstractMysqlAdapter, and possibly another third party adapter)
34
+ ::ActiveRecord::ConnectionAdapters::AbstractAdapter.descendants.each do |adapter|
35
+ # In order not to touch AbstractMysqlAdapter thing, but to surely patch the concrete classes
36
+ next if adapter.descendants.any?
37
+
38
+ adapter.send :prepend, DatabaseRewinder::InsertRecorder unless adapter < DatabaseRewinder::InsertRecorder
39
+ end
40
+
41
+ # Third party adapters that might be loaded in the future
28
42
  def (::ActiveRecord::ConnectionAdapters::AbstractAdapter).inherited(adapter)
29
- adapter.prepend DatabaseRewinder::InsertRecorder
43
+ adapter.send :prepend, DatabaseRewinder::InsertRecorder
30
44
  end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'lease_connection_compat'
3
4
  require_relative 'multiple_statements_executor'
4
5
 
6
+ using DatabaseRewinder::LeaseConnectionCompat
5
7
  using DatabaseRewinder::MultipleStatementsExecutor
6
8
 
7
9
  module DatabaseRewinder
@@ -17,6 +19,10 @@ module DatabaseRewinder
17
19
  config['database']
18
20
  end
19
21
 
22
+ def host
23
+ config['host']
24
+ end
25
+
20
26
  def clean(multiple: true)
21
27
  return if !pool || inserted_tables.empty?
22
28
 
@@ -25,14 +31,14 @@ module DatabaseRewinder
25
31
  # In this case, we have to reconnect to the database to clean inserted
26
32
  # tables.
27
33
  with_automatic_reconnect(pool) do
28
- delete_all (ar_conn = pool.connection), DatabaseRewinder.all_table_names(ar_conn) & inserted_tables, multiple: multiple
34
+ delete_all (ar_conn = pool.lease_connection), DatabaseRewinder.all_table_names(ar_conn) & inserted_tables, multiple: multiple
29
35
  end
30
36
  reset
31
37
  end
32
38
 
33
39
  def clean_all(multiple: true)
34
40
  if pool
35
- ar_conn = pool.connection
41
+ ar_conn = pool.lease_connection
36
42
  delete_all ar_conn, DatabaseRewinder.all_table_names(ar_conn), multiple: multiple
37
43
  else
38
44
  require 'database_rewinder/dummy_model'
@@ -48,6 +54,8 @@ module DatabaseRewinder
48
54
  def delete_all(ar_conn, tables, multiple: true)
49
55
  tables = tables & @only if @only.any?
50
56
  tables -= @except if @except.any?
57
+ # in order to avoid referential integrity error as much as possible
58
+ tables.reverse!
51
59
  return if tables.empty?
52
60
 
53
61
  if multiple && tables.many? && ar_conn.supports_multiple_statements?
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseRewinder
4
+ module LeaseConnectionCompat
5
+ # Make old AR compatible with AR >= 7.2 by defining `lease_connection`
6
+ unless ActiveRecord::ConnectionAdapters::ConnectionPool.instance_methods.include? :lease_connection
7
+ refine ActiveRecord::ConnectionAdapters::ConnectionPool do
8
+ def lease_connection
9
+ connection
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -12,18 +12,18 @@ module DatabaseRewinder
12
12
  #TODO Use ADAPTER_NAME when we've dropped AR 4.1 support
13
13
  case self.class.name
14
14
  when 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
15
- disable_referential_integrity { log(sql) { @connection.exec sql } }
15
+ disable_referential_integrity { log(sql) { raw_connection_or_connection.exec sql } }
16
16
  when 'ActiveRecord::ConnectionAdapters::Mysql2Adapter'
17
- if @connection.query_options[:connect_flags] & Mysql2::Client::MULTI_STATEMENTS != 0
17
+ if raw_connection_or_connection.query_options[:connect_flags] & Mysql2::Client::MULTI_STATEMENTS != 0
18
18
  disable_referential_integrity do
19
- _result = log(sql) { @connection.query sql }
20
- while @connection.next_result
19
+ _result = log(sql) { raw_connection_or_connection.query sql }
20
+ while raw_connection_or_connection.next_result
21
21
  # just to make sure that all queries are finished
22
- _result = @connection.store_result
22
+ _result = raw_connection_or_connection.store_result
23
23
  end
24
24
  end
25
25
  else
26
- query_options = @connection.query_options.dup
26
+ query_options = raw_connection_or_connection.query_options.dup
27
27
  query_options[:connect_flags] |= Mysql2::Client::MULTI_STATEMENTS
28
28
  # opens another connection to the DB
29
29
  client = Mysql2::Client.new query_options
@@ -40,11 +40,17 @@ module DatabaseRewinder
40
40
  end
41
41
  end
42
42
  when 'ActiveRecord::ConnectionAdapters::SQLite3Adapter'
43
- disable_referential_integrity { log(sql) { @connection.execute_batch sql } }
43
+ disable_referential_integrity { log(sql) { raw_connection_or_connection.execute_batch sql } }
44
44
  else
45
45
  raise 'Multiple deletion is not supported with the current database adapter.'
46
46
  end
47
47
  end
48
+
49
+ private
50
+
51
+ def raw_connection_or_connection
52
+ defined?(@raw_connection) ? @raw_connection : @connection
53
+ end
48
54
  end
49
55
  end
50
56
  end
@@ -22,6 +22,8 @@ module DatabaseRewinder
22
22
  end
23
23
 
24
24
  def [](connection)
25
+ init unless defined? @cleaners
26
+
25
27
  @cleaners.detect {|c| c.connection_name == connection} || create_cleaner(connection)
26
28
  end
27
29
 
@@ -37,13 +39,14 @@ module DatabaseRewinder
37
39
  def record_inserted_table(connection, sql)
38
40
  config = connection.instance_variable_get(:'@config')
39
41
  database = config[:database]
42
+ host = config[:host]
40
43
  #NOTE What's the best way to get the app dir besides Rails.root? I know Dir.pwd here might not be the right solution, but it should work in most cases...
41
44
  root_dir = defined?(Rails) && Rails.respond_to?(:root) ? Rails.root : Dir.pwd
42
45
  cleaner = cleaners.detect do |c|
43
46
  if (config[:adapter] == 'sqlite3') && (config[:database] != ':memory:')
44
47
  File.expand_path(c.db, root_dir) == File.expand_path(database, root_dir)
45
48
  else
46
- c.db == database
49
+ c.db == database && c.host == host
47
50
  end
48
51
  end or return
49
52
 
@@ -75,9 +78,20 @@ module DatabaseRewinder
75
78
  def all_table_names(connection)
76
79
  cache_key = get_cache_key(connection.pool)
77
80
  #NOTE connection.tables warns on AR 5 with some adapters
78
- tables = ActiveSupport::Deprecation.silence { connection.tables }
81
+ tables =
82
+ if ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0
83
+ ActiveSupport::Deprecation.silence { connection.tables }
84
+ else
85
+ connection.tables
86
+ end
87
+ schema_migraion_table_name =
88
+ if ActiveRecord::SchemaMigration.respond_to?(:table_name)
89
+ ActiveRecord::SchemaMigration.table_name
90
+ else
91
+ ActiveRecord::SchemaMigration.new(connection).table_name # AR >= 7.1
92
+ end
79
93
  @table_names_cache[cache_key] ||= tables.reject do |t|
80
- (t == ActiveRecord::SchemaMigration.table_name) ||
94
+ (t == schema_migraion_table_name) ||
81
95
  (ActiveRecord::Base.respond_to?(:internal_metadata_table_name) && (t == ActiveRecord::Base.internal_metadata_table_name))
82
96
  end
83
97
  end
@@ -92,7 +106,7 @@ module DatabaseRewinder
92
106
 
93
107
  def configuration_hash_for(connection_name)
94
108
  if database_configuration.respond_to?(:configs_for)
95
- hash_config = database_configuration.configs_for(env_name: connection_name).first
109
+ hash_config = database_configuration_for(connection_name)
96
110
  if hash_config
97
111
  if hash_config.respond_to?(:configuration_hash)
98
112
  hash_config.configuration_hash.stringify_keys
@@ -104,6 +118,22 @@ module DatabaseRewinder
104
118
  database_configuration[connection_name]
105
119
  end
106
120
  end
121
+
122
+ def database_configuration_for(connection_name)
123
+ traditional_configuration_for(connection_name) || multiple_database_configuration_for(connection_name)
124
+ end
125
+
126
+ def traditional_configuration_for(connection_name)
127
+ database_configuration.configs_for(env_name: connection_name).first
128
+ end
129
+
130
+ def multiple_database_configuration_for(connection_name)
131
+ if (ActiveRecord::VERSION::MAJOR >= 7) || ((ActiveRecord::VERSION::MAJOR >= 6) && (ActiveRecord::VERSION::MINOR >= 1))
132
+ database_configuration.configs_for(name: connection_name)
133
+ else
134
+ database_configuration.configs_for(spec_name: connection_name)
135
+ end
136
+ end
107
137
  end
108
138
 
109
139
  private_class_method :configuration_hash_for, :get_cache_key
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_rewinder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Matsuda
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2021-04-26 00:00:00.000000000 Z
10
+ date: 2025-01-20 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: bundler
@@ -66,90 +65,27 @@ dependencies:
66
65
  - - ">="
67
66
  - !ruby/object:Gem::Version
68
67
  version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: sqlite3
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: mysql2
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: pg
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
- description: A minimalist's tiny and ultra-fast database cleaner
68
+ description: A minimalist's tiny and ultra-fast database cleaner for Active Record
112
69
  email:
113
70
  - ronnie@dio.jp
114
71
  executables: []
115
72
  extensions: []
116
73
  extra_rdoc_files: []
117
74
  files:
118
- - ".gitignore"
119
- - ".travis.yml"
120
- - Gemfile
121
75
  - MIT_LICENSE
122
76
  - README.md
123
- - Rakefile
124
- - database_rewinder.gemspec
125
- - gemfiles/rails_40.gemfile
126
- - gemfiles/rails_41.gemfile
127
- - gemfiles/rails_42.gemfile
128
- - gemfiles/rails_50.gemfile
129
- - gemfiles/rails_51.gemfile
130
- - gemfiles/rails_52.gemfile
131
- - gemfiles/rails_60.gemfile
132
- - gemfiles/rails_61.gemfile
133
- - gemfiles/rails_edge.gemfile
134
77
  - lib/database_rewinder.rb
135
78
  - lib/database_rewinder/active_record_monkey.rb
136
79
  - lib/database_rewinder/cleaner.rb
137
80
  - lib/database_rewinder/compatibility.rb
138
81
  - lib/database_rewinder/dummy_model.rb
82
+ - lib/database_rewinder/lease_connection_compat.rb
139
83
  - lib/database_rewinder/multiple_statements_executor.rb
140
84
  - lib/database_rewinder/railtie.rb
141
- - test/active_record_monkey_test.rb
142
- - test/cleaner_test.rb
143
- - test/config/database.yml
144
- - test/database_rewinder_test.rb
145
- - test/db/.keep
146
- - test/fake_app.rb
147
- - test/test_helper.rb
148
85
  homepage: https://github.com/amatsuda/database_rewinder
149
86
  licenses:
150
87
  - MIT
151
88
  metadata: {}
152
- post_install_message:
153
89
  rdoc_options: []
154
90
  require_paths:
155
91
  - lib
@@ -164,15 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
100
  - !ruby/object:Gem::Version
165
101
  version: '0'
166
102
  requirements: []
167
- rubygems_version: 3.3.0.dev
168
- signing_key:
103
+ rubygems_version: 3.7.0.dev
169
104
  specification_version: 4
170
105
  summary: A minimalist's tiny and ultra-fast database cleaner
171
- test_files:
172
- - test/active_record_monkey_test.rb
173
- - test/cleaner_test.rb
174
- - test/config/database.yml
175
- - test/database_rewinder_test.rb
176
- - test/db/.keep
177
- - test/fake_app.rb
178
- - test/test_helper.rb
106
+ test_files: []
data/.gitignore DELETED
@@ -1,21 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- gemfiles/*.lock
8
- InstalledFiles
9
- _yardoc
10
- coverage
11
- doc/
12
- lib/bundler/man
13
- pkg
14
- rdoc
15
- test/reports
16
- test/tmp
17
- test/version_tmp
18
- tmp
19
- log
20
- test/*.sqlite3
21
- test/db/*.sqlite3
data/.travis.yml DELETED
@@ -1,71 +0,0 @@
1
- language: ruby
2
-
3
- addons:
4
- postgresql: '9.4'
5
- mysql: '5.7'
6
-
7
- before_install:
8
- # install older versions of rubygems and bundler only on Ruby < 2.7
9
- - if [ `echo "${TRAVIS_RUBY_VERSION:0:3} < 2.7" | bc` == 1 ]; then gem i rubygems-update -v '<3' && update_rubygems; fi;
10
- - if [ `echo "${TRAVIS_RUBY_VERSION:0:3} < 2.7" | bc` == 1 ]; then gem i bundler -v '<2'; fi;
11
- - sudo service mysql restart
12
-
13
-
14
- cache: bundler
15
-
16
- env:
17
- matrix:
18
- - DB=sqlite3
19
- - DB=mysql
20
- - DB=postgresql
21
-
22
- rvm:
23
- - 2.7.3
24
-
25
- gemfile:
26
- - gemfiles/rails_52.gemfile
27
- - gemfiles/rails_60.gemfile
28
- - gemfiles/rails_61.gemfile
29
-
30
- sudo: false
31
-
32
- dist: xenial
33
-
34
- matrix:
35
- include:
36
- - rvm: 3.0.1
37
- gemfile: gemfiles/rails_edge.gemfile
38
- env: DB=postgresql
39
- - rvm: 3.0.1
40
- gemfile: gemfiles/rails_61.gemfile
41
- env: DB=postgresql
42
- - rvm: 2.6.7
43
- gemfile: gemfiles/rails_60.gemfile
44
- env: DB=postgresql
45
- - rvm: 2.6.7
46
- gemfile: gemfiles/rails_52.gemfile
47
- env: DB=postgresql
48
- - rvm: 2.5.9
49
- gemfile: gemfiles/rails_51.gemfile
50
- env: DB=postgresql
51
- - rvm: 2.4.10
52
- gemfile: gemfiles/rails_50.gemfile
53
- env: DB=postgresql
54
- - rvm: 2.3.8
55
- gemfile: gemfiles/rails_41.gemfile
56
- env: DB=postgresql
57
- - rvm: 2.3.8
58
- gemfile: gemfiles/rails_40.gemfile
59
- env: DB=postgresql
60
- - rvm: 2.2.10
61
- gemfile: gemfiles/rails_52.gemfile
62
- env: DB=postgresql
63
- - rvm: 2.1.10
64
- gemfile: gemfiles/rails_42.gemfile
65
- env: DB=postgresql
66
- - rvm: 2.0.0
67
- gemfile: gemfiles/rails_42.gemfile
68
- env: DB=postgresql
69
-
70
- allow_failures:
71
- - gemfile: gemfiles/rails_edge.gemfile
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- # Specify your gem's dependencies in database_rewinder.gemspec
6
- gemspec
data/Rakefile DELETED
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler'
4
- require 'bundler/setup'
5
- require "bundler/gem_tasks"
6
- require 'rake/testtask'
7
-
8
- Rake::TestTask.new do |t|
9
- t.libs << 'test'
10
- t.pattern = 'test/**/*_test.rb'
11
- t.warning = true
12
- t.verbose = true
13
- end
14
-
15
- task default: :test
@@ -1,30 +0,0 @@
1
- # coding: utf-8
2
- # frozen_string_literal: true
3
-
4
- lib = File.expand_path('../lib', __FILE__)
5
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
-
7
- Gem::Specification.new do |spec|
8
- spec.name = "database_rewinder"
9
- spec.version = '0.9.5'
10
- spec.authors = ["Akira Matsuda"]
11
- spec.email = ["ronnie@dio.jp"]
12
- spec.description = "A minimalist's tiny and ultra-fast database cleaner"
13
- spec.summary = "A minimalist's tiny and ultra-fast database cleaner"
14
- spec.homepage = 'https://github.com/amatsuda/database_rewinder'
15
- spec.license = "MIT"
16
-
17
- spec.files = `git ls-files`.split($/)
18
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
- spec.require_paths = ["lib"]
21
-
22
- spec.add_development_dependency 'bundler'
23
- spec.add_development_dependency "rake"
24
- spec.add_development_dependency 'test-unit-rails'
25
- spec.add_development_dependency 'rails'
26
- spec.add_development_dependency 'sqlite3'
27
- spec.add_development_dependency 'mysql2'
28
- spec.add_development_dependency 'pg'
29
- spec.required_ruby_version = '>= 2.0.0'
30
- end
@@ -1,9 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rails', '~> 4.0.0'
4
- gem 'pg', '~> 0.21'
5
- gem 'mysql2', '~> 0.3.10'
6
- gem 'sqlite3', '< 1.4'
7
- gem 'nokogiri', RUBY_VERSION < '2.1' ? '~> 1.6.0' : '>= 1.7'
8
-
9
- gemspec path: '../'
@@ -1,9 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rails', '~> 4.1.0'
4
- gem 'pg', '~> 0.21'
5
- gem 'mysql2', '~> 0.3.13'
6
- gem 'sqlite3', '< 1.4'
7
- gem 'nokogiri', RUBY_VERSION < '2.1' ? '~> 1.6.0' : '>= 1.7'
8
-
9
- gemspec path: '../'
@@ -1,9 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rails', '~> 4.2.0'
4
- gem 'pg', '~> 0.21'
5
- gem 'mysql2', '~> 0.4.0'
6
- gem 'sqlite3', '< 1.4'
7
- gem 'nokogiri', RUBY_VERSION < '2.1' ? '~> 1.6.0' : '>= 1.7'
8
-
9
- gemspec path: '../'
@@ -1,7 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rails', '~> 5.0.0'
4
- gem 'pg', '~> 0.21'
5
- gem 'sqlite3', '< 1.4'
6
-
7
- gemspec path: '../'
@@ -1,8 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rails', '~> 5.1.0'
4
- gem 'pg', '~> 0.21'
5
- gem 'capybara', '~> 2.13'
6
- gem 'puma'
7
-
8
- gemspec path: '../'
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rails', '~> 5.2.0', git: 'https://github.com/rails/rails', branch: '5-2-stable'
4
- gem 'pg', '~> 0.21'
5
-
6
- gemspec path: '../'
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rails', '~> 6.0.0'
4
- gem 'selenium-webdriver'
5
-
6
- gemspec path: '../'
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rails', '~> 6.1.0'
4
- gem 'selenium-webdriver'
5
-
6
- gemspec path: '../'
@@ -1,10 +0,0 @@
1
- source 'https://rubygems.org'
2
- git_source(:github) do |repo_name|
3
- repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
4
- "https://github.com/#{repo_name}.git"
5
- end
6
-
7
- gem 'rails', github: 'rails/rails', branch: 'main'
8
- gem 'selenium-webdriver'
9
-
10
- gemspec path: '../'
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class DatabaseRewinder::InsertRecorderTest < ActiveSupport::TestCase
6
- setup do
7
- DatabaseRewinder.init
8
- Foo.create! name: 'foo1'
9
- Bar.connection.execute "insert into bars (name) values ('bar1')"
10
- DatabaseRewinder.cleaners
11
- end
12
-
13
- test '#execute' do
14
- cleaner = DatabaseRewinder.instance_variable_get(:'@cleaners').detect {|c| c.db == (ENV['DB'] == 'sqlite3' ? 'db/database_rewinder_test.sqlite3' : 'database_rewinder_test')}
15
-
16
- assert_equal %w(foos bars), cleaner.inserted_tables
17
- assert_not_nil cleaner.pool
18
- end
19
- end
data/test/cleaner_test.rb DELETED
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class DatabaseRewinder::CleanerTest < ActiveSupport::TestCase
6
- sub_test_case '#strategy=' do
7
- setup { @cleaner = DatabaseRewinder::Cleaner.new(only: ['foos'], except: 'bars') }
8
-
9
- test 'without options' do
10
- @cleaner.strategy = :truncation
11
-
12
- # it should keep instance variables
13
- assert_equal ['foos'], @cleaner.instance_variable_get(:@only)
14
- assert_equal ['bars'], @cleaner.instance_variable_get(:@except)
15
- end
16
-
17
- test 'with options (an array or a string)' do
18
- @cleaner.strategy = :truncation, { only: ['bars'], except: 'bazs' }
19
-
20
- # it should overwrite instance variables
21
- assert_equal ['bars'], @cleaner.instance_variable_get(:@only)
22
- assert_equal ['bazs'], @cleaner.instance_variable_get(:@except)
23
- end
24
-
25
- test 'with options (an empty array or nil)' do
26
- @cleaner.strategy = :truncation, { only: [], except: nil }
27
-
28
- # it should overwrite instance variables even if they are empty/nil
29
- assert_equal [], @cleaner.instance_variable_get(:@only)
30
- assert_equal [], @cleaner.instance_variable_get(:@except)
31
- end
32
- end
33
- end
@@ -1,44 +0,0 @@
1
- <% case ENV['DB']
2
- when 'sqlite3' %>
3
- test:
4
- adapter: sqlite3
5
- database: db/database_rewinder_test.sqlite3
6
- pool: 5
7
- timeout: 5000
8
-
9
- test2:
10
- adapter: sqlite3
11
- database: db/database_rewinder_test2.sqlite3
12
- pool: 5
13
- timeout: 5000
14
-
15
- <% when 'mysql' %>
16
- test:
17
- adapter: mysql2
18
- host: localhost
19
- username: root
20
- password:
21
- database: database_rewinder_test
22
-
23
- test2:
24
- adapter: mysql2
25
- host: localhost
26
- username: root
27
- password:
28
- database: database_rewinder_test2
29
-
30
- <% when 'postgresql' %>
31
- test:
32
- adapter: postgresql
33
- host: localhost
34
- username: postgres
35
- password:
36
- database: database_rewinder_test
37
-
38
- test2:
39
- adapter: postgresql
40
- host: localhost
41
- username: postgres
42
- password:
43
- database: database_rewinder_test2
44
- <% end %>
@@ -1,258 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
6
- if ActiveRecord::VERSION::STRING >= '5'
7
- self.use_transactional_tests = false
8
- else
9
- self.use_transactional_fixtures = false
10
- end
11
-
12
- setup do
13
- DatabaseRewinder.init
14
- end
15
-
16
- sub_test_case '.[]' do
17
- teardown do
18
- DatabaseRewinder.database_configuration = nil
19
- end
20
- sub_test_case 'for connecting to an arbitrary database' do
21
- def assert_cleaners_added(cleaner_names)
22
- connection_names = DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name}
23
- yield
24
- assert_equal cleaner_names, DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name} - connection_names
25
- end
26
-
27
- test 'simply giving a connection name only' do
28
- assert_cleaners_added ['aaa'] do
29
- DatabaseRewinder.database_configuration = {'aaa' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
30
- DatabaseRewinder['aaa']
31
- end
32
- end
33
-
34
- test 'giving a connection name via Hash with :connection key' do
35
- assert_cleaners_added ['bbb'] do
36
- DatabaseRewinder.database_configuration = {'bbb' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
37
- DatabaseRewinder[connection: 'bbb']
38
- end
39
- end
40
-
41
- test 'the Cleaner compatible syntax' do
42
- assert_cleaners_added ['ccc'] do
43
- DatabaseRewinder.database_configuration = {'ccc' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
44
- DatabaseRewinder[:aho, connection: 'ccc']
45
- end
46
- end
47
- end
48
-
49
- test 'for connecting to multiple databases' do
50
- DatabaseRewinder[:active_record, connection: 'test']
51
- DatabaseRewinder[:active_record, connection: 'test2']
52
-
53
- Foo.create! name: 'foo1'
54
- Quu.create! name: 'quu1'
55
-
56
- DatabaseRewinder.clean
57
-
58
- # it should clean all configured databases
59
- assert_equal 0, Foo.count
60
- assert_equal 0, Quu.count
61
- end
62
- end
63
-
64
- sub_test_case '.record_inserted_table' do
65
- setup do
66
- DatabaseRewinder.cleaners
67
- end
68
-
69
- def perform_insert(sql)
70
- @cleaner = DatabaseRewinder.instance_variable_get(:'@cleaners').detect {|c| c.db == (ENV['DB'] == 'sqlite3' ? 'db/database_rewinder_test.sqlite3' : 'database_rewinder_test')}
71
-
72
- connection = ::ActiveRecord::Base.connection
73
- DatabaseRewinder.record_inserted_table(connection, sql)
74
- end
75
- teardown do
76
- DatabaseRewinder.database_configuration = nil
77
- end
78
-
79
- sub_test_case 'via General Active Record insertions' do
80
- setup do
81
- DatabaseRewinder.cleaners
82
- @cleaner = DatabaseRewinder.instance_variable_get(:'@cleaners').detect {|c| c.db == (ENV['DB'] == 'sqlite3' ? 'db/database_rewinder_test.sqlite3' : 'database_rewinder_test')}
83
- end
84
-
85
- test 'create' do
86
- Bar.create name: 'bar1'
87
- assert_equal ['bars'], @cleaner.inserted_tables
88
- end
89
-
90
- if ActiveRecord::VERSION::STRING >= '6'
91
- test 'insert_all' do
92
- Bar.insert_all! [{name: 'bar1'}]
93
- assert_equal ['bars'], @cleaner.inserted_tables
94
- end
95
- end
96
- end
97
-
98
- sub_test_case 'common database' do
99
- test 'include database name' do
100
- perform_insert 'INSERT INTO "database"."foos" ("name") VALUES (?)'
101
- assert_equal ['foos'], @cleaner.inserted_tables
102
- end
103
- test 'only table name' do
104
- perform_insert 'INSERT INTO "foos" ("name") VALUES (?)'
105
- assert_equal ['foos'], @cleaner.inserted_tables
106
- end
107
- test 'without "INTO"' do
108
- perform_insert 'INSERT "foos" ("name") VALUES (?)'
109
- assert_equal ['foos'], @cleaner.inserted_tables
110
- end
111
- test 'with space before "INSERT"' do
112
- perform_insert <<-SQL
113
- INSERT INTO "foos" ("name") VALUES (?)
114
- SQL
115
- assert_equal ['foos'], @cleaner.inserted_tables
116
- end
117
- test 'without spaces between table name and columns list' do
118
- perform_insert 'INSERT INTO foos(name) VALUES (?)'
119
- assert_equal ['foos'], @cleaner.inserted_tables
120
- end
121
-
122
- test 'with multi statement query' do
123
- perform_insert <<-SQL
124
- INSERT INTO "foos" ("name") VALUES (?);
125
- INSERT INTO "bars" ("name") VALUES (?)
126
- SQL
127
- assert_equal ['foos', 'bars'], @cleaner.inserted_tables
128
- end
129
- end
130
-
131
- sub_test_case 'Database accepts more than one dots in an object notation (e.g. SQLServer)' do
132
- test 'full joined' do
133
- perform_insert 'INSERT INTO server.database.schema.foos ("name") VALUES (?)'
134
- assert_equal ['foos'], @cleaner.inserted_tables
135
- end
136
- test 'missing one' do
137
- perform_insert 'INSERT INTO database..foos ("name") VALUES (?)'
138
- assert_equal ['foos'], @cleaner.inserted_tables
139
- end
140
-
141
- test 'missing two' do
142
- perform_insert 'INSERT INTO server...foos ("name") VALUES (?)'
143
- assert_equal ['foos'], @cleaner.inserted_tables
144
- end
145
- end
146
-
147
- test 'when database accepts INSERT IGNORE INTO statement' do
148
- perform_insert "INSERT IGNORE INTO `foos` (`name`) VALUES ('alice'), ('bob') ON DUPLICATE KEY UPDATE `foos`.`updated_at`=VALUES(`updated_at`)"
149
- assert_equal ['foos'], @cleaner.inserted_tables
150
- end
151
- end
152
-
153
- test '.clean' do
154
- bar = Bar.create! name: 'bar1'
155
- Foo.create! name: 'foo1', bar_id: bar.id
156
- DatabaseRewinder.clean
157
-
158
- assert_equal 0, Foo.count
159
- assert_equal 0, Bar.count
160
- end
161
-
162
- if ActiveRecord::VERSION::STRING >= '4'
163
- sub_test_case 'migrations' do
164
- test '.clean_all should not touch AR::SchemaMigration' do
165
- begin
166
- ActiveRecord::SchemaMigration.create_table
167
- ActiveRecord::SchemaMigration.create! version: '001'
168
- DatabaseRewinder.clean_all
169
-
170
- assert_equal 0, Foo.count
171
- assert_equal 1, ActiveRecord::SchemaMigration.count
172
- ensure
173
- ActiveRecord::SchemaMigration.drop_table
174
- end
175
- end
176
- end
177
- end
178
-
179
- sub_test_case '.clean_with' do
180
- def perform_clean(options)
181
- @cleaner = DatabaseRewinder.cleaners.first
182
- @only = @cleaner.instance_variable_get(:@only)
183
- @except = @cleaner.instance_variable_get(:@except)
184
- Foo.create! name: 'foo1'
185
- Bar.create! name: 'bar1'
186
- DatabaseRewinder.clean_with :truncation, **options
187
- end
188
-
189
- test 'with only option' do
190
- perform_clean only: ['foos']
191
- assert_equal 0, Foo.count
192
- assert_equal 1, Bar.count
193
- assert_equal @only, @cleaner.instance_variable_get(:@only)
194
- end
195
-
196
- test 'with except option' do
197
- perform_clean except: ['bars']
198
- assert_equal 0, Foo.count
199
- assert_equal 1, Bar.count
200
- assert_equal @except, @cleaner.instance_variable_get(:@except)
201
- end
202
- end
203
-
204
- sub_test_case '.cleaning' do
205
- test 'without exception' do
206
- DatabaseRewinder.cleaning do
207
- Foo.create! name: 'foo1'
208
- end
209
-
210
- assert_equal 0, Foo.count
211
- end
212
-
213
- test 'with exception' do
214
- assert_raises do
215
- DatabaseRewinder.cleaning do
216
- Foo.create! name: 'foo1'; fail
217
- end
218
- end
219
- assert_equal 0, Foo.count
220
- end
221
- end
222
-
223
- sub_test_case '.strategy=' do
224
- sub_test_case 'call first with options' do
225
- setup do
226
- DatabaseRewinder.strategy = :truncate, { only: ['foos'], except: ['bars'] }
227
- end
228
-
229
- test 'should set options' do
230
- assert_equal ['foos'], DatabaseRewinder.instance_variable_get(:@only)
231
- assert_equal ['bars'], DatabaseRewinder.instance_variable_get(:@except)
232
- end
233
-
234
- test 'should create cleaner with options' do
235
- cleaner = DatabaseRewinder.instance_variable_get(:@cleaners).first
236
- assert_equal ['foos'], cleaner.instance_variable_get(:@only)
237
- assert_equal ['bars'], cleaner.instance_variable_get(:@except)
238
- end
239
-
240
- sub_test_case 'call again with different options' do
241
- setup do
242
- DatabaseRewinder.strategy = :truncate, { only: ['bazs'], except: [] }
243
- end
244
-
245
- test 'should overwrite options' do
246
- assert_equal ['bazs'], DatabaseRewinder.instance_variable_get(:@only)
247
- assert_equal [], DatabaseRewinder.instance_variable_get(:@except)
248
- end
249
-
250
- test 'should overwrite cleaner with new options' do
251
- cleaner = DatabaseRewinder.instance_variable_get(:@cleaners).first
252
- assert_equal ['bazs'], cleaner.instance_variable_get(:@only)
253
- assert_equal [], cleaner.instance_variable_get(:@except)
254
- end
255
- end
256
- end
257
- end
258
- end
data/test/db/.keep DELETED
File without changes
data/test/fake_app.rb DELETED
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- ENV['DB'] ||= 'sqlite3'
4
- require 'active_record/railtie'
5
-
6
- module DatabaseRewinderTestApp
7
- Application = Class.new(Rails::Application) do
8
- # Rais.root
9
- config.root = __dir__
10
-
11
- config.eager_load = false
12
- config.active_support.deprecation = :log
13
- end.initialize!
14
- end
15
-
16
- load 'active_record/railties/databases.rake'
17
-
18
- require 'active_record/base'
19
- ActiveRecord::Tasks::DatabaseTasks.root ||= Rails.root
20
- ActiveRecord::Tasks::DatabaseTasks.drop_current 'test'
21
- ActiveRecord::Tasks::DatabaseTasks.drop_current 'test2'
22
- ActiveRecord::Tasks::DatabaseTasks.create_current 'test'
23
- ActiveRecord::Tasks::DatabaseTasks.create_current 'test2'
24
-
25
- # models
26
- class Foo < ActiveRecord::Base; end
27
- class Bar < ActiveRecord::Base; end
28
- class Baz < ActiveRecord::Base; end
29
- class Quu < ActiveRecord::Base
30
- establish_connection :test2
31
- end
32
-
33
- # migrations
34
- class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
35
- def self.up
36
- ActiveRecord::Base.establish_connection :test
37
- create_table(:bars) {|t| t.string :name; t.index :name, unique: true }
38
- create_table(:foos) {|t| t.string :name; t.references :bar, foreign_key: true }
39
- create_table(:bazs) {|t| t.string :name }
40
-
41
- test2_connection = ActiveRecord::Base.establish_connection(:test2).connection
42
- test2_connection.create_table(:quus) {|t| t.string :name }
43
- ActiveRecord::Base.establish_connection :test
44
- end
45
-
46
- def self.down
47
- drop_table(:foos) {|t| t.string :name }
48
- drop_table(:bars) {|t| t.string :name }
49
- drop_table(:bazs) {|t| t.string :name }
50
-
51
- test2_connection = ActiveRecord::Base.establish_connection(:test2).connection
52
- test2_connection.drop_table :quus
53
- ActiveRecord::Base.establish_connection :test
54
- end
55
- end
data/test/test_helper.rb DELETED
@@ -1,27 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- ENV['RAILS_ENV'] ||= 'test'
4
-
5
- $LOAD_PATH.unshift(File.join(__dir__, '..', 'lib'))
6
- $LOAD_PATH.unshift(__dir__)
7
-
8
- require 'rails'
9
- require 'active_record'
10
- require 'database_rewinder'
11
- require 'fake_app'
12
- require 'test/unit/rails/test_help'
13
- begin
14
- require 'selenium/webdriver' # rails 6
15
- rescue LoadError
16
- end
17
-
18
- migrated = ActiveRecord::Base.connection.respond_to?(:data_source_exists?) ? ActiveRecord::Base.connection.data_source_exists?('foos') : ActiveRecord::Base.connection.table_exists?('foos')
19
- CreateAllTables.up unless migrated
20
-
21
- module DeleteAllTables
22
- def teardown
23
- super
24
- [Foo, Bar, Baz, Quu].each {|m| m.delete_all }
25
- end
26
- end
27
- ActiveSupport::TestCase.send :prepend, DeleteAllTables