From 90604da93b0abcb7188f2ae7dc20952525b6e626 Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Sat, 7 Oct 2023 18:55:26 +0100 Subject: [PATCH] Savepoint queries should be internally executed so that they don't clear the query cache --- .../sqlserver/database_statements.rb | 13 ---------- .../sqlserver/savepoints.rb | 24 +++++++++++++++++++ .../connection_adapters/sqlserver_adapter.rb | 4 +++- 3 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 lib/active_record/connection_adapters/sqlserver/savepoints.rb diff --git a/lib/active_record/connection_adapters/sqlserver/database_statements.rb b/lib/active_record/connection_adapters/sqlserver/database_statements.rb index de5732f40..9d1f75885 100644 --- a/lib/active_record/connection_adapters/sqlserver/database_statements.rb +++ b/lib/active_record/connection_adapters/sqlserver/database_statements.rb @@ -100,19 +100,6 @@ def exec_rollback_db_transaction execute "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION", "TRANSACTION" end - include Savepoints - - def create_savepoint(name = current_savepoint_name) - execute "SAVE TRANSACTION #{name}", "TRANSACTION" - end - - def exec_rollback_to_savepoint(name = current_savepoint_name) - execute "ROLLBACK TRANSACTION #{name}", "TRANSACTION" - end - - def release_savepoint(name = current_savepoint_name) - end - def case_sensitive_comparison(attribute, value) column = column_for_attribute(attribute) diff --git a/lib/active_record/connection_adapters/sqlserver/savepoints.rb b/lib/active_record/connection_adapters/sqlserver/savepoints.rb new file mode 100644 index 000000000..e1bc69c7d --- /dev/null +++ b/lib/active_record/connection_adapters/sqlserver/savepoints.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module ActiveRecord + module ConnectionAdapters + module SQLServer + module Savepoints + def current_savepoint_name + current_transaction.savepoint_name + end + + def create_savepoint(name = current_savepoint_name) + internal_execute("SAVE TRANSACTION #{name}", "TRANSACTION") + end + + def exec_rollback_to_savepoint(name = current_savepoint_name) + internal_execute("ROLLBACK TRANSACTION #{name}", "TRANSACTION") + end + + def release_savepoint(_name) + end + end + end + end +end diff --git a/lib/active_record/connection_adapters/sqlserver_adapter.rb b/lib/active_record/connection_adapters/sqlserver_adapter.rb index f8179f86f..f9552ad1c 100644 --- a/lib/active_record/connection_adapters/sqlserver_adapter.rb +++ b/lib/active_record/connection_adapters/sqlserver_adapter.rb @@ -17,6 +17,7 @@ require "active_record/connection_adapters/sqlserver/database_limits" require "active_record/connection_adapters/sqlserver/database_statements" require "active_record/connection_adapters/sqlserver/database_tasks" +require "active_record/connection_adapters/sqlserver/savepoints" require "active_record/connection_adapters/sqlserver/transaction" require "active_record/connection_adapters/sqlserver/errors" require "active_record/connection_adapters/sqlserver/schema_creation" @@ -40,7 +41,8 @@ class SQLServerAdapter < AbstractAdapter SQLServer::Showplan, SQLServer::SchemaStatements, SQLServer::DatabaseLimits, - SQLServer::DatabaseTasks + SQLServer::DatabaseTasks, + SQLServer::Savepoints ADAPTER_NAME = "SQLServer".freeze