database_rewinder 0.2.0 → 0.3.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 +4 -4
- data/.travis.yml +4 -0
- data/README.md +3 -1
- data/database_rewinder.gemspec +2 -2
- data/lib/database_rewinder.rb +21 -22
- data/lib/database_rewinder/cleaner.rb +3 -14
- data/lib/database_rewinder/compatibility.rb +38 -0
- data/lib/database_rewinder/railtie.rb +0 -2
- data/spec/cleaner_spec.rb +14 -23
- data/spec/database_rewinder_spec.rb +64 -25
- data/spec/spec_helper.rb +3 -0
- metadata +9 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fffba2eb153141b4eba049ab2967097718de4e8
|
4
|
+
data.tar.gz: aa36ca325c1596c835ec595306412dd5c0f68267
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56db3ef0a01ee81a6c6d2e8dcbcfcd87172fd71eaa1c01df0b1492a2fe5266affc0ba8f446fc7c8d589d2fab515af8dc5ebd74aaa7763d82b55d6de597258a75
|
7
|
+
data.tar.gz: e54db71f5bcb3903bcdc4e9bf949be2cead0230df19eba62f15285b07302b331b8a4662b8ea9abc02f049646585ace7b8a2230ea66ff22f3bb4294e819c0665b
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# DatabaseRewinder
|
2
2
|
|
3
|
+
[](http://travis-ci.org/amatsuda/database\_rewinder)
|
4
|
+
|
3
5
|
database\_rewinder is a minimalist's tiny and ultra-fast database cleaner.
|
4
6
|
|
5
7
|
## Features
|
@@ -16,7 +18,7 @@ So, the more number of tables you have in your database, the more benefit you wi
|
|
16
18
|
|
17
19
|
### Credit
|
18
20
|
|
19
|
-
This strategy was originally devised and implemented by @eudoxa.
|
21
|
+
This strategy was originally devised and implemented by Shingo Morita (@eudoxa) at COOKPAD Inc.
|
20
22
|
|
21
23
|
## Supported versions
|
22
24
|
|
data/database_rewinder.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "database_rewinder"
|
7
|
-
spec.version = '0.
|
7
|
+
spec.version = '0.3.0'
|
8
8
|
spec.authors = ["Akira Matsuda"]
|
9
9
|
spec.email = ["ronnie@dio.jp"]
|
10
10
|
spec.description = "A minimalist's tiny and ultra-fast database cleaner"
|
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.add_development_dependency "bundler", "~> 1.3"
|
21
21
|
spec.add_development_dependency "rake"
|
22
|
-
spec.add_development_dependency 'rspec'
|
22
|
+
spec.add_development_dependency 'rspec', '< 2.99'
|
23
23
|
spec.add_development_dependency 'rails'
|
24
24
|
spec.add_development_dependency 'sqlite3'
|
25
25
|
end
|
data/lib/database_rewinder.rb
CHANGED
@@ -1,27 +1,28 @@
|
|
1
1
|
require_relative 'database_rewinder/cleaner'
|
2
|
-
require_relative 'database_rewinder/railtie'
|
3
2
|
|
4
3
|
module DatabaseRewinder
|
5
4
|
VERSION = Gem.loaded_specs['database_rewinder'].version.to_s
|
6
5
|
|
7
6
|
class << self
|
7
|
+
# Set your DB configuration here if you'd like to use something else than the AR configuration
|
8
|
+
attr_writer :database_configuration
|
9
|
+
|
8
10
|
def init
|
9
|
-
@cleaners, @table_names_cache, @clean_all, @only, @except = [], {}, false
|
10
|
-
|
11
|
+
@cleaners, @table_names_cache, @clean_all, @only, @except, @database_configuration = [], {}, false
|
12
|
+
end
|
13
|
+
|
14
|
+
def database_configuration
|
15
|
+
@database_configuration || ActiveRecord::Base.configurations
|
11
16
|
end
|
12
17
|
|
13
18
|
def create_cleaner(connection_name)
|
14
|
-
config =
|
19
|
+
config = database_configuration[connection_name] or raise %Q[Database configuration named "#{connection_name}" is not configured.]
|
15
20
|
|
16
21
|
Cleaner.new(db: config['database'], connection_name: connection_name, only: @only, except: @except).tap {|c| @cleaners << c}
|
17
22
|
end
|
18
23
|
|
19
24
|
def [](_orm, connection: nil, **)
|
20
|
-
|
21
|
-
return cl
|
22
|
-
end
|
23
|
-
|
24
|
-
create_cleaner connection
|
25
|
+
@cleaners.detect {|c| c.connection_name == connection} || create_cleaner(connection)
|
25
26
|
end
|
26
27
|
|
27
28
|
def all=(v)
|
@@ -36,9 +37,11 @@ module DatabaseRewinder
|
|
36
37
|
def record_inserted_table(connection, sql)
|
37
38
|
config = connection.instance_variable_get(:'@config')
|
38
39
|
database = config[:database]
|
40
|
+
#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...
|
41
|
+
root_dir = defined?(Rails) ? Rails.root : Dir.pwd
|
39
42
|
cleaner = cleaners.detect do |c|
|
40
43
|
if (config[:adapter] == 'sqlite3') && (config[:database] != ':memory:')
|
41
|
-
File.expand_path(c.db,
|
44
|
+
File.expand_path(c.db, root_dir) == File.expand_path(database, root_dir)
|
42
45
|
else
|
43
46
|
c.db == database
|
44
47
|
end
|
@@ -64,18 +67,6 @@ module DatabaseRewinder
|
|
64
67
|
cleaners.each {|c| c.clean_all}
|
65
68
|
end
|
66
69
|
|
67
|
-
# for database_cleaner compat
|
68
|
-
def clean_with(*args)
|
69
|
-
cleaners.each {|c| c.clean_with *args}
|
70
|
-
end
|
71
|
-
|
72
|
-
# for database_cleaner compat
|
73
|
-
def start; end
|
74
|
-
def strategy=(args)
|
75
|
-
options = args.is_a?(Array) ? args.extract_options! : {}
|
76
|
-
@only, @except = options[:only], options[:except]
|
77
|
-
end
|
78
|
-
|
79
70
|
# cache AR connection.tables
|
80
71
|
def all_table_names(connection)
|
81
72
|
db = connection.instance_variable_get(:'@config')[:database]
|
@@ -83,3 +74,11 @@ module DatabaseRewinder
|
|
83
74
|
end
|
84
75
|
end
|
85
76
|
end
|
77
|
+
|
78
|
+
begin
|
79
|
+
require 'rails'
|
80
|
+
require_relative 'database_rewinder/railtie'
|
81
|
+
rescue LoadError
|
82
|
+
DatabaseRewinder.init
|
83
|
+
require_relative 'database_rewinder/active_record_monkey'
|
84
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module DatabaseRewinder
|
2
2
|
class Cleaner
|
3
|
-
attr_accessor :db, :connection_name, :inserted_tables, :pool
|
3
|
+
attr_accessor :db, :connection_name, :only, :except, :inserted_tables, :pool
|
4
4
|
|
5
5
|
def initialize(db: nil, connection_name: nil, only: nil, except: nil)
|
6
6
|
@db, @connection_name, @only, @except = db, connection_name, Array(only), Array(except)
|
@@ -27,19 +27,6 @@ module DatabaseRewinder
|
|
27
27
|
reset
|
28
28
|
end
|
29
29
|
|
30
|
-
def clean_with(_strategy, only: nil, except: nil, **)
|
31
|
-
@only += Array(only) unless only.blank?
|
32
|
-
@except += Array(except) unless except.blank?
|
33
|
-
clean_all
|
34
|
-
end
|
35
|
-
|
36
|
-
# for database_cleaner compat
|
37
|
-
def strategy=(args)
|
38
|
-
options = args.is_a?(Array) ? args.extract_options! : {}
|
39
|
-
@only += Array(options[:only]) unless options[:only].blank?
|
40
|
-
@except += Array(options[:except]) unless options[:except].blank?
|
41
|
-
end
|
42
|
-
|
43
30
|
private
|
44
31
|
def delete_all(ar_conn, tables)
|
45
32
|
tables = tables & @only if @only.any?
|
@@ -66,3 +53,5 @@ module DatabaseRewinder
|
|
66
53
|
end
|
67
54
|
end
|
68
55
|
end
|
56
|
+
|
57
|
+
require_relative 'compatibility'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module DatabaseRewinder
|
2
|
+
module Compatibility
|
3
|
+
def clean_with(*args)
|
4
|
+
cleaners.each {|c| c.clean_with(*args)}
|
5
|
+
end
|
6
|
+
|
7
|
+
def start; end
|
8
|
+
|
9
|
+
def strategy=(args)
|
10
|
+
options = args.is_a?(Array) ? args.extract_options! : {}
|
11
|
+
@only, @except = options[:only], options[:except]
|
12
|
+
cleaners.each {|c| c.strategy = nil, options}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
class << self
|
16
|
+
include Compatibility
|
17
|
+
end
|
18
|
+
|
19
|
+
class Cleaner
|
20
|
+
module Compatibility
|
21
|
+
def clean_with(_strategy, only: nil, except: nil, **)
|
22
|
+
originals = @only, @except
|
23
|
+
self.only, self.except = Array(only), Array(except)
|
24
|
+
clean_all
|
25
|
+
ensure
|
26
|
+
self.only, self.except = originals
|
27
|
+
end
|
28
|
+
|
29
|
+
def strategy=(args)
|
30
|
+
options = args.is_a?(Array) ? args.extract_options! : {}
|
31
|
+
self.only = Array(options[:only]) if options.key?(:only)
|
32
|
+
self.except = Array(options[:except]) if options.key?(:except)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
include Compatibility
|
37
|
+
end
|
38
|
+
end
|
data/spec/cleaner_spec.rb
CHANGED
@@ -3,41 +3,32 @@ require 'spec_helper'
|
|
3
3
|
module DatabaseRewinder
|
4
4
|
describe Cleaner do
|
5
5
|
describe '#strategy=' do
|
6
|
-
before { @cleaner = described_class.new }
|
6
|
+
before { @cleaner = described_class.new(only: ['foos'], except: 'bars') }
|
7
7
|
|
8
|
-
context '
|
8
|
+
context 'without options' do
|
9
9
|
before { @cleaner.strategy = :truncation }
|
10
10
|
|
11
|
-
it 'should
|
12
|
-
expect(@cleaner.instance_variable_get(:@only)).to eq([])
|
13
|
-
expect(@cleaner.instance_variable_get(:@except)).to eq([])
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'when only option is given' do
|
18
|
-
before { @cleaner.strategy = :truncation, { only: ['foos'] } }
|
19
|
-
|
20
|
-
it 'should set only option' do
|
11
|
+
it 'should keep instance variables' do
|
21
12
|
expect(@cleaner.instance_variable_get(:@only)).to eq(['foos'])
|
22
|
-
expect(@cleaner.instance_variable_get(:@except)).to eq([])
|
13
|
+
expect(@cleaner.instance_variable_get(:@except)).to eq(['bars'])
|
23
14
|
end
|
24
15
|
end
|
25
16
|
|
26
|
-
context '
|
27
|
-
before { @cleaner.strategy = :truncation, {
|
17
|
+
context 'with options (an array or a string)' do
|
18
|
+
before { @cleaner.strategy = :truncation, { only: ['bars'], except: 'bazs' } }
|
28
19
|
|
29
|
-
it 'should
|
30
|
-
expect(@cleaner.instance_variable_get(:@only)).to eq([])
|
31
|
-
expect(@cleaner.instance_variable_get(:@except)).to eq(['
|
20
|
+
it 'should overwrite instance variables' do
|
21
|
+
expect(@cleaner.instance_variable_get(:@only)).to eq(['bars'])
|
22
|
+
expect(@cleaner.instance_variable_get(:@except)).to eq(['bazs'])
|
32
23
|
end
|
33
24
|
end
|
34
25
|
|
35
|
-
context '
|
36
|
-
before { @cleaner.strategy = :truncation, { only: [
|
26
|
+
context 'with options (an empty array or nil)' do
|
27
|
+
before { @cleaner.strategy = :truncation, { only: [], except: nil } }
|
37
28
|
|
38
|
-
it 'should
|
39
|
-
expect(@cleaner.instance_variable_get(:@only)).to eq([
|
40
|
-
expect(@cleaner.instance_variable_get(:@except)).to eq([
|
29
|
+
it 'should overwrite instance variables even if they are empty/nil' do
|
30
|
+
expect(@cleaner.instance_variable_get(:@only)).to eq([])
|
31
|
+
expect(@cleaner.instance_variable_get(:@except)).to eq([])
|
41
32
|
end
|
42
33
|
end
|
43
34
|
end
|
@@ -1,25 +1,33 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe DatabaseRewinder do
|
4
|
-
before
|
4
|
+
before do
|
5
|
+
DatabaseRewinder.init
|
6
|
+
end
|
5
7
|
|
6
8
|
describe '.[]' do
|
7
9
|
before do
|
8
|
-
DatabaseRewinder.
|
10
|
+
DatabaseRewinder.database_configuration = {'foo' => {'adapter' => 'sqlite3', 'database' => ':memory:'}}
|
9
11
|
DatabaseRewinder[:aho, connection: 'foo']
|
10
12
|
end
|
13
|
+
after do
|
14
|
+
DatabaseRewinder.database_configuration = nil
|
15
|
+
end
|
11
16
|
subject { DatabaseRewinder.instance_variable_get(:'@cleaners').map {|c| c.connection_name} }
|
12
17
|
it { should == ['foo'] }
|
13
18
|
end
|
14
19
|
|
15
20
|
describe '.record_inserted_table' do
|
16
21
|
before do
|
17
|
-
DatabaseRewinder.
|
22
|
+
DatabaseRewinder.database_configuration = {'foo' => {'adapter' => 'sqlite3', 'database' => 'db/test.sqlite3'}}
|
18
23
|
@cleaner = DatabaseRewinder.create_cleaner 'foo'
|
19
24
|
connection = double('connection').as_null_object
|
20
25
|
connection.instance_variable_set :'@config', {adapter: 'sqlite3', database: File.expand_path('db/test.sqlite3', Rails.root) }
|
21
26
|
DatabaseRewinder.record_inserted_table(connection, 'INSERT INTO "foos" ("name") VALUES (?)')
|
22
27
|
end
|
28
|
+
after do
|
29
|
+
DatabaseRewinder.database_configuration = nil
|
30
|
+
end
|
23
31
|
subject { @cleaner }
|
24
32
|
|
25
33
|
its(:inserted_tables) { should == ['foos'] }
|
@@ -51,36 +59,67 @@ describe DatabaseRewinder do
|
|
51
59
|
end
|
52
60
|
end
|
53
61
|
|
54
|
-
describe '.
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
62
|
+
describe '.clean_with' do
|
63
|
+
before do
|
64
|
+
@cleaner = DatabaseRewinder.cleaners.first
|
65
|
+
@only = @cleaner.instance_variable_get(:@only)
|
66
|
+
@except = @cleaner.instance_variable_get(:@except)
|
67
|
+
Foo.create! name: 'foo1'
|
68
|
+
Bar.create! name: 'bar1'
|
69
|
+
DatabaseRewinder.clean_with :truncation, options
|
61
70
|
end
|
62
71
|
|
63
|
-
context '
|
64
|
-
|
65
|
-
it 'should
|
66
|
-
|
67
|
-
|
72
|
+
context 'with only option' do
|
73
|
+
let(:options) { { only: ['foos'] } }
|
74
|
+
it 'should clean with only option and restore original one' do
|
75
|
+
Foo.count.should == 0
|
76
|
+
Bar.count.should == 1
|
77
|
+
expect(@cleaner.instance_variable_get(:@only)).to eq(@only)
|
68
78
|
end
|
69
79
|
end
|
70
80
|
|
71
|
-
context '
|
72
|
-
|
73
|
-
it 'should
|
74
|
-
|
75
|
-
|
81
|
+
context 'with except option' do
|
82
|
+
let(:options) { { except: ['bars'] } }
|
83
|
+
it 'should clean with except option and restore original one' do
|
84
|
+
Foo.count.should == 0
|
85
|
+
Bar.count.should == 1
|
86
|
+
expect(@cleaner.instance_variable_get(:@except)).to eq(@except)
|
76
87
|
end
|
77
88
|
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe '.strategy=' do
|
92
|
+
context 'call first with options' do
|
93
|
+
before do
|
94
|
+
DatabaseRewinder.strategy = :truncate, { only: ['foos'], except: ['bars'] }
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should set options' do
|
98
|
+
expect(DatabaseRewinder.instance_variable_get(:@only)).to eq(['foos'])
|
99
|
+
expect(DatabaseRewinder.instance_variable_get(:@except)).to eq(['bars'])
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'should create cleaner with options' do
|
103
|
+
cleaner = DatabaseRewinder.instance_variable_get(:@cleaners).first
|
104
|
+
expect(cleaner.instance_variable_get(:@only)).to eq(['foos'])
|
105
|
+
expect(cleaner.instance_variable_get(:@except)).to eq(['bars'])
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'call again with different options' do
|
109
|
+
before do
|
110
|
+
DatabaseRewinder.strategy = :truncate, { only: ['bazs'], except: [] }
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'should overwrite options' do
|
114
|
+
expect(DatabaseRewinder.instance_variable_get(:@only)).to eq(['bazs'])
|
115
|
+
expect(DatabaseRewinder.instance_variable_get(:@except)).to eq([])
|
116
|
+
end
|
78
117
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
118
|
+
it 'should overwrite cleaner with new options' do
|
119
|
+
cleaner = DatabaseRewinder.instance_variable_get(:@cleaners).first
|
120
|
+
expect(cleaner.instance_variable_get(:@only)).to eq(['bazs'])
|
121
|
+
expect(cleaner.instance_variable_get(:@except)).to eq([])
|
122
|
+
end
|
84
123
|
end
|
85
124
|
end
|
86
125
|
end
|
data/spec/spec_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.3.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: 2014-
|
11
|
+
date: 2014-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -42,16 +42,16 @@ dependencies:
|
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - <
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '2.99'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - <
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '2.99'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rails
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -88,6 +88,7 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- .gitignore
|
91
|
+
- .travis.yml
|
91
92
|
- Gemfile
|
92
93
|
- MIT_LICENSE
|
93
94
|
- README.md
|
@@ -96,6 +97,7 @@ files:
|
|
96
97
|
- lib/database_rewinder.rb
|
97
98
|
- lib/database_rewinder/active_record_monkey.rb
|
98
99
|
- lib/database_rewinder/cleaner.rb
|
100
|
+
- lib/database_rewinder/compatibility.rb
|
99
101
|
- lib/database_rewinder/railtie.rb
|
100
102
|
- spec/active_record_monkey_spec.rb
|
101
103
|
- spec/cleaner_spec.rb
|
@@ -123,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
125
|
version: '0'
|
124
126
|
requirements: []
|
125
127
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.
|
128
|
+
rubygems_version: 2.4.1
|
127
129
|
signing_key:
|
128
130
|
specification_version: 4
|
129
131
|
summary: A minimalist's tiny and ultra-fast database cleaner
|