github-ds 0.3.0 → 0.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/README.md +8 -0
 - data/lib/generators/github/ds/active_record_generator.rb +8 -0
 - data/lib/generators/github/ds/templates/migration.rb +5 -5
 - data/lib/github/ds/version.rb +1 -1
 - data/lib/github/kv.rb +27 -12
 - data/lib/github/kv/config.rb +13 -0
 - metadata +3 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 235c233b2750caaa258c1fc172f6b2b14d117d533f247fbce0cb34ca83779b96
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 88caf6d96cce86f1ad841e22397e2f21ed63982a00fc3469009ed230126001a1
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 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
         
     | 
| 
         @@ -1,19 +1,19 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            class CreateKeyValuesTable < ActiveRecord::Migration<%= migration_version %>
         
     | 
| 
       2 
2 
     | 
    
         
             
              def self.up
         
     | 
| 
       3 
     | 
    
         
            -
                create_table  
     | 
| 
      
 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  
     | 
| 
       11 
     | 
    
         
            -
                add_index  
     | 
| 
      
 10 
     | 
    
         
            +
                add_index  <%= table_name %>, :key, :unique => true
         
     | 
| 
      
 11 
     | 
    
         
            +
                add_index  <%= table_name %>, :expires_at
         
     | 
| 
       12 
12 
     | 
    
         | 
| 
       13 
     | 
    
         
            -
                change_column  
     | 
| 
      
 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  
     | 
| 
      
 17 
     | 
    
         
            +
                drop_table  <%= table_name %>
         
     | 
| 
       18 
18 
     | 
    
         
             
              end
         
     | 
| 
       19 
19 
     | 
    
         
             
            end
         
     | 
    
        data/lib/github/ds/version.rb
    CHANGED
    
    
    
        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( 
     | 
| 
       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  
     | 
| 
      
 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  
     | 
| 
      
 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  
     | 
| 
      
 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  
     | 
| 
      
 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  
     | 
| 
      
 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  
     | 
| 
      
 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  
     | 
| 
      
 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  
     | 
| 
      
 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  
     | 
| 
      
 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 
     | 
    
         | 
    
        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. 
     | 
| 
      
 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- 
     | 
| 
      
 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
         
     |