mongoid_collection_snapshot 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -1
- data/.travis.yml +1 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +6 -3
- data/README.md +4 -4
- data/Rakefile +0 -1
- data/lib/mongoid_collection_snapshot.rb +13 -4
- data/lib/mongoid_collection_snapshot/version.rb +1 -1
- data/mongoid_collection_snapshot.gemspec +2 -1
- data/spec/models/average_artist_price.rb +13 -5
- data/spec/models/custom_connection_snapshot.rb +22 -5
- data/spec/models/multi_collection_snapshot.rb +9 -3
- data/spec/mongoid/collection_snapshot_spec.rb +32 -10
- data/spec/spec_helper.rb +11 -1
- metadata +27 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee0eb2dd2be3c1d026371d32ebde78fccc17f5e2
|
4
|
+
data.tar.gz: 55f375b7f5e03c0c81bb42b063f4ef395f2c6865
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69524590f00970250e79cfc7d7952298c82ab312a1e32ed1f2c57457b5787f80ab3a9c57a81fb111e3c41469704cd0057b45fddb4fdaede2390f4a7976f4458a
|
7
|
+
data.tar.gz: cceda5bc748e34e4f528880023d5f2067110ac21188a9df969c2dea25951e620b84455c382769ddd5363e79cb5ecbf17156383c889689eb451c3a24988ab92cf
|
data/.rspec
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
--color
|
2
|
-
--format=
|
2
|
+
--format=documentation
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
source 'http://rubygems.org'
|
2
2
|
|
3
|
-
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
case version = ENV['MONGOID_VERSION'] || '~> 5.0'
|
6
|
+
when /5/
|
7
|
+
gem 'mongoid', '~> 5.0'
|
8
|
+
gem 'mongoid-slug', github: 'dblock/mongoid-slug', branch: 'mongoid-5'
|
4
9
|
when /4/
|
5
10
|
gem 'mongoid', '~> 4.0'
|
6
11
|
when /3/
|
@@ -9,8 +14,6 @@ else
|
|
9
14
|
gem 'mongoid', version
|
10
15
|
end
|
11
16
|
|
12
|
-
gem 'mongoid_slug'
|
13
|
-
|
14
17
|
group :development, :test do
|
15
18
|
gem 'rspec', '~> 3.1'
|
16
19
|
gem 'rake'
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Mongoid Collection Snapshot
|
2
2
|
===========================
|
3
3
|
|
4
|
-
Easy maintenance of collections of processed data in MongoDB with
|
4
|
+
Easy maintenance of collections of processed data in MongoDB with Mongoid 3, 4 and 5.
|
5
5
|
|
6
6
|
[![Build Status](https://travis-ci.org/aaw/mongoid_collection_snapshot.svg)](https://travis-ci.org/aaw/mongoid_collection_snapshot)
|
7
7
|
|
@@ -48,7 +48,7 @@ class AverageArtistPrice
|
|
48
48
|
EOS
|
49
49
|
|
50
50
|
Artwork.map_reduce(map, reduce).out(inline: 1).each do |doc|
|
51
|
-
collection_snapshot.
|
51
|
+
collection_snapshot.insert_one(
|
52
52
|
artist_id: doc['_id']['artist_id'],
|
53
53
|
count: doc['value']['count'],
|
54
54
|
sum: doc['value']['sum']
|
@@ -190,8 +190,8 @@ class ArtistStats
|
|
190
190
|
end
|
191
191
|
|
192
192
|
def self.snapshot_session
|
193
|
-
@@snapshot_session ||=
|
194
|
-
|
193
|
+
@@snapshot_session ||= Mongo::Client.new('mongodb://localhost:27017').tap do |c|
|
194
|
+
c.use :alternate_db
|
195
195
|
end
|
196
196
|
end
|
197
197
|
end
|
data/Rakefile
CHANGED
@@ -35,11 +35,19 @@ module Mongoid
|
|
35
35
|
collection_name = collection_snapshot(name).name
|
36
36
|
klass = Class.new do
|
37
37
|
include Mongoid::Document
|
38
|
-
|
38
|
+
if Mongoid::Compatibility::Version::mongoid5?
|
39
|
+
cattr_accessor :mongo_client
|
40
|
+
else
|
41
|
+
cattr_accessor :mongo_session
|
42
|
+
end
|
39
43
|
instance_eval(&document_block) if document_block
|
40
44
|
store_in collection: collection_name
|
41
45
|
end
|
42
|
-
|
46
|
+
if Mongoid::Compatibility::Version.mongoid5?
|
47
|
+
klass.mongo_client = snapshot_session
|
48
|
+
else
|
49
|
+
klass.mongo_session = snapshot_session
|
50
|
+
end
|
43
51
|
Object.const_set(class_name, klass)
|
44
52
|
klass
|
45
53
|
end
|
@@ -66,7 +74,8 @@ module Mongoid
|
|
66
74
|
end
|
67
75
|
|
68
76
|
def drop_snapshot_collections
|
69
|
-
snapshot_session.collections
|
77
|
+
collections = Mongoid::Compatibility::Version.mongoid5? ? snapshot_session.database.collections : snapshot_session.collections
|
78
|
+
collections.each do |collection|
|
70
79
|
collection.drop if collection.name =~ /^#{self.collection.name}\.([^\.]+\.)?#{slug}$/
|
71
80
|
end
|
72
81
|
end
|
@@ -83,7 +92,7 @@ module Mongoid
|
|
83
92
|
|
84
93
|
# Override to supply custom database connection for snapshots
|
85
94
|
def snapshot_session
|
86
|
-
Mongoid.default_session
|
95
|
+
Mongoid::Compatibility::Version.mongoid5? ? Mongoid.default_client : Mongoid.default_session
|
87
96
|
end
|
88
97
|
end
|
89
98
|
end
|
@@ -14,5 +14,6 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.licenses = ['MIT']
|
15
15
|
s.summary = 'Easy maintenence of collections of processed data in MongoDB with the Mongoid ODM.'
|
16
16
|
s.add_dependency 'mongoid', '>= 3.0'
|
17
|
-
s.add_dependency '
|
17
|
+
s.add_dependency 'mongoid-compatibility'
|
18
|
+
s.add_dependency 'mongoid-slug'
|
18
19
|
end
|
@@ -27,11 +27,19 @@ class AverageArtistPrice
|
|
27
27
|
EOS
|
28
28
|
|
29
29
|
Artwork.map_reduce(map, reduce).out(inline: 1).each do |doc|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
30
|
+
if Mongoid::Compatibility::Version.mongoid5?
|
31
|
+
collection_snapshot.insert_one(
|
32
|
+
artist_id: doc['_id']['artist_id'],
|
33
|
+
count: doc['value']['count'],
|
34
|
+
sum: doc['value']['sum']
|
35
|
+
)
|
36
|
+
else
|
37
|
+
collection_snapshot.insert(
|
38
|
+
artist_id: doc['_id']['artist_id'],
|
39
|
+
count: doc['value']['count'],
|
40
|
+
sum: doc['value']['sum']
|
41
|
+
)
|
42
|
+
end
|
35
43
|
end
|
36
44
|
end
|
37
45
|
|
@@ -2,9 +2,7 @@ class CustomConnectionSnapshot
|
|
2
2
|
include Mongoid::CollectionSnapshot
|
3
3
|
|
4
4
|
def self.snapshot_session
|
5
|
-
@snapshot_session ||=
|
6
|
-
session.use :snapshot_test
|
7
|
-
end
|
5
|
+
@snapshot_session ||= new_snapshot_session
|
8
6
|
end
|
9
7
|
|
10
8
|
def snapshot_session
|
@@ -12,7 +10,26 @@ class CustomConnectionSnapshot
|
|
12
10
|
end
|
13
11
|
|
14
12
|
def build
|
15
|
-
|
16
|
-
|
13
|
+
if Mongoid::Compatibility::Version.mongoid5?
|
14
|
+
collection_snapshot.insert_one('name' => 'foo')
|
15
|
+
collection_snapshot('foo').insert_one('name' => 'bar')
|
16
|
+
else
|
17
|
+
collection_snapshot.insert('name' => 'foo')
|
18
|
+
collection_snapshot('foo').insert('name' => 'bar')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def self.new_snapshot_session
|
25
|
+
if Mongoid::Compatibility::Version.mongoid5?
|
26
|
+
Mongo::Client.new('mongodb://localhost:27017').tap do |client|
|
27
|
+
client.use :snapshot_test
|
28
|
+
end
|
29
|
+
else
|
30
|
+
Moped::Session.new(['127.0.0.1:27017']).tap do |session|
|
31
|
+
session.use :snapshot_test
|
32
|
+
end
|
33
|
+
end
|
17
34
|
end
|
18
35
|
end
|
@@ -17,9 +17,15 @@ class MultiCollectionSnapshot
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def build
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
if Mongoid::Compatibility::Version.mongoid5?
|
21
|
+
collection_snapshot('foo').insert_one('name' => 'foo!', count: 1)
|
22
|
+
collection_snapshot('bar').insert_one('name' => 'bar!', number: 2)
|
23
|
+
collection_snapshot('baz').insert_one('name' => 'baz!', digit: 3)
|
24
|
+
else
|
25
|
+
collection_snapshot('foo').insert('name' => 'foo!', count: 1)
|
26
|
+
collection_snapshot('bar').insert('name' => 'bar!', number: 2)
|
27
|
+
collection_snapshot('baz').insert('name' => 'baz!', digit: 3)
|
28
|
+
end
|
23
29
|
end
|
24
30
|
|
25
31
|
def names
|
@@ -75,22 +75,31 @@ module Mongoid
|
|
75
75
|
|
76
76
|
it 'safely cleans up all collections used by the snapshot' do
|
77
77
|
# Create some collections with names close to the snapshots we'll create
|
78
|
-
Mongoid.
|
79
|
-
|
80
|
-
|
78
|
+
if Mongoid::Compatibility::Version.mongoid5?
|
79
|
+
Mongoid.default_client["#{MultiCollectionSnapshot.collection.name}.do.not_delete"].insert_one('a' => 1)
|
80
|
+
Mongoid.default_client["#{MultiCollectionSnapshot.collection.name}.snapshorty"].insert_one('a' => 1)
|
81
|
+
Mongoid.default_client["#{MultiCollectionSnapshot.collection.name}.hello.1"].insert_one('a' => 1)
|
82
|
+
else
|
83
|
+
Mongoid.default_session["#{MultiCollectionSnapshot.collection.name}.do.not_delete"].insert('a' => 1)
|
84
|
+
Mongoid.default_session["#{MultiCollectionSnapshot.collection.name}.snapshorty"].insert('a' => 1)
|
85
|
+
Mongoid.default_session["#{MultiCollectionSnapshot.collection.name}.hello.1"].insert('a' => 1)
|
86
|
+
end
|
81
87
|
|
82
88
|
MultiCollectionSnapshot.create
|
83
|
-
|
89
|
+
collections = Mongoid::Compatibility::Version.mongoid5? ? Mongoid.default_client.database.collections : Mongoid.default_session.collections
|
90
|
+
before_create = collections.map(&:name)
|
84
91
|
expect(before_create.length).to be > 0
|
85
92
|
|
86
93
|
Timecop.travel(1.second.from_now)
|
87
94
|
MultiCollectionSnapshot.create
|
88
|
-
|
95
|
+
collections = Mongoid::Compatibility::Version.mongoid5? ? Mongoid.default_client.database.collections : Mongoid.default_session.collections
|
96
|
+
after_create = collections.map(&:name)
|
89
97
|
collections_created = (after_create - before_create).sort
|
90
98
|
expect(collections_created.length).to eq(3)
|
91
99
|
|
92
100
|
MultiCollectionSnapshot.latest.destroy
|
93
|
-
|
101
|
+
collections = Mongoid::Compatibility::Version.mongoid5? ? Mongoid.default_client.database.collections : Mongoid.default_session.collections
|
102
|
+
after_destroy = collections.map(&:name)
|
94
103
|
collections_destroyed = (after_create - after_destroy).sort
|
95
104
|
expect(collections_created).to eq(collections_destroyed)
|
96
105
|
end
|
@@ -98,9 +107,17 @@ module Mongoid
|
|
98
107
|
|
99
108
|
context 'with a custom snapshot connection' do
|
100
109
|
around(:each) do |example|
|
101
|
-
|
110
|
+
if Mongoid::Compatibility::Version.mongoid5?
|
111
|
+
CustomConnectionSnapshot.snapshot_session.database.drop
|
112
|
+
else
|
113
|
+
CustomConnectionSnapshot.snapshot_session.drop
|
114
|
+
end
|
102
115
|
example.run
|
103
|
-
|
116
|
+
if Mongoid::Compatibility::Version.mongoid5?
|
117
|
+
CustomConnectionSnapshot.snapshot_session.database.drop
|
118
|
+
else
|
119
|
+
CustomConnectionSnapshot.snapshot_session.drop
|
120
|
+
end
|
104
121
|
end
|
105
122
|
|
106
123
|
it 'builds snapshot in custom database' do
|
@@ -109,14 +126,19 @@ module Mongoid
|
|
109
126
|
"#{CustomConnectionSnapshot.collection.name}.foo.#{snapshot.slug}",
|
110
127
|
"#{CustomConnectionSnapshot.collection.name}.#{snapshot.slug}"
|
111
128
|
].each do |collection_name|
|
112
|
-
|
129
|
+
session = Mongoid::Compatibility::Version.mongoid5? ? Mongoid.default_client : Mongoid.default_session
|
130
|
+
expect(session[collection_name].find.count).to eq(0)
|
113
131
|
expect(CustomConnectionSnapshot.snapshot_session[collection_name].find.count).to eq(1)
|
114
132
|
end
|
115
133
|
end
|
116
134
|
|
117
135
|
context '#documents' do
|
118
136
|
it 'uses the custom session' do
|
119
|
-
|
137
|
+
if Mongoid::Compatibility::Version.mongoid5?
|
138
|
+
expect(CustomConnectionSnapshot.new.documents.mongo_client).to eq CustomConnectionSnapshot.snapshot_session
|
139
|
+
else
|
140
|
+
expect(CustomConnectionSnapshot.new.documents.mongo_session).to eq CustomConnectionSnapshot.snapshot_session
|
141
|
+
end
|
120
142
|
end
|
121
143
|
it 'provides access to a Mongoid collection' do
|
122
144
|
snapshot = CustomConnectionSnapshot.create
|
data/spec/spec_helper.rb
CHANGED
@@ -12,12 +12,22 @@ end
|
|
12
12
|
require File.expand_path('../../lib/mongoid_collection_snapshot', __FILE__)
|
13
13
|
Dir["#{File.dirname(__FILE__)}/models/**/*.rb"].each { |f| require f }
|
14
14
|
|
15
|
+
require 'mongoid-compatibility'
|
16
|
+
|
15
17
|
RSpec.configure do |c|
|
18
|
+
c.before(:all) do
|
19
|
+
Mongoid.logger.level = Logger::INFO
|
20
|
+
Mongo::Logger.logger.level = Logger::INFO if Mongoid::Compatibility::Version.mongoid5?
|
21
|
+
end
|
16
22
|
c.before(:each) do
|
17
23
|
Mongoid.purge!
|
18
24
|
end
|
19
25
|
c.after(:all) do
|
20
|
-
Mongoid.
|
26
|
+
if Mongoid::Compatibility::Version.mongoid5?
|
27
|
+
Mongoid.default_client.database.drop
|
28
|
+
else
|
29
|
+
Mongoid.default_session.drop
|
30
|
+
end
|
21
31
|
end
|
22
32
|
end
|
23
33
|
|
metadata
CHANGED
@@ -1,41 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_collection_snapshot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Windsor
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '3.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: mongoid-compatibility
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mongoid-slug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '0'
|
41
55
|
description:
|
@@ -44,9 +58,9 @@ executables: []
|
|
44
58
|
extensions: []
|
45
59
|
extra_rdoc_files: []
|
46
60
|
files:
|
47
|
-
- .gitignore
|
48
|
-
- .rspec
|
49
|
-
- .travis.yml
|
61
|
+
- ".gitignore"
|
62
|
+
- ".rspec"
|
63
|
+
- ".travis.yml"
|
50
64
|
- CHANGELOG.md
|
51
65
|
- Gemfile
|
52
66
|
- LICENSE.txt
|
@@ -72,17 +86,17 @@ require_paths:
|
|
72
86
|
- lib
|
73
87
|
required_ruby_version: !ruby/object:Gem::Requirement
|
74
88
|
requirements:
|
75
|
-
- -
|
89
|
+
- - ">="
|
76
90
|
- !ruby/object:Gem::Version
|
77
91
|
version: '0'
|
78
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
|
-
- -
|
94
|
+
- - ">="
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: 1.3.6
|
83
97
|
requirements: []
|
84
98
|
rubyforge_project:
|
85
|
-
rubygems_version: 2.
|
99
|
+
rubygems_version: 2.2.2
|
86
100
|
signing_key:
|
87
101
|
specification_version: 4
|
88
102
|
summary: Easy maintenence of collections of processed data in MongoDB with the Mongoid
|