7
7
require "models/subscriber"
8
8
require "models/minimalistic"
9
9
require "models/college"
10
+ require "models/dog"
11
+ require "models/other_dog"
10
12
require "models/discount"
11
13
12
14
class AdapterTestSQLServer < ActiveRecord ::TestCase
13
15
fixtures :tasks
14
16
17
+ let ( :arunit_connection ) { Topic . lease_connection }
18
+ let ( :arunit2_connection ) { College . lease_connection }
19
+ let ( :arunit_database ) { arunit_connection . pool . db_config . database }
20
+ let ( :arunit2_database ) { arunit2_connection . pool . db_config . database }
21
+
15
22
let ( :basic_insert_sql ) { "INSERT INTO [funny_jokes] ([name]) VALUES('Knock knock')" }
16
23
let ( :basic_merge_sql ) { "MERGE INTO [ships] WITH (UPDLOCK, HOLDLOCK) AS target USING ( SELECT * FROM ( SELECT [id], [name], ROW_NUMBER() OVER ( PARTITION BY [id] ORDER BY [id] DESC ) AS rn_0 FROM ( VALUES (101, N'RSS Sir David Attenborough') ) AS t1 ([id], [name]) ) AS ranked_source WHERE rn_0 = 1 ) AS source ON (target.[id] = source.[id]) WHEN MATCHED THEN UPDATE SET target.[name] = source.[name]" }
17
24
let ( :basic_update_sql ) { "UPDATE [customers] SET [address_street] = NULL WHERE [id] = 2" }
@@ -52,8 +59,7 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
52
59
assert Topic . table_exists? , "Topics table name of 'dbo.topics' should return true for exists."
53
60
54
61
# Test when database and owner included in table name.
55
- db_config = ActiveRecord ::Base . configurations . configs_for ( env_name : "arunit" , name : "primary" )
56
- Topic . table_name = "#{ db_config . database } .dbo.topics"
62
+ Topic . table_name = "#{ arunit_database } .dbo.topics"
57
63
assert Topic . table_exists? , "Topics table name of '[DATABASE].dbo.topics' should return true for exists."
58
64
ensure
59
65
Topic . table_name = "topics"
@@ -225,6 +231,9 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
225
231
@identity_insert_sql_non_dbo_sp = "EXEC sp_executesql N'INSERT INTO [test].[aliens] ([id],[name]) VALUES (@0, @1)', N'@0 int, @1 nvarchar(255)', @0 = 420, @1 = N'Mork'"
226
232
@identity_insert_sql_non_dbo_unquoted_sp = "EXEC sp_executesql N'INSERT INTO test.aliens (id, name) VALUES (@0, @1)', N'@0 int, @1 nvarchar(255)', @0 = 420, @1 = N'Mork'"
227
233
@identity_insert_sql_non_dbo_unordered_sp = "EXEC sp_executesql N'INSERT INTO [test].[aliens] ([name],[id]) VALUES (@0, @1)', N'@0 nvarchar(255), @1 int', @0 = N'Mork', @1 = 420"
234
+
235
+ @non_identity_insert_sql_cross_database = "INSERT INTO #{ arunit2_database } .dbo.dogs SELECT * FROM #{ arunit_database } .dbo.dogs"
236
+ @identity_insert_sql_cross_database = "INSERT INTO #{ arunit2_database } .dbo.dogs(id) SELECT id FROM #{ arunit_database } .dbo.dogs"
228
237
end
229
238
230
239
it "return quoted table_name to #query_requires_identity_insert? when INSERT sql contains id column" do
@@ -245,20 +254,32 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
245
254
assert_equal "[test].[aliens]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_non_dbo_sp )
246
255
assert_equal "[test].[aliens]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_non_dbo_unquoted_sp )
247
256
assert_equal "[test].[aliens]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_non_dbo_unordered_sp )
257
+
258
+ assert_equal "[#{ arunit2_database } ].[dbo].[dogs]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_cross_database )
248
259
end
249
260
250
261
it "return false to #query_requires_identity_insert? for normal SQL" do
251
- [ basic_insert_sql , basic_merge_sql , basic_update_sql , basic_select_sql ] . each do |sql |
262
+ [ basic_insert_sql , basic_merge_sql , basic_update_sql , basic_select_sql , @non_identity_insert_sql_cross_database ] . each do |sql |
252
263
assert !connection . send ( :query_requires_identity_insert? , sql ) , "SQL was #{ sql } "
253
264
end
254
265
end
255
266
256
- it "find identity column using #identity_columns " do
267
+ it "find identity column" do
257
268
task_id_column = Task . columns_hash [ "id" ]
258
269
assert_equal task_id_column . name , connection . send ( :identity_columns , Task . table_name ) . first . name
259
270
assert_equal task_id_column . sql_type , connection . send ( :identity_columns , Task . table_name ) . first . sql_type
260
271
end
261
272
273
+ it "find identity column cross database" do
274
+ id_column = Dog . columns_hash [ "id" ]
275
+ assert_equal id_column . name , arunit2_connection . send ( :identity_columns , Dog . table_name ) . first . name
276
+ assert_equal id_column . sql_type , arunit2_connection . send ( :identity_columns , Dog . table_name ) . first . sql_type
277
+
278
+ id_column = OtherDog . columns_hash [ "id" ]
279
+ assert_equal id_column . name , arunit_connection . send ( :identity_columns , OtherDog . table_name ) . first . name
280
+ assert_equal id_column . sql_type , arunit_connection . send ( :identity_columns , OtherDog . table_name ) . first . sql_type
281
+ end
282
+
262
283
it "return an empty array when calling #identity_columns for a table_name with no identity" do
263
284
_ ( connection . send ( :identity_columns , Subscriber . table_name ) ) . must_equal [ ]
264
285
end
@@ -616,7 +637,7 @@ def setup
616
637
end
617
638
618
639
it 'raises an error when the foreign key is mismatched' do
619
- error = assert_raises ( ActiveRecord ::MismatchedForeignKey ) do
640
+ error = assert_raises ( ActiveRecord ::MismatchedForeignKey ) do
620
641
@conn . add_reference :engines , :old_car
621
642
@conn . add_foreign_key :engines , :old_cars
622
643
end
0 commit comments