提交 9a4ff4d6 编写于 作者: R Ryuta Kamizono

Add `supports_common_table_expressions?` for CTE testing

上级 f537b366
......@@ -416,6 +416,10 @@ def supports_optimizer_hints?
false
end
def supports_common_table_expressions?
false
end
def supports_lazy_transactions?
false
end
......
......@@ -110,6 +110,14 @@ def supports_optimizer_hints?
!mariadb? && database_version >= "5.7.7"
end
def supports_common_table_expressions?
if mariadb?
database_version >= "10.2.1"
else
database_version >= "8.0.1"
end
end
def supports_advisory_locks?
true
end
......
......@@ -67,7 +67,9 @@ def query(sql, name = nil) #:nodoc:
end
end
READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:begin, :commit, :explain, :select, :set, :show, :release, :savepoint, :rollback, :with) # :nodoc:
READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(
:begin, :commit, :explain, :select, :set, :show, :release, :savepoint, :rollback, :with
) # :nodoc:
private_constant :READ_QUERY
def write_query?(sql) # :nodoc:
......
......@@ -361,6 +361,10 @@ def supports_optimizer_hints?
@has_pg_hint_plan
end
def supports_common_table_expressions?
true
end
def supports_lazy_transactions?
true
end
......
......@@ -4,7 +4,9 @@ module ActiveRecord
module ConnectionAdapters
module SQLite3
module DatabaseStatements
READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(:begin, :commit, :explain, :select, :pragma, :release, :savepoint, :rollback, :with) # :nodoc:
READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(
:begin, :commit, :explain, :select, :pragma, :release, :savepoint, :rollback, :with
) # :nodoc:
private_constant :READ_QUERY
def write_query?(sql) # :nodoc:
......
......@@ -144,6 +144,10 @@ def supports_json?
true
end
def supports_common_table_expressions?
database_version >= "3.8.3"
end
def supports_insert_on_conflict?
database_version >= "3.24.0"
end
......
......@@ -223,6 +223,20 @@ def test_doesnt_error_when_a_select_query_is_called_while_preventing_writes
end
end
if ActiveRecord::Base.connection.supports_common_table_expressions?
def test_doesnt_error_when_a_read_query_with_a_cte_is_called_while_preventing_writes
@connection.insert("INSERT INTO subscribers(nick) VALUES ('138853948594')")
@connection_handler.while_preventing_writes do
result = @connection.select_all(<<~SQL)
WITH matching_subscribers AS (SELECT subscribers.* FROM subscribers WHERE nick = '138853948594')
SELECT * FROM matching_subscribers
SQL
assert_equal 1, result.length
end
end
end
def test_uniqueness_violations_are_translated_to_specific_exception
@connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
error = assert_raises(ActiveRecord::RecordNotUnique) do
......
......@@ -232,15 +232,6 @@ def test_doesnt_error_when_a_read_query_with_leading_chars_is_called_while_preve
end
end
def test_doesnt_error_when_a_read_query_with_a_cte_is_called_while_preventing_writes
@conn.execute("INSERT INTO `engines` (`car_id`) VALUES ('138853948594')")
@connection_handler.while_preventing_writes do
sql = "WITH matching_cars AS (SELECT `engines`.* FROM `engines` WHERE `engines`.`car_id` = '138853948594') SELECT * FROM matching_cars"
assert_equal 1, @conn.execute(sql).entries.count
end
end
def test_statement_timeout_error_codes
raw_conn = @conn.raw_connection
assert_raises(ActiveRecord::StatementTimeout) do
......
......@@ -457,17 +457,6 @@ def test_doesnt_error_when_a_read_query_with_leading_chars_is_called_while_preve
end
end
def test_doesnt_error_when_a_read_query_with_a_cte_is_called_while_preventing_writes
with_example_table do
@connection.execute("INSERT INTO ex (data) VALUES ('138853948594')")
@connection_handler.while_preventing_writes do
sql = "WITH matching_ex_values AS (SELECT * FROM ex WHERE data = '138853948594') SELECT * FROM matching_ex_values"
assert_equal 1, @connection.execute(sql).entries.count
end
end
end
private
def with_example_table(definition = "id serial primary key, number integer, data character varying(255)", &block)
......
......@@ -653,17 +653,6 @@ def test_doesnt_error_when_a_read_query_with_leading_chars_is_called_while_preve
end
end
def test_doesnt_error_when_a_read_query_with_a_cte_is_called_while_preventing_writes
with_example_table "id int, data string" do
@conn.execute("INSERT INTO ex (data) VALUES ('138853948594')")
@connection_handler.while_preventing_writes do
sql = "WITH matching_ex_values AS (SELECT * FROM ex WHERE data = '138853948594') SELECT * FROM matching_ex_values"
assert_equal 1, @conn.execute(sql).entries.count
end
end
end
private
def assert_logged(logs)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册