Skip to content

Commit e8d7b20

Browse files
committed
Cleanup
1 parent 3423fa6 commit e8d7b20

File tree

4 files changed

+26
-69
lines changed

4 files changed

+26
-69
lines changed

docker-compose.ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ services:
99
build:
1010
context: .
1111
dockerfile: Dockerfile.ci
12-
command: wait-for sqlserver:1433 -- bundle exec rake test TESTOPTS="-v"
12+
command: wait-for sqlserver:1433 -- bundle exec rake test
1313
depends_on:
1414
- "sqlserver"

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ def write_query?(sql) # :nodoc:
1414
end
1515

1616
def raw_execute(sql, name, async: false, allow_retry: false, materialize_transactions: true)
17-
# puts "raw_execute: #{sql}" if $AIDO
18-
1917
log(sql, name, async: async) do |notification_payload|
2018
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
2119
result = if id_insert_table_name = query_requires_identity_insert?(sql)
@@ -314,11 +312,8 @@ def sql_for_insert(sql, pk, binds, returning)
314312
# === SQLServer Specific ======================================== #
315313

316314
def set_identity_insert(table_name, conn, enable)
317-
# puts "set_identity_insert: #{table_name} #{enable}"
318-
319315
internal_raw_execute("SET IDENTITY_INSERT #{table_name} #{enable ? 'ON' : 'OFF'}", conn , perform_do: true)
320316
rescue Exception
321-
puts "IDENTITY_INSERT could not be turned #{enable ? 'ON' : 'OFF'} for table #{table_name}"
322317
raise ActiveRecordError, "IDENTITY_INSERT could not be turned #{enable ? 'ON' : 'OFF'} for table #{table_name}"
323318
end
324319

@@ -409,37 +404,12 @@ def exclude_output_inserted_id_sql_type(pk, exclude_output_inserted)
409404
end
410405

411406
def query_requires_identity_insert?(sql)
412-
# puts "query_requires_identity_insert?: #{sql}" if $AIDO
413-
414407
return false unless insert_sql?(sql)
415408

416409
raw_table_name = get_raw_table_name(sql)
417-
418-
# binding.pry
419-
# if $AIDO
420-
#
421-
# puts "xxxx"
422-
# return true
423-
# end
424-
425-
426-
427-
428410
id_column = identity_columns(raw_table_name).first
429411

430-
431-
if id_column && sql =~ /^\s*(INSERT|EXEC sp_executesql N'INSERT)[^(]+\([^)]*\b(#{id_column.name})\b,?[^)]*\)/i
432-
433-
# puts "xxx: raw_table_name=#{raw_table_name}, id_column=#{id_column.name}" if $AIDO
434-
# puts "xxx: quoted=#{SQLServer::Utils.extract_identifiers(raw_table_name).quoted}" if $AIDO
435-
436-
SQLServer::Utils.extract_identifiers(raw_table_name).quoted
437-
else
438-
false
439-
440-
end
441-
442-
# id_column && sql =~ /^\s*(INSERT|EXEC sp_executesql N'INSERT)[^(]+\([^)]*\b(#{id_column.name})\b,?[^)]*\)/i ? SQLServer::Utils.extract_identifiers(raw_table_name).quoted : false
412+
id_column && sql =~ /^\s*(INSERT|EXEC sp_executesql N'INSERT)[^(]+\([^)]*\b(#{id_column.name})\b,?[^)]*\)/i ? SQLServer::Utils.extract_identifiers(raw_table_name).quoted : false
443413
end
444414

445415
def insert_sql?(sql)
@@ -483,8 +453,6 @@ def finish_statement_handle(handle)
483453
# Getting around this by raising an exception ourselves while PR
484454
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
485455
def internal_raw_execute(sql, conn, perform_do: false)
486-
# puts "internal_raw_execute: #{sql}" if $AIDO
487-
488456
result = conn.execute(sql).tap do |_result|
489457
raise TinyTds::Error, "failed to execute statement" if _result.is_a?(FalseClass)
490458
end

test/cases/adapter_test_sqlserver.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,30 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
233233
it "return an empty array when calling #identity_columns for a table_name with no identity" do
234234
_(connection.send(:identity_columns, Subscriber.table_name)).must_equal []
235235
end
236+
237+
it "identity insert enabled for cross database insert" do
238+
arunit_connection = Dog.lease_connection
239+
arunit2_connection = OtherDog.lease_connection
240+
241+
arunit_database = arunit_connection.pool.db_config.database
242+
arunit2_database = arunit2_connection.pool.db_config.database
243+
244+
sql = <<~SQL
245+
INSERT INTO #{arunit2_database}.dbo.dogs(id) SELECT id FROM #{arunit_database}.dbo.dogs
246+
SQL
247+
248+
OtherDog.destroy_all
249+
250+
assert Dog.count, 1
251+
assert OtherDog.count, 0
252+
253+
assert_nothing_raised do
254+
arunit_connection.execute(sql)
255+
end
256+
257+
assert Dog.count, 1
258+
assert OtherDog.count, 1
259+
end
236260
end
237261

238262
describe "quoting" do

test/cases/temp_test_sqlserver.rb

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,4 @@ class TempTestSQLServer < ActiveRecord::TestCase
88
# it "assert true" do
99
# assert true
1010
# end
11-
12-
it "insert from one schema to another using raw SQL" do
13-
arunit_connection = Dog.lease_connection
14-
arunit2_connection = OtherDog.lease_connection
15-
16-
arunit_database = arunit_connection.pool.db_config.database
17-
arunit2_database = arunit2_connection.pool.db_config.database
18-
19-
sql = <<~SQL
20-
INSERT INTO #{arunit2_database}.dbo.dogs(id) SELECT id FROM #{arunit_database}.dbo.dogs (NOLOCK)
21-
SQL
22-
23-
#ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _start, _finish, _id, payload|
24-
#puts payload[:sql]
25-
#end
26-
27-
OtherDog.destroy_all
28-
29-
assert Dog.count, 1
30-
assert OtherDog.count, 0
31-
32-
33-
arunit_connection.transaction do
34-
35-
# binding.pry
36-
puts "*** isolation level=#{arunit_connection.user_options_isolation_level}"
37-
assert_nothing_raised do
38-
arunit_connection.execute(sql)
39-
end
40-
41-
end
42-
43-
assert Dog.count, 1
44-
assert OtherDog.count, 1
45-
end
4611
end

0 commit comments

Comments
 (0)