database_rewinder 0.9.1 → 0.9.8

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: b65d98970a4cf3742c0ea0254aedb60cc3d64260f9b3bbca199bad7880a178aa
4
- data.tar.gz: 131690c4430bc96b9fb06d00eaf6474bbe23f9bb038e44f18c00240f5ff9372d
3
+ metadata.gz: 33fb09096b416644a0a992422fd4bd0b1f8b8957d447f96e6c825f61d0c1246a
4
+ data.tar.gz: 7f01e681757326b0ef190d046bc9620151b18736036b4529ccafbc57b261f2dd
5
5
  SHA512:
6
- metadata.gz: 02cdab1001bce6f92944c765d696ee94e6ea2cdddb4ffe9b438a184d62fd0adb8ba9cb2521d1c3a655cfbe52a653ab62f56f3259244cc55ea5f420240aa814be
7
- data.tar.gz: 11a8155b683374275680fbb16c03b2d6f5f8af4fa51310e6e05e49b1330f6dbf5ba8793643617085cb6d2765372c61d5ef7a0887639e351554b52492c68e0417
6
+ metadata.gz: bc22e635b796d03d57828b0bfae32ad0c611f1596cf2a945f91408285149b8ab151469ce696c4b359a592861611e7731e5bbad21f95b36b7ff0af074be4a4cee
7
+ data.tar.gz: b4f10e3069e8141bab9537a85ac48bfadf93b5cc029bfddfe35f9ce0c60c045a131c205a7587ca0e7fae19d1ccbc8912c4bdceeda1ab0bae6011d29572ada129
@@ -0,0 +1,111 @@
1
+ name: build
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ services:
8
+ postgres:
9
+ image: postgres:10
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.0', '2.7', '2.6']
31
+ rails_version: [edge, '7.0', '6.1', '6.0', '5.2']
32
+
33
+ exclude:
34
+ - ruby_version: '2.6'
35
+ rails_version: edge
36
+
37
+ - ruby_version: '2.6'
38
+ rails_version: '7.0'
39
+
40
+ - ruby_version: ruby-head
41
+ rails_version: '6.1'
42
+
43
+ - ruby_version: ruby-head
44
+ rails_version: '6.0'
45
+ - ruby_version: '3.0'
46
+ rails_version: '6.0'
47
+
48
+ - ruby_version: ruby-head
49
+ rails_version: '5.2'
50
+ - ruby_version: '3.0'
51
+ rails_version: '5.2'
52
+
53
+ include:
54
+ - ruby_version: '2.4'
55
+ rails_version: '5.2'
56
+ db: sqlite3
57
+ - ruby_version: '2.4'
58
+ rails_version: '5.2'
59
+ db: postgresql
60
+ - ruby_version: '2.4'
61
+ rails_version: '5.2'
62
+ db: mysql
63
+
64
+ - ruby_version: '2.6'
65
+ rails_version: '5.1'
66
+ db: sqlite3
67
+ - ruby_version: '2.6'
68
+ rails_version: '5.1'
69
+ db: postgresql
70
+ - ruby_version: '2.6'
71
+ rails_version: '5.1'
72
+ db: mysql
73
+
74
+ - ruby_version: '2.5'
75
+ rails_version: '5.0'
76
+ db: sqlite3
77
+ - ruby_version: '2.5'
78
+ rails_version: '5.0'
79
+ db: postgresql
80
+ - ruby_version: '2.5'
81
+ rails_version: '5.0'
82
+ db: mysql
83
+
84
+ - ruby_version: '2.4'
85
+ rails_version: '4.2'
86
+ db: sqlite3
87
+ - ruby_version: '2.4'
88
+ rails_version: '4.2'
89
+ db: postgresql
90
+ - ruby_version: '2.4'
91
+ rails_version: '4.2'
92
+ db: mysql
93
+
94
+ runs-on: ubuntu-18.04
95
+
96
+ env:
97
+ RAILS_VERSION: ${{ matrix.rails_version }}
98
+ DB: ${{ matrix.db }}
99
+
100
+ steps:
101
+ - uses: actions/checkout@v2
102
+
103
+ - uses: ruby/setup-ruby@v1
104
+ with:
105
+ ruby-version: ${{ matrix.ruby_version }}
106
+ bundler: ${{ startsWith(matrix.rails_version, '4.') && '1' || 'latest' }}
107
+ bundler-cache: true
108
+ continue-on-error: ${{ (matrix.ruby_version == 'ruby-head') || (matrix.allow_failures == 'true') }}
109
+
110
+ - run: bundle exec rake
111
+ continue-on-error: ${{ (matrix.ruby_version == 'ruby-head') || (matrix.rails_version == 'edge') || (matrix.allow_failures == 'true') }}
data/Gemfile CHANGED
@@ -4,3 +4,31 @@ source 'https://rubygems.org'
4
4
 
5
5
  # Specify your gem's dependencies in database_rewinder.gemspec
6
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 'selenium-webdriver'
18
+
19
+ rails_version = ENV['RAILS_VERSION'] || '∞'
20
+
21
+ case ENV['DB']
22
+ when 'postgresql'
23
+ gem 'pg', rails_version <= '5.2' ? '~> 0.21' : '>= 1'
24
+ when 'mysql'
25
+ if rails_version <= '4.1'
26
+ gem 'mysql2', '~> 0.3.13'
27
+ elsif rails_version <= '4.2'
28
+ gem 'mysql2', '~> 0.4.0'
29
+ else
30
+ gem 'mysql2'
31
+ end
32
+ else
33
+ gem 'sqlite3', rails_version <= '5.0' ? '< 1.4' : '>= 1.4'
34
+ end
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 (edge)
26
+ * ActiveRecord 4.2, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0 (edge)
27
27
 
28
- * Ruby 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6 (trunk)
28
+ * Ruby 2.4, 2.5, 2.6, 2.7, 3.0, 3.1 (trunk)
29
29
 
30
30
  ## Installation
31
31
 
@@ -98,7 +98,7 @@ In cases of this situation, bundle database\_rewinder and add the following conf
98
98
 
99
99
  ```ruby
100
100
  RSpec.configure do |config|
101
- config.use_transactional_examples = false
101
+ config.use_transactional_tests = false
102
102
 
103
103
  ...
104
104
  end
@@ -6,10 +6,10 @@ $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.1'
9
+ spec.version = '0.9.8'
10
10
  spec.authors = ["Akira Matsuda"]
11
11
  spec.email = ["ronnie@dio.jp"]
12
- spec.description = "A minimalist's tiny and ultra-fast database cleaner"
12
+ spec.description = "A minimalist's tiny and ultra-fast database cleaner for Active Record"
13
13
  spec.summary = "A minimalist's tiny and ultra-fast database cleaner"
14
14
  spec.homepage = 'https://github.com/amatsuda/database_rewinder'
15
15
  spec.license = "MIT"
@@ -19,12 +19,9 @@ Gem::Specification.new do |spec|
19
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency 'bundler'
23
23
  spec.add_development_dependency "rake"
24
24
  spec.add_development_dependency 'test-unit-rails'
25
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
26
  spec.required_ruby_version = '>= 2.0.0'
30
27
  end
@@ -2,22 +2,71 @@
2
2
 
3
3
  module DatabaseRewinder
4
4
  module InsertRecorder
5
- def execute(sql, *)
6
- DatabaseRewinder.record_inserted_table self, sql
7
- super
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
8
24
  end
9
25
 
10
- def exec_query(sql, *)
11
- DatabaseRewinder.record_inserted_table self, sql
12
- super
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
+ # 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
+ 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
56
+ end
57
+ end
13
58
  end
14
59
  end
15
60
  end
16
61
 
17
- ::ActiveRecord::ConnectionAdapters::SQLite3Adapter.send :prepend, DatabaseRewinder::InsertRecorder if defined? ::ActiveRecord::ConnectionAdapters::SQLite3Adapter
18
- ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send :prepend, DatabaseRewinder::InsertRecorder if defined? ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
19
- ::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.send :prepend, DatabaseRewinder::InsertRecorder if defined? ::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
20
62
 
63
+ # Already loaded adapters (SQLite3Adapter, PostgreSQLAdapter, AbstractMysqlAdapter, and possibly another third party adapter)
64
+ ::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
66
+ adapter.send :prepend, DatabaseRewinder::InsertRecorder unless adapter < DatabaseRewinder::InsertRecorder
67
+ end
68
+
69
+ # Third party adapters that might be loaded in the future
21
70
  def (::ActiveRecord::ConnectionAdapters::AbstractAdapter).inherited(adapter)
22
- adapter.prepend DatabaseRewinder::InsertRecorder
71
+ adapter.send :prepend, DatabaseRewinder::InsertRecorder
23
72
  end
@@ -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?
@@ -2,8 +2,8 @@
2
2
 
3
3
  module DatabaseRewinder
4
4
  module Compatibility
5
- def clean_with(*args)
6
- cleaners.each {|c| c.clean_with(*args)}
5
+ def clean_with(*args, **opts)
6
+ cleaners.each {|c| c.clean_with(*args, **opts)}
7
7
  end
8
8
 
9
9
  def cleaning
@@ -36,7 +36,7 @@ module DatabaseRewinder
36
36
  # DatabaseRewinder[:active_record, connection: 'the_db_name']
37
37
  #
38
38
  # You can cleanup multiple databases for each test using this configuration.
39
- def [](orm, connection: nil, **)
39
+ def [](orm = nil, connection: nil, **)
40
40
  if connection.nil?
41
41
  if orm.is_a? String
42
42
  connection = orm
@@ -16,7 +16,7 @@ module DatabaseRewinder
16
16
  end
17
17
 
18
18
  def create_cleaner(connection_name)
19
- config = database_configuration[connection_name] or raise %Q[Database configuration named "#{connection_name}" is not configured.]
19
+ config = configuration_hash_for(connection_name) or raise %Q[Database configuration named "#{connection_name}" is not configured.]
20
20
 
21
21
  Cleaner.new(config: config, connection_name: connection_name, only: @only, except: @except).tap {|c| @cleaners << c}
22
22
  end
@@ -48,7 +48,7 @@ module DatabaseRewinder
48
48
  end or return
49
49
 
50
50
  sql.split(';').each do |statement|
51
- match = statement.match(/\A\s*INSERT(?:\s+IGNORE)?(?:\s+INTO)?\s+(?:\.*[`"]?([^.\s`"]+)[`"]?)*/i)
51
+ match = statement.match(/\A\s*INSERT(?:\s+IGNORE)?(?:\s+INTO)?\s+(?:\.*[`"]?([^.\s`"(]+)[`"]?)*/i)
52
52
  next unless match
53
53
 
54
54
  table = match[1]
@@ -73,7 +73,7 @@ module DatabaseRewinder
73
73
 
74
74
  # cache AR connection.tables
75
75
  def all_table_names(connection)
76
- cache_key = connection.pool.spec.config
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
79
  @table_names_cache[cache_key] ||= tables.reject do |t|
@@ -81,7 +81,48 @@ module DatabaseRewinder
81
81
  (ActiveRecord::Base.respond_to?(:internal_metadata_table_name) && (t == ActiveRecord::Base.internal_metadata_table_name))
82
82
  end
83
83
  end
84
+
85
+ def get_cache_key(connection_pool)
86
+ if connection_pool.respond_to?(:db_config) # ActiveRecord >= 6.1
87
+ connection_pool.db_config.configuration_hash
88
+ else
89
+ connection_pool.spec.config
90
+ end
91
+ end
92
+
93
+ def configuration_hash_for(connection_name)
94
+ if database_configuration.respond_to?(:configs_for)
95
+ hash_config = database_configuration_for(connection_name)
96
+ if hash_config
97
+ if hash_config.respond_to?(:configuration_hash)
98
+ hash_config.configuration_hash.stringify_keys
99
+ else
100
+ hash_config.config
101
+ end
102
+ end
103
+ else
104
+ database_configuration[connection_name]
105
+ end
106
+ end
107
+
108
+ def database_configuration_for(connection_name)
109
+ traditional_configuration_for(connection_name) || multiple_database_configuration_for(connection_name)
110
+ end
111
+
112
+ def traditional_configuration_for(connection_name)
113
+ database_configuration.configs_for(env_name: connection_name).first
114
+ end
115
+
116
+ def multiple_database_configuration_for(connection_name)
117
+ if (ActiveRecord::VERSION::MAJOR >= 7) || ((ActiveRecord::VERSION::MAJOR >= 6) && (ActiveRecord::VERSION::MINOR >= 1))
118
+ database_configuration.configs_for(name: connection_name)
119
+ else
120
+ database_configuration.configs_for(spec_name: connection_name)
121
+ end
122
+ end
84
123
  end
124
+
125
+ private_class_method :configuration_hash_for, :get_cache_key
85
126
  end
86
127
 
87
128
  begin
@@ -1,45 +1,52 @@
1
- sqlite3: &sqlite3
1
+ <% case ENV['DB']
2
+ when 'sqlite3' %>
3
+ test:
2
4
  adapter: sqlite3
3
5
  database: db/database_rewinder_test.sqlite3
4
6
  pool: 5
5
7
  timeout: 5000
6
8
 
7
- sqlite3_2: &sqlite3_2
9
+ test2:
8
10
  adapter: sqlite3
9
11
  database: db/database_rewinder_test2.sqlite3
10
12
  pool: 5
11
13
  timeout: 5000
12
14
 
13
- mysql: &mysql
15
+ <% when 'mysql' %>
16
+ test:
14
17
  adapter: mysql2
15
- host: localhost
18
+ host: 127.0.0.1
16
19
  username: root
17
20
  password:
18
21
  database: database_rewinder_test
19
22
 
20
- mysql_2: &mysql_2
23
+ test2:
21
24
  adapter: mysql2
22
- host: localhost
25
+ host: 127.0.0.1
23
26
  username: root
24
27
  password:
25
28
  database: database_rewinder_test2
26
29
 
27
- postgresql: &postgresql
30
+ <% when 'postgresql' %>
31
+ superuser_connection:
28
32
  adapter: postgresql
29
33
  host: localhost
34
+ # this user has to be a superuser for the tests to pass
30
35
  username: postgres
31
- password:
32
- database: database_rewinder_test
36
+ password: postgres
37
+ database: postgres
33
38
 
34
- postgresql_2: &postgresql_2
39
+ test:
35
40
  adapter: postgresql
36
41
  host: localhost
37
- username: postgres
38
- password:
39
- database: database_rewinder_test2
40
-
41
- test:
42
- <<: *<%= ENV['DB'] %>
42
+ username: database_rewinder_user
43
+ password: postgres
44
+ database: database_rewinder_test
43
45
 
44
46
  test2:
45
- <<: *<%= "#{ENV['DB']}_2" %>
47
+ adapter: postgresql
48
+ host: localhost
49
+ username: database_rewinder_user
50
+ password: postgres
51
+ database: database_rewinder_test2
52
+ <% end %>
@@ -3,7 +3,7 @@
3
3
  require 'test_helper'
4
4
 
5
5
  class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
6
- if ActiveRecord::VERSION::STRING >= '5'
6
+ if ActiveRecord::VERSION::MAJOR >= 5
7
7
  self.use_transactional_tests = false
8
8
  else
9
9
  self.use_transactional_fixtures = false
@@ -44,6 +44,54 @@ class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
44
44
  DatabaseRewinder[:aho, connection: 'ccc']
45
45
  end
46
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
47
95
  end
48
96
 
49
97
  test 'for connecting to multiple databases' do
@@ -76,6 +124,25 @@ class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
76
124
  DatabaseRewinder.database_configuration = nil
77
125
  end
78
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
+
79
146
  sub_test_case 'common database' do
80
147
  test 'include database name' do
81
148
  perform_insert 'INSERT INTO "database"."foos" ("name") VALUES (?)'
@@ -95,6 +162,10 @@ class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
95
162
  SQL
96
163
  assert_equal ['foos'], @cleaner.inserted_tables
97
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
98
169
 
99
170
  test 'with multi statement query' do
100
171
  perform_insert <<-SQL
@@ -136,7 +207,7 @@ class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
136
207
  assert_equal 0, Bar.count
137
208
  end
138
209
 
139
- if ActiveRecord::VERSION::STRING >= '4'
210
+ if ActiveRecord::VERSION::MAJOR >= 4
140
211
  sub_test_case 'migrations' do
141
212
  test '.clean_all should not touch AR::SchemaMigration' do
142
213
  begin
@@ -160,7 +231,7 @@ class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
160
231
  @except = @cleaner.instance_variable_get(:@except)
161
232
  Foo.create! name: 'foo1'
162
233
  Bar.create! name: 'bar1'
163
- DatabaseRewinder.clean_with :truncation, options
234
+ DatabaseRewinder.clean_with :truncation, **options
164
235
  end
165
236
 
166
237
  test 'with only option' do
data/test/fake_app.rb CHANGED
@@ -10,37 +10,54 @@ module DatabaseRewinderTestApp
10
10
 
11
11
  config.eager_load = false
12
12
  config.active_support.deprecation = :log
13
+
14
+ rake_tasks do
15
+ load 'active_record/railties/databases.rake'
16
+ end
13
17
  end.initialize!
14
18
  end
15
19
 
16
- load 'active_record/railties/databases.rake'
17
-
18
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
+
19
36
  ActiveRecord::Tasks::DatabaseTasks.root ||= Rails.root
20
- ActiveRecord::Tasks::DatabaseTasks.drop_current ENV['DB']
21
- ActiveRecord::Tasks::DatabaseTasks.drop_current "#{ENV['DB']}_2"
22
- ActiveRecord::Tasks::DatabaseTasks.create_current ENV['DB']
23
- ActiveRecord::Tasks::DatabaseTasks.create_current "#{ENV['DB']}_2"
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'
24
41
 
25
42
  # models
26
43
  class Foo < ActiveRecord::Base; end
27
44
  class Bar < ActiveRecord::Base; end
28
45
  class Baz < ActiveRecord::Base; end
29
46
  class Quu < ActiveRecord::Base
30
- establish_connection "#{ENV['DB']}_2".to_sym
47
+ establish_connection :test2
31
48
  end
32
49
 
33
50
  # migrations
34
51
  class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
35
52
  def self.up
36
- ActiveRecord::Base.establish_connection ENV['DB'].to_sym
37
- create_table(:bars) {|t| t.string :name }
53
+ ActiveRecord::Base.establish_connection :test
54
+ create_table(:bars) {|t| t.string :name; t.index :name, unique: true }
38
55
  create_table(:foos) {|t| t.string :name; t.references :bar, foreign_key: true }
39
56
  create_table(:bazs) {|t| t.string :name }
40
57
 
41
- test2_connection = ActiveRecord::Base.establish_connection("#{ENV['DB']}_2".to_sym).connection
58
+ test2_connection = ActiveRecord::Base.establish_connection(:test2).connection
42
59
  test2_connection.create_table(:quus) {|t| t.string :name }
43
- ActiveRecord::Base.establish_connection ENV['DB'].to_sym
60
+ ActiveRecord::Base.establish_connection :test
44
61
  end
45
62
 
46
63
  def self.down
@@ -48,8 +65,8 @@ class CreateAllTables < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migrat
48
65
  drop_table(:bars) {|t| t.string :name }
49
66
  drop_table(:bazs) {|t| t.string :name }
50
67
 
51
- test2_connection = ActiveRecord::Base.establish_connection("#{ENV['DB']}_2".to_sym).connection
68
+ test2_connection = ActiveRecord::Base.establish_connection(:test2).connection
52
69
  test2_connection.drop_table :quus
53
- ActiveRecord::Base.establish_connection ENV['DB'].to_sym
70
+ ActiveRecord::Base.establish_connection :test
54
71
  end
55
72
  end
data/test/test_helper.rb CHANGED
@@ -10,6 +10,10 @@ require 'active_record'
10
10
  require 'database_rewinder'
11
11
  require 'fake_app'
12
12
  require 'test/unit/rails/test_help'
13
+ begin
14
+ require 'selenium/webdriver' # rails 6
15
+ rescue LoadError
16
+ end
13
17
 
14
18
  migrated = ActiveRecord::Base.connection.respond_to?(:data_source_exists?) ? ActiveRecord::Base.connection.data_source_exists?('foos') : ActiveRecord::Base.connection.table_exists?('foos')
15
19
  CreateAllTables.up unless migrated
metadata CHANGED
@@ -1,31 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_rewinder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Matsuda
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-18 00:00:00.000000000 Z
11
+ date: 2022-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.3'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.3'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
17
  - - ">="
@@ -39,35 +25,7 @@ dependencies:
39
25
  - !ruby/object:Gem::Version
40
26
  version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
- name: test-unit-rails
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: rails
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: sqlite3
28
+ name: rake
71
29
  requirement: !ruby/object:Gem::Requirement
72
30
  requirements:
73
31
  - - ">="
@@ -81,7 +39,7 @@ dependencies:
81
39
  - !ruby/object:Gem::Version
82
40
  version: '0'
83
41
  - !ruby/object:Gem::Dependency
84
- name: mysql2
42
+ name: test-unit-rails
85
43
  requirement: !ruby/object:Gem::Requirement
86
44
  requirements:
87
45
  - - ">="
@@ -95,7 +53,7 @@ dependencies:
95
53
  - !ruby/object:Gem::Version
96
54
  version: '0'
97
55
  - !ruby/object:Gem::Dependency
98
- name: pg
56
+ name: rails
99
57
  requirement: !ruby/object:Gem::Requirement
100
58
  requirements:
101
59
  - - ">="
@@ -108,27 +66,20 @@ dependencies:
108
66
  - - ">="
109
67
  - !ruby/object:Gem::Version
110
68
  version: '0'
111
- description: A minimalist's tiny and ultra-fast database cleaner
69
+ description: A minimalist's tiny and ultra-fast database cleaner for Active Record
112
70
  email:
113
71
  - ronnie@dio.jp
114
72
  executables: []
115
73
  extensions: []
116
74
  extra_rdoc_files: []
117
75
  files:
76
+ - ".github/workflows/main.yml"
118
77
  - ".gitignore"
119
- - ".travis.yml"
120
78
  - Gemfile
121
79
  - MIT_LICENSE
122
80
  - README.md
123
81
  - Rakefile
124
82
  - 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_edge.gemfile
132
83
  - lib/database_rewinder.rb
133
84
  - lib/database_rewinder/active_record_monkey.rb
134
85
  - lib/database_rewinder/cleaner.rb
@@ -147,7 +98,7 @@ homepage: https://github.com/amatsuda/database_rewinder
147
98
  licenses:
148
99
  - MIT
149
100
  metadata: {}
150
- post_install_message:
101
+ post_install_message:
151
102
  rdoc_options: []
152
103
  require_paths:
153
104
  - lib
@@ -162,8 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
113
  - !ruby/object:Gem::Version
163
114
  version: '0'
164
115
  requirements: []
165
- rubygems_version: 3.0.0.beta3
166
- signing_key:
116
+ rubygems_version: 3.4.0.dev
117
+ signing_key:
167
118
  specification_version: 4
168
119
  summary: A minimalist's tiny and ultra-fast database cleaner
169
120
  test_files:
data/.travis.yml DELETED
@@ -1,47 +0,0 @@
1
- language: ruby
2
-
3
- before_install:
4
- - gem update --system
5
- - gem install bundler
6
-
7
- cache: bundler
8
-
9
- env:
10
- - DB=sqlite3
11
- - DB=mysql
12
- - DB=postgresql
13
-
14
- rvm:
15
- - 2.5.1
16
- - 2.4.4
17
- - 2.3.7
18
- - 2.2.10
19
-
20
- gemfile:
21
- - gemfiles/rails_52.gemfile
22
- - gemfiles/rails_51.gemfile
23
- - gemfiles/rails_50.gemfile
24
- - gemfiles/rails_42.gemfile
25
-
26
- sudo: false
27
-
28
- matrix:
29
- include:
30
- - rvm: 2.5.1
31
- gemfile: gemfiles/rails_edge.gemfile
32
- env: DB=sqlite3
33
- - rvm: 2.3.7
34
- gemfile: gemfiles/rails_41.gemfile
35
- env: DB=sqlite3
36
- - rvm: 2.3.7
37
- gemfile: gemfiles/rails_40.gemfile
38
- env: DB=sqlite3
39
- - rvm: 2.1.10
40
- gemfile: gemfiles/rails_42.gemfile
41
- env: DB=sqlite3
42
- - rvm: 2.0.0
43
- gemfile: gemfiles/rails_42.gemfile
44
- env: DB=sqlite3
45
-
46
- allow_failures:
47
- - gemfile: gemfiles/rails_edge.gemfile
@@ -1,8 +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 'nokogiri', RUBY_VERSION < '2.1' ? '~> 1.6.0' : '>= 1.7'
7
-
8
- gemspec path: '../'
@@ -1,8 +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 'nokogiri', RUBY_VERSION < '2.1' ? '~> 1.6.0' : '>= 1.7'
7
-
8
- gemspec path: '../'
@@ -1,8 +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 'nokogiri', RUBY_VERSION < '2.1' ? '~> 1.6.0' : '>= 1.7'
7
-
8
- gemspec path: '../'
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rails', '~> 5.0.0'
4
- gem 'pg', '~> 0.21'
5
-
6
- 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'
4
- gem 'pg', '~> 0.21'
5
-
6
- gemspec path: '../'
@@ -1,9 +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'
8
-
9
- gemspec path: '../'