github-ds 0.2.10 → 0.2.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 58267b1fe40225f667e6639dd0df6830b9397853
4
- data.tar.gz: 641de968bea62624f96c2dce9233d01e970050b5
2
+ SHA256:
3
+ metadata.gz: e66116af0b535ccdf52ef101f485bd38d4bbbc61de8ebee968576006b14fa24d
4
+ data.tar.gz: 037a17c54393e7a3b276393c521a40a39b891c859b214f440a9a54a0945a6881
5
5
  SHA512:
6
- metadata.gz: cd9e18016d02099599946110ae6814e78fc12d82de1d1bf964ad4a9e1fe60d7dd213a20f1867d8c6fe563cdd3731ab6a940db89dbcd8bbe2ac2548fa435e8839
7
- data.tar.gz: 6768c6a9631f0f7520370bd879da57d23d3d1c830e0d07349065f12b88896f5e8671aca9be43d1d8cb26a5175cf8394481499f789f6725e35ef8a5a7e6ee3e88
6
+ metadata.gz: 4d1f7260b82336ccf53659df796a3b7f013ae39fbcb6f766590f6f853b1e0295679a29c00db35d7919925ccf3953b54fa25d0be5e4c784c4297260c5fec3bd31
7
+ data.tar.gz: d297712f24a6169185f6a55fa29548e7b417842e4d9919fc23fcaeed0cf0cf72135d3ed225d122f70733c020e9f4a5dec925b995165a1f94737f4dda9642bc8f
@@ -1,5 +1,5 @@
1
1
  module GitHub
2
2
  module DS
3
- VERSION = "0.2.10"
3
+ VERSION = "0.2.11"
4
4
  end
5
5
  end
data/lib/github/kv.rb CHANGED
@@ -51,8 +51,24 @@ module GitHub
51
51
 
52
52
  class MissingConnectionError < StandardError; end
53
53
 
54
- def initialize(encapsulated_errors = [SystemCallError], &conn_block)
54
+ attr_accessor :use_local_time
55
+
56
+ # initialize :: [Exception], Boolean, Proc -> nil
57
+ #
58
+ # Initialize a new KV instance.
59
+ #
60
+ # encapsulated_errors - An Array of Exception subclasses that, when raised,
61
+ # will be replaced with UnavailableError.
62
+ # use_local_time: - Whether to use Ruby's `Time.now` instaed of MySQL's
63
+ # `NOW()` function. This is mostly useful in testing
64
+ # where time needs to be modified (eg. Timecop).
65
+ # Default false.
66
+ # &conn_block - A block to call to open a new database connection.
67
+ #
68
+ # Returns nothing.
69
+ def initialize(encapsulated_errors = [SystemCallError], use_local_time: false, &conn_block)
55
70
  @encapsulated_errors = encapsulated_errors
71
+ @use_local_time = use_local_time
56
72
  @conn_block = conn_block
57
73
  end
58
74
 
@@ -93,8 +109,8 @@ module GitHub
93
109
  validate_key_array(keys)
94
110
 
95
111
  Result.new {
96
- kvs = GitHub::SQL.results(<<-SQL, :keys => keys, :connection => connection).to_h
97
- SELECT `key`, value FROM key_values WHERE `key` IN :keys AND (`expires_at` IS NULL OR `expires_at` > NOW())
112
+ kvs = GitHub::SQL.results(<<-SQL, :keys => keys, :now => now, :connection => connection).to_h
113
+ SELECT `key`, value FROM key_values WHERE `key` IN :keys AND (`expires_at` IS NULL OR `expires_at` > :now)
98
114
  SQL
99
115
 
100
116
  keys.map { |key| kvs[key] }
@@ -137,7 +153,7 @@ module GitHub
137
153
 
138
154
  rows = kvs.map { |key, value|
139
155
  value = value.is_a?(GitHub::SQL::Literal) ? value : GitHub::SQL::BINARY(value)
140
- [key, value, GitHub::SQL::NOW, GitHub::SQL::NOW, expires || GitHub::SQL::NULL]
156
+ [key, value, now, now, expires || GitHub::SQL::NULL]
141
157
  }
142
158
 
143
159
  encapsulate_error do
@@ -186,8 +202,8 @@ module GitHub
186
202
  validate_key_array(keys)
187
203
 
188
204
  Result.new {
189
- existing_keys = GitHub::SQL.values(<<-SQL, :keys => keys, :connection => connection).to_set
190
- SELECT `key` FROM key_values WHERE `key` IN :keys AND (`expires_at` IS NULL OR `expires_at` > NOW())
205
+ existing_keys = GitHub::SQL.values(<<-SQL, :keys => keys, :now => now, :connection => connection).to_set
206
+ SELECT `key` FROM key_values WHERE `key` IN :keys AND (`expires_at` IS NULL OR `expires_at` > :now)
191
207
  SQL
192
208
 
193
209
  keys.map { |key| existing_keys.include?(key) }
@@ -222,14 +238,14 @@ module GitHub
222
238
  # achieve the same thing with the right INSERT ... ON DUPLICATE KEY UPDATE
223
239
  # query, but then we would not be able to rely on affected_rows
224
240
 
225
- GitHub::SQL.run(<<-SQL, :key => key, :connection => connection)
226
- DELETE FROM key_values WHERE `key` = :key AND expires_at <= NOW()
241
+ GitHub::SQL.run(<<-SQL, :key => key, :now => now, :connection => connection)
242
+ DELETE FROM key_values WHERE `key` = :key AND expires_at <= :now
227
243
  SQL
228
244
 
229
245
  value = value.is_a?(GitHub::SQL::Literal) ? value : GitHub::SQL::BINARY(value)
230
- sql = GitHub::SQL.run(<<-SQL, :key => key, :value => value, :expires => expires || GitHub::SQL::NULL, :connection => connection)
246
+ sql = GitHub::SQL.run(<<-SQL, :key => key, :value => value, :now => now, :expires => expires || GitHub::SQL::NULL, :connection => connection)
231
247
  INSERT IGNORE INTO key_values (`key`, value, created_at, updated_at, expires_at)
232
- VALUES (:key, :value, NOW(), NOW(), :expires)
248
+ VALUES (:key, :value, :now, :now, :expires)
233
249
  SQL
234
250
 
235
251
  sql.affected_rows > 0
@@ -288,14 +304,18 @@ module GitHub
288
304
  validate_key(key)
289
305
 
290
306
  Result.new {
291
- GitHub::SQL.value(<<-SQL, :key => key, :connection => connection)
307
+ GitHub::SQL.value(<<-SQL, :key => key, :now => now, :connection => connection)
292
308
  SELECT expires_at FROM key_values
293
- WHERE `key` = :key AND (expires_at IS NULL OR expires_at > NOW())
309
+ WHERE `key` = :key AND (expires_at IS NULL OR expires_at > :now)
294
310
  SQL
295
311
  }
296
312
  end
297
313
 
298
314
  private
315
+ def now
316
+ use_local_time ? Time.now : GitHub::SQL::NOW
317
+ end
318
+
299
319
  def validate_key(key, error_message: nil)
300
320
  unless key.is_a?(String)
301
321
  raise TypeError, error_message || "key must be a String in #{self.class.name}, but was #{key.class}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: github-ds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitHub Open Source
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2019-05-17 00:00:00.000000000 Z
12
+ date: 2019-07-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -185,8 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
185
185
  - !ruby/object:Gem::Version
186
186
  version: '0'
187
187
  requirements: []
188
- rubyforge_project:
189
- rubygems_version: 2.4.5
188
+ rubygems_version: 3.0.3
190
189
  signing_key:
191
190
  specification_version: 4
192
191
  summary: A collection of libraries for working with SQL on top of ActiveRecord's connection.