database_rewinder 0.6.5 → 0.7.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
  SHA1:
3
- metadata.gz: 95027bd40b3b8d879effcf6483f2c5406f2e9e25
4
- data.tar.gz: 08aefc2ce25711d7fb0839de87622804e8e3fbc9
3
+ metadata.gz: a320ff26da5261aed00fc4f589dfd7dd5abe1b1c
4
+ data.tar.gz: c9eb8839e2e6e55f4aa29b25cce186d87c85f9b0
5
5
  SHA512:
6
- metadata.gz: f8b1ea85af1d3314b99dbd57574553ac7c315ec60af4663581c876611f8a7a3bc7f9051f5664bb7ed6f42c8827b6f5cc682f893afdb4edd84d18a21848ee62c3
7
- data.tar.gz: c0aaa34d77f91e33b23d96b40b42b54528d1313502958e9fc7b31bc3e7b93c76e30540f584ac4f1594059627a8b08e94fcac004653d88e661ab83592b6ae2270
6
+ metadata.gz: 6c4e5a25f62290d2d81371992dcb24a9ce53618aad34e6422de624d3546b66a147d51d99773bab834fe1f0fa8aa97f622e3246147b4cba920069dc1bce8eb2ed
7
+ data.tar.gz: b593b6f822542fd18404308b5bbaac2489063b2038b5f43d039298b81be7451fbf654663bfe32d1721a226fd606b6e4851b4b6298e429053fe4f36941513cc3e
@@ -1,5 +1,10 @@
1
1
  language: ruby
2
2
 
3
+ env:
4
+ - DB=sqlite3
5
+ - DB=mysql
6
+ - DB=postgresql
7
+
3
8
  rvm:
4
9
  - 2.0.0
5
10
  - 2.1.10
@@ -7,7 +12,6 @@ rvm:
7
12
  - 2.3.1
8
13
 
9
14
  gemfile:
10
- - gemfiles/rails_32.gemfile
11
15
  - gemfiles/rails_40.gemfile
12
16
  - gemfiles/rails_41.gemfile
13
17
  - gemfiles/rails_42.gemfile
data/Gemfile CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  source 'https://rubygems.org'
2
3
 
3
4
  # Specify your gem's dependencies in database_rewinder.gemspec
data/README.md CHANGED
@@ -22,7 +22,7 @@ This strategy was originally devised and implemented by Shingo Morita (@eudoxa)
22
22
 
23
23
  ## Supported versions
24
24
 
25
- * ActiveRecord 3.2, 4.0, 4.1, 4.2, 5.0
25
+ * ActiveRecord 4.0, 4.1, 4.2, 5.0
26
26
 
27
27
  * Ruby 2.0, 2.1, 2.2, 2.3
28
28
 
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'bundler'
2
3
  require 'bundler/setup'
3
4
  require "bundler/gem_tasks"
@@ -1,10 +1,11 @@
1
1
  # coding: utf-8
2
+ # frozen_string_literal: true
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
 
5
6
  Gem::Specification.new do |spec|
6
7
  spec.name = "database_rewinder"
7
- spec.version = '0.6.5'
8
+ spec.version = '0.7.0'
8
9
  spec.authors = ["Akira Matsuda"]
9
10
  spec.email = ["ronnie@dio.jp"]
10
11
  spec.description = "A minimalist's tiny and ultra-fast database cleaner"
@@ -22,4 +23,6 @@ Gem::Specification.new do |spec|
22
23
  spec.add_development_dependency 'test-unit-rails'
23
24
  spec.add_development_dependency 'rails'
24
25
  spec.add_development_dependency 'sqlite3'
26
+ spec.add_development_dependency 'mysql2'
27
+ spec.add_development_dependency 'pg'
25
28
  end
@@ -1,5 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gem 'rails', '~> 4.0.0'
4
+ gem 'mysql2', '~> 0.3.10'
4
5
 
5
6
  gemspec path: '../'
@@ -1,5 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gem 'rails', '~> 4.1.0'
4
+ gem 'mysql2', '~> 0.3.13'
4
5
 
5
6
  gemspec path: '../'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require_relative 'database_rewinder/cleaner'
2
3
 
3
4
  module DatabaseRewinder
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module DatabaseRewinder
2
3
  module InsertRecorder
3
4
  def execute(sql, *)
@@ -1,3 +1,8 @@
1
+ # frozen_string_literal: true
2
+ require_relative 'multiple_statements_executor'
3
+
4
+ using DatabaseRewinder::MultipleStatementsExecutor
5
+
1
6
  module DatabaseRewinder
2
7
  class Cleaner
3
8
  attr_accessor :config, :connection_name, :only, :except, :inserted_tables, :pool
@@ -45,8 +50,12 @@ module DatabaseRewinder
45
50
  return if tables.empty?
46
51
 
47
52
  ar_conn.disable_referential_integrity do
48
- tables.each do |table_name|
49
- ar_conn.execute "DELETE FROM #{ar_conn.quote_table_name(table_name)};"
53
+ if ar_conn.supports_multiple_statements?
54
+ ar_conn.execute_multiple tables.map {|t| "DELETE FROM #{ar_conn.quote_table_name(t)}"}.join(';')
55
+ else
56
+ tables.each do |table_name|
57
+ ar_conn.execute "DELETE FROM #{ar_conn.quote_table_name(table_name)};"
58
+ end
50
59
  end
51
60
  end
52
61
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module DatabaseRewinder
2
3
  module Compatibility
3
4
  def clean_with(*args)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module DatabaseRewinder
2
3
  class DummyModel < ActiveRecord::Base
3
4
  class << self
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+ module DatabaseRewinder
3
+ module MultipleStatementsExecutor
4
+ refine ActiveRecord::ConnectionAdapters::AbstractAdapter do
5
+ def supports_multiple_statements?
6
+ #TODO Use ADAPTER_NAME when we've dropped AR 4.1 support
7
+ %w(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter ActiveRecord::ConnectionAdapters::Mysql2Adapter ActiveRecord::ConnectionAdapters::SQLite3Adapter).include? self.class.name
8
+ end
9
+
10
+ def execute_multiple(sql)
11
+ #TODO Use ADAPTER_NAME when we've dropped AR 4.1 support
12
+ case self.class.name
13
+ when 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
14
+ log(sql) { @connection.exec sql }
15
+ when 'ActiveRecord::ConnectionAdapters::Mysql2Adapter'
16
+ query_options = @connection.query_options.dup
17
+ if query_options[:connect_flags] & Mysql2::Client::MULTI_STATEMENTS != 0
18
+ result = log(sql) { @connection.query sql }
19
+ while @connection.next_result
20
+ # just to make sure that all queries are finished
21
+ result = @connection.store_result
22
+ end
23
+ else
24
+ query_options[:connect_flags] |= Mysql2::Client::MULTI_STATEMENTS
25
+ # opens another connection to the DB
26
+ client = Mysql2::Client.new query_options
27
+ begin
28
+ result = log(sql) { client.query sql }
29
+ while client.next_result
30
+ # just to make sure that all queries are finished
31
+ result = client.store_result
32
+ end
33
+ ensure
34
+ client.close
35
+ end
36
+ end
37
+ when 'ActiveRecord::ConnectionAdapters::SQLite3Adapter'
38
+ log(sql) { @connection.execute_batch sql }
39
+ else
40
+ execute sql
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module DatabaseRewinder
2
3
  class Railtie < ::Rails::Railtie
3
4
  initializer 'database_rewinder', after: 'active_record.initialize_database' do
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'test_helper'
2
3
 
3
4
  class DatabaseRewinder::InsertRecorderTest < ActiveSupport::TestCase
@@ -9,7 +10,7 @@ class DatabaseRewinder::InsertRecorderTest < ActiveSupport::TestCase
9
10
  end
10
11
 
11
12
  test '#execute' do
12
- cleaner = DatabaseRewinder.instance_variable_get(:'@cleaners').detect {|c| c.db == 'test.sqlite3'}
13
+ cleaner = DatabaseRewinder.instance_variable_get(:'@cleaners').detect {|c| c.db == (ENV['DB'] == 'sqlite3' ? 'database_rewinder_test.sqlite3' : 'database_rewinder_test')}
13
14
 
14
15
  assert_equal %w(foos bars), cleaner.inserted_tables
15
16
  assert_not_nil cleaner.pool
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'test_helper'
2
3
 
3
4
  class DatabaseRewinder::CleanerTest < ActiveSupport::TestCase
@@ -1,11 +1,45 @@
1
- test:
1
+ sqlite3: &sqlite3
2
2
  adapter: sqlite3
3
- database: test.sqlite3
3
+ database: database_rewinder_test.sqlite3
4
4
  pool: 5
5
5
  timeout: 5000
6
6
 
7
- test2:
7
+ sqlite3_2: &sqlite3_2
8
8
  adapter: sqlite3
9
- database: test2.sqlite3
9
+ database: database_rewinder_test2.sqlite3
10
10
  pool: 5
11
11
  timeout: 5000
12
+
13
+ mysql: &mysql
14
+ adapter: mysql2
15
+ host: localhost
16
+ username: root
17
+ password:
18
+ database: database_rewinder_test
19
+
20
+ mysql_2: &mysql_2
21
+ adapter: mysql2
22
+ host: localhost
23
+ username: root
24
+ password:
25
+ database: database_rewinder_test2
26
+
27
+ postgresql: &postgresql
28
+ adapter: postgresql
29
+ host: localhost
30
+ username: postgres
31
+ password:
32
+ database: database_rewinder_test
33
+
34
+ postgresql_2: &postgresql_2
35
+ adapter: postgresql
36
+ host: localhost
37
+ username: postgres
38
+ password:
39
+ database: database_rewinder_test2
40
+
41
+ test:
42
+ <<: *<%= ENV['DB'] %>
43
+
44
+ test2:
45
+ <<: *<%= "#{ENV['DB']}_2" %>
@@ -1,6 +1,13 @@
1
+ # frozen_string_literal: true
1
2
  require 'test_helper'
2
3
 
3
4
  class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
5
+ if ActiveRecord::VERSION::STRING >= '5'
6
+ self.use_transactional_tests = false
7
+ else
8
+ self.use_transactional_fixtures = false
9
+ end
10
+
4
11
  setup do
5
12
  DatabaseRewinder.init
6
13
  end
@@ -10,22 +17,31 @@ class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
10
17
  DatabaseRewinder.database_configuration = nil
11
18
  end
12
19
  sub_test_case 'for connecting to an arbitrary database' do
20
+ def assert_cleaners_added(cleaner_names)
21
+ connection_names = DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name}
22
+ yield
23
+ assert_equal cleaner_names, DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name} - connection_names
24
+ end
25
+
13
26
  test 'simply giving a connection name only' do
14
- DatabaseRewinder.database_configuration = {'aaa' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
15
- DatabaseRewinder['aaa']
16
- assert_equal ['aaa'], DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name}
27
+ assert_cleaners_added ['aaa'] do
28
+ DatabaseRewinder.database_configuration = {'aaa' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
29
+ DatabaseRewinder['aaa']
30
+ end
17
31
  end
18
32
 
19
33
  test 'giving a connection name via Hash with :connection key' do
20
- DatabaseRewinder.database_configuration = {'bbb' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
21
- DatabaseRewinder[connection: 'bbb']
22
- assert_equal ['bbb'], DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name}
34
+ assert_cleaners_added ['bbb'] do
35
+ DatabaseRewinder.database_configuration = {'bbb' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
36
+ DatabaseRewinder[connection: 'bbb']
37
+ end
23
38
  end
24
39
 
25
40
  test 'the Cleaner compatible syntax' do
26
- DatabaseRewinder.database_configuration = {'ccc' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
27
- DatabaseRewinder[:aho, connection: 'ccc']
28
- assert_equal ['ccc'], DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name}
41
+ assert_cleaners_added ['ccc'] do
42
+ DatabaseRewinder.database_configuration = {'ccc' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
43
+ DatabaseRewinder[:aho, connection: 'ccc']
44
+ end
29
45
  end
30
46
  end
31
47
 
@@ -45,11 +61,14 @@ class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
45
61
  end
46
62
 
47
63
  sub_test_case '.record_inserted_table' do
64
+ setup do
65
+ DatabaseRewinder.cleaners
66
+ end
67
+
48
68
  def perform_insert(sql)
49
- DatabaseRewinder.database_configuration = {'foo' => {'adapter' => 'sqlite3', 'database' => 'test_record_inserted_table.sqlite3'}}
50
- @cleaner = DatabaseRewinder.create_cleaner 'foo'
69
+ @cleaner = DatabaseRewinder.instance_variable_get(:'@cleaners').detect {|c| c.db == (ENV['DB'] == 'sqlite3' ? 'database_rewinder_test.sqlite3' : 'database_rewinder_test')}
51
70
 
52
- connection = ::ActiveRecord::Base.sqlite3_connection(adapter: "sqlite3", database: File.expand_path('test_record_inserted_table.sqlite3', Rails.root))
71
+ connection = ::ActiveRecord::Base.connection
53
72
  DatabaseRewinder.record_inserted_table(connection, sql)
54
73
  end
55
74
  teardown do
@@ -103,18 +122,18 @@ class DatabaseRewinder::DatabaseRewinderTest < ActiveSupport::TestCase
103
122
  end
104
123
 
105
124
  if ActiveRecord::VERSION::STRING >= '4'
106
- test '.clean_all should not touch AR::SchemaMigration' do
107
- begin
108
- ActiveRecord::SchemaMigration.create_table
109
- ActiveRecord::SchemaMigration.create! version: '001'
110
- Foo.create! name: 'foo1'
111
- DatabaseRewinder.clean_all
112
-
113
- assert_equal 0, Foo.count
114
- assert_equal 1, ActiveRecord::SchemaMigration.count
115
-
116
- ensure
117
- ActiveRecord::SchemaMigration.drop_table
125
+ sub_test_case 'migrations' do
126
+ test '.clean_all should not touch AR::SchemaMigration' do
127
+ begin
128
+ ActiveRecord::Base.connection.initialize_schema_migrations_table
129
+ ActiveRecord::SchemaMigration.create! version: '001'
130
+ DatabaseRewinder.clean_all
131
+
132
+ assert_equal 0, Foo.count
133
+ assert_equal 1, ActiveRecord::SchemaMigration.count
134
+ ensure
135
+ ActiveRecord::SchemaMigration.drop_table
136
+ end
118
137
  end
119
138
  end
120
139
  end
@@ -1,4 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ ENV['DB'] ||= 'sqlite3'
1
4
  require 'active_record/railtie'
5
+ load 'active_record/railties/databases.rake'
2
6
 
3
7
  module DatabaseRewinderTestApp
4
8
  Application = Class.new(Rails::Application) do
@@ -10,25 +14,31 @@ module DatabaseRewinderTestApp
10
14
  end.initialize!
11
15
  end
12
16
 
17
+ require 'active_record/base'
18
+ ActiveRecord::Tasks::DatabaseTasks.drop_current ENV['DB']
19
+ ActiveRecord::Tasks::DatabaseTasks.drop_current "#{ENV['DB']}_2"
20
+ ActiveRecord::Tasks::DatabaseTasks.create_current ENV['DB']
21
+ ActiveRecord::Tasks::DatabaseTasks.create_current "#{ENV['DB']}_2"
13
22
 
14
23
  # models
15
24
  class Foo < ActiveRecord::Base; end
16
25
  class Bar < ActiveRecord::Base; end
17
26
  class Baz < ActiveRecord::Base; end
18
27
  class Quu < ActiveRecord::Base
19
- establish_connection :test2
28
+ establish_connection "#{ENV['DB']}_2".to_sym
20
29
  end
21
30
 
22
31
  # migrations
23
32
  class CreateAllTables < ActiveRecord::Migration
24
33
  def self.up
34
+ ActiveRecord::Base.establish_connection ENV['DB'].to_sym
25
35
  create_table(:foos) {|t| t.string :name }
26
36
  create_table(:bars) {|t| t.string :name }
27
37
  create_table(:bazs) {|t| t.string :name }
28
38
 
29
- test2_connection = ActiveRecord::Base.establish_connection(:test2).connection
39
+ test2_connection = ActiveRecord::Base.establish_connection("#{ENV['DB']}_2".to_sym).connection
30
40
  test2_connection.create_table(:quus) {|t| t.string :name }
31
- ActiveRecord::Base.establish_connection :test
41
+ ActiveRecord::Base.establish_connection ENV['DB'].to_sym
32
42
  end
33
43
 
34
44
  def self.down
@@ -36,8 +46,8 @@ class CreateAllTables < ActiveRecord::Migration
36
46
  drop_table(:bars) {|t| t.string :name }
37
47
  drop_table(:bazs) {|t| t.string :name }
38
48
 
39
- test2_connection = ActiveRecord::Base.establish_connection(:test2).connection
49
+ test2_connection = ActiveRecord::Base.establish_connection("#{ENV['DB']}_2".to_sym).connection
40
50
  test2_connection.drop_table :quus
41
- ActiveRecord::Base.establish_connection :test
51
+ ActiveRecord::Base.establish_connection ENV['DB'].to_sym
42
52
  end
43
53
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  ENV['RAILS_ENV'] ||= 'test'
2
3
 
3
4
  $LOAD_PATH.unshift(File.join(__dir__, '..', 'lib'))
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.6.5
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Matsuda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-13 00:00:00.000000000 Z
11
+ date: 2016-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
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'
83
111
  description: A minimalist's tiny and ultra-fast database cleaner
84
112
  email:
85
113
  - ronnie@dio.jp
@@ -94,7 +122,6 @@ files:
94
122
  - README.md
95
123
  - Rakefile
96
124
  - database_rewinder.gemspec
97
- - gemfiles/rails_32.gemfile
98
125
  - gemfiles/rails_40.gemfile
99
126
  - gemfiles/rails_41.gemfile
100
127
  - gemfiles/rails_42.gemfile
@@ -105,6 +132,7 @@ files:
105
132
  - lib/database_rewinder/cleaner.rb
106
133
  - lib/database_rewinder/compatibility.rb
107
134
  - lib/database_rewinder/dummy_model.rb
135
+ - lib/database_rewinder/multiple_statements_executor.rb
108
136
  - lib/database_rewinder/railtie.rb
109
137
  - test/active_record_monkey_test.rb
110
138
  - test/cleaner_test.rb
@@ -1,6 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'rails', '~> 3.2.0'
4
- gem 'test-unit-rails', '1.0.2'
5
-
6
- gemspec path: '../'