wt_activerecord_index_spy 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 11d05e2c0947b38147e7410caf7659d87ecf28e07f684f075eb77034d0b01025
4
+ data.tar.gz: 17d2229914f89671fbd811a4e5dc5851354190d7e1eb90ec7444f1449731c4eb
5
+ SHA512:
6
+ metadata.gz: 8bf3bb1095e3b9ba88d69fdbd0dd00da66135308b4a2f2931abda47f225609aa8eef07f029b70a936087181d92b36832441c429d37b6b67ae076671236de847e
7
+ data.tar.gz: 7216232729e5f15769f2da26abb2d9e8922cb1411f67843cd2f64e2e08a3fe2b50045ea0481fac74b338dc941d5fad23e8763d6c8eeae11240a0359f17d957cc
data/.env.template ADDED
@@ -0,0 +1,3 @@
1
+ DATABASE_URL_MYSQL2='mysql2://USER:PASSWORD@localhost:3306'
2
+ # docker run --name some-postgres -e POSTGRES_PASSWORD=1234 -p 5432:5432 --rm postgres
3
+ DATABASE_URL_POSTGRESQL='postgresql://postgres:1234@localhost:5432'
@@ -0,0 +1,33 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Describe the bug**
11
+ A clear and concise description of what the bug is.
12
+
13
+ **To Reproduce**
14
+ Steps to reproduce the behavior:
15
+ 1. Go to '...'
16
+ 2. Click on '....'
17
+ 3. Scroll down to '....'
18
+ 4. See error
19
+
20
+ **Expected behavior**
21
+ A clear and concise description of what you expected to happen.
22
+
23
+ **Screenshots**
24
+ If applicable, add screenshots to help explain your problem.
25
+
26
+ **Desktop (please complete the following information):**
27
+ - OS: [e.g. Ubuntu 20.4]
28
+ - Database/version: [e.g. MySql 5.7]
29
+ - Ruby version: [e.g. Ruby 2.5]
30
+ - Activerecord version: [e.g. Ruby 5.2]
31
+
32
+ **Additional context**
33
+ Add any other context about the problem here.
@@ -0,0 +1,20 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: ''
5
+ labels: ''
6
+ assignees: ''
7
+
8
+ ---
9
+
10
+ **Is your feature request related to a problem? Please describe.**
11
+ A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12
+
13
+ **Describe the solution you'd like**
14
+ A clear and concise description of what you want to happen.
15
+
16
+ **Describe alternatives you've considered**
17
+ A clear and concise description of any alternative solutions or features you've considered.
18
+
19
+ **Additional context**
20
+ Add any other context or screenshots about the feature request here.
@@ -0,0 +1,29 @@
1
+ ## Description
2
+
3
+ Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
4
+
5
+ Fixes # (issue)
6
+
7
+ ## Type of change
8
+
9
+ Please delete options that are not relevant.
10
+
11
+ - [ ] Bug fix (non-breaking change which fixes an issue)
12
+ - [ ] New feature (non-breaking change which adds functionality)
13
+ - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
14
+ - [ ] This change requires a documentation update
15
+
16
+ ## How Has This Been Tested?
17
+
18
+ Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
19
+
20
+ ## Checklist:
21
+
22
+ - [ ] My code follows the style guidelines set by rubocop
23
+ - [ ] I have performed a self-review of my own code
24
+ - [ ] I have made corresponding changes to the documentation
25
+ - [ ] My changes generate no new warnings
26
+ - [ ] I have added tests that prove my fix is effective or that my feature works
27
+ - [ ] New and existing unit tests pass locally with my changes
28
+ - [ ] Any dependent changes have been merged and published in downstream modules
29
+ - [ ] I have checked my code and corrected any misspellings
@@ -0,0 +1,51 @@
1
+ name: Ruby
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ name: Ruby ${{ matrix.ruby }}, ActiveRecord ${{ matrix.activerecord }}
8
+ runs-on: ubuntu-latest
9
+ strategy:
10
+ matrix:
11
+ ruby:
12
+ - 2.7
13
+ - 2.6
14
+ - 2.5
15
+ activerecord:
16
+ - '~> 6'
17
+ - '~> 5'
18
+ - '~> 4'
19
+ exclude:
20
+ - ruby: 2.7
21
+ activerecord: '~> 4'
22
+ env:
23
+ DATABASE_URL_MYSQL2: 'mysql2://root:root@127.0.0.1:3306'
24
+ DATABASE_URL_POSTGRESQL: 'postgresql://root:root@127.0.0.1:5432'
25
+ ACTIVE_RECORD_VERSION: ${{ matrix.activerecord }}
26
+ steps:
27
+ - uses: actions/checkout@v2
28
+ - name: Set up Ruby
29
+ uses: ruby/setup-ruby@v1
30
+ with:
31
+ ruby-version: ${{ matrix.ruby }}
32
+ - name: install gems
33
+ run: |
34
+ gem install bundler
35
+ bundle install
36
+ - name: Create databases
37
+ run: |
38
+ sudo systemctl start mysql.service
39
+ sudo systemctl start postgresql.service
40
+ echo "create user root password 'root';" >> /tmp/create_user.sql
41
+ echo "ALTER ROLE root WITH CREATEDB;" >> /tmp/create_user.sql
42
+ sudo su postgres -c 'psql --file=/tmp/create_user.sql'
43
+ sudo su postgres -c 'createdb root'
44
+ bundle exec rake db:drop db:create db:migrate
45
+ ADAPTER=postgresql bundle exec rake db:drop db:create db:migrate
46
+ - name: Tests for mysql2
47
+ run: |
48
+ bundle exec rspec
49
+ - name: Tests for postgresql
50
+ run: |
51
+ ADAPTER=postgresql bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .env
14
+ Gemfile.lock
15
+
16
+ .ruby-version
17
+
18
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ SuggestExtensions: false
4
+ NewCops: enable
5
+
6
+ Style/StringLiterals:
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/StringLiteralsInInterpolation:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Layout/LineLength:
15
+ Max: 120
16
+
17
+ Metrics/BlockLength:
18
+ Exclude:
19
+ - 'spec/**/*'
20
+ - 'Rakefile'
21
+
22
+ Metrics/MethodLength:
23
+ Exclude:
24
+ - 'spec/**/*'
25
+ - 'Rakefile'
26
+
27
+ Style/ModuleFunction:
28
+ EnforcedStyle: extend_self
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ ## 0.3.0
2
+ * add support for PostgreSQL (unfortunately all results are uncertain)
3
+ * add support for Ruby >= 2.5 and Activerecord >= 4
4
+ * sort lines in the HTML report by certainity_level, identifier, query
5
+ * improve the query analyser to not analyse a query again if a similar query was analysed before with a prepared statement (Postgres use prepared statements by default)
6
+ * improve the logic to detect if a query was fired from a test file or not
7
+ * remove the auto require for the module TestHelpers
8
+
9
+ ## 0.2.0
10
+ * Improve RSpec matcher to cache EXPLAIN results
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at fabio.perrella@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/CONTRIBUTORS.md ADDED
@@ -0,0 +1,3 @@
1
+ - @fabioperrella
2
+ - @julik
3
+ - @lorenzograndi4
data/Gemfile ADDED
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in wt_activerecord_index_spy.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
13
+
14
+ active_record_version = ENV.fetch("ACTIVE_RECORD_VERSION", "~> 4")
15
+
16
+ gem "activerecord", active_record_version
17
+ gem "activesupport", active_record_version
18
+
19
+ if active_record_version == "~> 4"
20
+ gem "pg", "~> 0.15"
21
+ else
22
+ gem "pg"
23
+ end
data/LICENSE.md ADDED
@@ -0,0 +1,33 @@
1
+ Copyright 2021 WeTransfer
2
+
3
+ Hippocratic License Version Number: 2.1.
4
+
5
+ Purpose. The purpose of this License is for the Licensor named above to permit the Licensee (as defined below) broad permission, if consistent with Human Rights Laws and Human Rights Principles (as each is defined below), to use and work with the Software (as defined below) within the full scope of Licensor’s copyright and patent rights, if any, in the Software, while ensuring attribution and protecting the Licensor from liability.
6
+
7
+ Permission and Conditions. The Licensor grants permission by this license (“License”), free of charge, to the extent of Licensor’s rights under applicable copyright and patent law, to any person or entity (the “Licensee”) obtaining a copy of this software and associated documentation files (the “Software”), to do everything with the Software that would otherwise infringe (i) the Licensor’s copyright in the Software or (ii) any patent claims to the Software that the Licensor can license or becomes able to license, subject to all of the following terms and conditions:
8
+
9
+ - Acceptance. This License is automatically offered to every person and entity subject to its terms and conditions. Licensee accepts this License and agrees to its terms and conditions by taking any action with the Software that, absent this License, would infringe any intellectual property right held by Licensor.
10
+
11
+ - Notice. Licensee must ensure that everyone who gets a copy of any part of this Software from Licensee, with or without changes, also receives the License and the above copyright notice (and if included by the Licensor, patent, trademark and attribution notice). Licensee must cause any modified versions of the Software to carry prominent notices stating that Licensee changed the Software. For clarity, although Licensee is free to create modifications of the Software and distribute only the modified portion created by Licensee with additional or different terms, the portion of the Software not modified must be distributed pursuant to this License. If anyone notifies Licensee in writing that Licensee has not complied with this Notice section, Licensee can keep this License by taking all practical steps to comply within 30 days after the notice. If Licensee does not do so, Licensee’s License (and all rights licensed hereunder) shall end immediately.
12
+
13
+ - Compliance with Human Rights Principles and Human Rights Laws.
14
+
15
+ 1. Human Rights Principles.
16
+
17
+ (a) Licensee is advised to consult the articles of the United Nations Universal Declaration of Human Rights and the United Nations Global Compact that define recognized principles of international human rights (the “Human Rights Principles”). Licensee shall use the Software in a manner consistent with Human Rights Principles.
18
+
19
+ (b) Unless the Licensor and Licensee agree otherwise, any dispute, controversy, or claim arising out of or relating to (i) Section 1(a) regarding Human Rights Principles, including the breach of Section 1(a), termination of this License for breach of the Human Rights Principles, or invalidity of Section 1(a) or (ii) a determination of whether any Law is consistent or in conflict with Human Rights Principles pursuant to Section 2, below, shall be settled by arbitration in accordance with the Hague Rules on Business and Human Rights Arbitration (the “Rules”); provided, however, that Licensee may elect not to participate in such arbitration, in which event this License (and all rights licensed hereunder) shall end immediately. The number of arbitrators shall be one unless the Rules require otherwise.
20
+
21
+ Unless both the Licensor and Licensee agree to the contrary: (1) All documents and information concerning the arbitration shall be public and may be disclosed by any party; (2) The repository referred to under Article 43 of the Rules shall make available to the public in a timely manner all documents concerning the arbitration which are communicated to it, including all submissions of the parties, all evidence admitted into the record of the proceedings, all transcripts or other recordings of hearings and all orders, decisions and awards of the arbitral tribunal, subject only to the arbitral tribunal's powers to take such measures as may be necessary to safeguard the integrity of the arbitral process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3) Article 26(6) of the Rules shall not apply.
22
+
23
+ 2. Human Rights Laws. The Software shall not be used by any person or entity for any systems, activities, or other uses that violate any Human Rights Laws. “Human Rights Laws” means any applicable laws, regulations, or rules (collectively, “Laws”) that protect human, civil, labor, privacy, political, environmental, security, economic, due process, or similar rights; provided, however, that such Laws are consistent and not in conflict with Human Rights Principles (a dispute over the consistency or a conflict between Laws and Human Rights Principles shall be determined by arbitration as stated above). Where the Human Rights Laws of more than one jurisdiction are applicable or in conflict with respect to the use of the Software, the Human Rights Laws that are most protective of the individuals or groups harmed shall apply.
24
+
25
+ 3. Indemnity. Licensee shall hold harmless and indemnify Licensor (and any other contributor) against all losses, damages, liabilities, deficiencies, claims, actions, judgments, settlements, interest, awards, penalties, fines, costs, or expenses of whatever kind, including Licensor’s reasonable attorneys’ fees, arising out of or relating to Licensee’s use of the Software in violation of Human Rights Laws or Human Rights Principles.
26
+
27
+ - Failure to Comply. Any failure of Licensee to act according to the terms and conditions of this License is both a breach of the License and an infringement of the intellectual property rights of the Licensor (subject to exceptions under Laws, e.g., fair use). In the event of a breach or infringement, the terms and conditions of this License may be enforced by Licensor under the Laws of any jurisdiction to which Licensee is subject. Licensee also agrees that the Licensor may enforce the terms and conditions of this License against Licensee through specific performance (or similar remedy under Laws) to the extent permitted by Laws. For clarity, except in the event of a breach of this License, infringement, or as otherwise stated in this License, Licensor may not terminate this License with Licensee.
28
+
29
+ - Enforceability and Interpretation. If any term or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, then such invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction; provided, however, subject to a court modification pursuant to the immediately following sentence, if any term or provision of this License pertaining to Human Rights Laws or Human Rights Principles is deemed invalid, illegal, or unenforceable against Licensee by a court of competent jurisdiction, all rights in the Software granted to Licensee shall be deemed null and void as between Licensor and Licensee. Upon a determination that any term or provision is invalid, illegal, or unenforceable, to the extent permitted by Laws, the court may modify this License to affect the original purpose that the Software be used in compliance with Human Rights Principles and Human Rights Laws as closely as possible. The language in this License shall be interpreted as to its fair meaning and not strictly for or against any party.
30
+
31
+ - Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES “AS IS,” WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
32
+
33
+ This Hippocratic License is an Ethical Source license (https://ethicalsource.dev) and is offered for use by licensors and licensees at their own risk, on an “AS IS” basis, and with no warranties express or implied, to the maximum extent permitted by Laws.
data/README.md ADDED
@@ -0,0 +1,188 @@
1
+ # wt_activerecord_index_spy
2
+
3
+ [![Gem](https://img.shields.io/gem/v/wt_activerecord_index_spy)](https://rubygems.org/gems/wt_activerecord_index_spy)
4
+ ![GitHub Actions Workflow](https://github.com/WeTransfer/wt_activerecord_index_spy/actions/workflows/main.yml/badge.svg)
5
+ [![Hippocratic License](https://img.shields.io/badge/license-Hippocratic-green)](https://github.com/WeTransfer/wt_activerecord_index_spy/blob/main/LICENSE.md)
6
+
7
+ A Ruby library to watch and analyze queries that run using `ActiveRecord` to check
8
+ if they use a proper index.
9
+
10
+ It subscribes to `sql.active_record` notification using `ActiveSupport::Notifications`.
11
+
12
+ It was designed to be used in tests, but it's also possible to use it in
13
+ staging or production, carefully.
14
+
15
+ ## Why would I use this?
16
+
17
+ Imagine you have an application running in production and after a deploy, it starts to slow down.
18
+
19
+ After a perhaps exhaustive debugging session, you may find that a new query or perhaps a change
20
+ in the database schema was responsible for starting to have queries that do not use database indexes.
21
+
22
+ Then, you create the appropriate index and the problem is solved!
23
+
24
+ By using this gem, you can get those queries that are not using suitable database indexes in
25
+ your test suite. So you won't have surprises like the example above, after a deploy.
26
+
27
+ You can also enable the gem in your development/staging environment, generate a report
28
+ and analyze if there is any missing index.
29
+
30
+ ## Installation
31
+
32
+ Add this line to your application's Gemfile:
33
+
34
+ ```ruby
35
+ gem 'wt_activerecord_index_spy'
36
+ ```
37
+
38
+ And then execute:
39
+
40
+ $ bundle install
41
+
42
+ Or install it yourself as:
43
+
44
+ $ gem install wt_activerecord_index_spy
45
+
46
+ ## Usage
47
+
48
+ There are 3 different modes to use it:
49
+
50
+ ### 1 - Using a test matcher
51
+
52
+ Include the helper in your RSpec configuration:
53
+
54
+ ```ruby
55
+ require 'wt_activerecord_index_spy/test_helpers'
56
+
57
+ RSpec.configure do |config|
58
+ config.include(WtActiverecordIndexSpy::TestHelpers)
59
+ end
60
+ ```
61
+
62
+ Use the helper `have_used_db_indexes` where you want to check if all queries used database indexes:
63
+
64
+ ```ruby
65
+ it 'uses an index for all the queries' do
66
+ expect { SomeClass.some_method }.to have_used_db_indexes
67
+ end
68
+ ```
69
+
70
+ Given that some results are uncertain, it's also possible to set the matcher to fail only with certain results:
71
+
72
+ ```ruby
73
+ it 'uses an index for all the queries' do
74
+ expect { SomeClass.some_method }.to have_used_db_indexes(only_certains: true)
75
+ end
76
+ ```
77
+
78
+ ### 2 - Watching all queries from a start point
79
+
80
+ Add this line to enable it:
81
+
82
+ ```ruby
83
+ WtActiverecordIndexSpy.watch_queries
84
+ ```
85
+
86
+ After that, `wt_activerecord_index_spy` will run an `EXPLAIN` query for every query
87
+ fired with `ActiveRecord` that has a `WHERE` condition.
88
+
89
+ Finally, you can generate a report with the results:
90
+
91
+ ```ruby
92
+ WtActiverecordIndexSpy.export_html_results
93
+ ```
94
+
95
+ This method creates an HTML file with a report and prints its location to STDOUT.
96
+
97
+ The content of this file is similar to this:
98
+
99
+ | Level | Identifier | Query | Origin |
100
+ | ---- | ---------- | ----- | ------ |
101
+ | certain | User Load | SELECT `users`.* FROM `users` WHERE `users`.`name` = 'lala' LIMIT 1 | spec/wt_activerecord_index_spy_spec.rb:162 |
102
+ | uncertain | User Load | SELECT `users`.* FROM `users` WHERE `users`.`city_id` IN (SELECT `cities`.`id` FROM `cities` WHERE `cities`.`name` = 'Santo Andre') | spec/wt_activerecord_index_spy_spec.rb:173 |
103
+
104
+ Where:
105
+ - **Level**: `certain` when it is certain that an index is missing, or `uncertain` when it's not possible to be sure
106
+ - **Identifier**: is the query identifier reported `ActiveRecord` notification
107
+ - **Origin**: is the line the query was fired
108
+
109
+ This mode, by default, **ignores** queries that were originated in test code. For that, it considers files which path includes `test/` or `spec/`.
110
+
111
+ It's possible to disable it as follows:
112
+
113
+ ```ruby
114
+ WtActiverecordIndexSpy.watch_queries(ignore_queries_originated_in_test_code: false)
115
+ ```
116
+
117
+ If the same query runs in many places, only one origin will be added to the report.
118
+
119
+ It's also possible to get the results using the following methods:
120
+
121
+ ```ruby
122
+ # Returns a list of certain results
123
+ WtActiverecordIndexSpy.certain_results
124
+
125
+ # Returns a list of certain and uncertain results mixed
126
+ WtActiverecordIndexSpy.results
127
+ ```
128
+
129
+ ### 3 - Watching all queries given a block
130
+
131
+ It's also possible to enable it in a specific context, using a block:
132
+
133
+ ```ruby
134
+ WtActiverecordIndexSpy.watch_queries do
135
+ # some code...
136
+ end
137
+ ```
138
+
139
+ After that, you can generate the HTML report with:
140
+
141
+ ```ruby
142
+ WtActiverecordIndexSpy.export_html_results
143
+ ```
144
+
145
+ ## Supported versions
146
+
147
+ Currently, it supports:
148
+
149
+ **Ruby**: 2.7, 2.6, 2.5
150
+
151
+ **Mysql**: 5.7
152
+
153
+ **PostgreSQL**: 13.2
154
+
155
+ Note: Currently, the PostgreSQL query analyser is not so intelligent and can't be
156
+ certain if an index is missing or not. So all results are `uncertain`. More
157
+ details in https://github.com/WeTransfer/wt_activerecord_index_spy/issues/12
158
+
159
+ **ActiveRecord**: 6, 5, 4
160
+
161
+ **RSpec**: 3.x
162
+
163
+ ## Development
164
+
165
+ After checking out the repo, run `cp .env.template .env` and set database credentials to run the tests.
166
+
167
+ Run `bin/setup` to install dependencies.
168
+
169
+ Then, run `bundle exec rake db:create db:migrate` to create the Mysql databases and `ADAPTER=postgresql bundle exec rake db:create db:migrate` to create the PostgresSQL database.
170
+
171
+ Then, run `bundle exec rspec` to run the tests for Mysql and `ADAPTER=postgresql bundle exec rspec`.
172
+
173
+ You can also run `bundle exec bin/console` for an interactive prompt that will allow you to experiment.
174
+
175
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
176
+
177
+ ## Contributing
178
+
179
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wetransfer/wt_activerecord_index_spy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](./CODE_OF_CONDUCT.md).
180
+ Please add your name to the [CONTRIBUTORS.md](./CONTRIBUTORS.md)
181
+
182
+ ## License
183
+
184
+ The gem is available as open source under the terms of the [Hippocratic License](https://firstdonoharm.dev/version/2/1/license.html).
185
+
186
+ ## Code of Conduct
187
+
188
+ Everyone interacting in the WT Activerecord Index Spy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](./CODE_OF_CONDUCT.md).