database_rewinder 1.0.0 → 1.1.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: fba90991ea75253ed37624e82b4f6c26ae379f4c3b1d24a522192befb8529757
4
- data.tar.gz: 70c3c776b0e291b49a873e503f4111cb537e9685479176527cd94de9a663cff2
3
+ metadata.gz: f8f897592605b10d934e9c74fd24476a6ef52e44e3292f0aa39caec97d2a65ba
4
+ data.tar.gz: 8c670decda9d1d418f8cf81c27ed3e47eb90fd17ac15e1f7d715f63623712e39
5
5
  SHA512:
6
- metadata.gz: 457ed185619931c3aba3c9dd0b1a03b2156e712bfae655300d737bb1ac54ccc86a2d0256878ca7e765a2bd55e8798f6a855a9420eb5c2b8123cbd68b1298646b
7
- data.tar.gz: ad40ddaa9b69d852a3001d60c6673dcd335a4171c7c77d05245b94f0c022a71a33bef1c323d7ddc6c1297498ea18ad52037ea78f8c17335a15d7a280fcf378a8
6
+ metadata.gz: 82d4794103447bebd78945eb398ec8ea37329827ac417a2b11a5bf39366cf4b71194b5c550f5d4dad36b54b3f491103660b7e261ec1ec9296fbf9f87b80c4a2b
7
+ data.tar.gz: 95e396cdc60bb54640890caf57f0c5c169544eb53171a8d060f23edde5837cec4cbd9f8c19892f01ec740a0f3d464188b14604b5043cf94371f771106a16a8d7
@@ -4,7 +4,7 @@ module DatabaseRewinder
4
4
  module InsertRecorder
5
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
6
  def self.prepended(mod)
7
- [:execute, :exec_query, :internal_exec_query].each do |method_name|
7
+ [:execute, :exec_insert, :exec_query, :internal_exec_query].each do |method_name|
8
8
  if mod.instance_methods.include?(method_name) && (meth = mod.instance_method(method_name))
9
9
  method_body = if meth.parameters.any? {|type, _name| [:key, :keyreq, :keyrest].include? type }
10
10
  <<-RUBY
@@ -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'
@@ -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) { (@raw_connection || @connection).exec sql } }
15
+ disable_referential_integrity { log(sql) { raw_connection_or_connection.exec sql } }
16
16
  when 'ActiveRecord::ConnectionAdapters::Mysql2Adapter'
17
- if (@raw_connection || @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) { (@raw_connection || @connection).query sql }
20
- while (@raw_connection || @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 = (@raw_connection || @connection).store_result
22
+ _result = raw_connection_or_connection.store_result
23
23
  end
24
24
  end
25
25
  else
26
- query_options = (@raw_connection || @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) { (@raw_connection || @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
@@ -39,13 +39,14 @@ module DatabaseRewinder
39
39
  def record_inserted_table(connection, sql)
40
40
  config = connection.instance_variable_get(:'@config')
41
41
  database = config[:database]
42
+ host = config[:host]
42
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...
43
44
  root_dir = defined?(Rails) && Rails.respond_to?(:root) ? Rails.root : Dir.pwd
44
45
  cleaner = cleaners.detect do |c|
45
46
  if (config[:adapter] == 'sqlite3') && (config[:database] != ':memory:')
46
47
  File.expand_path(c.db, root_dir) == File.expand_path(database, root_dir)
47
48
  else
48
- c.db == database
49
+ c.db == database && c.host == host
49
50
  end
50
51
  end or return
51
52
 
@@ -78,10 +79,10 @@ module DatabaseRewinder
78
79
  cache_key = get_cache_key(connection.pool)
79
80
  #NOTE connection.tables warns on AR 5 with some adapters
80
81
  tables =
81
- if Rails.application.respond_to?(:deprecators) # AR >= 7.1
82
- Rails.application.deprecators.silence { connection.tables }
83
- else
82
+ if ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR == 0
84
83
  ActiveSupport::Deprecation.silence { connection.tables }
84
+ else
85
+ connection.tables
85
86
  end
86
87
  schema_migraion_table_name =
87
88
  if ActiveRecord::SchemaMigration.respond_to?(:table_name)
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: 1.0.0
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: 2024-01-09 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
@@ -73,32 +72,20 @@ executables: []
73
72
  extensions: []
74
73
  extra_rdoc_files: []
75
74
  files:
76
- - ".github/workflows/main.yml"
77
- - ".gitignore"
78
- - Gemfile
79
75
  - MIT_LICENSE
80
76
  - README.md
81
- - Rakefile
82
- - database_rewinder.gemspec
83
77
  - lib/database_rewinder.rb
84
78
  - lib/database_rewinder/active_record_monkey.rb
85
79
  - lib/database_rewinder/cleaner.rb
86
80
  - lib/database_rewinder/compatibility.rb
87
81
  - lib/database_rewinder/dummy_model.rb
82
+ - lib/database_rewinder/lease_connection_compat.rb
88
83
  - lib/database_rewinder/multiple_statements_executor.rb
89
84
  - lib/database_rewinder/railtie.rb
90
- - test/active_record_monkey_test.rb
91
- - test/cleaner_test.rb
92
- - test/config/database.yml
93
- - test/database_rewinder_test.rb
94
- - test/db/.keep
95
- - test/fake_app.rb
96
- - test/test_helper.rb
97
85
  homepage: https://github.com/amatsuda/database_rewinder
98
86
  licenses:
99
87
  - MIT
100
88
  metadata: {}
101
- post_install_message:
102
89
  rdoc_options: []
103
90
  require_paths:
104
91
  - lib
@@ -113,15 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
100
  - !ruby/object:Gem::Version
114
101
  version: '0'
115
102
  requirements: []
116
- rubygems_version: 3.6.0.dev
117
- signing_key:
103
+ rubygems_version: 3.7.0.dev
118
104
  specification_version: 4
119
105
  summary: A minimalist's tiny and ultra-fast database cleaner
120
- test_files:
121
- - test/active_record_monkey_test.rb
122
- - test/cleaner_test.rb
123
- - test/config/database.yml
124
- - test/database_rewinder_test.rb
125
- - test/db/.keep
126
- - test/fake_app.rb
127
- - test/test_helper.rb
106
+ test_files: []
@@ -1,165 +0,0 @@
1
- name: build
2
-
3
- on: [push, pull_request]
4
-
5
- jobs:
6
- build:
7
- services:
8
- postgres:
9
- image: postgres:12
10
- env:
11
- POSTGRES_USER: postgres
12
- POSTGRES_PASSWORD: postgres
13
- ports:
14
- - 5432:5432
15
- options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
16
- mysql:
17
- image: mysql:5.7
18
- env:
19
- MYSQL_ROOT_PASSWORD:
20
- MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
21
- ports:
22
- - 3306:3306
23
- options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 10
24
-
25
- strategy:
26
- fail-fast: false
27
-
28
- matrix:
29
- db: [sqlite3, postgresql, mysql]
30
- ruby_version: [ruby-head, '3.3', '3.2', '3.1']
31
- rails_version: [edge, '7.1', '7.0', '6.1']
32
-
33
- exclude:
34
- - ruby_version: ruby-head
35
- rails_version: '6.1'
36
-
37
- include:
38
- - ruby_version: '3.0'
39
- rails_version: '7.1'
40
- db: sqlite3
41
- - ruby_version: '3.0'
42
- rails_version: '7.1'
43
- db: postgresql
44
- - ruby_version: '3.0'
45
- rails_version: '7.1'
46
- db: mysql
47
-
48
- - ruby_version: '2.7'
49
- rails_version: '7.1'
50
- db: sqlite3
51
- - ruby_version: '2.7'
52
- rails_version: '7.1'
53
- db: postgresql
54
- - ruby_version: '2.7'
55
- rails_version: '7.1'
56
- db: mysql
57
-
58
- - ruby_version: '2.7'
59
- rails_version: '7.0'
60
- db: sqlite3
61
- - ruby_version: '2.7'
62
- rails_version: '7.0'
63
- db: postgresql
64
- - ruby_version: '2.7'
65
- rails_version: '7.0'
66
- db: mysql
67
-
68
- - ruby_version: '3.0'
69
- rails_version: '6.1'
70
- db: sqlite3
71
- - ruby_version: '3.0'
72
- rails_version: '6.1'
73
- db: postgresql
74
- - ruby_version: '3.0'
75
- rails_version: '6.1'
76
- db: mysql
77
-
78
- - ruby_version: '2.6'
79
- rails_version: '6.1'
80
- db: sqlite3
81
- - ruby_version: '2.6'
82
- rails_version: '6.1'
83
- db: postgresql
84
- - ruby_version: '2.6'
85
- rails_version: '6.1'
86
- db: mysql
87
-
88
- - ruby_version: '2.7'
89
- rails_version: '6.0'
90
- db: sqlite3
91
- - ruby_version: '2.7'
92
- rails_version: '6.0'
93
- db: postgresql
94
- - ruby_version: '2.7'
95
- rails_version: '6.0'
96
- db: mysql
97
-
98
- - ruby_version: '2.7'
99
- rails_version: '5.2'
100
- db: sqlite3
101
- - ruby_version: '2.7'
102
- rails_version: '5.2'
103
- db: postgresql
104
- - ruby_version: '2.7'
105
- rails_version: '5.2'
106
- db: mysql
107
- - ruby_version: '2.4'
108
- rails_version: '5.2'
109
- db: sqlite3
110
- - ruby_version: '2.4'
111
- rails_version: '5.2'
112
- db: postgresql
113
- - ruby_version: '2.4'
114
- rails_version: '5.2'
115
- db: mysql
116
-
117
- - ruby_version: '2.6'
118
- rails_version: '5.1'
119
- db: sqlite3
120
- - ruby_version: '2.6'
121
- rails_version: '5.1'
122
- db: postgresql
123
- - ruby_version: '2.6'
124
- rails_version: '5.1'
125
- db: mysql
126
-
127
- - ruby_version: '2.5'
128
- rails_version: '5.0'
129
- db: sqlite3
130
- - ruby_version: '2.5'
131
- rails_version: '5.0'
132
- db: postgresql
133
- - ruby_version: '2.5'
134
- rails_version: '5.0'
135
- db: mysql
136
-
137
- - ruby_version: '2.4'
138
- rails_version: '4.2'
139
- db: sqlite3
140
- - ruby_version: '2.4'
141
- rails_version: '4.2'
142
- db: postgresql
143
- - ruby_version: '2.4'
144
- rails_version: '4.2'
145
- db: mysql
146
-
147
- runs-on: ubuntu-20.04
148
-
149
- env:
150
- RAILS_VERSION: ${{ matrix.rails_version }}
151
- DB: ${{ matrix.db }}
152
-
153
- steps:
154
- - uses: actions/checkout@v4
155
-
156
- - uses: ruby/setup-ruby@v1
157
- with:
158
- ruby-version: ${{ matrix.ruby_version }}
159
- rubygems: ${{ matrix.ruby_version < '2.6' && 'default' || 'latest' }}
160
- bundler: ${{ startsWith(matrix.rails_version, '4.') && '1' || 'latest' }}
161
- bundler-cache: true
162
- continue-on-error: ${{ (matrix.ruby_version == 'ruby-head') || (matrix.allow_failures == 'true') }}
163
-
164
- - run: bundle exec rake
165
- continue-on-error: ${{ (matrix.ruby_version == 'ruby-head') || (matrix.rails_version == 'edge') || (matrix.allow_failures == 'true') }}
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/Gemfile DELETED
@@ -1,35 +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
7
-
8
- if ENV['RAILS_VERSION'] == 'edge'
9
- gem 'rails', git: 'https://github.com/rails/rails.git'
10
- elsif ENV['RAILS_VERSION']
11
- gem 'rails', "~> #{ENV['RAILS_VERSION']}.0"
12
- else
13
- gem 'rails'
14
- end
15
-
16
- gem 'nokogiri', RUBY_VERSION < '2.1' ? '~> 1.6.0' : '>= 1.7'
17
- gem 'loofah', RUBY_VERSION < '2.5' ? '< 2.21.0' : '>= 0'
18
- gem 'selenium-webdriver'
19
-
20
- rails_version = ENV['RAILS_VERSION'] || '∞'
21
-
22
- case ENV['DB']
23
- when 'postgresql'
24
- gem 'pg', rails_version <= '5.2' ? '~> 0.21' : '>= 1'
25
- when 'mysql'
26
- if rails_version <= '4.1'
27
- gem 'mysql2', '~> 0.3.13'
28
- elsif rails_version <= '4.2'
29
- gem 'mysql2', '~> 0.4.0'
30
- else
31
- gem 'mysql2'
32
- end
33
- else
34
- gem 'sqlite3', rails_version <= '5.0' ? '< 1.4' : '>= 1.4'
35
- end
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,27 +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 = '1.0.0'
10
- spec.authors = ["Akira Matsuda"]
11
- spec.email = ["ronnie@dio.jp"]
12
- spec.description = "A minimalist's tiny and ultra-fast database cleaner for Active Record"
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.required_ruby_version = '>= 2.0.0'
27
- end
@@ -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,52 +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: 127.0.0.1
19
- username: root
20
- password:
21
- database: database_rewinder_test
22
-
23
- test2:
24
- adapter: mysql2
25
- host: 127.0.0.1
26
- username: root
27
- password:
28
- database: database_rewinder_test2
29
-
30
- <% when 'postgresql' %>
31
- superuser_connection:
32
- adapter: postgresql
33
- host: localhost
34
- # this user has to be a superuser for the tests to pass
35
- username: postgres
36
- password: postgres
37
- database: postgres
38
-
39
- test:
40
- adapter: postgresql
41
- host: localhost
42
- username: database_rewinder_user
43
- password: postgres
44
- database: database_rewinder_test
45
-
46
- test2:
47
- adapter: postgresql
48
- host: localhost
49
- username: database_rewinder_user
50
- password: postgres
51
- database: database_rewinder_test2
52
- <% end %>
@@ -1,324 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
6
- if ActiveRecord::VERSION::MAJOR >= 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
-
48
- if ActiveRecord::VERSION::MAJOR >= 6
49
- sub_test_case 'with traditional configurations' do
50
- test 'simply giving a connection name only' do
51
- assert_cleaners_added ['aaa'] do
52
- DatabaseRewinder.database_configuration = ActiveRecord::DatabaseConfigurations.new({'aaa' => {'adapter' => 'sqlite3', 'database' => ':memory:'}})
53
- DatabaseRewinder['aaa']
54
- end
55
- end
56
-
57
- test 'giving a connection name via Hash with :connection key' do
58
- assert_cleaners_added ['bbb'] do
59
- DatabaseRewinder.database_configuration = ActiveRecord::DatabaseConfigurations.new({'bbb' => {'adapter' => 'sqlite3', 'database' => ':memory:'}})
60
- DatabaseRewinder[connection: 'bbb']
61
- end
62
- end
63
-
64
- test 'the Cleaner compatible syntax' do
65
- assert_cleaners_added ['ccc'] do
66
- DatabaseRewinder.database_configuration = ActiveRecord::DatabaseConfigurations.new({'ccc' => {'adapter' => 'sqlite3', 'database' => ':memory:'}})
67
- DatabaseRewinder[:aho, connection: 'ccc']
68
- end
69
- end
70
- end
71
-
72
- sub_test_case 'with multiple database configurations' do
73
- test 'simply giving a connection name only' do
74
- assert_cleaners_added ['aaa'] do
75
- DatabaseRewinder.database_configuration = ActiveRecord::DatabaseConfigurations.new({'test' => {'aaa' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}})
76
- DatabaseRewinder['aaa']
77
- end
78
- end
79
-
80
- test 'giving a connection name via Hash with :connection key' do
81
- assert_cleaners_added ['bbb'] do
82
- DatabaseRewinder.database_configuration = ActiveRecord::DatabaseConfigurations.new({'test' => {'bbb' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}})
83
- DatabaseRewinder[connection: 'bbb']
84
- end
85
- end
86
-
87
- test 'the Cleaner compatible syntax' do
88
- assert_cleaners_added ['ccc'] do
89
- DatabaseRewinder.database_configuration = ActiveRecord::DatabaseConfigurations.new({'test' => {'ccc' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}})
90
- DatabaseRewinder[:aho, connection: 'ccc']
91
- end
92
- end
93
- end
94
- end
95
- end
96
-
97
- test 'for connecting to multiple databases' do
98
- DatabaseRewinder[:active_record, connection: 'test']
99
- DatabaseRewinder[:active_record, connection: 'test2']
100
-
101
- Foo.create! name: 'foo1'
102
- Quu.create! name: 'quu1'
103
-
104
- DatabaseRewinder.clean
105
-
106
- # it should clean all configured databases
107
- assert_equal 0, Foo.count
108
- assert_equal 0, Quu.count
109
- end
110
- end
111
-
112
- sub_test_case '.record_inserted_table' do
113
- setup do
114
- DatabaseRewinder.cleaners
115
- end
116
-
117
- def perform_insert(sql)
118
- @cleaner = DatabaseRewinder.instance_variable_get(:'@cleaners').detect {|c| c.db == (ENV['DB'] == 'sqlite3' ? 'db/database_rewinder_test.sqlite3' : 'database_rewinder_test')}
119
-
120
- connection = ::ActiveRecord::Base.connection
121
- DatabaseRewinder.record_inserted_table(connection, sql)
122
- end
123
- teardown do
124
- DatabaseRewinder.database_configuration = nil
125
- end
126
-
127
- sub_test_case 'via General Active Record insertions' do
128
- setup do
129
- DatabaseRewinder.cleaners
130
- @cleaner = DatabaseRewinder.instance_variable_get(:'@cleaners').detect {|c| c.db == (ENV['DB'] == 'sqlite3' ? 'db/database_rewinder_test.sqlite3' : 'database_rewinder_test')}
131
- end
132
-
133
- test 'create' do
134
- Bar.create name: 'bar1'
135
- assert_equal ['bars'], @cleaner.inserted_tables
136
- end
137
-
138
- if ActiveRecord::VERSION::MAJOR >= 6
139
- test 'insert_all' do
140
- Bar.insert_all! [{name: 'bar1'}]
141
- assert_equal ['bars'], @cleaner.inserted_tables
142
- end
143
- end
144
- end
145
-
146
- sub_test_case 'common database' do
147
- test 'include database name' do
148
- perform_insert 'INSERT INTO "database"."foos" ("name") VALUES (?)'
149
- assert_equal ['foos'], @cleaner.inserted_tables
150
- end
151
- test 'only table name' do
152
- perform_insert 'INSERT INTO "foos" ("name") VALUES (?)'
153
- assert_equal ['foos'], @cleaner.inserted_tables
154
- end
155
- test 'without "INTO"' do
156
- perform_insert 'INSERT "foos" ("name") VALUES (?)'
157
- assert_equal ['foos'], @cleaner.inserted_tables
158
- end
159
- test 'with space before "INSERT"' do
160
- perform_insert <<-SQL
161
- INSERT INTO "foos" ("name") VALUES (?)
162
- SQL
163
- assert_equal ['foos'], @cleaner.inserted_tables
164
- end
165
- test 'without spaces between table name and columns list' do
166
- perform_insert 'INSERT INTO foos(name) VALUES (?)'
167
- assert_equal ['foos'], @cleaner.inserted_tables
168
- end
169
-
170
- test 'with multi statement query' do
171
- perform_insert <<-SQL
172
- INSERT INTO "foos" ("name") VALUES (?);
173
- INSERT INTO "bars" ("name") VALUES (?)
174
- SQL
175
- assert_equal ['foos', 'bars'], @cleaner.inserted_tables
176
- end
177
- end
178
-
179
- sub_test_case 'Database accepts more than one dots in an object notation (e.g. SQLServer)' do
180
- test 'full joined' do
181
- perform_insert 'INSERT INTO server.database.schema.foos ("name") VALUES (?)'
182
- assert_equal ['foos'], @cleaner.inserted_tables
183
- end
184
- test 'missing one' do
185
- perform_insert 'INSERT INTO database..foos ("name") VALUES (?)'
186
- assert_equal ['foos'], @cleaner.inserted_tables
187
- end
188
-
189
- test 'missing two' do
190
- perform_insert 'INSERT INTO server...foos ("name") VALUES (?)'
191
- assert_equal ['foos'], @cleaner.inserted_tables
192
- end
193
- end
194
-
195
- test 'when database accepts INSERT IGNORE INTO statement' do
196
- perform_insert "INSERT IGNORE INTO `foos` (`name`) VALUES ('alice'), ('bob') ON DUPLICATE KEY UPDATE `foos`.`updated_at`=VALUES(`updated_at`)"
197
- assert_equal ['foos'], @cleaner.inserted_tables
198
- end
199
- end
200
-
201
- test '.clean' do
202
- bar = Bar.create! name: 'bar1'
203
- Foo.create! name: 'foo1', bar_id: bar.id
204
- DatabaseRewinder.clean
205
-
206
- assert_equal 0, Foo.count
207
- assert_equal 0, Bar.count
208
- end
209
-
210
- if ActiveRecord::VERSION::MAJOR >= 4
211
- sub_test_case 'migrations' do
212
- if ActiveRecord::VERSION::STRING >= '7.1'
213
- test '.clean_all should not touch AR::SchemaMigration' do
214
- schema_migration = ActiveRecord::SchemaMigration.new(ActiveRecord::Base.connection)
215
- schema_migration.create_table
216
-
217
- begin
218
- schema_migration.create_version '001'
219
- DatabaseRewinder.clean_all
220
-
221
- assert_equal 0, Foo.count
222
- assert_equal 1, schema_migration.count
223
- ensure
224
- schema_migration.drop_table
225
- end
226
- end
227
- else
228
- test '.clean_all should not touch AR::SchemaMigration' do
229
- ActiveRecord::SchemaMigration.create_table
230
-
231
- begin
232
- ActiveRecord::SchemaMigration.create! version: '001'
233
- DatabaseRewinder.clean_all
234
-
235
- assert_equal 0, Foo.count
236
- assert_equal 1, ActiveRecord::SchemaMigration.count
237
- ensure
238
- ActiveRecord::SchemaMigration.drop_table
239
- end
240
- end
241
- end
242
- end
243
- end
244
-
245
- sub_test_case '.clean_with' do
246
- def perform_clean(options)
247
- @cleaner = DatabaseRewinder.cleaners.first
248
- @only = @cleaner.instance_variable_get(:@only)
249
- @except = @cleaner.instance_variable_get(:@except)
250
- Foo.create! name: 'foo1'
251
- Bar.create! name: 'bar1'
252
- DatabaseRewinder.clean_with :truncation, **options
253
- end
254
-
255
- test 'with only option' do
256
- perform_clean only: ['foos']
257
- assert_equal 0, Foo.count
258
- assert_equal 1, Bar.count
259
- assert_equal @only, @cleaner.instance_variable_get(:@only)
260
- end
261
-
262
- test 'with except option' do
263
- perform_clean except: ['bars']
264
- assert_equal 0, Foo.count
265
- assert_equal 1, Bar.count
266
- assert_equal @except, @cleaner.instance_variable_get(:@except)
267
- end
268
- end
269
-
270
- sub_test_case '.cleaning' do
271
- test 'without exception' do
272
- DatabaseRewinder.cleaning do
273
- Foo.create! name: 'foo1'
274
- end
275
-
276
- assert_equal 0, Foo.count
277
- end
278
-
279
- test 'with exception' do
280
- assert_raises do
281
- DatabaseRewinder.cleaning do
282
- Foo.create! name: 'foo1'; fail
283
- end
284
- end
285
- assert_equal 0, Foo.count
286
- end
287
- end
288
-
289
- sub_test_case '.strategy=' do
290
- sub_test_case 'call first with options' do
291
- setup do
292
- DatabaseRewinder.strategy = :truncate, { only: ['foos'], except: ['bars'] }
293
- end
294
-
295
- test 'should set options' do
296
- assert_equal ['foos'], DatabaseRewinder.instance_variable_get(:@only)
297
- assert_equal ['bars'], DatabaseRewinder.instance_variable_get(:@except)
298
- end
299
-
300
- test 'should create cleaner with options' do
301
- cleaner = DatabaseRewinder.instance_variable_get(:@cleaners).first
302
- assert_equal ['foos'], cleaner.instance_variable_get(:@only)
303
- assert_equal ['bars'], cleaner.instance_variable_get(:@except)
304
- end
305
-
306
- sub_test_case 'call again with different options' do
307
- setup do
308
- DatabaseRewinder.strategy = :truncate, { only: ['bazs'], except: [] }
309
- end
310
-
311
- test 'should overwrite options' do
312
- assert_equal ['bazs'], DatabaseRewinder.instance_variable_get(:@only)
313
- assert_equal [], DatabaseRewinder.instance_variable_get(:@except)
314
- end
315
-
316
- test 'should overwrite cleaner with new options' do
317
- cleaner = DatabaseRewinder.instance_variable_get(:@cleaners).first
318
- assert_equal ['bazs'], cleaner.instance_variable_get(:@only)
319
- assert_equal [], cleaner.instance_variable_get(:@except)
320
- end
321
- end
322
- end
323
- end
324
- end
data/test/db/.keep DELETED
File without changes
data/test/fake_app.rb DELETED
@@ -1,72 +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
-
14
- rake_tasks do
15
- load 'active_record/railties/databases.rake'
16
- end
17
- end.initialize!
18
- end
19
-
20
- require 'active_record/base'
21
-
22
- if ENV['DB'] == 'postgresql'
23
- ActiveRecord::Base.establish_connection(:superuser_connection).connection.execute(<<-CREATE_ROLE_SQL)
24
- DO
25
- $do$
26
- BEGIN
27
- IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'database_rewinder_user') THEN
28
- CREATE ROLE database_rewinder_user LOGIN PASSWORD 'postgres' createdb;
29
- END IF;
30
- END
31
- $do$
32
- CREATE_ROLE_SQL
33
- ActiveRecord::Base.remove_connection
34
- end
35
-
36
- ActiveRecord::Tasks::DatabaseTasks.root ||= Rails.root
37
- ActiveRecord::Tasks::DatabaseTasks.drop_current 'test'
38
- ActiveRecord::Tasks::DatabaseTasks.drop_current 'test2'
39
- ActiveRecord::Tasks::DatabaseTasks.create_current 'test'
40
- ActiveRecord::Tasks::DatabaseTasks.create_current 'test2'
41
-
42
- # models
43
- class Foo < ActiveRecord::Base; end
44
- class Bar < ActiveRecord::Base; end
45
- class Baz < ActiveRecord::Base; end
46
- class Quu < ActiveRecord::Base
47
- establish_connection :test2
48
- end
49
-
50
- # migrations
51
- class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
52
- def self.up
53
- ActiveRecord::Base.establish_connection :test
54
- create_table(:bars) {|t| t.string :name; t.index :name, unique: true }
55
- create_table(:foos) {|t| t.string :name; t.references :bar, foreign_key: true }
56
- create_table(:bazs) {|t| t.string :name }
57
-
58
- test2_connection = ActiveRecord::Base.establish_connection(:test2).connection
59
- test2_connection.create_table(:quus) {|t| t.string :name }
60
- ActiveRecord::Base.establish_connection :test
61
- end
62
-
63
- def self.down
64
- drop_table(:foos) {|t| t.string :name }
65
- drop_table(:bars) {|t| t.string :name }
66
- drop_table(:bazs) {|t| t.string :name }
67
-
68
- test2_connection = ActiveRecord::Base.establish_connection(:test2).connection
69
- test2_connection.drop_table :quus
70
- ActiveRecord::Base.establish_connection :test
71
- end
72
- 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