Skip to content

Commit

Permalink
Raise ActiveRecord::StatementInvalid for Rails 3.1 and below
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Aug 3, 2014
1 parent 377c797 commit e80c0df
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
4 changes: 4 additions & 0 deletions lib/pghero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,13 @@ def rds?
end

def explain(sql)
sql = sql.to_s
explanation = nil
# use transaction for safety
Connection.transaction do
if ActiveRecord::VERSION::MAJOR == 3 and ActiveRecord::VERSION::MINOR < 2 and sql.include?(";")
raise ActiveRecord::StatementInvalid
end
explanation = select_all("EXPLAIN #{sql}").map{|v| v["QUERY PLAN"] }.join("\n")
raise ActiveRecord::Rollback
end
Expand Down
7 changes: 1 addition & 6 deletions test/pghero_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ def test_explain

def test_explain_multiple_statements
User.create!
if ActiveRecord::VERSION::MAJOR == 3 and ActiveRecord::VERSION::MINOR < 2
PgHero.explain("ANALYZE DELETE FROM users; DELETE FROM users; COMMIT")
assert_equal 1, User.count
else
assert_raises(ActiveRecord::StatementInvalid){ PgHero.explain("ANALYZE DELETE FROM users; DELETE FROM users; COMMIT") }
end
assert_raises(ActiveRecord::StatementInvalid){ PgHero.explain("ANALYZE DELETE FROM users; DELETE FROM users; COMMIT") }
end

end

0 comments on commit e80c0df

Please sign in to comment.