{"id":15010134,"url":"https://github.com/jumpn/ecto_replay_sandbox","last_synced_at":"2025-04-09T22:51:21.920Z","repository":{"id":57493609,"uuid":"83198953","full_name":"jumpn/ecto_replay_sandbox","owner":"jumpn","description":"A custom Ecto Sandbox for CockroachDB to run your tests","archived":false,"fork":false,"pushed_at":"2023-11-27T15:34:33.000Z","size":39,"stargazers_count":10,"open_issues_count":0,"forks_count":3,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-24T00:41:36.582Z","etag":null,"topics":["cockroachdb","ecto","elixir","testing"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jumpn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"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":"2017-02-26T10:01:08.000Z","updated_at":"2023-11-27T15:16:59.000Z","dependencies_parsed_at":"2024-09-24T19:31:05.254Z","dependency_job_id":"fb7287d0-70a5-470d-9af8-296029f9340f","html_url":"https://github.com/jumpn/ecto_replay_sandbox","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"a170e997f1c831b2c941e59f8c8e7d0c867e647a"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jumpn%2Fecto_replay_sandbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jumpn%2Fecto_replay_sandbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jumpn%2Fecto_replay_sandbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jumpn%2Fecto_replay_sandbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jumpn","download_url":"https://codeload.github.com/jumpn/ecto_replay_sandbox/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247631833,"owners_count":20970069,"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":["cockroachdb","ecto","elixir","testing"],"created_at":"2024-09-24T19:30:36.727Z","updated_at":"2025-04-09T22:51:21.868Z","avatar_url":"https://github.com/jumpn.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# EctoReplaySandbox\n\nThis is a custom implementation of Ecto.Adapters.SQL.Sandbox designed to work with CockroachDB by not leveraging savepoints.\nEach test runs inside a transaction managed by the Sandbox, just like with default Ecto Sandbox.\n\nInside your test, when your code opens a transaction block, given that CockroachDB does not support nested transactions or savepoints, no actual database transaction is created.\nInstead the sandbox is using a log approach described below and such transaction are called pseudo transaction.\n\nThe sandbox maintains 2 logs for a given managed transaction:\n- Sandbox log\n- Transaction log\n\nThe Sandbox log contains the statements that have been commited to the database by your test, either outside of a pseudo transaction or once a pseudo transaction is commited.\nThe Transaction log keeps track of statements that are running inside a pseudo transaction and it is eventually appended to the Sandbox log once the pseudo transaction is successfully committed.\n\nWhen an error occurs or when a pseudo transaction is being explicitely rollbacked, the managed transaction is being rollbacked and a new transaction is being created.\nThen the Sandbox log is being replayed to restore the state to how it was before the error or the rollbacked pseudo transaction started.\n\nOnce the test finishes, the managed transaction is being rollbacked to restore the state of the database to how it was before the test began.\n\n## Installation\n\nThe package can be installed by adding `ecto_replay_sandbox` to your list of dependencies in `mix.exs`.\n\n```elixir\ndef deps do\n  [\n    {:ecto_replay_sandbox, \"~\u003e 2.1\", only: :test},\n  ]\nend\n```\n\n## Usage\n\nIn your `config/test.ex`\n```elixir\nconfig :my_app, MyApp.Repo,\n  pool: EctoReplaySandbox\n```\n\nIn your `test/test_helper.ex`\n\nReplace the following line:\n```elixir\nEcto.Adapters.SQL.Sandbox.mode(MyApp.Repo, :manual)\n```\n\nwith:\n```elixir\nsandbox = Application.get_env(:my_app, MyApp.Repo)[:pool]\nsandbox.mode(MyApp.Repo, :manual)\n```\n\nDo the same for your `test/support/xxx_case.ex` files, for example:\nReplace the following line:\n\n```elixir\n\nsetup tags do\n  :ok = Ecto.Adapters.SQL.Sandbox.checkout(MyApp.Repo)\n\n  unless tags[:async] do\n    Ecto.Adapters.SQL.Sandbox.mode(MyApp.Repo, :manual)\n  end\n\n  {:ok, conn: Phoenix.ConnTest.build_conn()}\nend\n```\n\nwith:\n```elixir\nsetup tags do\n  sandbox = Application.get_env(:myapp, MyApp.Repo)[:pool]\n  :ok = sandbox.checkout(MyApp.Repo)\n\n  unless tags[:async] do\n    sandbox.mode(MyApp.Repo, {:shared, self()})\n  end\n\n  {:ok, conn: Phoenix.ConnTest.build_conn()}\nend\n```\n\nThis effectively removes the hardcoded usage of `Ecto.Adapters.SQL.Sandbox` with a dynamic lookup of the configured pool.\n\n## Credits\n\nI'd like to give special thanks to James Fish (@fishcakez) for his help getting this working and always taking the time to offer guidance or otherwise feedback the CockroachDB team so that we can use Ecto with CockroachDB with little to no friction.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjumpn%2Fecto_replay_sandbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjumpn%2Fecto_replay_sandbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjumpn%2Fecto_replay_sandbox/lists"}