database_rewinder 0.6.5 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -1
- data/Gemfile +1 -0
- data/README.md +1 -1
- data/Rakefile +1 -0
- data/database_rewinder.gemspec +4 -1
- data/gemfiles/rails_40.gemfile +1 -0
- data/gemfiles/rails_41.gemfile +1 -0
- data/lib/database_rewinder.rb +1 -0
- data/lib/database_rewinder/active_record_monkey.rb +1 -0
- data/lib/database_rewinder/cleaner.rb +11 -2
- data/lib/database_rewinder/compatibility.rb +1 -0
- data/lib/database_rewinder/dummy_model.rb +1 -0
- data/lib/database_rewinder/multiple_statements_executor.rb +45 -0
- data/lib/database_rewinder/railtie.rb +1 -0
- data/test/active_record_monkey_test.rb +2 -1
- data/test/cleaner_test.rb +1 -0
- data/test/config/database.yml +38 -4
- data/test/database_rewinder_test.rb +43 -24
- data/test/fake_app.rb +15 -5
- data/test/test_helper.rb +1 -0
- metadata +31 -3
- data/gemfiles/rails_32.gemfile +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a320ff26da5261aed00fc4f589dfd7dd5abe1b1c
|
4
|
+
data.tar.gz: c9eb8839e2e6e55f4aa29b25cce186d87c85f9b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c4e5a25f62290d2d81371992dcb24a9ce53618aad34e6422de624d3546b66a147d51d99773bab834fe1f0fa8aa97f622e3246147b4cba920069dc1bce8eb2ed
|
7
|
+
data.tar.gz: b593b6f822542fd18404308b5bbaac2489063b2038b5f43d039298b81be7451fbf654663bfe32d1721a226fd606b6e4851b4b6298e429053fe4f36941513cc3e
|
data/.travis.yml
CHANGED
@@ -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
data/README.md
CHANGED
data/Rakefile
CHANGED
data/database_rewinder.gemspec
CHANGED
@@ -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.
|
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
|
data/gemfiles/rails_40.gemfile
CHANGED
data/gemfiles/rails_41.gemfile
CHANGED
data/lib/database_rewinder.rb
CHANGED
@@ -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
|
-
|
49
|
-
ar_conn.
|
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
|
@@ -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
|
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 == '
|
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
|
data/test/cleaner_test.rb
CHANGED
data/test/config/database.yml
CHANGED
@@ -1,11 +1,45 @@
|
|
1
|
-
|
1
|
+
sqlite3: &sqlite3
|
2
2
|
adapter: sqlite3
|
3
|
-
database:
|
3
|
+
database: database_rewinder_test.sqlite3
|
4
4
|
pool: 5
|
5
5
|
timeout: 5000
|
6
6
|
|
7
|
-
|
7
|
+
sqlite3_2: &sqlite3_2
|
8
8
|
adapter: sqlite3
|
9
|
-
database:
|
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
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
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.
|
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
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
data/test/fake_app.rb
CHANGED
@@ -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
|
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(
|
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
|
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(
|
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
|
51
|
+
ActiveRecord::Base.establish_connection ENV['DB'].to_sym
|
42
52
|
end
|
43
53
|
end
|
data/test/test_helper.rb
CHANGED
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.
|
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-
|
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
|