database_flusher 0.2.3 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 75d8472e33710dc5a47fa06ae32e618e929d6b20
4
- data.tar.gz: 5eaf9adc3245e5f0ab400a592db7d884382cc66a
3
+ metadata.gz: a5757e018763a6a24a1ab1d4458342652e37c6dd
4
+ data.tar.gz: eef0f72db7a2688c90cb43829dce03f5cbc04484
5
5
  SHA512:
6
- metadata.gz: d95f2e35fa0107d9011119bf590cb655f06850f1436a3aafae43c3101e8c5c356eace416d2138b5fc9d2287d51a469b0b4126ff0d35b0f7593fb48c312f677e9
7
- data.tar.gz: 23e7639d7fb80545489f65820a48f7d48465060699a8959a8a2cd567000192f38ba68460a8372d576f50eb2a42aaeebe2e8aeb2ba8c3a387add426f65fd1ba05
6
+ metadata.gz: 8202dd0665c71467cd1d9ad20740453e94e7b674f432c6dd4c6238a2ae02cb1f061a017ea84faea86e0c73d57268ddd179edfd1f9aef1adeaf63a76a6729dc51
7
+ data.tar.gz: 19f3166b15a7d8933d478a00b01fe875acf0d15fd00fd4fccf06317ec956c518e88416936b5a88a75b822e3ac8e3e713aaa0d5b57fc753b135a36e6b630a609c
data/README.md CHANGED
@@ -96,6 +96,26 @@ After do
96
96
  end
97
97
  ```
98
98
 
99
+ In case of unclean test shutdown, you can clean the whole database, using:
100
+
101
+ ```ruby
102
+ RSpec.configure do |config|
103
+ config.before :suite do
104
+ if File.exists?('.~lock')
105
+ puts "Unclean shutdown, cleaning the whole database..."
106
+ DatabaseFlusher[:active_record].clean_with(:deletion)
107
+ DatabaseFlusher[:mongoid].clean_with(:deletion)
108
+ else
109
+ File.open('.~lock', 'a') {}
110
+ end
111
+ end
112
+
113
+ config.after :suite do
114
+ File.unlink('.~lock')
115
+ end
116
+ end
117
+ ```
118
+
99
119
  ## Contributing
100
120
 
101
121
  Bug reports and pull requests are welcome on GitHub at https://github.com/ebeigarts/database_flusher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
@@ -7,7 +7,7 @@ require 'database_flusher/active_record/adapters/sqlite_adapter'
7
7
  module DatabaseFlusher
8
8
  module ActiveRecord
9
9
  class DeletionStrategy
10
- attr_reader :tables, :adapter
10
+ attr_reader :tables
11
11
 
12
12
  class Subscriber
13
13
  def initialize(strategy)
@@ -34,10 +34,6 @@ module DatabaseFlusher
34
34
  'sql.active_record',
35
35
  Subscriber.new(self)
36
36
  )
37
- connection = ::ActiveRecord::Base.connection
38
- @adapter = DatabaseFlusher::ActiveRecord.
39
- const_get("#{connection.adapter_name}Adapter").
40
- new(connection)
41
37
  end
42
38
 
43
39
  def stop
@@ -55,6 +51,31 @@ module DatabaseFlusher
55
51
 
56
52
  tables.clear
57
53
  end
54
+
55
+ def clean_all
56
+ adapter.delete(*all_tables)
57
+ end
58
+
59
+ private
60
+
61
+ def connection
62
+ @connection ||= ::ActiveRecord::Base.connection
63
+ end
64
+
65
+ def adapter
66
+ @adapter ||= DatabaseFlusher::ActiveRecord.
67
+ const_get("#{connection.adapter_name}Adapter").
68
+ new(connection)
69
+ end
70
+
71
+ def all_tables
72
+ tables = connection.tables
73
+ tables.reject do |t|
74
+ (t == ::ActiveRecord::Migrator.schema_migrations_table_name) ||
75
+ (::ActiveRecord::Base.respond_to?(:internal_metadata_table_name) &&
76
+ (t == ::ActiveRecord::Base.internal_metadata_table_name))
77
+ end
78
+ end
58
79
  end
59
80
  end
60
81
  end
@@ -22,7 +22,7 @@ module DatabaseFlusher
22
22
 
23
23
  def clean_with(name)
24
24
  self.strategy = name
25
- strategy.clean
25
+ strategy.clean_all
26
26
  end
27
27
 
28
28
  def start
@@ -10,11 +10,9 @@ module DatabaseFlusher
10
10
  end
11
11
 
12
12
  def started(event)
13
- if event.command_name == :insert || event.command_name == 'insert'.freeze
14
- collection = event.command['insert'.freeze]
15
- if collection
16
- @strategy.collections << collection
17
- end
13
+ collection = event.command['insert'.freeze]
14
+ if collection
15
+ @strategy.collections << collection
18
16
  end
19
17
  end
20
18
 
@@ -29,7 +27,7 @@ module DatabaseFlusher
29
27
  end
30
28
 
31
29
  def start
32
- @subscriber ||= Mongo::Monitoring::Global.subscribe(
30
+ @subscriber ||= client.subscribe(
33
31
  Mongo::Monitoring::COMMAND,
34
32
  Subscriber.new(self)
35
33
  )
@@ -42,12 +40,27 @@ module DatabaseFlusher
42
40
  def clean
43
41
  return if collections.empty?
44
42
  # puts "Cleaning #{collections.inspect}"
45
- client = ::Mongoid::Clients.default
46
43
  collections.each do |name|
47
44
  client[name].delete_many
48
45
  end
49
46
  collections.clear
50
47
  end
48
+
49
+ def clean_all
50
+ all_collections.each do |name|
51
+ client[name].delete_many
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def client
58
+ @client ||= ::Mongoid::Clients.default
59
+ end
60
+
61
+ def all_collections
62
+ client.database.collections.collect { |c| c.namespace.split('.',2)[1] }
63
+ end
51
64
  end
52
65
  end
53
66
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module DatabaseFlusher
3
- VERSION = '0.2.3'.freeze
3
+ VERSION = '0.3.0'.freeze
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: database_flusher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edgars Beigarts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-12 00:00:00.000000000 Z
11
+ date: 2016-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler