Skip to content

Commit

Permalink
Lease the connection
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan committed Mar 11, 2024
1 parent 81daac8 commit 86078a5
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion test/cases/adapter_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ class AdapterTestSQLServer < ActiveRecord::TestCase

describe "block writes to a database" do
def setup
@conn = ActiveRecord::Base.connection
@conn = ActiveRecord::Base.lease_connection
end

def test_errors_when_an_insert_query_is_called_while_preventing_writes
Expand Down
22 changes: 11 additions & 11 deletions test/cases/coerced_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class BasicsTest < ActiveRecord::TestCase
# Use square brackets as SQL Server escaped character
coerce_tests! :test_column_names_are_escaped
def test_column_names_are_escaped_coerced
conn = ActiveRecord::Base.connection
conn = ActiveRecord::Base.lease_connection
assert_equal "[t]]]", conn.quote_column_name("t]")
end

Expand Down Expand Up @@ -236,7 +236,7 @@ def test_update_date_time_attributes_with_default_timezone_local
ActiveRecord::Base.while_preventing_writes do
assert_queries(1, ignore_none: true) do
Bird.transaction do
ActiveRecord::Base.connection.materialize_transactions
ActiveRecord::Base.lease_connection.materialize_transactions
end
end
end
Expand Down Expand Up @@ -1542,9 +1542,9 @@ def test_dump_schema_information_outputs_lexically_reverse_ordered_versions_rega
@schema_migration.create_version(v)
end

schema_info = ActiveRecord::Base.connection.dump_schema_information
schema_info = ActiveRecord::Base.lease_connection.dump_schema_information
expected = <<~STR
INSERT INTO #{ActiveRecord::Base.connection.quote_table_name("schema_migrations")} (version) VALUES
INSERT INTO #{ActiveRecord::Base.lease_connection.quote_table_name("schema_migrations")} (version) VALUES
(N'20100301010101'),
(N'20100201010101'),
(N'20100101010101');
Expand Down Expand Up @@ -1602,7 +1602,7 @@ class SchemaDumperDefaultsCoerceTest < ActiveRecord::TestCase
include SchemaDumpingHelper

setup do
@connection = ActiveRecord::Base.connection
@connection = ActiveRecord::Base.lease_connection
@connection.create_table :dump_defaults, force: true do |t|
t.string :string_with_default, default: "Hello!"
t.date :date_with_default, default: "2014-06-05"
Expand Down Expand Up @@ -2213,15 +2213,15 @@ def test_insert_all_coerced
Task.cache { Task.insert({ starting: Time.now }) }
end

assert_called(ActiveRecord::Base.connection, :clear_query_cache, times: 2) do
assert_called(ActiveRecord::Base.lease_connection, :clear_query_cache, times: 2) do
Task.cache { Task.insert_all!([{ starting: Time.now }]) }
end

assert_called(ActiveRecord::Base.connection, :clear_query_cache, times: 2) do
assert_called(ActiveRecord::Base.lease_connection, :clear_query_cache, times: 2) do
Task.cache { Task.insert!({ starting: Time.now }) }
end

assert_called(ActiveRecord::Base.connection, :clear_query_cache, times: 2) do
assert_called(ActiveRecord::Base.lease_connection, :clear_query_cache, times: 2) do
Task.cache { Task.insert_all!([{ starting: Time.now }]) }
end

Expand Down Expand Up @@ -2287,7 +2287,7 @@ class MarshalSerializationTest < ActiveRecord::TestCase
undef_method :marshal_fixture_path
def marshal_fixture_path(file_name)
File.expand_path(
"support/marshal_compatibility_fixtures/#{ActiveRecord::Base.connection.adapter_name}/#{file_name}.dump",
"support/marshal_compatibility_fixtures/#{ActiveRecord::Base.lease_connection.adapter_name}/#{file_name}.dump",
ARTest::SQLServer.test_root_sqlserver
)
end
Expand Down Expand Up @@ -2379,7 +2379,7 @@ class BasePreventWritesTest < ActiveRecord::TestCase
ActiveRecord::Base.while_preventing_writes do
assert_queries(1, ignore_none: true) do
Bird.transaction do
ActiveRecord::Base.connection.materialize_transactions
ActiveRecord::Base.lease_connection.materialize_transactions
end
end
end
Expand Down Expand Up @@ -2489,7 +2489,7 @@ def test_sqlcommenter_format_value_string_coercible_coerced
def test_invalid_encoding_query_coerced
ActiveRecord::QueryLogs.tags = [ :application ]
assert_raises ActiveRecord::StatementInvalid do
ActiveRecord::Base.connection.execute "select 1 as '\xFF'"
ActiveRecord::Base.lease_connection.execute "select 1 as '\xFF'"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/cases/disconnected_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TestDisconnectedAdapter < ActiveRecord::TestCase

undef_method :setup
def setup
@connection = ActiveRecord::Base.connection
@connection = ActiveRecord::Base.lease_connection
end

teardown do
Expand Down
2 changes: 1 addition & 1 deletion test/cases/json_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "cases/helper_sqlserver"

if ActiveRecord::Base.connection.supports_json?
if ActiveRecord::Base.lease_connection.supports_json?
class JsonTestSQLServer < ActiveRecord::TestCase
before do
@o1 = SSTestDatatypeMigrationJson.create! json_col: { "a" => "a", "b" => "b", "c" => "c" }
Expand Down
4 changes: 2 additions & 2 deletions test/cases/lateral_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LateralTestSQLServer < ActiveRecord::TestCase
eq = Arel::Nodes::Equality.new(one, one)

sql = author.project(Arel.star).where(author[:name].matches("David")).outer_join(subselect.lateral.as("bar")).on(eq).to_sql
results = ActiveRecord::Base.connection.exec_query sql
results = ActiveRecord::Base.lease_connection.exec_query sql
assert_equal sql, "SELECT * FROM [authors] OUTER APPLY (SELECT * FROM [posts] WHERE [posts].[author_id] = [authors].[id] AND [posts].[id] = 42 ORDER BY [posts].[id] ASC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY) AS bar WHERE [authors].[name] LIKE N'David'"
assert_equal results.length, 1
end
Expand All @@ -27,7 +27,7 @@ class LateralTestSQLServer < ActiveRecord::TestCase
subselect = post.project(Arel.star).take(1).where(post[:author_id].eq(author[:id])).where(post[:id].eq(42))

sql = author.project(Arel.star).where(author[:name].matches("David")).join(subselect.lateral.as("bar")).to_sql
results = ActiveRecord::Base.connection.exec_query sql
results = ActiveRecord::Base.lease_connection.exec_query sql

assert_equal sql, "SELECT * FROM [authors] CROSS APPLY (SELECT * FROM [posts] WHERE [posts].[author_id] = [authors].[id] AND [posts].[id] = 42 ORDER BY [posts].[id] ASC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY) AS bar WHERE [authors].[name] LIKE N'David'"
assert_equal results.length, 0
Expand Down
4 changes: 2 additions & 2 deletions test/cases/primary_keys_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Barcode < ActiveRecord::Base
end

setup do
@connection = ActiveRecord::Base.connection
@connection = ActiveRecord::Base.lease_connection
@connection.create_table(:barcodes, primary_key: "code", id: :uuid, force: true)
end

Expand Down Expand Up @@ -50,7 +50,7 @@ class Widget < ActiveRecord::Base
end

setup do
@connection = ActiveRecord::Base.connection
@connection = ActiveRecord::Base.lease_connection
end

teardown do
Expand Down
4 changes: 2 additions & 2 deletions test/cases/schema_dumper_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class SchemaDumperTestSQLServer < ActiveRecord::TestCase
before { all_tables }

let(:all_tables) { ActiveRecord::Base.connection.tables }
let(:all_tables) { ActiveRecord::Base.lease_connection.tables }
let(:schema) { @generated_schema }

it "sst_datatypes" do
Expand Down Expand Up @@ -166,7 +166,7 @@ def generate_schema_for_table(*table_names)

stream = StringIO.new
ActiveRecord::SchemaDumper.ignore_tables = all_tables - table_names
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.lease_connection, stream)
@generated_schema = stream.string
yield @generated_schema if block_given?
@schema_lines = Hash.new
Expand Down
4 changes: 2 additions & 2 deletions test/cases/specific_schema_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ def quoted_id

it "returns a new id via connection newid_function" do
acceptable_uuid = ActiveRecord::ConnectionAdapters::SQLServer::Type::Uuid::ACCEPTABLE_UUID
db_uuid = ActiveRecord::Base.connection.newid_function
db_uuid = ActiveRecord::Base.lease_connection.newid_function
_(db_uuid).must_match(acceptable_uuid)
end

# with similar table definition in two schemas

it "returns the correct primary columns" do
connection = ActiveRecord::Base.connection
connection = ActiveRecord::Base.lease_connection
assert_equal "field_1", connection.columns("test.sst_schema_test_mulitple_schema").detect(&:is_primary?).name
assert_equal "field_2", connection.columns("test2.sst_schema_test_mulitple_schema").detect(&:is_primary?).name
end
Expand Down
2 changes: 1 addition & 1 deletion test/cases/view_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "cases/helper_sqlserver"

class ViewTestSQLServer < ActiveRecord::TestCase
let(:connection) { ActiveRecord::Base.connection }
let(:connection) { ActiveRecord::Base.lease_connection }

describe 'view with default values' do
before do
Expand Down
2 changes: 1 addition & 1 deletion test/support/connection_reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module ConnectionReflection
included { extend ConnectionReflection }

def connection
ActiveRecord::Base.connection
ActiveRecord::Base.lease_connection
end

def connection_options
Expand Down
4 changes: 2 additions & 2 deletions test/support/test_in_memory_oltp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

ARTest.connect

if ActiveRecord::Base.connection.supports_in_memory_oltp?
if ActiveRecord::Base.lease_connection.supports_in_memory_oltp?
puts "Configuring In-Memory OLTP..."
inmem_file = ARTest::SQLServer.test_root_sqlserver, "schema", "enable-in-memory-oltp.sql"
inmem_sql = File.read File.join(inmem_file)
ActiveRecord::Base.connection.execute(inmem_sql)
ActiveRecord::Base.lease_connection.execute(inmem_sql)
end
end

0 comments on commit 86078a5

Please sign in to comment.