activerecord-mysql-sql-cache 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd23d2756d2744f20bea19fcd7bab8ed9f527e92
4
+ data.tar.gz: 122ba41bf13f3eb4f0ab8ec06a6ca078607f585c
5
+ SHA512:
6
+ metadata.gz: f607f20d2d13e0fe7e473901e3479b789ebad041dd79a75a46f70433edea3131e70b148154c09d83ed4b5481ca22583a7b52998f5f20f86594fa46c03df63716
7
+ data.tar.gz: f6b0fb44bdf2545dfb379f5198cf6c125e7eb34f5e77330f8d847189954ddfdca283a13268842075eabb23b3748c77562711c5fdf91781ebc704d2a988c6a018
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,15 @@
1
+ env:
2
+ - mysql2://travis@127.0.0.1/ar_test
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.6
7
+ - 2.2.2
8
+ install: bundle install
9
+ script: bundle exec rake spec
10
+ before_script:
11
+ - mysql -e 'create database ar_test;'
12
+ gemfile:
13
+ - gemfiles/4.0.gemfile
14
+ - gemfiles/4.1.gemfile
15
+ - gemfiles/4.2.gemfile
@@ -0,0 +1,11 @@
1
+ appraise '4.0' do
2
+ gem "activerecord", "~> 4.0.0"
3
+ end
4
+
5
+ appraise '4.1' do
6
+ gem "activerecord", "~> 4.1.0"
7
+ end
8
+
9
+ appraise '4.2' do
10
+ gem "activerecord", "~> 4.2.0"
11
+ end
@@ -0,0 +1,3 @@
1
+ # Change Log
2
+ ## 0.1.0
3
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-mysql-sql-cache.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Issei Naruta
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,2 @@
1
+ #boot2docker: boot2docker ssh -N -L 8806:localhost:3306
2
+ docker_mysql: docker run -p 0.0.0.0:3306:3306 -e MYSQL_DATABASE=ar_test -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql
@@ -0,0 +1,67 @@
1
+ [![Build Status](https://travis-ci.org/mirakui/activerecord-mysql-sql-cache.svg)](https://travis-ci.org/mirakui/activerecord-mysql-sql-cache)
2
+
3
+ # activerecord-mysql-sql-cache
4
+
5
+ An ActiveRecord extension for enabling SQL\_CACHE and SQL\_NO\_CACHE in MySQL queries
6
+
7
+ ## Dependencies
8
+
9
+ Supported versions:
10
+
11
+ - Ruby 1.9+
12
+ - ActiveRecord 4.0+ (Arel 4.0+)
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'activerecord-mysql-sql-cache'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install activerecord-mysql-sql-cache
29
+
30
+ ## Usage
31
+
32
+ Methods that `#sql_cache` and `#sql_no_cache` are added into `ActiveRecord::Relation`.
33
+
34
+ ```ruby
35
+ class Product < ActiveRecord::Base
36
+ end
37
+
38
+ Product.where(id: 1).to_sql
39
+ # => SELECT `products`.`*` FROM `products` WHERE `id` = 1;
40
+
41
+ Product.where(id: 1).sql_cache.to_sql
42
+ # => SELECT SQL_CACHE `products`.`*` FROM `products` WHERE `id` = 1;
43
+
44
+ Product.where(id: 1).sql_no_cache.to_sql
45
+ # => SELECT SQL_NO_CACHE `products`.`*` FROM `products` WHERE `id` = 1;
46
+ ```
47
+
48
+ `#sql_cache` accepts a boolean argument:
49
+
50
+ ```ruby
51
+ # default: true
52
+ Product.where(id: 1).sql_cache(true).to_sql
53
+ # => SELECT SQL_CACHE `products`.`*` FROM `products` WHERE `id` = 1;
54
+
55
+ Product.where(id: 1).sql_cache(false).to_sql
56
+ # => SELECT SQL_NO_CACHE `products`.`*` FROM `products` WHERE `id` = 1;
57
+
58
+ Product.where(id: 1).sql_cache(nil).to_sql
59
+ # => SELECT `products`.`*` FROM `products` WHERE `id` = 1;
60
+ ```
61
+
62
+ ## License
63
+ Arproxy is released under the MIT license:
64
+
65
+ * www.opensource.org/licenses/MIT
66
+
67
+ Copyright (c) 2015 Issei Naruta
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activerecord-mysql-sql-cache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-mysql-sql-cache"
8
+ spec.version = ActiverecordMysqlSqlCache::VERSION
9
+ spec.authors = ["Issei Naruta"]
10
+ spec.email = ["naruta@cookpad.com"]
11
+ spec.summary = %q{An ActiveRecord extension for enabling SQL\_CACHE and SQL\_NO\_CACHE in MySQL queries}
12
+ spec.homepage = "https://github.com/mirakui/activerecord-mysql-sql-cache"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activerecord", ">= 4.0"
21
+ spec.add_dependency "arel", ">= 4.0"
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "arproxy"
25
+ spec.add_development_dependency "appraisal"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "mysql2"
28
+ end
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.0.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ activerecord-mysql-sql-cache (0.0.1)
5
+ activerecord (>= 4.0)
6
+ arel (>= 4.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.0.13)
12
+ activesupport (= 4.0.13)
13
+ builder (~> 3.1.0)
14
+ activerecord (4.0.13)
15
+ activemodel (= 4.0.13)
16
+ activerecord-deprecated_finders (~> 1.0.2)
17
+ activesupport (= 4.0.13)
18
+ arel (~> 4.0.0)
19
+ activerecord-deprecated_finders (1.0.4)
20
+ activesupport (4.0.13)
21
+ i18n (~> 0.6, >= 0.6.9)
22
+ minitest (~> 4.2)
23
+ multi_json (~> 1.3)
24
+ thread_safe (~> 0.1)
25
+ tzinfo (~> 0.3.37)
26
+ appraisal (2.0.1)
27
+ activesupport (>= 3.2.21)
28
+ bundler
29
+ rake
30
+ thor (>= 0.14.0)
31
+ arel (4.0.2)
32
+ arproxy (0.1.3)
33
+ activerecord (>= 3.0.0)
34
+ builder (3.1.4)
35
+ diff-lcs (1.2.5)
36
+ i18n (0.7.0)
37
+ minitest (4.7.5)
38
+ multi_json (1.11.0)
39
+ mysql2 (0.3.18)
40
+ rake (10.4.2)
41
+ rspec (3.2.0)
42
+ rspec-core (~> 3.2.0)
43
+ rspec-expectations (~> 3.2.0)
44
+ rspec-mocks (~> 3.2.0)
45
+ rspec-core (3.2.3)
46
+ rspec-support (~> 3.2.0)
47
+ rspec-expectations (3.2.1)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.2.0)
50
+ rspec-mocks (3.2.1)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.2.0)
53
+ rspec-support (3.2.2)
54
+ thor (0.19.1)
55
+ thread_safe (0.3.5)
56
+ tzinfo (0.3.43)
57
+
58
+ PLATFORMS
59
+ ruby
60
+
61
+ DEPENDENCIES
62
+ activerecord (~> 4.0.0)
63
+ activerecord-mysql-sql-cache!
64
+ appraisal
65
+ arproxy
66
+ bundler (~> 1.7)
67
+ mysql2
68
+ rake (~> 10.0)
69
+ rspec
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.1.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ activerecord-mysql-sql-cache (0.0.1)
5
+ activerecord (>= 4.0)
6
+ arel (>= 4.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.1.10)
12
+ activesupport (= 4.1.10)
13
+ builder (~> 3.1)
14
+ activerecord (4.1.10)
15
+ activemodel (= 4.1.10)
16
+ activesupport (= 4.1.10)
17
+ arel (~> 5.0.0)
18
+ activesupport (4.1.10)
19
+ i18n (~> 0.6, >= 0.6.9)
20
+ json (~> 1.7, >= 1.7.7)
21
+ minitest (~> 5.1)
22
+ thread_safe (~> 0.1)
23
+ tzinfo (~> 1.1)
24
+ appraisal (2.0.1)
25
+ activesupport (>= 3.2.21)
26
+ bundler
27
+ rake
28
+ thor (>= 0.14.0)
29
+ arel (5.0.1.20140414130214)
30
+ arproxy (0.1.3)
31
+ activerecord (>= 3.0.0)
32
+ builder (3.2.2)
33
+ diff-lcs (1.2.5)
34
+ i18n (0.7.0)
35
+ json (1.8.2)
36
+ minitest (5.6.0)
37
+ mysql2 (0.3.18)
38
+ rake (10.4.2)
39
+ rspec (3.2.0)
40
+ rspec-core (~> 3.2.0)
41
+ rspec-expectations (~> 3.2.0)
42
+ rspec-mocks (~> 3.2.0)
43
+ rspec-core (3.2.3)
44
+ rspec-support (~> 3.2.0)
45
+ rspec-expectations (3.2.1)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.2.0)
48
+ rspec-mocks (3.2.1)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.2.0)
51
+ rspec-support (3.2.2)
52
+ thor (0.19.1)
53
+ thread_safe (0.3.5)
54
+ tzinfo (1.2.2)
55
+ thread_safe (~> 0.1)
56
+
57
+ PLATFORMS
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ activerecord (~> 4.1.0)
62
+ activerecord-mysql-sql-cache!
63
+ appraisal
64
+ arproxy
65
+ bundler (~> 1.7)
66
+ mysql2
67
+ rake (~> 10.0)
68
+ rspec
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 4.2.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ activerecord-mysql-sql-cache (0.0.1)
5
+ activerecord (>= 4.0)
6
+ arel (>= 4.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.2.1)
12
+ activesupport (= 4.2.1)
13
+ builder (~> 3.1)
14
+ activerecord (4.2.1)
15
+ activemodel (= 4.2.1)
16
+ activesupport (= 4.2.1)
17
+ arel (~> 6.0)
18
+ activesupport (4.2.1)
19
+ i18n (~> 0.7)
20
+ json (~> 1.7, >= 1.7.7)
21
+ minitest (~> 5.1)
22
+ thread_safe (~> 0.3, >= 0.3.4)
23
+ tzinfo (~> 1.1)
24
+ appraisal (2.0.1)
25
+ activesupport (>= 3.2.21)
26
+ bundler
27
+ rake
28
+ thor (>= 0.14.0)
29
+ arel (6.0.0)
30
+ arproxy (0.1.3)
31
+ activerecord (>= 3.0.0)
32
+ builder (3.2.2)
33
+ diff-lcs (1.2.5)
34
+ i18n (0.7.0)
35
+ json (1.8.2)
36
+ minitest (5.6.0)
37
+ mysql2 (0.3.18)
38
+ rake (10.4.2)
39
+ rspec (3.2.0)
40
+ rspec-core (~> 3.2.0)
41
+ rspec-expectations (~> 3.2.0)
42
+ rspec-mocks (~> 3.2.0)
43
+ rspec-core (3.2.3)
44
+ rspec-support (~> 3.2.0)
45
+ rspec-expectations (3.2.1)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.2.0)
48
+ rspec-mocks (3.2.1)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.2.0)
51
+ rspec-support (3.2.2)
52
+ thor (0.19.1)
53
+ thread_safe (0.3.5)
54
+ tzinfo (1.2.2)
55
+ thread_safe (~> 0.1)
56
+
57
+ PLATFORMS
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ activerecord (~> 4.2.0)
62
+ activerecord-mysql-sql-cache!
63
+ appraisal
64
+ arproxy
65
+ bundler (~> 1.7)
66
+ mysql2
67
+ rake (~> 10.0)
68
+ rspec
@@ -0,0 +1,20 @@
1
+ patche_targets = %w[
2
+ active_record/base
3
+ active_record/relation
4
+ arel/nodes/select_core
5
+ arel/visitors/mysql
6
+ arel/select_manager
7
+ ]
8
+
9
+ ActiveSupport.on_load :active_record do
10
+ patche_targets.each do |target|
11
+ require target
12
+ require_relative "./activerecord-mysql-sql-cache/patches/#{target}.rb"
13
+
14
+ camelized_target = target.camelize
15
+ camelized_target.gsub!('Mysql', 'MySQL')
16
+ target_cls = camelized_target.constantize
17
+ patch_mod = "ActiverecordMysqlSqlCache::Patches::#{camelized_target}".constantize
18
+ target_cls.send :include, patch_mod
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module ActiverecordMysqlSqlCache
2
+ module Patches
3
+ module ActiveRecord
4
+ module Base
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ def self.sql_cache(enabled=true)
9
+ all.sql_cache(enabled)
10
+ end
11
+
12
+ def self.sql_no_cache
13
+ sql_cache(false)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,42 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActiverecordMysqlSqlCache
4
+ module Patches
5
+ module ActiveRecord
6
+ module Relation
7
+ extend ActiveSupport::Concern
8
+
9
+ def mysql_sql_cache_value=(value)
10
+ @values[:mysql_sql_cache] = value
11
+ end
12
+
13
+ def mysql_sql_cache_value
14
+ @values[:mysql_sql_cache]
15
+ end
16
+
17
+ def sql_cache(enabled=true)
18
+ if enabled.nil?
19
+ self.mysql_sql_cache_value = nil
20
+ else
21
+ self.mysql_sql_cache_value = enabled ? ' SQL_CACHE ' : ' SQL_NO_CACHE '
22
+ end
23
+ self
24
+ end
25
+
26
+ def sql_no_cache
27
+ sql_cache(false)
28
+ end
29
+
30
+ included do
31
+ def build_arel_with_sql_cache
32
+ build_arel_without_sql_cache.tap do |arel|
33
+ arel.mysql_sql_cache = self.mysql_sql_cache_value
34
+ end
35
+ end
36
+ alias_method_chain :build_arel, :sql_cache
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
@@ -0,0 +1,11 @@
1
+ module ActiverecordMysqlSqlCache
2
+ module Patches
3
+ module Arel
4
+ module Nodes
5
+ module SelectCore
6
+ attr_accessor :mysql_sql_cache
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module ActiverecordMysqlSqlCache
2
+ module Patches
3
+ module Arel
4
+ module SelectManager
5
+ def mysql_sql_cache=(value)
6
+ @ctx.mysql_sql_cache = value
7
+ end
8
+
9
+ def mysql_sql_cache
10
+ @ctx.mysql_sql_cache
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActiverecordMysqlSqlCache
4
+ module Patches
5
+ module Arel
6
+ module Visitors
7
+ module MySQL
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ alias_method_chain :visit_Arel_Nodes_SelectCore, :sql_cache
12
+ end
13
+
14
+ def visit_Arel_Nodes_SelectCore_with_sql_cache(o, collector)
15
+ result = visit_Arel_Nodes_SelectCore_without_sql_cache(o, collector)
16
+ case result
17
+ when String
18
+ distinct = ' DISTINCT '
19
+ select = 'SELECT '
20
+ if result =~ /^#{select}/
21
+ if idx = result.index(distinct)
22
+ result.insert(idx + distinct.length, o.mysql_sql_cache.to_s)
23
+ else
24
+ result.insert(select.length, o.mysql_sql_cache.to_s)
25
+ end
26
+ end
27
+ else # Arel 6.0+
28
+ if idx = result.value.index('DISTINCT')
29
+ result.value.insert(idx + 1, o.mysql_sql_cache.to_s)
30
+ else
31
+ result.value.insert(1, o.mysql_sql_cache.to_s)
32
+ end
33
+ end
34
+ result
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module ActiverecordMysqlSqlCache
2
+ VERSION = "0.1.0"
3
+ end
File without changes
@@ -0,0 +1,92 @@
1
+ require 'active_record'
2
+ require 'activerecord-mysql-sql-cache'
3
+ require 'arproxy'
4
+
5
+ class LastQueryLogger < Arproxy::Base
6
+ def execute(sql, name=nil)
7
+ Thread.current[:last_query] = sql
8
+ super
9
+ end
10
+ end
11
+
12
+ class Product < ActiveRecord::Base
13
+ belongs_to :user
14
+ end
15
+
16
+ class User < ActiveRecord::Base
17
+ has_many :products
18
+ end
19
+
20
+ def initialize_database
21
+ db_url = ENV['DATABASE_URL'] || 'mysql2://root@0.0.0.0/ar_test'
22
+
23
+ ActiveRecord::Base.logger = Logger.new('log/test.log')
24
+ ActiveRecord::Base.establish_connection db_url
25
+
26
+ Arproxy.configure do |config|
27
+ config.adapter = "mysql2"
28
+ config.logger = ActiveRecord::Base.logger
29
+ config.use LastQueryLogger
30
+ end
31
+ Arproxy.enable!
32
+
33
+ con = ActiveRecord::Base.connection
34
+
35
+ con.execute 'DROP TABLE IF EXISTS products'
36
+ con.execute <<-DDL
37
+ CREATE TABLE products (
38
+ id INT PRIMARY KEY AUTO_INCREMENT,
39
+ name VARCHAR(100) NOT NULL,
40
+ user_id INT
41
+ )
42
+ DDL
43
+ con.execute 'DROP TABLE IF EXISTS users'
44
+ con.execute <<-DDL
45
+ CREATE TABLE users (
46
+ id INT PRIMARY KEY AUTO_INCREMENT,
47
+ name VARCHAR(100) NOT NULL
48
+ )
49
+ DDL
50
+
51
+ alice = User.create name: 'alice'
52
+ Product.new name: 'chocolate', user: alice
53
+ Product.new name: 'cookie', user: alice
54
+ end
55
+
56
+ describe 'ActiveRecord MySQL SQL_CACHE support' do
57
+ before(:all) do
58
+ initialize_database
59
+ end
60
+
61
+ context 'with AR::Relation' do
62
+ let(:rel) { Product.limit(1) }
63
+
64
+ it { expect(rel.to_sql).to be_sql_like("SELECT `products`.* FROM `products` LIMIT 1") }
65
+ it { expect(rel.sql_cache(nil).to_sql).to be_sql_like("SELECT `products`.* FROM `products` LIMIT 1") }
66
+ it { expect(rel.sql_cache.to_sql).to be_sql_like("SELECT SQL_CACHE `products`.* FROM `products` LIMIT 1") }
67
+ it { expect(rel.sql_cache(true).to_sql).to be_sql_like("SELECT SQL_CACHE `products`.* FROM `products` LIMIT 1") }
68
+ it { expect(rel.sql_cache(false).to_sql).to be_sql_like("SELECT SQL_NO_CACHE `products`.* FROM `products` LIMIT 1") }
69
+ it { expect(rel.sql_no_cache.to_sql).to be_sql_like("SELECT SQL_NO_CACHE `products`.* FROM `products` LIMIT 1") }
70
+
71
+ it do
72
+ expect(rel.distinct.select(:name).sql_cache.to_sql).to be_sql_like(
73
+ Arel::VERSION < '5.0' ?
74
+ "SELECT DISTINCT SQL_CACHE name FROM `products` LIMIT 1" :
75
+ "SELECT DISTINCT SQL_CACHE `products`.`name` FROM `products` LIMIT 1"
76
+ )
77
+ end
78
+
79
+ it do
80
+ Product.all.sql_cache.count
81
+ expect(Thread.current[:last_query]).to be_sql_like("SELECT SQL_CACHE COUNT(*) FROM `products`")
82
+ end
83
+ end
84
+
85
+ context 'with AR::Base' do
86
+ it { expect(Product.sql_cache(nil).limit(1).to_sql).to be_sql_like("SELECT `products`.* FROM `products` LIMIT 1") }
87
+ it { expect(Product.sql_cache.limit(1).to_sql).to be_sql_like("SELECT SQL_CACHE `products`.* FROM `products` LIMIT 1") }
88
+ it { expect(Product.sql_cache(true).limit(1).to_sql).to be_sql_like("SELECT SQL_CACHE `products`.* FROM `products` LIMIT 1") }
89
+ it { expect(Product.sql_cache(false).limit(1).to_sql).to be_sql_like("SELECT SQL_NO_CACHE `products`.* FROM `products` LIMIT 1") }
90
+ it { expect(Product.sql_no_cache.limit(1).to_sql).to be_sql_like("SELECT SQL_NO_CACHE `products`.* FROM `products` LIMIT 1") }
91
+ end
92
+ end
@@ -0,0 +1,97 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Limits the available syntax to the non-monkey patched syntax that is
54
+ # recommended. For more details, see:
55
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58
+ config.disable_monkey_patching!
59
+
60
+ # This setting enables warnings. It's recommended, but in some cases may
61
+ # be too noisy due to issues in dependencies.
62
+ config.warnings = true
63
+
64
+ # Many RSpec users commonly either run the entire suite or an individual
65
+ # file, and it's useful to allow more verbose output when running an
66
+ # individual spec file.
67
+ if config.files_to_run.one?
68
+ # Use the documentation formatter for detailed output,
69
+ # unless a formatter has already been configured
70
+ # (e.g. via a command-line flag).
71
+ config.default_formatter = 'doc'
72
+ end
73
+
74
+ # Print the 10 slowest examples and example groups at the
75
+ # end of the spec run, to help surface which specs are running
76
+ # particularly slow.
77
+ config.profile_examples = 10
78
+
79
+ # Run specs in random order to surface order dependencies. If you find an
80
+ # order dependency and want to debug it, you can fix the order by providing
81
+ # the seed, which is printed after each run.
82
+ # --seed 1234
83
+ config.order = :random
84
+
85
+ # Seed global randomization in this process using the `--seed` CLI option.
86
+ # Setting this allows you to use `--seed` to deterministically reproduce
87
+ # test failures related to randomization by passing the same `--seed` value
88
+ # as the one that triggered the failure.
89
+ Kernel.srand config.seed
90
+ =end
91
+ end
92
+
93
+ RSpec::Matchers.define(:be_sql_like) do |expected|
94
+ match do |actual|
95
+ actual.gsub(/ +/, ' ') == expected.gsub(/ +/, ' ')
96
+ end
97
+ end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-mysql-sql-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Issei Naruta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: arel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: arproxy
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: appraisal
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: mysql2
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - naruta@cookpad.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - Appraisals
136
+ - ChangeLog.md
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - Procfile
140
+ - README.md
141
+ - Rakefile
142
+ - activerecord-mysql-sql-cache.gemspec
143
+ - gemfiles/4.0.gemfile
144
+ - gemfiles/4.0.gemfile.lock
145
+ - gemfiles/4.1.gemfile
146
+ - gemfiles/4.1.gemfile.lock
147
+ - gemfiles/4.2.gemfile
148
+ - gemfiles/4.2.gemfile.lock
149
+ - lib/activerecord-mysql-sql-cache.rb
150
+ - lib/activerecord-mysql-sql-cache/patches/active_record/base.rb
151
+ - lib/activerecord-mysql-sql-cache/patches/active_record/relation.rb
152
+ - lib/activerecord-mysql-sql-cache/patches/arel/nodes/select_core.rb
153
+ - lib/activerecord-mysql-sql-cache/patches/arel/select_manager.rb
154
+ - lib/activerecord-mysql-sql-cache/patches/arel/visitors/mysql.rb
155
+ - lib/activerecord-mysql-sql-cache/version.rb
156
+ - log/.gitkeep
157
+ - spec/activerecord-mysql-sql-cache_spec.rb
158
+ - spec/spec_helper.rb
159
+ homepage: https://github.com/mirakui/activerecord-mysql-sql-cache
160
+ licenses:
161
+ - MIT
162
+ metadata: {}
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ requirements: []
178
+ rubyforge_project:
179
+ rubygems_version: 2.4.5
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: An ActiveRecord extension for enabling SQL\_CACHE and SQL\_NO\_CACHE in MySQL
183
+ queries
184
+ test_files:
185
+ - spec/activerecord-mysql-sql-cache_spec.rb
186
+ - spec/spec_helper.rb