Install gems listed in Gemfile (See below for how to install mysql gem.)
MySql installed and running. The supplied database.yml file specifies the 'test' database. You may wish to change that. This file uses the Rails format.
Create SQL log file from some other app that uses MySql with ActiveRecord using instructions below. Note that there is a sample log file in test/index_tester/fixture if you want to try it quickly.
If not using Bundler:
env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
If using Bundler:
edit Gemfile to add line for mysql
export ARCHFLAGS=“-arch x86_64”
bundle config build.mysql –with-mysql-config=/usr/local/mysql/bin/mysql_config
If you don't have an appropriate app, you can use mine github.com/ghendry/demo-sinatra It already has the log notification code in main.rb and is otherwise the world's most boring web app, but it does have an index error to be found by IndexTester. Load it with several people with the same last name and different first names and then search by name to see the index error that will slow down queries.
For classy Sinatra apps, add the code in setup/index_tester/sinatra_notifications.rb to your Sinatra code, after the line
class App > Sinatra::Base
Specify the file name for the log file
For Rails 3 apps, copy notifications.rb from setup/index_tester to your application’s /config/initalizers directory. Specify the file name for the log file
From root directory, run
ruby script/index_tester/write_index_tests.rb mysql-query-file database-yml-file > test-output.rb
You may also specify a third parameter to choose an environment other than development from the yml file, and a fourth parameter to change the default minimum query rating from 70 to some other number between 0 and 100
This script generates code in the test-output.rb file which should be moved to the test directory of your app that generated the MySql logs so it can run in your test suite. These tests run the queries so they are not suitable for unit testing. The only change you may need to make to the output file so your tests run is the path to you database.yml file.
1.0 first submission
1.1 added generation of query tests
1.2 added real MySql data access
More databases. Start with PostgreSql then SQLite (but gem needs to be fixed to do explain correctl)
Package as gem.
Copyright © 2011 Ginny Hendry
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.