Skip to content

Commit 0fbb349

Browse files
committed
Debug
1 parent ebc4f78 commit 0fbb349

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
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 TEST_FILES="test/cases/temp_test_sqlserver.rb" TESTOPTS="-v"
1313
depends_on:
1414
- "sqlserver"

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ 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+
1719
log(sql, name, async: async) do |notification_payload|
1820
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
1921
result = if id_insert_table_name = query_requires_identity_insert?(sql)
@@ -312,8 +314,11 @@ def sql_for_insert(sql, pk, binds, returning)
312314
# === SQLServer Specific ======================================== #
313315

314316
def set_identity_insert(table_name, conn, enable)
317+
puts "set_identity_insert: #{table_name} #{enable}"
318+
315319
internal_raw_execute("SET IDENTITY_INSERT #{table_name} #{enable ? 'ON' : 'OFF'}", conn , perform_do: true)
316320
rescue Exception
321+
puts "IDENTITY_INSERT could not be turned #{enable ? 'ON' : 'OFF'} for table #{table_name}"
317322
raise ActiveRecordError, "IDENTITY_INSERT could not be turned #{enable ? 'ON' : 'OFF'} for table #{table_name}"
318323
end
319324

@@ -404,12 +409,37 @@ def exclude_output_inserted_id_sql_type(pk, exclude_output_inserted)
404409
end
405410

406411
def query_requires_identity_insert?(sql)
412+
# puts "query_requires_identity_insert?: #{sql}" if $AIDO
413+
407414
return false unless insert_sql?(sql)
408415

409416
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+
410428
id_column = identity_columns(raw_table_name).first
411429

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
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
413443
end
414444

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

test/cases/temp_test_sqlserver.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@ class TempTestSQLServer < ActiveRecord::TestCase
2020
INSERT INTO #{arunit2_database}.dbo.dogs(id) SELECT id FROM #{arunit_database}.dbo.dogs (NOLOCK)
2121
SQL
2222

23+
ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _start, _finish, _id, payload|
24+
puts payload[:sql]
25+
end
26+
2327
OtherDog.destroy_all
2428

2529
assert Dog.count, 1
2630
assert OtherDog.count, 0
2731

2832
# Dog.transaction do
29-
arunit_connection.transaction do
33+
# arunit_connection.transaction do
3034
assert_nothing_raised do
3135
arunit_connection.execute(sql)
3236
end
33-
end
37+
# end
3438
# end
3539

3640
assert Dog.count, 1

0 commit comments

Comments
 (0)