ndb-rails 1.3.0 → 1.4.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/lib/ndb/rails/associations.rb +205 -0
- data/lib/ndb/rails/configuration.rb +4 -0
- data/lib/ndb/rails/database_cleaner.rb +55 -0
- data/lib/ndb/rails/minitest.rb +20 -0
- data/lib/ndb/rails/persistence.rb +17 -0
- data/lib/ndb/rails/record.rb +5 -2
- data/lib/ndb/rails/relation.rb +33 -0
- data/lib/ndb/rails/rspec.rb +20 -0
- data/lib/ndb/rails/testing.rb +105 -0
- data/lib/ndb/rails/version.rb +1 -1
- data/lib/ndb/rails.rb +7 -0
- metadata +6 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ec4084b532f5782d3ece22387483c65efeb301e0400e1dba65877a7bfaff994e
|
|
4
|
+
data.tar.gz: df994a34746b3f4c69f22bd3a2682de9285923eda5d2acd39eb70e9d0eb891af
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c372e9f85577c6c1d680603f6b0798ed12cf28fdbee6f9ebd73300a9357e47facf5f59c8e8450395f2b3d659a5794de48bf7f048cdd0c006981bbea322b49f5b
|
|
7
|
+
data.tar.gz: bbaa5fbd8596877cbd8c3f8041bd8782dda7d3c179ff9dcba9652deff2803268158e5815f81245c8cba3641ac3d2db9cb67a8b4a3e681a92c0cc1ca339c1d5e0
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ndb
|
|
4
|
+
# belongs_to, has_many and has_one, read like ActiveRecord: includes loads
|
|
5
|
+
# them with the rows in one call (the daemon lookup), and a record without
|
|
6
|
+
# includes loads one on first use (one more call, like an N+1).
|
|
7
|
+
#
|
|
8
|
+
# class Order < Ndb::Record
|
|
9
|
+
# belongs_to :customer # customer_id -> Customer._id
|
|
10
|
+
# has_many :items, sort: { pos: 1 } # Item.order_id -> _id
|
|
11
|
+
# has_one :invoice # Invoice.order_id -> _id
|
|
12
|
+
# end
|
|
13
|
+
#
|
|
14
|
+
# Order.where(status: "paid").includes(:customer, items: :product).to_a
|
|
15
|
+
#
|
|
16
|
+
# has_many and has_one index the foreign key of the other model by
|
|
17
|
+
# themselves (the lookup needs it).
|
|
18
|
+
module Associations
|
|
19
|
+
extend ActiveSupport::Concern
|
|
20
|
+
|
|
21
|
+
# One declared association.
|
|
22
|
+
Reflection = Data.define(:name, :kind, :class_name, :foreign_key, :primary_key, :options) do
|
|
23
|
+
def klass = class_name.constantize
|
|
24
|
+
|
|
25
|
+
def many? = kind == :has_many
|
|
26
|
+
|
|
27
|
+
# The relation for the lookup key of a search.
|
|
28
|
+
def lookup(nested = {})
|
|
29
|
+
relation = { kind.to_s => klass.collection_name, "fk" => foreign_key.to_s }
|
|
30
|
+
|
|
31
|
+
relation["pk"] = primary_key.to_s if primary_key.to_s != "_id"
|
|
32
|
+
|
|
33
|
+
options.each { |k, v| relation[k.to_s] = Ndb.deep_stringify(v) }
|
|
34
|
+
|
|
35
|
+
relation["lookup"] = klass.lookup_for(nested) if nested.any?
|
|
36
|
+
relation
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
class_methods do
|
|
41
|
+
# The keys are inferred like Rails (customer -> customer_id, Customer;
|
|
42
|
+
# has_many in Order -> order_id). foreign_key: (fk:) and primary_key:
|
|
43
|
+
# (pk:) replace them.
|
|
44
|
+
def belongs_to(name, class_name: nil, foreign_key: nil, fk: nil, primary_key: nil, pk: nil)
|
|
45
|
+
foreign_key = either(name, :foreign_key, foreign_key, :fk, fk) || "#{name}_id"
|
|
46
|
+
|
|
47
|
+
associate(name, :belongs_to, class_name || name.to_s.camelize, foreign_key, keys_pk(name, primary_key, pk), {})
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def has_many(name, class_name: nil, foreign_key: nil, fk: nil, primary_key: nil, pk: nil, sort: nil, limit: nil, where: nil)
|
|
51
|
+
options = { sort: sort, limit: limit, query: where }.compact
|
|
52
|
+
foreign_key = either(name, :foreign_key, foreign_key, :fk, fk) || "#{model_name.singular}_id"
|
|
53
|
+
|
|
54
|
+
associate(name, :has_many, class_name || name.to_s.singularize.camelize, foreign_key, keys_pk(name, primary_key, pk), options)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def has_one(name, class_name: nil, foreign_key: nil, fk: nil, primary_key: nil, pk: nil, sort: nil, where: nil)
|
|
58
|
+
options = { sort: sort, query: where }.compact
|
|
59
|
+
foreign_key = either(name, :foreign_key, foreign_key, :fk, fk) || "#{model_name.singular}_id"
|
|
60
|
+
|
|
61
|
+
associate(name, :has_one, class_name || name.to_s.camelize, foreign_key, keys_pk(name, primary_key, pk), options)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def reflections
|
|
65
|
+
inherited = superclass.respond_to?(:reflections) ? superclass.reflections : {}
|
|
66
|
+
|
|
67
|
+
inherited.merge(own_reflections)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# The lookup key for includes(:a, b: :c).
|
|
71
|
+
def lookup_for(names)
|
|
72
|
+
normalize_includes(names).to_h do |name, nested|
|
|
73
|
+
reflection = reflections[name] || raise(ArgumentError, "#{self.name} has no association #{name}")
|
|
74
|
+
|
|
75
|
+
reflection.klass.prepare_lookup(reflection)
|
|
76
|
+
|
|
77
|
+
[name, reflection.lookup(nested)]
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Declares the collection of the other side, and the index the lookup
|
|
82
|
+
# needs there (once per process).
|
|
83
|
+
def prepare_lookup(reflection)
|
|
84
|
+
ensure_collection!
|
|
85
|
+
|
|
86
|
+
return if reflection.kind == :belongs_to || reflection.foreign_key.to_s == "_id"
|
|
87
|
+
|
|
88
|
+
@lookup_indexes ||= Set.new
|
|
89
|
+
|
|
90
|
+
key = "#{collection_name}\0#{reflection.foreign_key}"
|
|
91
|
+
|
|
92
|
+
return if @lookup_indexes.include?(key)
|
|
93
|
+
|
|
94
|
+
Rails.client.ensure_index(collection_name, reflection.foreign_key.to_s)
|
|
95
|
+
@lookup_indexes << key
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
private
|
|
99
|
+
|
|
100
|
+
def keys_pk(name, primary_key, pk)
|
|
101
|
+
either(name, :primary_key, primary_key, :pk, pk) || "_id"
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# either takes one of two names of the same option.
|
|
105
|
+
def either(name, long, long_value, short, short_value)
|
|
106
|
+
if long_value && short_value
|
|
107
|
+
raise ArgumentError, "#{self.name}.#{name}: #{long}: and #{short}: are the same option; pass one"
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
(long_value || short_value)&.to_s
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def own_reflections
|
|
114
|
+
@own_reflections ||= {}
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def associate(name, kind, class_name, foreign_key, primary_key, options)
|
|
118
|
+
name = name.to_s
|
|
119
|
+
|
|
120
|
+
own_reflections[name] = Reflection.new(name: name, kind: kind, class_name: class_name.to_s, foreign_key: foreign_key.to_s,
|
|
121
|
+
primary_key: primary_key.to_s, options: options)
|
|
122
|
+
|
|
123
|
+
define_method(name) { association(name) }
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# includes(:a, b: :c) -> { "a" => {}, "b" => { "c" => {} } }
|
|
127
|
+
def normalize_includes(names)
|
|
128
|
+
return names.to_h { |k, v| [k.to_s, normalize_includes(v)] } if names.is_a?(Hash)
|
|
129
|
+
|
|
130
|
+
Array(names).each_with_object({}) do |item, out|
|
|
131
|
+
item.is_a?(Hash) ? out.merge!(normalize_includes(item)) : out[item.to_s] = {}
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# The associated record, or for a has_many a Ndb::Relation (lazy and
|
|
137
|
+
# chainable, like ActiveRecord). It is kept on the record for the key it
|
|
138
|
+
# was read with: a change of the foreign key (or reload) reads it again.
|
|
139
|
+
def association(name)
|
|
140
|
+
reflection = self.class.reflections.fetch(name)
|
|
141
|
+
key = association_key(reflection)
|
|
142
|
+
cached = (@associations ||= {})[name]
|
|
143
|
+
|
|
144
|
+
return cached[:value] if cached && cached[:key] == key
|
|
145
|
+
|
|
146
|
+
value = load_association(reflection, key)
|
|
147
|
+
|
|
148
|
+
@associations[name] = { key: key, value: value }
|
|
149
|
+
|
|
150
|
+
value
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Sets the associations that a lookup brought in row.
|
|
154
|
+
def load_associations(row)
|
|
155
|
+
self.class.reflections.each do |name, reflection|
|
|
156
|
+
next unless row.key?(name)
|
|
157
|
+
|
|
158
|
+
key = association_key(reflection)
|
|
159
|
+
|
|
160
|
+
(@associations ||= {})[name] = { key: key, value: build_association(reflection, key, row[name]) }
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
private
|
|
165
|
+
|
|
166
|
+
# The field of this record the association reads: the foreign key of a
|
|
167
|
+
# belongs_to, the primary key of a has_many or has_one.
|
|
168
|
+
def association_key(reflection)
|
|
169
|
+
field = reflection.kind == :belongs_to ? reflection.foreign_key : reflection.primary_key
|
|
170
|
+
|
|
171
|
+
respond_to?(field) ? public_send(field) : nil
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def build_association(reflection, key, value)
|
|
175
|
+
target = reflection.klass
|
|
176
|
+
|
|
177
|
+
return association_scope(reflection, key).preload(Array(value).map { |doc| target.instantiate(doc) }) if reflection.many?
|
|
178
|
+
return nil if value.nil?
|
|
179
|
+
return value.map { |doc| target.instantiate(doc) } if value.is_a?(Array)
|
|
180
|
+
|
|
181
|
+
target.instantiate(value)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def load_association(reflection, key)
|
|
185
|
+
if reflection.kind == :belongs_to
|
|
186
|
+
return key.nil? ? nil : reflection.klass.find_by(reflection.primary_key => key)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
scope = association_scope(reflection, key)
|
|
190
|
+
|
|
191
|
+
reflection.many? ? scope : scope.first
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# The relation of a has_many or has_one: the other side whose foreign
|
|
195
|
+
# key is key, with the declared where, sort and limit.
|
|
196
|
+
def association_scope(reflection, key)
|
|
197
|
+
scope = reflection.klass.where(reflection.foreign_key => key)
|
|
198
|
+
scope = scope.where_raw(reflection.options[:query]) if reflection.options[:query]
|
|
199
|
+
scope = scope.order(reflection.options[:sort]) if reflection.options[:sort]
|
|
200
|
+
scope = scope.limit(reflection.options[:limit]) if reflection.options[:limit]
|
|
201
|
+
|
|
202
|
+
scope
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
|
@@ -11,6 +11,10 @@ module Ndb
|
|
|
11
11
|
class Configuration
|
|
12
12
|
attr_accessor :url, :socket, :token, :timeout, :pool_size, :ready_timeout
|
|
13
13
|
|
|
14
|
+
# collection_prefix goes before every collection name of the models
|
|
15
|
+
# ("test2_" for a parallel test worker). nil by default.
|
|
16
|
+
attr_accessor :collection_prefix
|
|
17
|
+
|
|
14
18
|
def initialize
|
|
15
19
|
@timeout = 30
|
|
16
20
|
@pool_size = 16
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# DatabaseCleaner (2.x): in the test setup,
|
|
4
|
+
#
|
|
5
|
+
# require "ndb/rails/database_cleaner"
|
|
6
|
+
#
|
|
7
|
+
# DatabaseCleaner[:ndb].strategy = :touched # the default: what the test used
|
|
8
|
+
# DatabaseCleaner[:ndb].strategy = :truncation, { only: %w[calls] }
|
|
9
|
+
# DatabaseCleaner[:ndb].clean_with(:truncation) # every collection, before the suite
|
|
10
|
+
#
|
|
11
|
+
# Needs the database_cleaner-core gem. ndb has no transactions: both
|
|
12
|
+
# strategies truncate, which takes a few ms per collection whatever its
|
|
13
|
+
# size. :touched truncates the collections the tests used since the last
|
|
14
|
+
# clean; :truncation truncates every collection of the worker (or only:,
|
|
15
|
+
# less except:).
|
|
16
|
+
|
|
17
|
+
require "database_cleaner/core"
|
|
18
|
+
require "ndb/rails"
|
|
19
|
+
require "ndb/rails/testing"
|
|
20
|
+
|
|
21
|
+
module DatabaseCleaner
|
|
22
|
+
module Ndb
|
|
23
|
+
# Truncates the collections the tests used since the last clean.
|
|
24
|
+
class Touched < DatabaseCleaner::Strategy
|
|
25
|
+
# DatabaseCleaner passes the options as a Hash: strategy = :touched, { except: [...] }
|
|
26
|
+
def initialize(options = {}, **keywords)
|
|
27
|
+
super()
|
|
28
|
+
|
|
29
|
+
@except = options.merge(keywords).fetch(:except, [])
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def clean
|
|
33
|
+
::Ndb::Testing.clean!(except: @except)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Truncates every engine collection of the worker, or only these.
|
|
38
|
+
class Truncation < DatabaseCleaner::Strategy
|
|
39
|
+
def initialize(options = {}, **keywords)
|
|
40
|
+
super()
|
|
41
|
+
|
|
42
|
+
options = options.merge(keywords)
|
|
43
|
+
|
|
44
|
+
@only = options[:only]
|
|
45
|
+
@except = options.fetch(:except, [])
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def clean
|
|
49
|
+
@only ? ::Ndb::Testing.clean!(only: @only, except: @except) : ::Ndb::Testing.clean_all!(except: @except)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
DatabaseCleaner[:ndb].strategy = :touched
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Minitest, the Rails way: in test/test_helper.rb, after rails/test_help,
|
|
4
|
+
#
|
|
5
|
+
# require "ndb/rails/minitest"
|
|
6
|
+
#
|
|
7
|
+
# Every ActiveSupport::TestCase truncates the ndb collections it used after
|
|
8
|
+
# each test, and each worker of `parallelize` gets collections of its own.
|
|
9
|
+
# A test case opts out with `self.ndb_clean = false`.
|
|
10
|
+
|
|
11
|
+
require "ndb/rails"
|
|
12
|
+
require "ndb/rails/testing"
|
|
13
|
+
|
|
14
|
+
Ndb::Testing.worker!(ENV.fetch("TEST_ENV_NUMBER", nil)) if ENV.key?("TEST_ENV_NUMBER")
|
|
15
|
+
|
|
16
|
+
ActiveSupport.on_load(:active_support_test_case) do
|
|
17
|
+
include Ndb::Testing::Minitest
|
|
18
|
+
|
|
19
|
+
parallelize_setup { |worker| Ndb::Testing.worker!(worker) } if respond_to?(:parallelize_setup)
|
|
20
|
+
end
|
|
@@ -46,6 +46,7 @@ module Ndb
|
|
|
46
46
|
record = new(row.slice(*attribute_names))
|
|
47
47
|
|
|
48
48
|
record.send(:loaded!, version, source)
|
|
49
|
+
record.load_associations(row) if record.respond_to?(:load_associations)
|
|
49
50
|
|
|
50
51
|
record
|
|
51
52
|
end
|
|
@@ -194,6 +195,22 @@ module Ndb
|
|
|
194
195
|
add_to!(attribute, -by)
|
|
195
196
|
end
|
|
196
197
|
|
|
198
|
+
# Reads the document again, like ActiveRecord reload: the attributes,
|
|
199
|
+
# the version and the associations (they load again on next use).
|
|
200
|
+
def reload
|
|
201
|
+
raise RecordNotSaved, "Cannot reload a record that is not saved" unless persisted?
|
|
202
|
+
|
|
203
|
+
fresh = self.class.find(id)
|
|
204
|
+
|
|
205
|
+
@attributes = fresh.instance_variable_get(:@attributes)
|
|
206
|
+
@version = fresh.version
|
|
207
|
+
@associations = nil
|
|
208
|
+
|
|
209
|
+
clear_changes_information
|
|
210
|
+
|
|
211
|
+
self
|
|
212
|
+
end
|
|
213
|
+
|
|
197
214
|
def ==(other)
|
|
198
215
|
other.instance_of?(self.class) && !id.nil? && id == other.id
|
|
199
216
|
end
|
data/lib/ndb/rails/record.rb
CHANGED
|
@@ -10,6 +10,7 @@ module Ndb
|
|
|
10
10
|
include ActiveModel::Dirty
|
|
11
11
|
include ActiveModel::Serializers::JSON
|
|
12
12
|
include Persistence
|
|
13
|
+
include Associations
|
|
13
14
|
|
|
14
15
|
extend ActiveSupport::DescendantsTracker
|
|
15
16
|
extend DynamicFinders
|
|
@@ -23,7 +24,7 @@ module Ndb
|
|
|
23
24
|
|
|
24
25
|
class << self
|
|
25
26
|
QUERY_METHODS = %i[
|
|
26
|
-
where where_raw order limit offset page after group find find_by find_by! first count pluck exists?
|
|
27
|
+
where where_raw order limit offset page after group includes find find_by find_by! first last count pluck exists?
|
|
27
28
|
find_each in_batches sum average minimum maximum to_a each destroy_all delete_all
|
|
28
29
|
].freeze
|
|
29
30
|
|
|
@@ -128,7 +129,7 @@ module Ndb
|
|
|
128
129
|
end
|
|
129
130
|
|
|
130
131
|
def collection_name
|
|
131
|
-
@collection_name || model_name.plural
|
|
132
|
+
"#{Rails.configuration.collection_prefix}#{@collection_name || model_name.plural}"
|
|
132
133
|
end
|
|
133
134
|
|
|
134
135
|
def collection_name=(name)
|
|
@@ -207,6 +208,8 @@ module Ndb
|
|
|
207
208
|
def ensure_collection!
|
|
208
209
|
return if collection_pattern?
|
|
209
210
|
|
|
211
|
+
Testing.touch(collection_name) if defined?(Testing) && !source
|
|
212
|
+
|
|
210
213
|
client = Rails.client
|
|
211
214
|
|
|
212
215
|
return if @ensured_client.equal?(client)
|
data/lib/ndb/rails/relation.rb
CHANGED
|
@@ -10,6 +10,7 @@ module Ndb
|
|
|
10
10
|
DEFAULT_PER_PAGE = 25
|
|
11
11
|
BATCH_SIZE = 1000
|
|
12
12
|
EMPTY = { where: [].freeze, order: {}.freeze, group: [].freeze }.freeze
|
|
13
|
+
PRELOADED = Data.define(:rows, :next, :total).new(rows: [].freeze, next: nil, total: nil)
|
|
13
14
|
|
|
14
15
|
attr_reader :klass, :values
|
|
15
16
|
|
|
@@ -55,6 +56,12 @@ module Ndb
|
|
|
55
56
|
with(limit: count)
|
|
56
57
|
end
|
|
57
58
|
|
|
59
|
+
# Loads associations with the rows, in the same call (the daemon
|
|
60
|
+
# lookup): includes(:customer, items: :product).
|
|
61
|
+
def includes(*names)
|
|
62
|
+
with(includes: Array(values[:includes]) + names)
|
|
63
|
+
end
|
|
64
|
+
|
|
58
65
|
def offset(count)
|
|
59
66
|
with(offset: count)
|
|
60
67
|
end
|
|
@@ -84,6 +91,7 @@ module Ndb
|
|
|
84
91
|
query["sort"] = values[:order] unless values[:order].empty?
|
|
85
92
|
query.merge!(window)
|
|
86
93
|
query["after"] = values[:after] if values[:after]
|
|
94
|
+
query["lookup"] = klass.lookup_for(values[:includes]) if values[:includes]&.any?
|
|
87
95
|
|
|
88
96
|
query
|
|
89
97
|
end
|
|
@@ -145,12 +153,37 @@ module Ndb
|
|
|
145
153
|
records.length
|
|
146
154
|
end
|
|
147
155
|
|
|
156
|
+
# Like ActiveRecord: the number of loaded records when loaded, else a
|
|
157
|
+
# count in the daemon.
|
|
158
|
+
def size
|
|
159
|
+
loaded? ? @records.length : count
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def empty?
|
|
163
|
+
size.zero?
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Holds records that were read already (an includes): reading the
|
|
167
|
+
# relation does not query; a builder on it (where, order...) does.
|
|
168
|
+
def preload(records)
|
|
169
|
+
@records = records
|
|
170
|
+
@result = PRELOADED
|
|
171
|
+
|
|
172
|
+
self
|
|
173
|
+
end
|
|
174
|
+
|
|
148
175
|
def next_cursor
|
|
149
176
|
load
|
|
150
177
|
|
|
151
178
|
@result.next
|
|
152
179
|
end
|
|
153
180
|
|
|
181
|
+
# The last record (or count of them) of the loaded records, in the
|
|
182
|
+
# order of the relation.
|
|
183
|
+
def last(count = nil)
|
|
184
|
+
count ? records.last(count) : records.last
|
|
185
|
+
end
|
|
186
|
+
|
|
154
187
|
def first(count = nil)
|
|
155
188
|
return count ? records.first(count) : records.first if loaded?
|
|
156
189
|
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# RSpec: in spec/rails_helper.rb (or spec_helper.rb),
|
|
4
|
+
#
|
|
5
|
+
# require "ndb/rails/rspec"
|
|
6
|
+
#
|
|
7
|
+
# Each example truncates the ndb collections it used. `ndb_clean: false`
|
|
8
|
+
# on an example or a group skips it. Under parallel_tests each process gets
|
|
9
|
+
# collections of its own (TEST_ENV_NUMBER).
|
|
10
|
+
|
|
11
|
+
require "ndb/rails"
|
|
12
|
+
require "ndb/rails/testing"
|
|
13
|
+
|
|
14
|
+
Ndb::Testing.worker!(ENV.fetch("TEST_ENV_NUMBER", nil)) if ENV.key?("TEST_ENV_NUMBER")
|
|
15
|
+
|
|
16
|
+
RSpec.configure do |config|
|
|
17
|
+
config.after do |example|
|
|
18
|
+
Ndb::Testing.clean! unless example.metadata[:ndb_clean] == false
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ndb
|
|
4
|
+
# Test support: clean the collections a test used, and give each parallel
|
|
5
|
+
# worker collections of its own. ndb has no transactions, so a test cannot
|
|
6
|
+
# roll back: the collections it touched are truncated after it, which
|
|
7
|
+
# takes a few ms whatever their size and keeps their definition and
|
|
8
|
+
# indexes.
|
|
9
|
+
#
|
|
10
|
+
# require "ndb/rails/minitest" # Minitest (ActiveSupport::TestCase)
|
|
11
|
+
# require "ndb/rails/rspec" # RSpec
|
|
12
|
+
# require "ndb/rails/database_cleaner" # DatabaseCleaner[:ndb]
|
|
13
|
+
#
|
|
14
|
+
# Every clean refuses to run outside the test environment (a safeguard,
|
|
15
|
+
# like DatabaseCleaner's): set NDB_ALLOW_CLEAN=1 to force it.
|
|
16
|
+
module Testing
|
|
17
|
+
# Raised when a clean would run outside the test environment.
|
|
18
|
+
class UnsafeClean < Ndb::Error; end
|
|
19
|
+
|
|
20
|
+
# The Minitest hooks: truncate after each test. ndb/rails/minitest puts
|
|
21
|
+
# them in ActiveSupport::TestCase; include them in another Minitest::Test.
|
|
22
|
+
# `self.ndb_clean = false` turns them off for a test case.
|
|
23
|
+
module Minitest
|
|
24
|
+
def self.included(base)
|
|
25
|
+
base.class_attribute :ndb_clean, default: true if base.respond_to?(:class_attribute)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def after_teardown
|
|
29
|
+
super
|
|
30
|
+
ensure
|
|
31
|
+
Ndb::Testing.clean! if !respond_to?(:ndb_clean) || ndb_clean
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
@touched = Set.new
|
|
36
|
+
@lock = Mutex.new
|
|
37
|
+
|
|
38
|
+
class << self
|
|
39
|
+
# Records that a model used a collection (Ndb::Record calls it).
|
|
40
|
+
def touch(name)
|
|
41
|
+
@lock.synchronize { @touched << name }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def touched
|
|
45
|
+
@lock.synchronize { @touched.to_a }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Truncates the collections the tests used since the last clean (or
|
|
49
|
+
# only these), less except.
|
|
50
|
+
def clean!(only: nil, except: [])
|
|
51
|
+
guard!
|
|
52
|
+
|
|
53
|
+
names = (only || touched).map(&:to_s) - except.map(&:to_s)
|
|
54
|
+
|
|
55
|
+
names.each { |name| truncate(name) }
|
|
56
|
+
|
|
57
|
+
@lock.synchronize { @touched.clear }
|
|
58
|
+
|
|
59
|
+
names
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Truncates every engine collection of the daemon (with the prefix of
|
|
63
|
+
# this worker, when there is one), less except: for the start of a run.
|
|
64
|
+
def clean_all!(except: [])
|
|
65
|
+
guard!
|
|
66
|
+
|
|
67
|
+
prefix = Rails.configuration.collection_prefix.to_s
|
|
68
|
+
|
|
69
|
+
names = Rails.client.collections.filter_map do |definition|
|
|
70
|
+
name = definition["name"]
|
|
71
|
+
|
|
72
|
+
next if definition["write"] == "direct" || definition["partition_of"]
|
|
73
|
+
next unless name.start_with?(prefix)
|
|
74
|
+
|
|
75
|
+
name
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
clean!(only: names, except: except)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Gives a parallel worker collections of its own: "test<n>_calls".
|
|
82
|
+
# Rails parallelize and parallel_tests (TEST_ENV_NUMBER) call it.
|
|
83
|
+
def worker!(number)
|
|
84
|
+
Rails.configuration.collection_prefix = "test#{number.to_s.empty? ? 1 : number}_"
|
|
85
|
+
Rails.reset_client!
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def guard!
|
|
89
|
+
return if ENV["NDB_ALLOW_CLEAN"] == "1"
|
|
90
|
+
return if defined?(::Rails) && ::Rails.respond_to?(:env) && ::Rails.env.test?
|
|
91
|
+
return if ENV["RAILS_ENV"] == "test" || ENV["RACK_ENV"] == "test"
|
|
92
|
+
|
|
93
|
+
raise UnsafeClean, "ndb test clean refused: this is not the test environment (set NDB_ALLOW_CLEAN=1 to force it)"
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
private
|
|
97
|
+
|
|
98
|
+
def truncate(name)
|
|
99
|
+
Rails.client.truncate(name)
|
|
100
|
+
rescue Ndb::UnknownCollection, Ndb::ReadOnlyCollection
|
|
101
|
+
nil
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
data/lib/ndb/rails/version.rb
CHANGED
data/lib/ndb/rails.rb
CHANGED
|
@@ -11,6 +11,7 @@ require "ndb/rails/dynamic_finders"
|
|
|
11
11
|
require "ndb/rails/relation"
|
|
12
12
|
require "ndb/rails/blob"
|
|
13
13
|
require "ndb/rails/persistence"
|
|
14
|
+
require "ndb/rails/associations"
|
|
14
15
|
require "ndb/rails/record"
|
|
15
16
|
|
|
16
17
|
module Ndb
|
|
@@ -26,6 +27,12 @@ module Ndb
|
|
|
26
27
|
@client = nil
|
|
27
28
|
end
|
|
28
29
|
|
|
30
|
+
# A new client on next use (after a configuration change); models
|
|
31
|
+
# declare their collections again.
|
|
32
|
+
def reset_client!
|
|
33
|
+
@client = nil
|
|
34
|
+
end
|
|
35
|
+
|
|
29
36
|
def client
|
|
30
37
|
@client ||= Client.new(
|
|
31
38
|
url: configuration.url,
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ndb-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- thadeu
|
|
@@ -62,14 +62,19 @@ files:
|
|
|
62
62
|
- README.md
|
|
63
63
|
- lib/ndb-rails.rb
|
|
64
64
|
- lib/ndb/rails.rb
|
|
65
|
+
- lib/ndb/rails/associations.rb
|
|
65
66
|
- lib/ndb/rails/blob.rb
|
|
66
67
|
- lib/ndb/rails/configuration.rb
|
|
68
|
+
- lib/ndb/rails/database_cleaner.rb
|
|
67
69
|
- lib/ndb/rails/dynamic_finders.rb
|
|
68
70
|
- lib/ndb/rails/errors.rb
|
|
71
|
+
- lib/ndb/rails/minitest.rb
|
|
69
72
|
- lib/ndb/rails/persistence.rb
|
|
70
73
|
- lib/ndb/rails/railtie.rb
|
|
71
74
|
- lib/ndb/rails/record.rb
|
|
72
75
|
- lib/ndb/rails/relation.rb
|
|
76
|
+
- lib/ndb/rails/rspec.rb
|
|
77
|
+
- lib/ndb/rails/testing.rb
|
|
73
78
|
- lib/ndb/rails/version.rb
|
|
74
79
|
homepage: https://github.com/thadeu/ndb
|
|
75
80
|
licenses:
|