database_rewinder 0.9.7 → 0.9.9

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: 3faca08aeeb413648502e9d1f981e3588602c1a16443c5c5eb5761eacc87958b
4
- data.tar.gz: cb98bb919d0385f4a0fb576f3ce5be2a563d51d68f86925fe23496413d17d0ac
3
+ metadata.gz: 395cf45a9155494fbaa28f887a793d3780c6c4dea757e53d9c9057ddec96baca
4
+ data.tar.gz: 68088ac4f44554359f87e946db585812301557287c5f3b63b88c1c45b29f865d
5
5
  SHA512:
6
- metadata.gz: d9319fdd90028c57093eba269657626af32dde21253a068949d20b325179f92749e1ad28953175c9a2c3805745167425cccb152aab689bf4811f2beb5ea88a29
7
- data.tar.gz: cb93c0847c649a3dfbace111dd3c5082c5950c95db7c4f31adbc6161daa0d2ff373e13f772b2e234293179a8ddc94e9c61021cadfea55c8fd9c40661ca1c2ef0
6
+ metadata.gz: 80e0acaed09f0606b8ae715e072c9d9198c97fcd07cedbda94c1442ef256970e89da10a983935724440fb98362210bf986f800051b79223c828061828e563b68
7
+ data.tar.gz: d0c1275d8ee0a691271a5569c499f1c23c98167089935161b798090d84b1136039ca4c7d9b34186688d48671c821b18a754614c3fc2683b93907ffdf92bbdd4d
@@ -6,7 +6,7 @@ jobs:
6
6
  build:
7
7
  services:
8
8
  postgres:
9
- image: postgres:10
9
+ image: postgres:12
10
10
  env:
11
11
  POSTGRES_USER: postgres
12
12
  POSTGRES_PASSWORD: postgres
@@ -27,30 +27,51 @@ jobs:
27
27
 
28
28
  matrix:
29
29
  db: [sqlite3, postgresql, mysql]
30
- ruby_version: [ruby-head, '3.0', '2.7', '2.6']
31
- rails_version: [edge, '7.0', '6.1', '6.0', '5.2']
30
+ ruby_version: [ruby-head, '3.2', '3.1', '3.0', '2.7', '2.6']
31
+ rails_version: [edge, '7.1', '7.0', '6.1']
32
32
 
33
33
  exclude:
34
34
  - ruby_version: '2.6'
35
35
  rails_version: edge
36
36
 
37
+ - ruby_version: '2.6'
38
+ rails_version: '7.1'
39
+
37
40
  - ruby_version: '2.6'
38
41
  rails_version: '7.0'
39
42
 
40
43
  - ruby_version: ruby-head
41
44
  rails_version: '6.1'
42
45
 
43
- - ruby_version: ruby-head
46
+ include:
47
+ - ruby_version: '2.7'
48
+ rails_version: '6.0'
49
+ db: sqlite3
50
+ - ruby_version: '2.7'
51
+ rails_version: '6.0'
52
+ db: postgresql
53
+ - ruby_version: '2.7'
54
+ rails_version: '6.0'
55
+ db: mysql
56
+ - ruby_version: '2.6'
57
+ rails_version: '6.0'
58
+ db: sqlite3
59
+ - ruby_version: '2.6'
44
60
  rails_version: '6.0'
45
- - ruby_version: '3.0'
61
+ db: postgresql
62
+ - ruby_version: '2.6'
46
63
  rails_version: '6.0'
64
+ db: mysql
47
65
 
48
- - ruby_version: ruby-head
66
+ - ruby_version: '2.7'
49
67
  rails_version: '5.2'
50
- - ruby_version: '3.0'
68
+ db: sqlite3
69
+ - ruby_version: '2.7'
51
70
  rails_version: '5.2'
52
-
53
- include:
71
+ db: postgresql
72
+ - ruby_version: '2.7'
73
+ rails_version: '5.2'
74
+ db: mysql
54
75
  - ruby_version: '2.4'
55
76
  rails_version: '5.2'
56
77
  db: sqlite3
@@ -91,18 +112,19 @@ jobs:
91
112
  rails_version: '4.2'
92
113
  db: mysql
93
114
 
94
- runs-on: ubuntu-18.04
115
+ runs-on: ubuntu-20.04
95
116
 
96
117
  env:
97
118
  RAILS_VERSION: ${{ matrix.rails_version }}
98
119
  DB: ${{ matrix.db }}
99
120
 
100
121
  steps:
101
- - uses: actions/checkout@v2
122
+ - uses: actions/checkout@v4
102
123
 
103
124
  - uses: ruby/setup-ruby@v1
104
125
  with:
105
126
  ruby-version: ${{ matrix.ruby_version }}
127
+ rubygems: ${{ matrix.ruby_version < '2.6' && 'default' || 'latest' }}
106
128
  bundler: ${{ startsWith(matrix.rails_version, '4.') && '1' || 'latest' }}
107
129
  bundler-cache: true
108
130
  continue-on-error: ${{ (matrix.ruby_version == 'ruby-head') || (matrix.allow_failures == 'true') }}
data/Gemfile CHANGED
@@ -14,6 +14,7 @@ else
14
14
  end
15
15
 
16
16
  gem 'nokogiri', RUBY_VERSION < '2.1' ? '~> 1.6.0' : '>= 1.7'
17
+ gem 'loofah', RUBY_VERSION < '2.5' ? '< 2.21.0' : '>= 0'
17
18
  gem 'selenium-webdriver'
18
19
 
19
20
  rails_version = ENV['RAILS_VERSION'] || '∞'
data/README.md CHANGED
@@ -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.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.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
 
@@ -6,7 +6,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
6
 
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = "database_rewinder"
9
- spec.version = '0.9.7'
9
+ spec.version = '0.9.9'
10
10
  spec.authors = ["Akira Matsuda"]
11
11
  spec.email = ["ronnie@dio.jp"]
12
12
  spec.description = "A minimalist's tiny and ultra-fast database cleaner for Active Record"
@@ -2,57 +2,27 @@
2
2
 
3
3
  module DatabaseRewinder
4
4
  module InsertRecorder
5
- module Execute
6
- module NoKwargs
7
- def execute(sql, *)
8
- DatabaseRewinder.record_inserted_table self, sql
9
- super
10
- end
11
- end
12
-
13
- module WithKwargs
14
- def execute(sql, *, **)
15
- DatabaseRewinder.record_inserted_table self, sql
16
- super
17
- end
18
-
19
- def raw_execute(sql, *, **)
20
- DatabaseRewinder.record_inserted_table self, sql
21
- super
22
- end
23
- end
24
- end
25
-
26
- module ExecQuery
27
- module NoKwargs
28
- def exec_query(sql, *)
29
- DatabaseRewinder.record_inserted_table self, sql
30
- super
31
- end
32
- end
33
-
34
- module WithKwargs
35
- def exec_query(sql, *, **)
36
- DatabaseRewinder.record_inserted_table self, sql
37
- super
38
- end
39
- end
40
- end
41
-
42
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
43
6
  def self.prepended(mod)
44
- if meth = mod.instance_method(:execute)
45
- if meth.parameters.any? {|type, _name| [:key, :keyreq, :keyrest].include? type }
46
- mod.send :prepend, Execute::WithKwargs
47
- else
48
- mod.send :prepend, Execute::NoKwargs
49
- end
50
- end
51
- if meth = mod.instance_method(:exec_query)
52
- if meth.parameters.any? {|type, _name| [:key, :keyreq, :keyrest].include? type }
53
- mod.send :prepend, ExecQuery::WithKwargs
54
- else
55
- mod.send :prepend, ExecQuery::NoKwargs
7
+ [:execute, :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
24
+
25
+ mod.send :prepend, Module.new { class_eval method_body }
56
26
  end
57
27
  end
58
28
  end
@@ -62,7 +32,9 @@ end
62
32
 
63
33
  # Already loaded adapters (SQLite3Adapter, PostgreSQLAdapter, AbstractMysqlAdapter, and possibly another third party adapter)
64
34
  ::ActiveRecord::ConnectionAdapters::AbstractAdapter.descendants.each do |adapter|
65
- # Note: this would only prepend on AbstractMysqlAdapter and not on Mysql2Adapter because ```Mysql2Adapter < InsertRecorder``` becomes true immediately after AbstractMysqlAdapter prepends InsertRecorder
35
+ # In order not to touch AbstractMysqlAdapter thing, but to surely patch the concrete classes
36
+ next if adapter.descendants.any?
37
+
66
38
  adapter.send :prepend, DatabaseRewinder::InsertRecorder unless adapter < DatabaseRewinder::InsertRecorder
67
39
  end
68
40
 
@@ -48,6 +48,8 @@ module DatabaseRewinder
48
48
  def delete_all(ar_conn, tables, multiple: true)
49
49
  tables = tables & @only if @only.any?
50
50
  tables -= @except if @except.any?
51
+ # in order to avoid referential integrity error as much as possible
52
+ tables.reverse!
51
53
  return if tables.empty?
52
54
 
53
55
  if multiple && tables.many? && ar_conn.supports_multiple_statements?
@@ -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 || @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 || @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 || @connection).query sql }
20
+ while (@raw_connection || @connection).next_result
21
21
  # just to make sure that all queries are finished
22
- _result = @connection.store_result
22
+ _result = (@raw_connection || @connection).store_result
23
23
  end
24
24
  end
25
25
  else
26
- query_options = @connection.query_options.dup
26
+ query_options = (@raw_connection || @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,7 +40,7 @@ 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 || @connection).execute_batch sql } }
44
44
  else
45
45
  raise 'Multiple deletion is not supported with the current database adapter.'
46
46
  end
@@ -76,8 +76,14 @@ module DatabaseRewinder
76
76
  cache_key = get_cache_key(connection.pool)
77
77
  #NOTE connection.tables warns on AR 5 with some adapters
78
78
  tables = ActiveSupport::Deprecation.silence { connection.tables }
79
+ schema_migraion_table_name =
80
+ if ActiveRecord::SchemaMigration.respond_to?(:table_name)
81
+ ActiveRecord::SchemaMigration.table_name
82
+ else
83
+ ActiveRecord::SchemaMigration.new(connection).table_name # AR >= 7.1
84
+ end
79
85
  @table_names_cache[cache_key] ||= tables.reject do |t|
80
- (t == ActiveRecord::SchemaMigration.table_name) ||
86
+ (t == schema_migraion_table_name) ||
81
87
  (ActiveRecord::Base.respond_to?(:internal_metadata_table_name) && (t == ActiveRecord::Base.internal_metadata_table_name))
82
88
  end
83
89
  end
@@ -28,18 +28,25 @@ test2:
28
28
  database: database_rewinder_test2
29
29
 
30
30
  <% when 'postgresql' %>
31
- test:
31
+ superuser_connection:
32
32
  adapter: postgresql
33
33
  host: localhost
34
34
  # this user has to be a superuser for the tests to pass
35
35
  username: postgres
36
36
  password: postgres
37
+ database: postgres
38
+
39
+ test:
40
+ adapter: postgresql
41
+ host: localhost
42
+ username: database_rewinder_user
43
+ password: postgres
37
44
  database: database_rewinder_test
38
45
 
39
46
  test2:
40
47
  adapter: postgresql
41
48
  host: localhost
42
- username: postgres
49
+ username: database_rewinder_user
43
50
  password: postgres
44
51
  database: database_rewinder_test2
45
52
  <% end %>
@@ -209,16 +209,34 @@ class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
209
209
 
210
210
  if ActiveRecord::VERSION::MAJOR >= 4
211
211
  sub_test_case 'migrations' do
212
- test '.clean_all should not touch AR::SchemaMigration' do
213
- begin
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
214
229
  ActiveRecord::SchemaMigration.create_table
215
- ActiveRecord::SchemaMigration.create! version: '001'
216
- DatabaseRewinder.clean_all
217
230
 
218
- assert_equal 0, Foo.count
219
- assert_equal 1, ActiveRecord::SchemaMigration.count
220
- ensure
221
- ActiveRecord::SchemaMigration.drop_table
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
222
240
  end
223
241
  end
224
242
  end
data/test/fake_app.rb CHANGED
@@ -18,6 +18,21 @@ module DatabaseRewinderTestApp
18
18
  end
19
19
 
20
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
+
21
36
  ActiveRecord::Tasks::DatabaseTasks.root ||= Rails.root
22
37
  ActiveRecord::Tasks::DatabaseTasks.drop_current 'test'
23
38
  ActiveRecord::Tasks::DatabaseTasks.drop_current 'test2'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_rewinder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.7
4
+ version: 0.9.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Matsuda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-23 00:00:00.000000000 Z
11
+ date: 2023-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
115
  requirements: []
116
- rubygems_version: 3.3.3
116
+ rubygems_version: 3.5.0.dev
117
117
  signing_key:
118
118
  specification_version: 4
119
119
  summary: A minimalist's tiny and ultra-fast database cleaner