{"id":19631695,"url":"https://github.com/noma4i/ar_shard","last_synced_at":"2025-04-28T06:33:24.485Z","repository":{"id":59150710,"uuid":"287668215","full_name":"noma4i/ar_shard","owner":"noma4i","description":"ActiveRecord Model Sharding","archived":false,"fork":false,"pushed_at":"2020-08-18T02:26:03.000Z","size":22,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-22T08:13:34.220Z","etag":null,"topics":["activerecord","multibase","shards"],"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/noma4i.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-15T03:12:16.000Z","updated_at":"2021-04-21T12:51:06.000Z","dependencies_parsed_at":"2022-09-13T10:50:20.690Z","dependency_job_id":null,"html_url":"https://github.com/noma4i/ar_shard","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noma4i%2Far_shard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noma4i%2Far_shard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noma4i%2Far_shard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noma4i%2Far_shard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/noma4i","download_url":"https://codeload.github.com/noma4i/ar_shard/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251266060,"owners_count":21561775,"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","multibase","shards"],"created_at":"2024-11-11T12:11:07.900Z","updated_at":"2025-04-28T06:33:24.196Z","avatar_url":"https://github.com/noma4i.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AR Shard\n[![Gem Version](https://badge.fury.io/rb/ar_shard.svg)](https://badge.fury.io/rb/ar_shard)\n[![Build Status](https://travis-ci.com/noma4i/ar_shard.svg?branch=master)](https://travis-ci.com/noma4i/ar_shard)\n\nAR Shard is non-intrusive extenstion for ActiveRecord to add threadsafe Model based sharding or just multidatabase connection. As this gem uses connection_handlers it can work only with ActiveRecord version 5 and above.\n\n## Features\n- Multiple Databases support for Rails5\n- Define config as a Proc\n- Shard only part of you ActiveRecord w/o sharing connection\n- Work with you Sharded Model and AR model at the same time\n- Threadsafe. Battle-tested with Sidekiq and 4 mixed Databases\n\n## Limitations\n- Doesn't care about migrations\n- Due to nature of connection_handlers all DB connections are established at boot time\n- if data structure is different between databases you should use `ModelName.reset_column_information`\n\n## Installation\n\nAdd this line to your application’s Gemfile:\n\n```ruby\ngem 'ar_shard', '~\u003e 1.0'\n```\n\n## Usage\n\nDefine your separate AR like:\n\n```ruby\nclass ShardRecord \u003c ActiveRecord::Base\n  self.abstract_class = true\n\n  connected_shards shards: [\n    { shard_1: { adapter: 'sqlite3', database: 'shard_1' } },\n    { shard_2: { adapter: 'postgresql', database: 'shard_2' } },\n    { shard_3: { adapter: 'sqlite3', database: 'shard_3' } }\n  ]\nend\n```\n\nor with Proc\n\n```ruby\nclass ShardRecord \u003c ActiveRecord::Base\n  self.abstract_class = true\n\n  connected_shards shards: -\u003e { YAML.load_file('path/to/config/shards.yml') }\nend\n```\n\n Where `shards.yml` is like:\n\n```yaml\n- shard_1:\n    adapter: mysql2\n    host: blabla\n    username: blabla\n    password: secret\n    database: remote_database_name\n    port: 3306\n    pool: 10\n- shard_2:\n    adapter: postgresql\n    host: blabla\n    username: blabla\n    password: secret\n    database: shard_name\n    port: 5432\n    pool: 10\n```\n\n\ngiven we have models like:\n\n```ruby\nclass User \u003c ActiveRecord::Base\n\nend\n\nclass Sharded::User \u003c ShardRecord\n\nend\n```\n\nAR Shard is isolating and rotating connections. Same like Rails Multibase support but without overlap.\nNow we can do crazy things like:\n\n```ruby\n# Connecting to another DB\nShardRecord.with_shard(:shard_name) do\n  Sharded::User.find_each do |user|\n    # Working with our DB and another at the same time!\n\n    u = User.find_by(email: user.email)\n    u.update(name: user.name)\n  end\nend\n```\n\n## Contributing\n\n- [Report bugs](https://github.com/noma4i/ar_shard/issues)\n- Fix bugs and [submit pull requests](https://github.com/noma4i/ar_shard/pulls)\n\nTo get started with development:\n\n```sh\ngit clone https://github.com/noma4i/ar_shard.git\ncd ar_shard\nbundle install\nbundle exec appraisal rake test\n```\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoma4i%2Far_shard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnoma4i%2Far_shard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoma4i%2Far_shard/lists"}