{"id":13879054,"url":"https://github.com/lipanski/sql-spy","last_synced_at":"2025-08-27T19:21:11.334Z","repository":{"id":59156171,"uuid":"301495428","full_name":"lipanski/sql-spy","owner":"lipanski","description":"A gem to track all SQL queries performed inside a block of code.","archived":false,"fork":false,"pushed_at":"2023-11-24T09:42:10.000Z","size":25,"stargazers_count":27,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T12:21:46.688Z","etag":null,"topics":["activerecord","debug","gem","gems","minitest","query","rspec","ruby","spy","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/lipanski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":"lipanski"}},"created_at":"2020-10-05T17:58:27.000Z","updated_at":"2024-11-20T02:52:07.000Z","dependencies_parsed_at":"2024-01-13T20:56:34.761Z","dependency_job_id":"b09e2f26-ccd1-4179-8fe6-f5810edf6b8e","html_url":"https://github.com/lipanski/sql-spy","commit_stats":{"total_commits":23,"total_committers":3,"mean_commits":7.666666666666667,"dds":"0.26086956521739135","last_synced_commit":"9ac5aa434a211c2d19203d7fada8dc99ae2e5d02"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lipanski%2Fsql-spy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lipanski%2Fsql-spy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lipanski%2Fsql-spy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lipanski%2Fsql-spy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lipanski","download_url":"https://codeload.github.com/lipanski/sql-spy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248226274,"owners_count":21068172,"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","debug","gem","gems","minitest","query","rspec","ruby","spy","sql"],"created_at":"2024-08-06T08:02:08.261Z","updated_at":"2025-04-10T13:42:57.299Z","avatar_url":"https://github.com/lipanski.png","language":"Ruby","readme":"# SqlSpy\n\n\u003ca href=\"https://travis-ci.com/github/lipanski/sql-spy\"\u003e\u003cimg src=\"https://api.travis-ci.com/lipanski/sql-spy.svg?branch=master\"\u003e\u003c/a\u003e\n\nA gem to track all SQL queries performed inside a block of code.\n\nWith **SqlSpy** you can test:\n\n- The total amount of queries performed by a piece of code\n- The amount of queries performed on a specific table\n- The type of these queries: *SELECT*, *INSERT*, *UPDATE*, *DELETE*\n- The query duration\n- The SQL produced by your *ActiveRecord* code\n- N+1 queries\n\nThe implementation is inspired by how [ActiveRecord is tested](https://github.com/rails/rails/blob/6-0-stable/activerecord/test/cases/test_case.rb).\n\nCheck out [my blog post](https://lipanski.com/posts/activerecord-eager-loading#preventing-n1-regressions-with-tests) about writing controller tests with **SqlSpy** in order to prevent N+1 regressions.\n\n## Usage\n\nAdd the gem to your `Gemfile`:\n\n```ruby\ngem \"sql_spy\"\n```\n\nInstall it:\n\n```sh\nbundle install\n```\n\nWrap the code you'd like to track inside `SqlSpy.track {}`:\n\n```ruby\nrequire \"sql_spy\"\n\nqueries = SqlSpy.track do\n  # Some code that triggers ActiveRecord queries\nend\n```\n\nThe `SqlSpy.track` method will return **an array containing all the queries performed inside the block**. Every object inside this array exposes the following **methods**:\n\n- `#model_name`: The model name (e.g. \"User\").\n- `#sql`: The SQL query that was performed.\n- `#duration`: The duration of the query in milliseconds.\n- `#select?`: Is this a *SELECT* query?\n- `#insert?`: Is this an *INSERT* query?\n- `#update?`: Is this an *UPDATE* query?\n- `#delete?`: Is this a *DELETE* query?\n\nHere are some **ideas** of how you could use this:\n\n```ruby\n# Expect less than 5 queries\nassert queries.count \u003c 5\n\n# Expect 1 INSERT query\nassert_equal 1, queries.select(\u0026:insert?).size\n\n# Expect 2 queries to the posts table\nassert_equal 2, queries.select { |query| query.model_name == \"Post\" }.size\n\n# None of the queries should be slower than 100ms\nassert queries.none? { |query| query.duration \u003e 100 }\n\n# Fail on N+1 queries: expect at most 1 query per table\nqueries_by_model = queries.group_by(\u0026:model_name)\nassert queries_by_model.none? { |model_name, queries| queries.count \u003e 1 }\n```\n\n## Development\n\nThe gem is tested with Postgres 9.6 and SQLite 3.\n\nTo run tests with SQLite:\n\n```sh\nrake\n```\n\nTo run tests with Postgres:\n\n```sh\n# Create a test database\ncretedb sql_spy_test\n\nDATABASE_URL=postgres:///sql_spy_test rake\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","funding_links":["https://github.com/sponsors/lipanski"],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flipanski%2Fsql-spy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flipanski%2Fsql-spy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flipanski%2Fsql-spy/lists"}