Skip to content

Commit ee9b042

Browse files
committed
Added test case
Fix sql Use database when constructing object ID argument Wrap insert in transaction Update temp_test_sqlserver.rb Update temp_test_sqlserver.rb Update temp_test_sqlserver.rb Update temp_test_sqlserver.rb Timeout CI tests after 10 minutes Update temp_test_sqlserver.rb Verbose Debug Update temp_test_sqlserver.rb Debug Update temp_test_sqlserver.rb Debug Cleanup Update adapter_test_sqlserver.rb Update adapter_test_sqlserver.rb Update adapter_test_sqlserver.rb Update adapter_test_sqlserver.rb
1 parent bea0578 commit ee9b042

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ jobs:
66
test:
77
name: Run test suite
88
runs-on: ubuntu-latest
9+
timeout-minutes: 10
910

1011
env:
1112
COMPOSE_FILE: docker-compose.ci.yml

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ def column_definitions(table_name)
571571
end
572572

573573
def column_definitions_sql(database, identifier)
574+
database = "TEMPDB" if identifier.temporary_table?
574575
schema_name = "schema_name()"
575576

576577
if prepared_statements
@@ -581,12 +582,8 @@ def column_definitions_sql(database, identifier)
581582
schema_name = quote(identifier.schema) if identifier.schema.present?
582583
end
583584

584-
object_id_arg = identifier.schema.present? ? "CONCAT(#{schema_name},'.',#{object_name})" : object_name
585-
586-
if identifier.temporary_table?
587-
database = "TEMPDB"
588-
object_id_arg = "CONCAT('#{database}','..',#{object_name})"
589-
end
585+
object_id_arg = identifier.schema.present? ? "CONCAT('.',#{schema_name},'.',#{object_name})" : "CONCAT('..',#{object_name})"
586+
object_id_arg = "CONCAT('#{database}',#{object_id_arg})"
590587

591588
%{
592589
SELECT

test/cases/adapter_test_sqlserver.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,31 @@ 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+
#
251+
# assert Dog.count, 1
252+
# assert OtherDog.count, 0
253+
254+
assert_nothing_raised do
255+
arunit_connection.execute(sql)
256+
end
257+
258+
# assert Dog.count, 1
259+
# assert OtherDog.count, 1
260+
end
236261
end
237262

238263
describe "quoting" do

test/cases/temp_test_sqlserver.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
require "cases/helper_sqlserver"
4+
require "models/dog"
5+
require "models/other_dog"
6+
7+
class TempTestSQLServer < ActiveRecord::TestCase
8+
# it "assert true" do
9+
# assert true
10+
# end
11+
end

0 commit comments

Comments
 (0)