mongoid_monkey 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mongoid_monkey.rb +5 -13
- data/lib/patches/atomic.rb +5 -0
- data/lib/patches/big_decimal.rb +24 -21
- data/lib/patches/db_commands.rb +98 -28
- data/lib/patches/instrument.rb +27 -24
- data/lib/patches/reorder.rb +8 -5
- data/lib/version.rb +1 -1
- data/spec/app/models/account.rb +4 -0
- data/spec/app/models/address.rb +7 -0
- data/spec/app/models/draft.rb +7 -0
- data/spec/app/models/person.rb +11 -0
- data/spec/app/models/user.rb +6 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/unit/db_commands/mongoid3_indexes_spec.rb +408 -0
- data/spec/unit/db_commands/mongoid3_tasks_spec.rb +112 -0
- data/spec/unit/db_commands/mongoid4_indexes_spec.rb +527 -0
- data/spec/unit/db_commands/mongoid4_tasks_spec.rb +163 -0
- data/spec/unit/db_commands/moped_database_spec.rb +62 -0
- data/spec/unit/db_commands/moped_indexes_spec.rb +85 -0
- metadata +23 -3
- data/spec/unit/db_commands_spec.rb +0 -1086
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5814ac5f6752cc4afee52c517ed3996b7d4373f
|
4
|
+
data.tar.gz: 5c42d28b26b8a8c5aa7d1ca71c29397d3392560c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 858f9e0888815dcebe7974e7ddd3737e3c5ce9a5117d47dc0436c9e87626e68ad639722207760dda09202f56ec7a8180325799ba7bc39cd7aaf1dc76824bc40b
|
7
|
+
data.tar.gz: d0c827a916d331114e8b36a75a12d118c83284d491fb055ec80d670c3924856765621a514f1b4acfe7df63505ec42239488704e782bf5d4a9145158aa0b6d275
|
data/lib/mongoid_monkey.rb
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
require 'version'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
if Mongoid::VERSION =~ /\A[345]\./
|
9
|
-
require 'patches/big_decimal'
|
10
|
-
end
|
11
|
-
|
12
|
-
if defined?(Moped)
|
13
|
-
require 'patches/instrument' if Moped::VERSION =~ /\A1\./
|
14
|
-
require 'patches/db_commands'
|
15
|
-
end
|
3
|
+
require 'patches/atomic'
|
4
|
+
require 'patches/big_decimal'
|
5
|
+
require 'patches/db_commands'
|
6
|
+
require 'patches/instrument'
|
7
|
+
require 'patches/reorder'
|
data/lib/patches/atomic.rb
CHANGED
data/lib/patches/big_decimal.rb
CHANGED
@@ -1,38 +1,41 @@
|
|
1
1
|
# Fixes inconsistent behavior of BigDecimal. This can be removed after
|
2
2
|
# https://github.com/mongodb/mongoid/pull/4164 is merged, planned for Mongoid 6.
|
3
3
|
|
4
|
-
|
5
|
-
module Mongoid
|
6
|
-
module Extensions
|
7
|
-
module BigDecimal
|
4
|
+
if Mongoid::VERSION =~ /\A[345]\./
|
8
5
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
module ClassMethods
|
6
|
+
module MongoidMonkey
|
7
|
+
module Mongoid
|
8
|
+
module Extensions
|
9
|
+
module BigDecimal
|
14
10
|
|
15
|
-
def
|
16
|
-
|
11
|
+
def numeric?
|
12
|
+
true
|
17
13
|
end
|
18
14
|
|
19
|
-
|
20
|
-
|
15
|
+
module ClassMethods
|
16
|
+
|
17
|
+
def demongoize(object)
|
18
|
+
object && object.numeric? ? ::BigDecimal.new(object.to_s) : nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def mongoize(object)
|
22
|
+
object && object.numeric? ? object.to_s : nil
|
23
|
+
end
|
21
24
|
end
|
22
25
|
end
|
23
|
-
end
|
24
26
|
|
25
|
-
|
27
|
+
module String
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
+
def numeric?
|
30
|
+
true if Float(self) rescue (self =~ /^NaN|\-?Infinity$/)
|
31
|
+
end
|
29
32
|
end
|
30
33
|
end
|
31
34
|
end
|
32
35
|
end
|
33
|
-
end
|
34
36
|
|
35
|
-
::BigDecimal.__send__(:include, MongoidMonkey::Mongoid::Extensions::BigDecimal)
|
36
|
-
::BigDecimal.extend(MongoidMonkey::Mongoid::Extensions::BigDecimal::ClassMethods)
|
37
|
+
::BigDecimal.__send__(:include, MongoidMonkey::Mongoid::Extensions::BigDecimal)
|
38
|
+
::BigDecimal.extend(MongoidMonkey::Mongoid::Extensions::BigDecimal::ClassMethods)
|
37
39
|
|
38
|
-
::String.__send__(:include, MongoidMonkey::Mongoid::Extensions::String)
|
40
|
+
::String.__send__(:include, MongoidMonkey::Mongoid::Extensions::String)
|
41
|
+
end
|
data/lib/patches/db_commands.rb
CHANGED
@@ -4,49 +4,119 @@
|
|
4
4
|
# - listIndexes
|
5
5
|
# - createIndexes
|
6
6
|
|
7
|
-
|
8
|
-
class Database
|
7
|
+
if defined?(Moped)
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
module Moped
|
10
|
+
class Database
|
11
|
+
|
12
|
+
def collection_names
|
13
|
+
namespaces = command(listCollections: 1, filter: { name: { "$not" => /system\.|\$/ } })
|
14
|
+
namespaces["cursor"]["firstBatch"].map do |doc|
|
15
|
+
doc["name"]
|
16
|
+
end
|
14
17
|
end
|
15
18
|
end
|
16
19
|
end
|
17
|
-
end
|
18
20
|
|
19
|
-
module Moped
|
20
|
-
|
21
|
+
module Moped
|
22
|
+
class Indexes
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
24
|
+
def [](key)
|
25
|
+
list_indexes_command.detect do |index|
|
26
|
+
(index['name'] == key) || (index['key'] == normalize_keys(key))
|
27
|
+
end
|
25
28
|
end
|
26
|
-
end
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
def create(key, options = {})
|
31
|
+
spec = options.merge(ns: namespace, key: key)
|
32
|
+
spec[:name] ||= key.to_a.join("_")
|
33
|
+
database.command(createIndexes: collection_name, indexes: [spec])
|
34
|
+
end
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
+
def each(&block)
|
37
|
+
list_indexes_command.each(&block)
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def list_indexes_command
|
43
|
+
database.command(listIndexes: collection_name)["cursor"]["firstBatch"]
|
44
|
+
end
|
45
|
+
|
46
|
+
def normalize_keys(spec)
|
47
|
+
return false if spec.is_a?(String)
|
48
|
+
spec.reduce({}) do |transformed, (key, value)|
|
49
|
+
transformed[key.to_s] = value
|
50
|
+
transformed
|
51
|
+
end
|
52
|
+
end
|
36
53
|
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
if Mongoid::VERSION =~ /\A3\./ && defined?(Rails::Mongoid)
|
37
58
|
|
38
|
-
|
59
|
+
module Rails::Mongoid
|
39
60
|
|
40
|
-
def
|
41
|
-
|
61
|
+
def remove_indexes(*globs)
|
62
|
+
models(*globs).each do |model|
|
63
|
+
next if model.embedded?
|
64
|
+
begin
|
65
|
+
indexes = model.collection.indexes.map{ |doc| doc["name"] }
|
66
|
+
indexes.delete_one("_id_")
|
67
|
+
model.remove_indexes
|
68
|
+
rescue Moped::Errors::OperationFailure
|
69
|
+
next
|
70
|
+
end
|
71
|
+
logger.info("MONGOID: Removing indexes on: #{model} for: #{indexes.join(', ')}.")
|
72
|
+
model
|
73
|
+
end.compact
|
42
74
|
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
if Mongoid::VERSION =~ /\A4\./
|
43
79
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
80
|
+
module Mongoid::Tasks::Database
|
81
|
+
|
82
|
+
def undefined_indexes(models = ::Mongoid.models)
|
83
|
+
undefined_by_model = {}
|
84
|
+
|
85
|
+
models.each do |model|
|
86
|
+
unless model.embedded?
|
87
|
+
begin
|
88
|
+
model.collection.indexes.each do |index|
|
89
|
+
# ignore default index
|
90
|
+
unless index['name'] == '_id_'
|
91
|
+
key = index['key'].symbolize_keys
|
92
|
+
spec = model.index_specification(key)
|
93
|
+
unless spec
|
94
|
+
# index not specified
|
95
|
+
undefined_by_model[model] ||= []
|
96
|
+
undefined_by_model[model] << index
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
rescue Moped::Errors::OperationFailure; end
|
101
|
+
end
|
49
102
|
end
|
103
|
+
|
104
|
+
undefined_by_model
|
105
|
+
end
|
106
|
+
|
107
|
+
def remove_indexes(models = ::Mongoid.models)
|
108
|
+
models.each do |model|
|
109
|
+
next if model.embedded?
|
110
|
+
begin
|
111
|
+
indexes = model.collection.indexes.map{ |doc| doc["name"] }
|
112
|
+
indexes.delete_one("_id_")
|
113
|
+
model.remove_indexes
|
114
|
+
rescue Moped::Errors::OperationFailure
|
115
|
+
next
|
116
|
+
end
|
117
|
+
logger.info("MONGOID: Removing indexes on: #{model} for: #{indexes.join(', ')}.")
|
118
|
+
model
|
119
|
+
end.compact
|
50
120
|
end
|
51
121
|
end
|
52
122
|
end
|
data/lib/patches/instrument.rb
CHANGED
@@ -1,45 +1,48 @@
|
|
1
1
|
# Instrument Moped 1.x same as Moped 2.x.
|
2
2
|
# Useful for integration with third-party services.
|
3
3
|
|
4
|
-
|
5
|
-
module Instrumentable
|
6
|
-
class Noop
|
4
|
+
if defined?(Moped) && Moped::VERSION =~ /\A1\./
|
7
5
|
|
8
|
-
|
6
|
+
module Moped
|
7
|
+
module Instrumentable
|
8
|
+
class Noop
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
class << self
|
11
|
+
|
12
|
+
# Do not instrument anything.
|
13
|
+
def instrument(name, payload = {})
|
14
|
+
yield payload if block_given?
|
15
|
+
end
|
13
16
|
end
|
14
17
|
end
|
15
18
|
end
|
16
19
|
end
|
17
|
-
end
|
18
20
|
|
19
|
-
module Moped
|
20
|
-
|
21
|
+
module Moped
|
22
|
+
module Instrumentable
|
21
23
|
|
22
|
-
|
24
|
+
TOPIC = "query.moped"
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
|
26
|
+
def instrumenter
|
27
|
+
@instrumenter ||= Moped::Instrumentable::Noop
|
28
|
+
end
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
+
def instrument(name, payload = {}, &block)
|
31
|
+
instrumenter.instrument(name, payload, &block)
|
32
|
+
end
|
30
33
|
end
|
31
34
|
end
|
32
|
-
end
|
33
35
|
|
34
|
-
module Moped
|
35
|
-
|
36
|
-
|
36
|
+
module Moped
|
37
|
+
class Node
|
38
|
+
include Moped::Instrumentable
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
-
|
40
|
+
def logging_with_instrument(operations, &block)
|
41
|
+
instrument(TOPIC, prefix: " MOPED: #{resolved_address}", ops: operations) do
|
42
|
+
logging_without_instrument(operations, &block)
|
43
|
+
end
|
41
44
|
end
|
45
|
+
alias_method_chain :logging, :instrument
|
42
46
|
end
|
43
|
-
alias_method_chain :logging, :instrument
|
44
47
|
end
|
45
48
|
end
|
data/lib/patches/reorder.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
# Backport of Criteria#reorder method from Mongoid 4 to Mongoid 3.
|
2
2
|
|
3
|
-
|
4
|
-
module Optional
|
3
|
+
if Mongoid::VERSION =~ /\A3\./
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
module Origin
|
6
|
+
module Optional
|
7
|
+
|
8
|
+
def reorder(*spec)
|
9
|
+
options.delete(:sort)
|
10
|
+
order_by(*spec)
|
11
|
+
end
|
9
12
|
end
|
10
13
|
end
|
11
14
|
end
|
data/lib/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
2
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
MODELS = File.join(File.dirname(__FILE__), "app/models")
|
5
|
+
$LOAD_PATH.unshift(MODELS)
|
6
|
+
|
3
7
|
$database_name = 'mongoid_monkey_test'
|
4
8
|
|
5
9
|
require 'mongoid'
|
10
|
+
require 'rails/mongoid' if Mongoid::VERSION =~ /\A3\./
|
6
11
|
require 'mongoid_monkey'
|
7
12
|
require 'rspec'
|
8
13
|
|
@@ -13,6 +18,12 @@ end
|
|
13
18
|
Mongoid.logger.level = Logger::INFO
|
14
19
|
Mongo::Logger.logger.level = Logger::INFO unless Mongoid::VERSION =~ /\A[34]\./
|
15
20
|
|
21
|
+
# Autoload every model for the test suite that sits in spec/app/models.
|
22
|
+
Dir[ File.join(MODELS, "*.rb") ].sort.each do |file|
|
23
|
+
name = File.basename(file, ".rb")
|
24
|
+
autoload name.camelize.to_sym, name
|
25
|
+
end
|
26
|
+
|
16
27
|
RSpec.configure do |config|
|
17
28
|
config.after(:all) do
|
18
29
|
if Mongoid::VERSION =~ /\A[34]\./
|