{"id":13879438,"url":"https://github.com/github/github-ds","last_synced_at":"2025-05-14T05:00:26.546Z","repository":{"id":41413233,"uuid":"75887039","full_name":"github/github-ds","owner":"github","description":"A collection of Ruby libraries for working with SQL on top of ActiveRecord's connection","archived":false,"fork":false,"pushed_at":"2025-03-21T21:01:27.000Z","size":303,"stargazers_count":701,"open_issues_count":8,"forks_count":28,"subscribers_count":319,"default_branch":"master","last_synced_at":"2025-04-14T04:58:29.936Z","etag":null,"topics":["activerecord","key-value","mysql","rails","ruby","sql"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/github.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-12-08T00:19:19.000Z","updated_at":"2025-03-21T00:44:37.000Z","dependencies_parsed_at":"2024-06-19T13:33:41.990Z","dependency_job_id":"ccf6b35b-f68e-4573-b09d-96fcc2850943","html_url":"https://github.com/github/github-ds","commit_stats":{"total_commits":228,"total_committers":23,"mean_commits":9.91304347826087,"dds":0.4956140350877193,"last_synced_commit":"f964ab2d75272d5b5d2fef68416abcab7bd2dc3a"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fgithub-ds","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fgithub-ds/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fgithub-ds/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/github%2Fgithub-ds/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/github","download_url":"https://codeload.github.com/github/github-ds/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254076329,"owners_count":22010596,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","key-value","mysql","rails","ruby","sql"],"created_at":"2024-08-06T08:02:21.143Z","updated_at":"2025-05-14T05:00:24.457Z","avatar_url":"https://github.com/github.png","language":"Ruby","readme":"# GitHub::DS\n\n`GitHub::DS` is a collection of Ruby libraries for working with SQL on top of ActiveRecord's connection.\n\n* `GitHub::KV` is a key/value data store backed by MySQL.\n* `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.\n* `GitHub::Result` makes it easier to bake in resiliency through the use of a Result object instead of raising exceptions.\n\n**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.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'github-ds'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install github-ds\n\n## Usage\n\nBelow is a taste of what you can do with these libraries. If you want to see more, check out the [examples directory](./examples/).\n\n### GitHub::KV\n\nFirst, you'll need to create the `key_values` table using the included Rails migration generator.\n\n```\nrails generate github:ds:active_record\nrails db:migrate\n```\n\nIf 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:\n\n```\nGitHub::KV.configure do |config|\n  config.table_name = \"new_key_values_table\"\nend\n```\n\nOnce you have created and executed the migration, KV can do neat things like this:\n\n```ruby\nrequire \"pp\"\n\n# Create new instance using ActiveRecord's default connection.\nkv = GitHub::KV.new { ActiveRecord::Base.connection }\n\n# Get a key.\npp kv.get(\"foo\")\n#\u003cGitHub::Result:0x3fd88cd3ea9c value: nil\u003e\n\n# Set a key.\nkv.set(\"foo\", \"bar\")\n# nil\n\n# Get the key again.\npp kv.get(\"foo\")\n#\u003cGitHub::Result:0x3fe810d06e4c value: \"bar\"\u003e\n\n# Get multiple keys at once.\npp kv.mget([\"foo\", \"bar\"])\n#\u003cGitHub::Result:0x3fccccd1b57c value: [\"bar\", nil]\u003e\n\n# Check for existence of a key.\npp kv.exists(\"foo\")\n#\u003cGitHub::Result:0x3fd4ae55ce8c value: true\u003e\n\n# Check for existence of key that does not exist.\npp kv.exists(\"bar\")\n#\u003cGitHub::Result:0x3fd4ae55c554 value: false\u003e\n\n# Check for existence of multiple keys at once.\npp kv.mexists([\"foo\", \"bar\"])\n#\u003cGitHub::Result:0x3ff1e98e18e8 value: [true, false]\u003e\n\n# Set a key's value if the key does not already exist.\npp kv.setnx(\"foo\", \"bar\")\n# false\n\n# Delete a key.\npp kv.del(\"bar\")\n# nil\n\n# Delete multiple keys at once.\npp kv.mdel([\"foo\", \"bar\"])\n# nil\n```\n\nNote that due to MySQL's default collation, KV keys are case-insensitive.\n\n### GitHub::SQL\n\n```ruby\n# Select, insert, update, delete or whatever you need...\nGitHub::SQL.results \u003c\u003c-SQL\n  SELECT * FROM example_key_values\nSQL\n\nGitHub::SQL.run \u003c\u003c-SQL, key: \"foo\", value: \"bar\"\n  INSERT INTO example_key_values (`key`, `value`)\n  VALUES (:key, :value)\nSQL\n\nGitHub::SQL.value \u003c\u003c-SQL, key: \"foo\"\n  SELECT value FROM example_key_values WHERE `key` = :key\nSQL\n\n# Or slowly build up a query based on conditionals...\nsql = GitHub::SQL.new \u003c\u003c-SQL\n  SELECT `value` FROM example_key_values\nSQL\n\nkey = ENV[\"KEY\"]\nunless key.nil?\n  sql.add \u003c\u003c-SQL, key: key\n    WHERE `key` = :key\n  SQL\nend\n\nlimit = ENV[\"LIMIT\"]\nunless limit.nil?\n  sql.add \u003c\u003c-SQL, limit: limit.to_i\n    ORDER BY `key` ASC\n    LIMIT :limit\n  SQL\nend\n\np sql.results\n```\n\n### GitHub::Result\n\n```ruby\ndef do_something\n  1\nend\n\ndef do_something_that_errors\n  raise \"noooooppppeeeee\"\nend\n\nresult = GitHub::Result.new { do_something }\np result.ok? # =\u003e true\np result.value! # =\u003e 1\n\nresult = GitHub::Result.new { do_something_that_errors }\np result.ok? # =\u003e false\np result.value { \"default when error happens\" } # =\u003e \"default when error happens\"\nbegin\n  result.value! # raises exception because error happened\nrescue =\u003e error\n  p result.error\n  p error\nend\n\n# Outputs Step 1, 2, 3\nresult = GitHub::Result.new {\n  GitHub::Result.new { puts \"Step 1: success!\" }\n}.then { |value|\n  GitHub::Result.new { puts \"Step 2: success!\" }\n}.then { |value|\n  GitHub::Result.new { puts \"Step 3: success!\" }\n}\np result.ok? # =\u003e true\n\n# Outputs Step 1, 2 and stops.\nresult = GitHub::Result.new {\n  GitHub::Result.new { puts \"Step 1: success!\" }\n}.then { |value|\n  GitHub::Result.new {\n    puts \"Step 2: failed!\"\n    raise\n  }\n}.then { |value|\n  GitHub::Result.new {\n    puts \"Step 3: should not get here because previous step failed!\"\n  }\n}\np result.ok? # =\u003e false\n```\n\n## Caveats\n\n### GitHub::KV Expiration\n\nKV 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:\n\n* **index_name**: `\"index_key_values_on_expires_at\"`\n* **limit**: `1000`\n* **where**: `\"expires_at \u003c= NOW()\"`\n\n## Development\n\nAfter 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.\n\n**Note**: You will need a MySQL database with no password set for the root user for the tests. Running `docker-compose up` will boot just that. This functionality is not currently used by GitHub and was from a contributor, so please let us know if it does not work or gets out of date (pull request is best, but an issue will do).\n\nTo 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).\n\n## Contributing\n\nBug 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.\n\n## Roadmap\n\nNothing currently on our radar other than continued maintenance. Have a big idea? [Let us know](http://github.com/github/github-ds/issues/new).\n\n## Maintainers\n\n| pic | @mention |\n|---|---|\n| ![@haileysome](https://avatars3.githubusercontent.com/u/179065?s=64) | [@haileysome](https://github.com/haileysome) |\n| ![@jnunemaker](https://avatars3.githubusercontent.com/u/235?s=64) | [@jnunemaker](https://github.com/jnunemaker) |\n| ![@miguelff](https://avatars3.githubusercontent.com/u/210307?s=64) | [@miguelff](https://github.com/miguelff) |\n| ![@zerowidth](https://avatars3.githubusercontent.com/u/3999?s=64) | [@zerowidth](https://github.com/zerowidth) |\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithub%2Fgithub-ds","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgithub%2Fgithub-ds","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithub%2Fgithub-ds/lists"}