github-ds 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +33 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +201 -0
- data/Rakefile +10 -0
- data/examples/example_setup.rb +38 -0
- data/examples/kv.rb +45 -0
- data/examples/result.rb +50 -0
- data/examples/sql.rb +44 -0
- data/examples/sql_add.rb +42 -0
- data/examples/sql_with_connection.rb +32 -0
- data/github-ds.gemspec +42 -0
- data/lib/generators/github/ds/active_record_generator.rb +22 -0
- data/lib/generators/github/ds/templates/migration.rb +19 -0
- data/lib/github-ds.rb +1 -0
- data/lib/github/ds.rb +8 -0
- data/lib/github/ds/version.rb +5 -0
- data/lib/github/kv.rb +343 -0
- data/lib/github/result.rb +229 -0
- data/lib/github/sql.rb +447 -0
- data/script/bootstrap +6 -0
- data/script/console +14 -0
- data/script/install +6 -0
- data/script/release +6 -0
- data/script/test +6 -0
- metadata +202 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 06371d875489add31a6dada7ff308cee7355759a
|
4
|
+
data.tar.gz: 1cfaebb92a0ca5d4b75a71d89a4dcaf9fbda6634
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 65ce5774cd9519de0383bd3597c277246a977a038bec24cbe945d21b55116d1211487b98134170adb4a9851eb44addb1aa40d38004d09b4e756c3fdd43d79d5b
|
7
|
+
data.tar.gz: 2af9c3b7bc6fd3eaa5543f7c8272afca16afc3e639df64f83074d8ed740c91de37f69ef33c45544fc85bbce1febefeb1e5be92981ff36aef32faefede2d992a9
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at nunemaker@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
## Contributing
|
2
|
+
|
3
|
+
[fork]: https://github.com/github/github-ds/fork
|
4
|
+
[pr]: https://github.com/github/github-ds/compare
|
5
|
+
[style]: https://github.com/styleguide/ruby
|
6
|
+
[code-of-conduct]: CODE_OF_CONDUCT.md
|
7
|
+
|
8
|
+
Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great.
|
9
|
+
|
10
|
+
Please note that this project is released with a [Contributor Code of Conduct][code-of-conduct]. By participating in this project you agree to abide by its terms.
|
11
|
+
|
12
|
+
## Submitting a pull request
|
13
|
+
|
14
|
+
0. [Fork][fork] and clone the repository
|
15
|
+
0. Configure and install the dependencies: `script/bootstrap`
|
16
|
+
0. Make sure the tests pass on your machine: `script/test`
|
17
|
+
0. Create a new branch: `git checkout -b my-branch-name`
|
18
|
+
0. Make your change, add tests, and make sure the tests still pass
|
19
|
+
0. Push to your fork and [submit a pull request][pr]
|
20
|
+
0. Pat your self on the back and wait for your pull request to be reviewed and merged.
|
21
|
+
|
22
|
+
Here are a few things you can do that will increase the likelihood of your pull request being accepted:
|
23
|
+
|
24
|
+
- Follow the [style guide][style].
|
25
|
+
- Write tests.
|
26
|
+
- Keep your change as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests.
|
27
|
+
- Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html).
|
28
|
+
|
29
|
+
## Resources
|
30
|
+
|
31
|
+
- [Contributing to Open Source on GitHub](https://guides.github.com/activities/contributing-to-open-source/)
|
32
|
+
- [Using Pull Requests](https://help.github.com/articles/about-pull-requests/)
|
33
|
+
- [GitHub Help](https://help.github.com)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 GitHub, Inc.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
# GitHub::DS
|
2
|
+
|
3
|
+
`GitHub::DS` is a collection of Ruby libraries for working with SQL on top of ActiveRecord's connection.
|
4
|
+
|
5
|
+
* `GitHub::KV` is a key/value data store backed by MySQL.
|
6
|
+
* `GitHub::SQL` is for building and executing a SQL query. This class uses ActiveRecord's connection class, but provides a better API for bind values and raw data access.
|
7
|
+
* `GitHub::Result` makes it easier to bake in resiliency through the use of a Result object instead of raising exceptions.
|
8
|
+
|
9
|
+
**Current Status**: Used in production extensively at GitHub. Because of this, all changes will be thoroughly vetted, which could slow down the process of contributing. We will do our best to actively communicate status of pull requests with any contributors. If you have any substantial changes that you would like to make, it would be great to first [open an issue](http://github.com/github/github-ds/issues/new) to discuss them with us.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'github-ds'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install github-ds
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Below is a taste of what you can do with these libraries. If you want to see more, check out the [examples directory](./examples/).
|
30
|
+
|
31
|
+
### GitHub::KV
|
32
|
+
|
33
|
+
First, you'll need to create the `key_values` table using the included Rails migration generator.
|
34
|
+
|
35
|
+
```
|
36
|
+
rails generate github:kv:active_record
|
37
|
+
rails db:migrate
|
38
|
+
```
|
39
|
+
|
40
|
+
Once you have created and executed the migration, KV can do neat things like this:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
require "pp"
|
44
|
+
require "github/kv"
|
45
|
+
|
46
|
+
# Create new instance using ActiveRecord's default connection.
|
47
|
+
kv = GitHub::KV.new { ActiveRecord::Base.connection }
|
48
|
+
|
49
|
+
# Get a key.
|
50
|
+
pp kv.get("foo")
|
51
|
+
#<GitHub::Result:0x3fd88cd3ea9c value: nil>
|
52
|
+
|
53
|
+
# Set a key.
|
54
|
+
kv.set("foo", "bar")
|
55
|
+
# nil
|
56
|
+
|
57
|
+
# Get the key again.
|
58
|
+
pp kv.get("foo")
|
59
|
+
#<GitHub::Result:0x3fe810d06e4c value: "bar">
|
60
|
+
|
61
|
+
# Get multiple keys at once.
|
62
|
+
pp kv.mget(["foo", "bar"])
|
63
|
+
#<GitHub::Result:0x3fccccd1b57c value: ["bar", nil]>
|
64
|
+
|
65
|
+
# Check for existence of a key.
|
66
|
+
pp kv.exists("foo")
|
67
|
+
#<GitHub::Result:0x3fd4ae55ce8c value: true>
|
68
|
+
|
69
|
+
# Check for existence of key that does not exist.
|
70
|
+
pp kv.exists("bar")
|
71
|
+
#<GitHub::Result:0x3fd4ae55c554 value: false>
|
72
|
+
|
73
|
+
# Check for existence of multiple keys at once.
|
74
|
+
pp kv.mexists(["foo", "bar"])
|
75
|
+
#<GitHub::Result:0x3ff1e98e18e8 value: [true, false]>
|
76
|
+
|
77
|
+
# Set a key's value if the key does not already exist.
|
78
|
+
pp kv.setnx("foo", "bar")
|
79
|
+
# false
|
80
|
+
|
81
|
+
# Delete a key.
|
82
|
+
pp kv.del("bar")
|
83
|
+
# nil
|
84
|
+
|
85
|
+
# Delete multiple keys at once.
|
86
|
+
pp kv.mdel(["foo", "bar"])
|
87
|
+
# nil
|
88
|
+
```
|
89
|
+
|
90
|
+
### GitHub::SQL
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
# Select, insert, update, delete or whatever you need...
|
94
|
+
statement = "INSERT INTO example_key_values (`key`, `value`) VALUES (:key, :value)"
|
95
|
+
GitHub::SQL.run statement, key: "foo", value: "bar"
|
96
|
+
|
97
|
+
statement = "SELECT value FROM example_key_values WHERE `key` = :key"
|
98
|
+
GitHub::SQL.value select_statement, key: "foo"
|
99
|
+
|
100
|
+
# Or slowly build up a query based on conditionals...
|
101
|
+
sql = GitHub::SQL.new "SELECT `VALUE` FROM example_key_values"
|
102
|
+
|
103
|
+
key = ENV["KEY"]
|
104
|
+
unless key.nil?
|
105
|
+
sql.add "WHERE `key` = :key", key: key
|
106
|
+
end
|
107
|
+
|
108
|
+
limit = ENV["LIMIT"]
|
109
|
+
unless limit.nil?
|
110
|
+
sql.add "ORDER BY `key` ASC"
|
111
|
+
sql.add "LIMIT :limit", limit: limit.to_i
|
112
|
+
end
|
113
|
+
|
114
|
+
p sql.results
|
115
|
+
```
|
116
|
+
|
117
|
+
### GitHub::Result
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
def do_something
|
121
|
+
1
|
122
|
+
end
|
123
|
+
|
124
|
+
def do_something_that_errors
|
125
|
+
raise "noooooppppeeeee"
|
126
|
+
end
|
127
|
+
|
128
|
+
result = GitHub::Result.new { do_something }
|
129
|
+
p result.ok? # => true
|
130
|
+
p result.value! # => 1
|
131
|
+
|
132
|
+
result = GitHub::Result.new { do_something_that_errors }
|
133
|
+
p result.ok? # => false
|
134
|
+
p result.value { "default when error happens" } # => "default when error happens"
|
135
|
+
begin
|
136
|
+
result.value! # raises exception because error happened
|
137
|
+
rescue => error
|
138
|
+
p result.error
|
139
|
+
p error
|
140
|
+
end
|
141
|
+
|
142
|
+
# Outputs Step 1, 2, 3
|
143
|
+
result = GitHub::Result.new {
|
144
|
+
GitHub::Result.new { puts "Step 1: success!" }
|
145
|
+
}.then { |value|
|
146
|
+
GitHub::Result.new { puts "Step 2: success!" }
|
147
|
+
}.then { |value|
|
148
|
+
GitHub::Result.new { puts "Step 3: success!" }
|
149
|
+
}
|
150
|
+
p result.ok? # => true
|
151
|
+
|
152
|
+
# Outputs Step 1, 2 and stops.
|
153
|
+
result = GitHub::Result.new {
|
154
|
+
GitHub::Result.new { puts "Step 1: success!" }
|
155
|
+
}.then { |value|
|
156
|
+
GitHub::Result.new {
|
157
|
+
puts "Step 2: failed!"
|
158
|
+
raise
|
159
|
+
}
|
160
|
+
}.then { |value|
|
161
|
+
GitHub::Result.new {
|
162
|
+
puts "Step 3: should not get here because previous step failed!"
|
163
|
+
}
|
164
|
+
}
|
165
|
+
p result.ok? # => false
|
166
|
+
```
|
167
|
+
|
168
|
+
## Caveats
|
169
|
+
|
170
|
+
### GitHub::KV Expiration
|
171
|
+
|
172
|
+
KV supports expiring keys and obeys expiration when performing operations, but does not actually purge expired rows. At GitHub, we use [pt-archiver](https://www.percona.com/doc/percona-toolkit/2.1/pt-archiver.html) to nibble expired rows. We configure it to do a replica lag check and use the following options:
|
173
|
+
|
174
|
+
* **index_name**: `"index_key_values_on_expires_at"`
|
175
|
+
* **limit**: `1000`
|
176
|
+
* **where**: `"expires_at <= NOW()"`
|
177
|
+
|
178
|
+
## Development
|
179
|
+
|
180
|
+
After checking out the repo, run `script/bootstrap` to install dependencies. Then, run `script/test` to run the tests. You can also run `script/console` for an interactive prompt that will allow you to experiment.
|
181
|
+
|
182
|
+
To install this gem onto your local machine, run `script/install`. To release a new version, update the version number in `version.rb`, commit, and then run `script/release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
183
|
+
|
184
|
+
## Contributing
|
185
|
+
|
186
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/github/github-ds. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. We recommend reading the [contributing guide](./CONTRIBUTING.md) as well.
|
187
|
+
|
188
|
+
## Roadmap
|
189
|
+
|
190
|
+
Nothing currently on our radar other than continued maintenance. Have a big idea? [Let us know](http://github.com/github/github-ds/issues/new).
|
191
|
+
|
192
|
+
## Maintainers
|
193
|
+
|
194
|
+
| pic | @mention |
|
195
|
+
|---|---|
|
196
|
+
| ![@jnunemaker](https://avatars3.githubusercontent.com/u/235?s=64) | [@jnunemaker](https://github.com/jnunemaker) |
|
197
|
+
| ![@charliesome](https://avatars3.githubusercontent.com/u/179065?s=64) | [@charliesome](https://github.com/charliesome) |
|
198
|
+
|
199
|
+
## License
|
200
|
+
|
201
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "pp"
|
2
|
+
require "pathname"
|
3
|
+
root_path = Pathname(__FILE__).dirname.join("..").expand_path
|
4
|
+
lib_path = root_path.join("lib")
|
5
|
+
$:.unshift(lib_path)
|
6
|
+
|
7
|
+
require "active_record"
|
8
|
+
|
9
|
+
attempts = 0
|
10
|
+
begin
|
11
|
+
ActiveRecord::Base.establish_connection({
|
12
|
+
adapter: "mysql2",
|
13
|
+
database: "github_ds_test",
|
14
|
+
})
|
15
|
+
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `key_values`")
|
16
|
+
require "generators/github/ds/templates/migration"
|
17
|
+
ActiveRecord::Migration.verbose = false
|
18
|
+
CreateKeyValuesTable.up
|
19
|
+
rescue ActiveRecord::NoDatabaseError
|
20
|
+
raise if attempts >= 1
|
21
|
+
ActiveRecord::Base.establish_connection({
|
22
|
+
adapter: "mysql2",
|
23
|
+
})
|
24
|
+
ActiveRecord::Base.connection.execute("CREATE DATABASE `github_ds_test`")
|
25
|
+
attempts += 1
|
26
|
+
retry
|
27
|
+
end
|
28
|
+
|
29
|
+
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `example_key_values`")
|
30
|
+
ActiveRecord::Base.connection.execute(<<-SQL)
|
31
|
+
CREATE TABLE `example_key_values` (
|
32
|
+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
|
33
|
+
`key` varchar(255) NOT NULL,
|
34
|
+
`value` blob NOT NULL,
|
35
|
+
PRIMARY KEY (`id`),
|
36
|
+
UNIQUE KEY `index_key_values_on_key` (`key`)
|
37
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8
|
38
|
+
SQL
|
data/examples/kv.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path("../example_setup", __FILE__)
|
2
|
+
require "github/kv"
|
3
|
+
|
4
|
+
# Create new instance using ActiveRecord's default connection.
|
5
|
+
kv = GitHub::KV.new { ActiveRecord::Base.connection }
|
6
|
+
|
7
|
+
# Get a key.
|
8
|
+
pp kv.get("foo")
|
9
|
+
#<GitHub::Result:0x3fd88cd3ea9c value: nil>
|
10
|
+
|
11
|
+
# Set a key.
|
12
|
+
kv.set("foo", "bar")
|
13
|
+
# nil
|
14
|
+
|
15
|
+
# Get the key again.
|
16
|
+
pp kv.get("foo")
|
17
|
+
#<GitHub::Result:0x3fe810d06e4c value: "bar">
|
18
|
+
|
19
|
+
# Get multiple keys at once.
|
20
|
+
pp kv.mget(["foo", "bar"])
|
21
|
+
#<GitHub::Result:0x3fccccd1b57c value: ["bar", nil]>
|
22
|
+
|
23
|
+
# Check for existence of a key.
|
24
|
+
pp kv.exists("foo")
|
25
|
+
#<GitHub::Result:0x3fd4ae55ce8c value: true>
|
26
|
+
|
27
|
+
# Check for existence of key that does not exist.
|
28
|
+
pp kv.exists("bar")
|
29
|
+
#<GitHub::Result:0x3fd4ae55c554 value: false>
|
30
|
+
|
31
|
+
# Check for existence of multiple keys at once.
|
32
|
+
pp kv.mexists(["foo", "bar"])
|
33
|
+
#<GitHub::Result:0x3ff1e98e18e8 value: [true, false]>
|
34
|
+
|
35
|
+
# Set a key's value if the key does not already exist.
|
36
|
+
pp kv.setnx("foo", "bar")
|
37
|
+
# false
|
38
|
+
|
39
|
+
# Delete a key.
|
40
|
+
pp kv.del("bar")
|
41
|
+
# nil
|
42
|
+
|
43
|
+
# Delete multiple keys at once.
|
44
|
+
pp kv.mdel(["foo", "bar"])
|
45
|
+
# nil
|