nobrainer 0.32.0 → 0.33.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: 28eb4dcb91f5c381ec20a21ed37d01992a3d6eb7
4
- data.tar.gz: 395bfd26595f42e290805ebb9ebfa82dda89b99a
3
+ metadata.gz: df0adf36ad09c91dff1d954dd2f82ed93a26be28
4
+ data.tar.gz: 8c7f6c102a9be2b93a8086ac1d2969e3800ac2c3
5
5
  SHA512:
6
- metadata.gz: 390139f43cf843350f0c345f42b15dfef87174961123376643d193dfd0265975daf4c2b3979f6497b605d4ed08175570df76e96e907a01ab3ed8168e71d27253
7
- data.tar.gz: 5fb98fd5f37020b9bda60837a0e86d1f761953a5b0c0220a1e22e79d2f823419a39a927e1c6592791bd6e90111c1cc9bf1f33c48137a01cd6d33e40fb545792a
6
+ metadata.gz: 9fac2dbf39646bf1cee3a0921101a41453cdf82577733347d327026e0c05306faaf10622d281de9f0cb4c41d5f94f3b527135e7136ef8edd0cc6bfd25d5ac884
7
+ data.tar.gz: 825bfeb592461326469dd3fdef0f74ac0d0877297dbbf0cdd291f6b0721144de9df10661dad60427be7be693606234af843f4bdab99f6d361669562344c7d998
@@ -10,9 +10,10 @@ module NoBrainer::Config
10
10
  :logger => { :default => ->{ default_logger } },
11
11
  :colorize_logger => { :default => ->{ true }, :valid_values => [true, false] },
12
12
  :warn_on_active_record => { :default => ->{ true }, :valid_values => [true, false] },
13
- :durability => { :default => ->{ default_durability }, :valid_values => [:hard, :soft] },
13
+ :durability => { :default => ->{ nil } }, # legacy
14
14
  :table_options => { :default => ->{ {:shards => 1, :replicas => 1, :write_acks => :majority} },
15
15
  :valid_keys => [:shards, :replicas, :primary_replica_tag, :write_acks, :durability] },
16
+ :run_options => { :default => ->{ {:durability => default_durability} } },
16
17
  :max_string_length => { :default => ->{ 255 } },
17
18
  :user_timezone => { :default => ->{ :local }, :valid_values => [:unchanged, :utc, :local] },
18
19
  :db_timezone => { :default => ->{ :utc }, :valid_values => [:unchanged, :utc, :local] },
@@ -51,6 +52,10 @@ module NoBrainer::Config
51
52
  @geo_options = value.try(:symbolize_keys)
52
53
  end
53
54
 
55
+ def run_options=(value)
56
+ @run_options = value.try(:symbolize_keys)
57
+ end
58
+
54
59
  def assert_valid_options
55
60
  SETTINGS.each do |k,v|
56
61
  assert_value_in(k, v[:valid_values]) if v[:valid_values]
@@ -119,9 +124,10 @@ module NoBrainer::Config
119
124
  db ||= "#{self.app_name}_#{self.environment}" if self.app_name && self.environment
120
125
  host = ENV['RETHINKDB_HOST'] || ENV['RDB_HOST'] || 'localhost'
121
126
  port = ENV['RETHINKDB_PORT'] || ENV['RDB_PORT']
122
- auth = ENV['RETHINKDB_AUTH'] || ENV['RDB_AUTH']
127
+ user = ENV['RETHINKDB_USER'] || ENV['RDB_USER']
128
+ pass = ENV['RETHINKDB_PASSWORD'] || ENV['RDB_PASSWORD'] || ENV['RETHINKDB_AUTH'] || ENV['RDB_AUTH']
123
129
  url = ENV['RETHINKDB_URL'] || ENV['RDB_URL']
124
- url ||= "rethinkdb://#{":#{auth}@" if auth}#{host}#{":#{port}" if port}/#{db}" if db
130
+ url ||= "rethinkdb://#{"#{user}:#{pass}@" if (user || pass)}#{host}#{":#{port}" if port}/#{db}" if db
125
131
  url
126
132
  end
127
133
 
@@ -133,7 +139,8 @@ module NoBrainer::Config
133
139
  end
134
140
 
135
141
  def default_logger
136
- defined?(Rails.logger) ? Rails.logger : Logger.new(STDERR).tap { |l| l.level = Logger::WARN }
142
+ (Rails.logger if defined?(Rails.logger)) ||
143
+ Logger.new(STDERR).tap { |l| l.level = Logger::WARN }
137
144
  end
138
145
 
139
146
  def default_durability
@@ -18,7 +18,9 @@ class NoBrainer::Connection
18
18
  "Invalid URI. Expecting something like rethinkdb://host:port/database. Got #{uri}"
19
19
  end
20
20
 
21
- { :auth_key => uri.password,
21
+ {
22
+ :user => uri.user && URI.decode(uri.user),
23
+ :password => uri.password && URI.decode(uri.password),
22
24
  :host => uri.host,
23
25
  :port => uri.port || 28015,
24
26
  :db => uri.path.gsub(/^\//, ''),
@@ -27,7 +29,7 @@ class NoBrainer::Connection
27
29
  end
28
30
 
29
31
  def uri
30
- "rethinkdb://#{'****@' if parsed_uri[:auth_key]}#{parsed_uri[:host]}:#{parsed_uri[:port]}/#{parsed_uri[:db]}"
32
+ "rethinkdb://#{'****@' if parsed_uri[:password]}#{parsed_uri[:host]}:#{parsed_uri[:port]}/#{parsed_uri[:db]}"
31
33
  end
32
34
 
33
35
  def raw
@@ -45,6 +47,6 @@ class NoBrainer::Connection
45
47
  end
46
48
 
47
49
  def current_db
48
- NoBrainer.current_run_options.try(:[], :db) || default_db
50
+ NoBrainer.current_run_options[:db] || default_db
49
51
  end
50
52
  end
@@ -16,6 +16,6 @@ module NoBrainer::Criteria::Update
16
16
  # can't use without_distinct when passed a block as the operation may be
17
17
  # performed many times, which might not be idempotent.
18
18
  clause = block ? self : without_distinct
19
- run { clause.without_plucking.to_rql.__send__(type, *args, &block) }
19
+ run { clause.without_plucking.without_ordering.to_rql.__send__(type, *args, &block) }
20
20
  end
21
21
  end
@@ -35,7 +35,7 @@ class NoBrainer::Document::Association::BelongsTo
35
35
  end
36
36
 
37
37
  def base_criteria
38
- target_model.without_ordering.unscoped
38
+ target_model.without_ordering
39
39
  end
40
40
 
41
41
  def hook
@@ -97,13 +97,16 @@ class NoBrainer::Document::Association::HasMany
97
97
  ->(target){ set_inverses_of([target]) if target.is_a?(NoBrainer::Document) }
98
98
  end
99
99
 
100
+ def dependent_criteria
101
+ target_criteria.unscoped
102
+ end
103
+
100
104
  def before_destroy_callback
101
- criteria = target_criteria.unscoped.without_cache
102
105
  case metadata.options[:dependent]
103
- when :destroy then criteria.destroy_all
104
- when :delete then criteria.delete_all
105
- when :nullify then criteria.update_all(foreign_key => nil)
106
- when :restrict then raise NoBrainer::Error::ChildrenExist unless criteria.count.zero?
106
+ when :destroy then dependent_criteria.destroy_all
107
+ when :delete then dependent_criteria.delete_all
108
+ when :nullify then dependent_criteria.update_all(foreign_key => nil)
109
+ when :restrict then raise NoBrainer::Error::ChildrenExist unless dependent_criteria.empty?
107
110
  end
108
111
  end
109
112
  end
@@ -70,6 +70,7 @@ module NoBrainer::Document::Attributes
70
70
  end
71
71
 
72
72
  def assign_attributes(attrs, options={})
73
+ attrs = attrs.to_h if !attrs.is_a?(Hash) && attrs.respond_to?(:to_h)
73
74
  raise ArgumentError, "To assign attributes, please pass a hash instead of `#{attrs.class}'" unless attrs.is_a?(Hash)
74
75
 
75
76
  if options[:pristine]
@@ -10,7 +10,7 @@ class NoBrainer::Document::Index::MetaStore
10
10
 
11
11
  field :table_name, :type => String, :required => true
12
12
  field :index_name, :type => String, :required => true
13
- field :rql_function, :type => String, :required => true
13
+ field :rql_function, :type => Text, :required => true
14
14
 
15
15
  def rql_function=(value)
16
16
  super(JSON.dump(value))
@@ -1 +1 @@
1
- # Look in lib/no_brainer/geo.rb instead
1
+ # Look in lib/no_brainer/geo/*.rb instead
@@ -8,7 +8,7 @@ class NoBrainer::Lock
8
8
  # Since PKs are limited to 127 characters, we can't use the user's key as a PK
9
9
  # as it could be arbitrarily long.
10
10
  field :key_hash, :type => String, :primary_key => true, :default => ->{ Digest::SHA1.base64digest(key.to_s) }
11
- field :key, :type => String
11
+ field :key, :type => Text
12
12
  field :instance_token, :type => String, :default => ->{ get_new_instance_token }
13
13
  field :expires_at, :type => Time
14
14
 
@@ -27,6 +27,7 @@ class NoBrainer::Lock
27
27
  @default_options = options.slice(:expire, :timeout)
28
28
  options.delete(:expire); options.delete(:timeout);
29
29
 
30
+ key = key.to_s if key.is_a?(Symbol)
30
31
  super(options.merge(:key => key))
31
32
  raise ArgumentError unless valid?
32
33
  end
@@ -16,7 +16,8 @@ class NoBrainer::QueryRunner::Reconnect < NoBrainer::QueryRunner::Middleware
16
16
  when RethinkDB::RqlError
17
17
  e.message =~ /lost contact/ ||
18
18
  e.message =~ /(P|p)rimary .* not available/||
19
- e.message =~ /Connection.*closed/
19
+ e.message =~ /Connection.*closed/ ||
20
+ e.message =~ /Connection.*refused/
20
21
  else
21
22
  false
22
23
  end
@@ -10,7 +10,10 @@ class NoBrainer::QueryRunner::RunOptions < NoBrainer::QueryRunner::Middleware
10
10
  end
11
11
 
12
12
  def self.current_run_options
13
- Thread.current[:nobrainer_run_with] || {}
13
+ options = NoBrainer::Config.run_options
14
+ options = options.merge(:durability => NoBrainer::Config.durability) if NoBrainer::Config.durability
15
+ options = options.merge(Thread.current[:nobrainer_run_with]) if Thread.current[:nobrainer_run_with]
16
+ options
14
17
  end
15
18
 
16
19
  def self.run_with(options={}, &block)
@@ -29,17 +32,9 @@ class NoBrainer::QueryRunner::RunOptions < NoBrainer::QueryRunner::Middleware
29
32
  end
30
33
 
31
34
  def call(env)
32
- options = env[:options].symbolize_keys
33
- options = self.class.current_run_options.merge(options)
34
-
35
- if NoBrainer::Config.durability.to_s != 'hard'
36
- options[:durability] ||= NoBrainer::Config.durability
37
- end
38
-
39
- options[:db] = options[:db].to_s if options[:db]
40
- if options[:db].blank? || options[:db] == NoBrainer.default_db
41
- options.delete(:db)
42
- end
35
+ options = self.class.current_run_options
36
+ options = options.merge(env[:options].symbolize_keys)
37
+ options = prune_default_run_options(options)
43
38
 
44
39
  env[:criteria] = options.delete(:criteria)
45
40
 
@@ -52,4 +47,14 @@ class NoBrainer::QueryRunner::RunOptions < NoBrainer::QueryRunner::Middleware
52
47
  env[:options] = options
53
48
  @runner.call(env)
54
49
  end
50
+
51
+ def prune_default_run_options(options)
52
+ options = options.dup
53
+ options.delete(:durability) if options[:durability].to_s == 'hard'
54
+
55
+ options[:db] = options[:db].to_s if options[:db]
56
+ options.delete(:db) if options[:db].blank? || options[:db] == NoBrainer.default_db
57
+
58
+ options
59
+ end
55
60
  end
@@ -26,8 +26,10 @@ class NoBrainer::Railtie < Rails::Railtie
26
26
  config.after_initialize do
27
27
  NoBrainer::Config.configure unless NoBrainer::Config.configured?
28
28
 
29
- (NoBrainer.rails5? ? ActiveSupport::Reloader : ActionDispatch::Reloader).to_prepare do
30
- NoBrainer::Loader.cleanup
29
+ if NoBrainer.rails5?
30
+ ActiveSupport::Reloader.before_class_unload { NoBrainer::Loader.cleanup }
31
+ else
32
+ ActionDispatch::Reloader.to_prepare { NoBrainer::Loader.cleanup }
31
33
  end
32
34
  end
33
35
 
@@ -34,7 +34,9 @@ module NoBrainer::Generators
34
34
  end
35
35
 
36
36
  def remove_active_record
37
- (Dir['config/environments/*'] + ['config/application.rb']).each do |config_file|
37
+ (Dir['config/environments/*'] +
38
+ Dir['config/initializers/*'] +
39
+ ['config/application.rb']).each do |config_file|
38
40
  comment_lines(config_file, /active_record/)
39
41
  end
40
42
 
@@ -13,9 +13,8 @@ NoBrainer.configure do |config|
13
13
  # The default is to use localhost, with a database name matching the
14
14
  # application name and the environment.
15
15
  # NoBrainer also reads environment variables when defined:
16
- # * RETHINKDB_URL, RDB_URL
17
- # * RETHINKDB_HOST, RETHINKDB_PORT, RETHINKDB_DB, RETHINKDB_AUTH
18
- # * RDB_HOST, RDB_PORT, RDB_DB, RDB_AUTH
16
+ # * RDB_URL, RDB_USER, RDB_PASSWORD, RDB_HOST, RDB_PORT, RDB_DB
17
+ # * All the above, but with RETHINKDB instead of RDB
19
18
  # config.rethinkdb_urls = [config.default_rethinkdb_url]
20
19
 
21
20
  # ssl_options may be set to {:ca_certs => '/path/to/ca.crt'} to establish
@@ -43,9 +42,10 @@ NoBrainer.configure do |config|
43
42
  # You can turn off the warning if you want to use both.
44
43
  # config.warn_on_active_record = true
45
44
 
46
- # Configures the durability for database writes.
47
- # The default is :soft for development or test environment, otherwise :hard.
48
- # config.durability = config.default_durability
45
+ # Configures the run options passed to r.run() when executing queries.
46
+ # The options are listed in the RethinkDB run() documentation.
47
+ # The default durability is :soft for development or test environments, otherwise :hard.
48
+ # config.run_options = { :durability => config.default_durability }
49
49
 
50
50
  # Configures the default table configuration options. These values are
51
51
  # reflected to the database when running `rake nobrainer:sync_schema'.
@@ -56,17 +56,17 @@ NoBrainer.configure do |config|
56
56
  # config.max_string_length = 255
57
57
 
58
58
  # user_timezone can be configured with :utc, :local, or :unchanged.
59
- # When reading an attribute from a model which type is Time, the timezone
60
- # of that time is translated according to this setting.
59
+ # When reading a Time attribute from a model, the timezone of that attribute
60
+ # is set according to the following setting.
61
61
  # config.user_timezone = :local
62
62
 
63
63
  # db_timezone can be configured with :utc, :local, or :unchanged.
64
- # When writting to the database, the timezone of Time attributes are
65
- # translated according to this setting.
64
+ # When writing a Time attribute into the database, the timezone of that
65
+ # attribute is set according to the following setting.
66
66
  # config.db_timezone = :utc
67
67
 
68
68
  # Default options used when compiling geo queries.
69
- # config.geo_options => { :geo_system => 'WGS84', :unit => 'm' }
69
+ # config.geo_options = { :geo_system => 'WGS84', :unit => 'm' }
70
70
 
71
71
  # Configures which mechanism to use in order to perform non-racy uniqueness
72
72
  # validations. More about this behavior in the Distributed Locks section.
@@ -90,7 +90,7 @@ NoBrainer.configure do |config|
90
90
  # generated without conflicts.
91
91
  # config.machine_id = config.default_machine_id
92
92
 
93
- # Criteria cache elements. For example, the result of a has_many association
93
+ # Criteria cache documents. For example, the result of a has_many association
94
94
  # is cached. The per criteria cache is disabled if it grows too big to avoid
95
95
  # out of memory issues.
96
96
  # config.criteria_cache_max_entries = 10_000
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nobrainer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.32.0
4
+ version: 0.33.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Viennot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-05 00:00:00.000000000 Z
11
+ date: 2016-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rethinkdb
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 2.1.0
19
+ version: 2.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
- version: 2.1.0
26
+ version: 2.3.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement