Skip to content

Instantly share code, notes, and snippets.

@kirs
Last active July 18, 2019 10:49
Show Gist options
  • Save kirs/7bd8ca4e00b4797e4a795d5e8b911d85 to your computer and use it in GitHub Desktop.
Save kirs/7bd8ca4e00b4797e4a795d5e8b911d85 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
gem "sqlite3"
gem "pry"
end
require "active_record"
require "minitest/autorun"
require "logger"
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.integer :shop_id
end
create_table :shops, force: true do |t|
end
end
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def self.relation
super.optimizer_hints!("MAX_EXECUTION_TIME(20)")
end
private_class_method :relation
end
class Shop < ApplicationRecord
has_many :posts
end
class Post < ApplicationRecord
belongs_to :shop
end
class BugTest < Minitest::Test
def test_default_hint
# passes
expected = 'SELECT /*+ MAX_EXECUTION_TIME(20) */ "shops".* FROM "shops"'
assert_equal expected, Shop.all.to_sql
shop = Shop.create!
# fails
expected = 'SELECT /*+ MAX_EXECUTION_TIME(20) */ "posts".* FROM "posts" WHERE "posts"."shop_id" = 1'
assert_equal expected, shop.posts.to_sql
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment