[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-7952":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":16,"subscribersCount":16,"size":16,"stars1d":16,"stars7d":16,"stars30d":16,"stars90d":16,"forks30d":16,"starsTrendScore":16,"compositeScore":17,"rankGlobal":10,"rankLanguage":10,"license":10,"archived":18,"fork":18,"defaultBranch":19,"hasWiki":20,"hasPages":20,"topics":21,"createdAt":10,"pushedAt":10,"updatedAt":26,"readmeContent":27,"aiSummary":28,"trendingCount":16,"starSnapshotCount":16,"syncStatus":29,"lastSyncTime":30,"discoverSource":31},7952,"octopus","thiagopradi\u002Foctopus","thiagopradi","Database Sharding for ActiveRecord","",null,"Ruby",2547,503,55,92,0,57.11,false,"master",true,[22,23,24,25],"activerecord","ruby","ruby-on-rails","sharding","2026-06-12 04:00:36","# Important Notice:\n\nOctopus will enter into maintainance mode once Rails 6 is released - since Rails 6 will include all the features for Database Sharding \u002F Replication into Rails Core (PR: https:\u002F\u002Fgithub.com\u002Frails\u002Frails\u002Fpull\u002F34052).\nOnce the first version of Rails 6 beta is released, there will be a migration guide to help users migrate from Octopus to Rails 6.\n\n# Octopus - Easy Database Sharding for ActiveRecord\n\n[![Build Status](https:\u002F\u002Ftravis-ci.org\u002Fthiagopradi\u002Foctopus.svg)](https:\u002F\u002Ftravis-ci.org\u002Fthiagopradi\u002Foctopus) [![Code Climate](https:\u002F\u002Fcodeclimate.com\u002Fbadge.png)](https:\u002F\u002Fcodeclimate.com\u002Fgithub\u002Fthiagopradi\u002Foctopus)\n\nOctopus is a better way to do Database Sharding in ActiveRecord. Sharding allows multiple databases in the same rails application. While there are several projects that implement Sharding (e.g. DbCharmer, DataFabric, MultiDb), each project has its own limitations. The main goal of octopus project is to provide a better way of doing Database Sharding.\n\n## Feature list:\n\nThe api is designed to be simple as possible. Octopus focuses on the end user, giving the power of multiple databases but with reliable code and flexibility. Octopus is compatible with Rails 4 and Rails 5.\n\nOctopus supports:\n\n- Sharding (with multiple shards, and grouped shards).\n- Replication (Master\u002Fslave support, with multiple slaves).\n- Moving data between shards with migrations.\n- Tools to manage database configurations. (soon)\n\n### Replication\n\nWhen using replication, all writes queries will be sent to master, and read queries to slaves. More info could be found at: \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fthiagopradi\u002Foctopus\u002Fwiki\u002Freplication\"> Wiki\u003C\u002Fa>\n\n### Sharding\n\nWhen using sharding, you need to specify which shard to send the query. Octopus supports selecting the shard inside a controller, or manually in each object. More could be found at \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fthiagopradi\u002Foctopus\u002Fwiki\u002Fsharding\"> Wiki\u003C\u002Fa>\n\n### Replication + Sharding\n\nWhen using replication and sharding concurrently, you must specify a shard, and can optionally specify a \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fthiagopradi\u002Foctopus\u002Fwiki\u002FSlave-Groups\">slave group\u003C\u002Fa>.\nAll write queries will be sent to each shard's master. If the slave group is specified read queries will be sent to slaves in it, or else to shard's master.\nMore info could be found at \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fthiagopradi\u002Foctopus\u002Fwiki\u002FSlave-Groups\"> Wiki\u003C\u002Fa>\n\n## Install\n\nAdd this line to Gemfile:\n\n```\ngem 'ar-octopus'\n```\n\nCurrently, Octopus doesn't support Rails 2. If you need support for rails 2, please use the version 0.5.0.\n\n## Upgrading\n\n### From \u003C 0.5.0\n\nOctopus \u003C 0.5.0 stored schema version information in the master database defined in the database.yml file, and assumed\nthat each shard's schema matched the others and the master database. Beginning with Octopus 0.5.0, the schema version\ninformation for each shard is stored within that shard's database.\n\nIf you are upgrading from \u003C 0.5.0 run the `copy_schema_versions` rake task to copy the schema version information in the\nmaster database to each of the shards:\n\n```bash\nrake octopus:copy_schema_versions\n```\n\nOnce the task completes migrations will operate normally and schema information will be stored in each shard database\ngoing forward.\n\n## How to use Octopus?\n\nFirst, you need to create a config file, shards.yml, inside your config\u002F directory. to see the syntax and how this file should look, please checkout \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fthiagopradi\u002Foctopus\u002Fwiki\u002Fconfig-file\">this page on wiki\u003C\u002Fa>.\n\n### Syntax\n\nOctopus adds a method to each AR Class and object: the using method is used to select the shard like this:\n\n```ruby\nUser.where(:name => \"Boba\").limit(3).using(:read_replica_one)\n```\n\nOctopus also supports queries within a block. When you pass a block to the using method, all queries inside the block will be sent to the specified shard.\n\n```ruby\nOctopus.using(:read_replica_two) do\n  User.create(:name => \"Thiago\")\nend\n```\n\nIf you want to use the same code for all shards or all shards in a specific group (for example in `db\u002Fseeds.rb`), you can use this syntax.\n\n```ruby\n# This will return a list of the given block's results, per shard.\nOctopus.using_all do\n  User.create_from_csv!\nend\n\n# This will return a list of the given block's results, per shard in history_shards group.\nOctopus.using_group(:history_shards) do\n  HistoryCategory.create_from_csv!\nend\n```\n\nEach model instance knows which shard it came from so this will work automatically:\n\n```ruby\n# This will find the user in the shard1\n@user = User.using(:shard1).find_by_name(\"Joao\")\n\n# This will find the user in the master database\n@user2 = User.find_by_name(\"Jose\")\n\n#Sets the name\n@user.name = \"Mike\"\n\n# Save the user in the correct shard, shard1.\n@user.save\n```\n\n### Migrations\n\nIn migrations, you also have access to the using method. The syntax is basically the same. This migration will run in the brazil and canada shards.\n\n```ruby\nclass CreateUsersOnBothShards \u003C ActiveRecord::Migration\n  using(:brazil, :canada)\n\n  def self.up\n    User.create!(:name => \"Both\")\n  end\n\n  def self.down\n    User.delete_all\n  end\nend\n```\n\nYou also could send a migration to a group of shards. This migration will be sent to all shards that belongs to history_shards group, specified in shards.yml:\n\n```ruby\nclass CreateUsersOnMultiplesGroups \u003C ActiveRecord::Migration\n  using_group(:history_shards)\n\n  def self.up\n    User.create!(:name => \"MultipleGroup\")\n  end\n\n  def self.down\n    User.delete_all\n  end\nend\n```\n\nYou can specify a `default_migration_group` for migrations, so that modifications to each individual migration file are not needed:\n\n```yaml\noctopus:\n  default_migration_group: europe_databases\n```\n\nThere is no need for a corresponding `default_migration_shard` - simply define that database to be your master. You might want this setting if all of your databases have identical schemas, but are not replicated.\n\nYou can configure a master shard for the rails application, to connect to when rails is going up. The safest would be to configure this to the shard specified in `database.yml` (some things still use it).\n\n```yaml\noctopus:\n  master_shard: \u003C%= ENV['SHARD'] || 'shard1' %>\n```\n\nThen you can use the `SHARD` environment variable to override the `master_shard` specified in `config\u002Fshards.yml`, useful for running rake tasks.\n\n```bash\nSHARD=shard1 rake db:setup && SHARD=shard2 rake db:setup\n```\n\n### Rails Controllers\n\nIf you want to send a specified action, or all actions from a controller, to a specific shard, use this syntax:\n\n```ruby\nclass ApplicationController \u003C ActionController::Base\n  around_filter :select_shard\n\n  def select_shard(&block)\n    Octopus.using(:brazil, &block)\n  end\nend\n```\n\nTo see the complete list of features and syntax, please check out our \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fthiagopradi\u002Foctopus\u002Fwiki\u002F\"> Wiki\u003C\u002Fa>\nWant to see sample rails applications using octopus features? please check it out: \u003Ca href=\"http:\u002F\u002Fgithub.com\u002Fthiagopradi\u002Foctopus_sharding_example\">Sharding Example\u003C\u002Fa> and \u003Ca href=\"http:\u002F\u002Fgithub.com\u002Fthiagopradi\u002Foctopus_replication_example\">Replication Example\u003C\u002Fa>. Also, we have an example that shows how to use Octopus without Rails: \u003Ca href=\"http:\u002F\u002Fgithub.com\u002Fthiagopradi\u002Foctopus_sinatra\"> Octopus + Sinatra Example\u003C\u002Fa>.\n\n## Mixing Octopus with the Rails multiple database model\n\nIf you want to set a custom connection to a specific model, use the syntax `octopus_establish_connection` syntax:\n\n```ruby\n#This class sets its own connection\nclass CustomConnection \u003C ActiveRecord::Base\n  octopus_establish_connection(:adapter => \"mysql\", :database => \"octopus_shard2\")\nend\n```\n\n### allow_shard\n\nIf you'd like to use specific shards with a model that has a Rails-managed connection, you can use `allow_shard`:\n\n```ruby\nclass CustomConnectedModel\n   octopus_establish_connection(...)\n   allow_shard :my_shard\nend\n\n#This uses :my_shard\nCustomConnectedModel.using(:my_shard).first\n\n#This uses the Rails-managed connection pool (the call to 'using' is ignored)\nCustomConnectedModel.using(:some_other_shard).first\n```\n\nThis can be useful if you have a model that lives in a separate database and would like to add sharding or replication to it. For other use cases, you may be better off with \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fthiagopradi\u002Foctopus\u002Fwiki\u002FSlave-Groups\">slave groups\u003C\u002Fa>.\n\n## Contributing with Octopus\n\nContributors are welcome! To run the test suite, you need mysql, postgresql and sqlite3 installed. This is what you need to setup your Octopus development environment:\n\n```bash\ngit clone http:\u002F\u002Fgithub.com\u002Fthiagopradi\u002Foctopus.git\ncd octopus\nbundle install\nbundle exec rake db:prepare\nbundle exec rake appraisal:install\nbundle exec rake spec\n```\n\nThis command will run the spec suite for all rails versions supported.\nTo run our integrations tests inside sample_app, you need to following commands:\n\n```bash\ncd sample_app\nbundle install\ncucumber\n```\n\nIf you are having issues running the octopus spec suite, verify your database users and passwords match those inside the config files and your permissions are correct.\n\n## Contributors:\n\n- \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fthiagopradi\u002Foctopus\u002Fcontributors\">All Contributors\u003C\u002Fa>\n\n## Mailing List:\n\n- \u003Ca href=\"http:\u002F\u002Fgroups.google.com\u002Fgroup\u002Foctopus-activerecord\u002F\">Octopus Mailing List\u003C\u002Fa>\n\n## Thanks\n\nThis project is sponsored by the \u003Ca href=\"http:\u002F\u002Fwww.rubysoc.org\">Ruby Summer of Code\u003C\u002Fa>, Rapid River Software,\nand my mentors \u003Ca href=\"http:\u002F\u002Fgithub.com\u002Fmperham\">Mike Perham\u003C\u002Fa> and \u003Ca href=\"http:\u002F\u002Fgithub.com\u002Famitagarwal\">Amit Agarwal\u003C\u002Fa>.\n\n## Copyright\n\nCopyright (c) Thiago Pradi, released under the MIT license.\n","Octopus 是一个为 ActiveRecord 提供数据库分片的解决方案。它支持多数据库在同一 Rails 应用中运行，具备分片、主从复制以及数据迁移等功能。Octopus 通过简洁的 API 设计让用户能够轻松管理多个数据库，同时保证代码的可靠性和灵活性。特别适合需要处理大量数据和高并发访问的应用场景，如大型电商网站或社交平台。需要注意的是，随着 Rails 6 的发布，Octopus 将进入维护模式，因为 Rails 6 核心已包含所有分片与复制功能。",2,"2026-06-11 03:15:20","top_language"]