{"id":13879456,"url":"https://github.com/umbrellio/sequel-connection_guard","last_synced_at":"2025-05-06T19:27:53.178Z","repository":{"id":56894997,"uuid":"187833947","full_name":"umbrellio/sequel-connection_guard","owner":"umbrellio","description":"A Sequel extension that provides a set of abstractions for working with unreliable database connections.","archived":false,"fork":false,"pushed_at":"2019-05-23T11:52:39.000Z","size":4,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-06T19:27:48.879Z","etag":null,"topics":["database","sequel","sequel-extension","sequel-plugin"],"latest_commit_sha":null,"homepage":"https://www.rubydoc.info/gems/sequel-connection_guard","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/umbrellio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-21T12:36:25.000Z","updated_at":"2020-06-15T17:51:52.000Z","dependencies_parsed_at":"2022-08-21T01:20:35.254Z","dependency_job_id":null,"html_url":"https://github.com/umbrellio/sequel-connection_guard","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umbrellio%2Fsequel-connection_guard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umbrellio%2Fsequel-connection_guard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umbrellio%2Fsequel-connection_guard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umbrellio%2Fsequel-connection_guard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/umbrellio","download_url":"https://codeload.github.com/umbrellio/sequel-connection_guard/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252752192,"owners_count":21798747,"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":["database","sequel","sequel-extension","sequel-plugin"],"created_at":"2024-08-06T08:02:21.675Z","updated_at":"2025-05-06T19:27:53.135Z","avatar_url":"https://github.com/umbrellio.png","language":"Ruby","readme":"# sequel-connection_guard\n[![Build Status](https://travis-ci.org/umbrellio/sequel-connection_guard.svg?branch=master)](https://travis-ci.org/umbrellio/sequel-connection_guard)\n[![Coverage Status](https://coveralls.io/repos/github/umbrellio/sequel-connection_guard/badge.svg?branch=master)](https://coveralls.io/github/umbrellio/sequel-connection_guard?branch=master)\n[![Gem Version](https://badge.fury.io/rb/sequel-connection_guard.svg)](https://badge.fury.io/rb/sequel-connection_guard)\n\nThis Sequel extension provides a set of abstractions for working with databases that might not be\nreachable at any given moment in time.\n\n**This gem was only tested against PostgreSQL databases.**\n\nGoals:\n- Allow to bootstrap an application when a database server is down\n- Allow to safely and explicitly access a database\n- In case connection fails, retry on next attempt\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'sequel-connection_guard'\n```\n\nAnd then execute:\n```sh\n$ bundle\n```\n\nEnable the extension:\n```ruby\nSequel.extension :connection_guard\n```\n\n## Usage\n\nThis extension provides two main abstractions for accessing unreliable databases. These are almost\nidentical, but one allows you to reach a database handle (instance of `Sequel::Database`) and\nanother allows you to reach a Sequel model (instance of `Sequel::Model`).\n\n### Database guard\n\nA database guard is what you use to access a database handle. First, you need to instantiate one:\n```ruby\n::DB = Sequel::DatabaseGuard.new('postgres://localhost/mydb')\n```\n\nYou can perform additional actions upon DB initialization, such as enabling Sequel plugins:\n```ruby\n::DB = Sequel::DatabaseGuard.new('postgres://localhost/mydb') do |db|\n  db.extension :advisory_locking\n  db.extension :pg_json\nend\n```\n\nThere are two ways of using the guard.\n\n#### Safe access\n\nYou can safely access the database handle by using `#safe_execute`:\n\n```ruby\nusers = DB.safe_execute do\n  # if the database is reachable\n  alive do |db|\n    db[:users].all\n  end\n\n  # if the database could not be reached. NOTE: this is optional\n  dead do\n    []\n  end\nend\n```\n\n#### Unsafe access\n\nWhen you don't care about safety (or you're already inside a `safe_execute` context), use\n`#force_execute`:\n\n```ruby\nusers = DB.force_execute { |db| db[:users].all }\n```\n\n#### Accessing a raw database handle\n\nSometimes it's necessary to get access to a raw instance of `Sequel::Database` (for example, when\nusing the `database_cleaner` gem). You can get a raw handle like this:\n\n```ruby\nDB.raw_handle\n```\n\nBeware that this will raise `Sequel::DatabaseConnectionError` if the database is currently\nunreachable.\n\n### Model guard\n\nA model guard is what you use to access a model handle. To create a model guard:\n```ruby\n# NOTE: `DB` must be an instance of Sequel::DatabaseGuard\nUserGuard = Sequel::ModelGuard(DB[:users]) do\n  one_to_many :cookies, class: 'Cookie::RawModel'\n\n  def admin?\n    role == 'admin'\n  end\nend\n```\n\nThere are, again, two ways of using the guard.\n\n#### Safe access\n\nYou can safely access the model by using `#safe_execute`:\n\n```ruby\nusers = UserGuard.safe_execute do\n  # if the database is reachable\n  alive do |model|\n    model.all\n  end\n\n  # if the database could not be reached. NOTE: this is optional\n  dead do\n    []\n  end\nend\n```\n\n#### Unsafe access\n\nWhen you don't care about safety (or you're already inside a `safe_execute` context), use\n`#force_execute`:\n\n```ruby\nusers = UserGuard.force_execute { |model| model.all }\n```\n\n#### Accessing a raw model\n\nSometimes it's necessary to get access to a raw instance of `Sequel::Model` (good examples are\nusing this extension with `factory_bot` and describing associations like shown above).\nTo get the raw model:\n\n```ruby\nUser = UserGuard::RawModel\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/sequel-connection_guard.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Authors\nCreated by [Alexander Komarov](https://github.com/akxcv).\n\n\u003ca href=\"https://github.com/umbrellio/\"\u003e\n  \u003cimg style=\"float: left;\" src=\"https://umbrellio.github.io/Umbrellio/supported_by_umbrellio.svg\" alt=\"Supported by Umbrellio\" width=\"439\" height=\"72\"\u003e\n\u003c/a\u003e\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fumbrellio%2Fsequel-connection_guard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fumbrellio%2Fsequel-connection_guard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fumbrellio%2Fsequel-connection_guard/lists"}