database_rewinder 0.3.0 → 0.4.0

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
  SHA1:
3
- metadata.gz: 4fffba2eb153141b4eba049ab2967097718de4e8
4
- data.tar.gz: aa36ca325c1596c835ec595306412dd5c0f68267
3
+ metadata.gz: c43ef731e44e9d819825abd6bf2a0a013a46d5f1
4
+ data.tar.gz: 02505f6ad68fbf819d3bfff1da3377f7ed70afc5
5
5
  SHA512:
6
- metadata.gz: 56db3ef0a01ee81a6c6d2e8dcbcfcd87172fd71eaa1c01df0b1492a2fe5266affc0ba8f446fc7c8d589d2fab515af8dc5ebd74aaa7763d82b55d6de597258a75
7
- data.tar.gz: e54db71f5bcb3903bcdc4e9bf949be2cead0230df19eba62f15285b07302b331b8a4662b8ea9abc02f049646585ace7b8a2230ea66ff22f3bb4294e819c0665b
6
+ metadata.gz: 05c4efb51990bc523e2a441a06eb65bfedd042435fa1a9534bc08739e61be28366f2a8892605730e69de87463695a59d1ceb6a3309f949a756b71bc25033a3fc
7
+ data.tar.gz: 73c45aaecf4ac026b1903cca7ae61cdf9fdf9024b0833b81f4aa68bc0a95ed698001fad6f49e5bc36b11c9a8b893f31bad3a7e3f679baa5921fbc2184220d692
data/README.md CHANGED
@@ -63,6 +63,11 @@ So the following code will probably let your existing app work under database\_r
63
63
  DatabaseCleaner = DatabaseRewinder
64
64
  ```
65
65
 
66
+ ## TODO
67
+
68
+ Write documentation and specs about multiple databases.
69
+
70
+
66
71
  ## Contributing
67
72
 
68
73
  Send me your pull requests.
@@ -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.3.0'
7
+ spec.version = '0.4.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"
@@ -1,12 +1,16 @@
1
1
  module DatabaseRewinder
2
2
  class Cleaner
3
- attr_accessor :db, :connection_name, :only, :except, :inserted_tables, :pool
3
+ attr_accessor :config, :connection_name, :only, :except, :inserted_tables, :pool
4
4
 
5
- def initialize(db: nil, connection_name: nil, only: nil, except: nil)
6
- @db, @connection_name, @only, @except = db, connection_name, Array(only), Array(except)
5
+ def initialize(config: nil, connection_name: nil, only: nil, except: nil)
6
+ @config, @connection_name, @only, @except = config, connection_name, Array(only), Array(except)
7
7
  reset
8
8
  end
9
9
 
10
+ def db
11
+ config['database']
12
+ end
13
+
10
14
  def clean
11
15
  return if !pool || inserted_tables.empty?
12
16
 
@@ -21,9 +25,15 @@ module DatabaseRewinder
21
25
  end
22
26
 
23
27
  def clean_all
24
- ar_conn = pool ? pool.connection : ActiveRecord::Base.connection
28
+ if (ar_conn = (pool || find_already_connected_model).try :connection)
29
+ delete_all ar_conn, DatabaseRewinder.all_table_names(ar_conn)
30
+ else
31
+ require 'database_rewinder/dummy_model'
32
+ DummyModel.with_temporary_connection(config) do |ar_conn|
33
+ delete_all ar_conn, DatabaseRewinder.all_table_names(ar_conn)
34
+ end
35
+ end
25
36
 
26
- delete_all ar_conn, DatabaseRewinder.all_table_names(ar_conn)
27
37
  reset
28
38
  end
29
39
 
@@ -51,6 +61,10 @@ module DatabaseRewinder
51
61
  ensure
52
62
  pool.automatic_reconnect = reconnect
53
63
  end
64
+
65
+ def find_already_connected_model
66
+ ActiveRecord::Base.descendants.detect {|m| m.connection_pool.spec.config[:database] == db}
67
+ end
54
68
  end
55
69
  end
56
70
 
@@ -0,0 +1,11 @@
1
+ module DatabaseRewinder
2
+ class DummyModel < ActiveRecord::Base
3
+ class << self
4
+ def with_temporary_connection(config)
5
+ establish_connection config
6
+ yield connection
7
+ connection.pool.disconnect!
8
+ end
9
+ end
10
+ end
11
+ end
@@ -18,7 +18,7 @@ module DatabaseRewinder
18
18
  def create_cleaner(connection_name)
19
19
  config = database_configuration[connection_name] or raise %Q[Database configuration named "#{connection_name}" is not configured.]
20
20
 
21
- Cleaner.new(db: config['database'], connection_name: connection_name, only: @only, except: @except).tap {|c| @cleaners << c}
21
+ Cleaner.new(config: config, connection_name: connection_name, only: @only, except: @except).tap {|c| @cleaners << c}
22
22
  end
23
23
 
24
24
  def [](_orm, connection: nil, **)
@@ -69,7 +69,7 @@ module DatabaseRewinder
69
69
 
70
70
  # cache AR connection.tables
71
71
  def all_table_names(connection)
72
- db = connection.instance_variable_get(:'@config')[:database]
72
+ db = connection.pool.spec.config[:database]
73
73
  @table_names_cache[db] ||= connection.tables.reject{|t| t == ActiveRecord::Migrator.schema_migrations_table_name }
74
74
  end
75
75
  end
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.3.0
4
+ version: 0.4.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-08-20 00:00:00.000000000 Z
11
+ date: 2014-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,6 +98,7 @@ files:
98
98
  - lib/database_rewinder/active_record_monkey.rb
99
99
  - lib/database_rewinder/cleaner.rb
100
100
  - lib/database_rewinder/compatibility.rb
101
+ - lib/database_rewinder/dummy_model.rb
101
102
  - lib/database_rewinder/railtie.rb
102
103
  - spec/active_record_monkey_spec.rb
103
104
  - spec/cleaner_spec.rb