Skip to content

Commit

Permalink
Support with block and lvar
Browse files Browse the repository at this point in the history
  • Loading branch information
sue445 committed Nov 27, 2023
1 parent 9b10936 commit 6e6ac0d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/isucon/mixin/mysql2_xquery_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Mysql2XqueryMethods
# @!method find_xquery(node)
# @param node [RuboCop::AST::Node]
def_node_search :find_xquery, <<~PATTERN
(send (send nil? _) {:xquery | :query} (${str dstr lvar ivar cvar} $...) ...)
(send ({:send | :lvar} ...) {:xquery | :query} (${str dstr lvar ivar cvar} $...) ...)
PATTERN

NON_STRING_WARNING_MSG = "Warning: non-string was passed to `query` or `xquery` 1st argument. " \
Expand Down
36 changes: 36 additions & 0 deletions spec/rubocop/cop/isucon/mysql2/where_without_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,40 @@
end
end
end

context "with block" do
include_context :database_cop do
let(:schema) do
%w[
schemas/create_livestream_tags.rb
schemas/create_tags.rb
]
end
end

it "registers an offense" do
# c.f. https://github.com/isucon/isucon13/blob/d33a72acdb4029f1ca53ccbe90ff5f2348c8e5cc/webapp/ruby/app.rb#L105-L121
expect_offense(<<~RUBY)
def fill_livestream_response(tx, livestream_model)
# owner_model = tx.xquery('SELECT * FROM users WHERE id = ?', livestream_model.fetch(:user_id)).first
# owner = fill_user_response(tx, owner_model)
tags = tx.xquery('SELECT * FROM livestream_tags WHERE livestream_id = ?', livestream_model.fetch(:id)).map do |livestream_tag_model|
^^^^^^^^^^^^^^^^^ This where clause doesn't seem to have an index. (e.g. `ALTER TABLE livestream_tags ADD INDEX index_livestream_id (livestream_id)`)
tag_model = tx.xquery('SELECT * FROM tags WHERE id = ?', livestream_tag_model.fetch(:tag_id)).first
{
id: tag_model.fetch(:id),
name: tag_model.fetch(:name),
}
end
livestream_model.slice(:id, :title, :description, :playlist_url, :thumbnail_url, :start_at, :end_at).merge(
owner: owner,
tags: tags,
)
end
RUBY
end
end
end
10 changes: 10 additions & 0 deletions spec/schemas/create_livestream_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

ActiveRecord::Schema.verbose = false

ActiveRecord::Schema.define(version: 1) do
create_table :livestream_tags do |t|
t.integer :livestream_id, null: false
t.integer :tag_id, null: false
end
end
11 changes: 11 additions & 0 deletions spec/schemas/create_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

ActiveRecord::Schema.verbose = false

ActiveRecord::Schema.define(version: 1) do
create_table :tags do |t|
t.string :name, null: false
end

add_index :tags, :name, unique: true
end

0 comments on commit 6e6ac0d

Please sign in to comment.