github-ds 0.3.0 → 0.4.0

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
2
  SHA256:
3
- metadata.gz: 19e5298765d4c64c5b96a860463b3ffbc804b71388cc328a08c39acfbdce5b4b
4
- data.tar.gz: 42e94eb85be200d4f9183f406f9c0de52f4d5fd00fa007f82f50f159816b4152
3
+ metadata.gz: 235c233b2750caaa258c1fc172f6b2b14d117d533f247fbce0cb34ca83779b96
4
+ data.tar.gz: 88caf6d96cce86f1ad841e22397e2f21ed63982a00fc3469009ed230126001a1
5
5
  SHA512:
6
- metadata.gz: 0d0ace44595e6add1f4570b7d22dc9fe01e56172f8f3b63584d558353a1f25afec0ad744f788271b43b0d0e3b8c609d8d720cef5ee12072ab8843dcb70f52b85
7
- data.tar.gz: a9c3918434873b9bae164ecf5042cd87154965380d95f476a2ad6b793b8d8200fd529b1d9df0ea8552734379a12ed948844675e0c9e24ea0d2bb814c871948d3
6
+ metadata.gz: 47af7f674f862f846be53c0546cbb76ec38f9d96c8043b0107a7406a5bbf8bdfa7773fcbdf0071165d18105327471f7b06ee41c7e46f3464ea1c621d0f9dac0e
7
+ data.tar.gz: 5a73591ab5bd465376ec8d2e849aaf1fd10f269121eb50bc1428f1f7c42c91d9a4305299f70b21df1aecb9757c16de516ceab7594ba8f290d5e37d491615986b
data/README.md CHANGED
@@ -37,6 +37,14 @@ rails generate github:ds:active_record
37
37
  rails db:migrate
38
38
  ```
39
39
 
40
+ If you need to change the name of the table used for storing the key-values, you can configure your table name as such, before running the migration:
41
+
42
+ ```
43
+ GitHub::KV.configure do |config|
44
+ config.table_name = "new_key_values_table"
45
+ end
46
+ ```
47
+
40
48
  Once you have created and executed the migration, KV can do neat things like this:
41
49
 
42
50
  ```ruby
@@ -26,6 +26,14 @@ module Github
26
26
  def migration_version
27
27
  self.class.migration_version
28
28
  end
29
+
30
+ def self.table_name
31
+ ":#{GitHub::KV.config.table_name}"
32
+ end
33
+
34
+ def table_name
35
+ self.class.table_name
36
+ end
29
37
  end
30
38
  end
31
39
  end
@@ -1,19 +1,19 @@
1
1
  class CreateKeyValuesTable < ActiveRecord::Migration<%= migration_version %>
2
2
  def self.up
3
- create_table :key_values do |t|
3
+ create_table <%= table_name %> do |t|
4
4
  t.string :key, :null => false
5
5
  t.binary :value, :null => false
6
6
  t.datetime :expires_at, :null => true
7
7
  t.timestamps :null => false
8
8
  end
9
9
 
10
- add_index :key_values, :key, :unique => true
11
- add_index :key_values, :expires_at
10
+ add_index <%= table_name %>, :key, :unique => true
11
+ add_index <%= table_name %>, :expires_at
12
12
 
13
- change_column :key_values, :id, "bigint(20) NOT NULL AUTO_INCREMENT"
13
+ change_column <%= table_name %>, :id, "bigint(20) NOT NULL AUTO_INCREMENT"
14
14
  end
15
15
 
16
16
  def self.down
17
- drop_table :key_values
17
+ drop_table <%= table_name %>
18
18
  end
19
19
  end
@@ -1,5 +1,5 @@
1
1
  module GitHub
2
2
  module DS
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
data/lib/github/kv.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require_relative "kv/config"
1
2
  require_relative "result"
2
3
  require_relative "sql"
3
4
 
@@ -53,6 +54,19 @@ module GitHub
53
54
  class MissingConnectionError < StandardError; end
54
55
 
55
56
  attr_accessor :use_local_time
57
+ attr_writer :config
58
+
59
+ def self.config
60
+ @config ||= Config.new
61
+ end
62
+
63
+ def self.reset
64
+ @config = Config.new
65
+ end
66
+
67
+ def self.configure
68
+ yield(config)
69
+ end
56
70
 
57
71
  # initialize :: [Exception], Boolean, Proc -> nil
58
72
  #
@@ -67,9 +81,10 @@ module GitHub
67
81
  # &conn_block - A block to call to open a new database connection.
68
82
  #
69
83
  # Returns nothing.
70
- def initialize(encapsulated_errors = [SystemCallError], use_local_time: false, &conn_block)
71
- @encapsulated_errors = encapsulated_errors
72
- @use_local_time = use_local_time
84
+ def initialize(config: GitHub::KV.config, &conn_block)
85
+ @encapsulated_errors = config.encapsulated_errors
86
+ @use_local_time = config.use_local_time
87
+ @table_name = config.table_name
73
88
  @conn_block = conn_block
74
89
  end
75
90
 
@@ -111,7 +126,7 @@ module GitHub
111
126
 
112
127
  Result.new {
113
128
  kvs = GitHub::SQL.results(<<-SQL, :keys => keys, :now => now, :connection => connection).to_h
114
- SELECT `key`, value FROM key_values WHERE `key` IN :keys AND (`expires_at` IS NULL OR `expires_at` > :now)
129
+ SELECT `key`, value FROM #{@table_name} WHERE `key` IN :keys AND (`expires_at` IS NULL OR `expires_at` > :now)
115
130
  SQL
116
131
 
117
132
  keys.map { |key| kvs[key] }
@@ -159,7 +174,7 @@ module GitHub
159
174
 
160
175
  encapsulate_error do
161
176
  GitHub::SQL.run(<<-SQL, :rows => GitHub::SQL::ROWS(rows), :connection => connection)
162
- INSERT INTO key_values (`key`, value, created_at, updated_at, expires_at)
177
+ INSERT INTO #{@table_name} (`key`, value, created_at, updated_at, expires_at)
163
178
  VALUES :rows
164
179
  ON DUPLICATE KEY UPDATE
165
180
  value = VALUES(value),
@@ -204,7 +219,7 @@ module GitHub
204
219
 
205
220
  Result.new {
206
221
  existing_keys = GitHub::SQL.values(<<-SQL, :keys => keys, :now => now, :connection => connection).to_set
207
- SELECT `key` FROM key_values WHERE `key` IN :keys AND (`expires_at` IS NULL OR `expires_at` > :now)
222
+ SELECT `key` FROM #{@table_name} WHERE `key` IN :keys AND (`expires_at` IS NULL OR `expires_at` > :now)
208
223
  SQL
209
224
 
210
225
  keys.map { |key| existing_keys.include?(key) }
@@ -240,12 +255,12 @@ module GitHub
240
255
  # query, but then we would not be able to rely on affected_rows
241
256
 
242
257
  GitHub::SQL.run(<<-SQL, :key => key, :now => now, :connection => connection)
243
- DELETE FROM key_values WHERE `key` = :key AND expires_at <= :now
258
+ DELETE FROM #{@table_name} WHERE `key` = :key AND expires_at <= :now
244
259
  SQL
245
260
 
246
261
  value = value.is_a?(GitHub::SQL::Literal) ? value : GitHub::SQL::BINARY(value)
247
262
  sql = GitHub::SQL.run(<<-SQL, :key => key, :value => value, :now => now, :expires => expires || GitHub::SQL::NULL, :connection => connection)
248
- INSERT IGNORE INTO key_values (`key`, value, created_at, updated_at, expires_at)
263
+ INSERT IGNORE INTO #{@table_name} (`key`, value, created_at, updated_at, expires_at)
249
264
  VALUES (:key, :value, :now, :now, :expires)
250
265
  SQL
251
266
 
@@ -293,7 +308,7 @@ module GitHub
293
308
  # same and we raise an error.
294
309
  encapsulate_error {
295
310
  sql = GitHub::SQL.run(<<-SQL, key: key, amount: amount, now: now, expires: expires, touch: !touch_on_insert, connection: connection)
296
- INSERT INTO key_values (`key`, `value`, `created_at`, `updated_at`, `expires_at`)
311
+ INSERT INTO #{@table_name} (`key`, `value`, `created_at`, `updated_at`, `expires_at`)
297
312
  VALUES(:key, :amount, :now, :now, :expires)
298
313
  ON DUPLICATE KEY UPDATE
299
314
  `value`=IF(
@@ -378,7 +393,7 @@ module GitHub
378
393
 
379
394
  encapsulate_error do
380
395
  GitHub::SQL.run(<<-SQL, :keys => keys, :connection => connection)
381
- DELETE FROM key_values WHERE `key` IN :keys
396
+ DELETE FROM #{@table_name} WHERE `key` IN :keys
382
397
  SQL
383
398
  end
384
399
 
@@ -402,7 +417,7 @@ module GitHub
402
417
 
403
418
  Result.new {
404
419
  GitHub::SQL.value(<<-SQL, :key => key, :now => now, :connection => connection)
405
- SELECT expires_at FROM key_values
420
+ SELECT expires_at FROM #{@table_name}
406
421
  WHERE `key` = :key AND (expires_at IS NULL OR expires_at > :now)
407
422
  SQL
408
423
  }
@@ -422,7 +437,7 @@ module GitHub
422
437
 
423
438
  Result.new {
424
439
  kvs = GitHub::SQL.results(<<-SQL, :keys => keys, :now => now, :connection => connection).to_h
425
- SELECT `key`, expires_at FROM key_values
440
+ SELECT `key`, expires_at FROM #{@table_name}
426
441
  WHERE `key` in :keys AND (expires_at IS NULL OR expires_at > :now)
427
442
  SQL
428
443
 
@@ -0,0 +1,13 @@
1
+ module GitHub
2
+ class KV
3
+ class Config
4
+ attr_accessor :table_name, :encapsulated_errors, :use_local_time
5
+
6
+ def initialize
7
+ @table_name = 'key_values'
8
+ @encapsulated_errors = [SystemCallError]
9
+ @use_local_time = false
10
+ end
11
+ end
12
+ end
13
+ end
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.3.0
4
+ version: 0.4.0
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-08-26 00:00:00.000000000 Z
12
+ date: 2019-12-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -183,6 +183,7 @@ files:
183
183
  - lib/github/ds.rb
184
184
  - lib/github/ds/version.rb
185
185
  - lib/github/kv.rb
186
+ - lib/github/kv/config.rb
186
187
  - lib/github/result.rb
187
188
  - lib/github/sql.rb
188
189
  - lib/github/sql/errors.rb