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" }
@@ -51,8 +58,7 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
51
58
assert Topic . table_exists? , "Topics table name of 'dbo.topics' should return true for exists."
52
59
53
60
# Test when database and owner included in table name.
54
- db_config = ActiveRecord ::Base . configurations . configs_for ( env_name : "arunit" , name : "primary" )
55
- Topic . table_name = "#{ db_config . database } .dbo.topics"
61
+ Topic . table_name = "#{ arunit_database } .dbo.topics"
56
62
assert Topic . table_exists? , "Topics table name of '[DATABASE].dbo.topics' should return true for exists."
57
63
ensure
58
64
Topic . table_name = "topics"
@@ -110,13 +116,13 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
110
116
db_config = ActiveRecord ::Base . configurations . configs_for ( env_name : "arunit" , name : "primary" )
111
117
configuration = db_config . configuration_hash . merge ( database : "nonexistent_activerecord_unittest" )
112
118
assert_not ActiveRecord ::ConnectionAdapters ::SQLServerAdapter . database_exists? ( configuration ) ,
113
- "expected database #{ configuration [ :database ] } to not exist"
119
+ "expected database #{ configuration [ :database ] } to not exist"
114
120
end
115
121
116
122
it "test database exists returns true when the database exists" do
117
123
db_config = ActiveRecord ::Base . configurations . configs_for ( env_name : "arunit" , name : "primary" )
118
124
assert ActiveRecord ::ConnectionAdapters ::SQLServerAdapter . database_exists? ( db_config . configuration_hash ) ,
119
- "expected database #{ db_config . database } to exist"
125
+ "expected database #{ db_config . database } to exist"
120
126
end
121
127
122
128
it "test primary key violation" do
@@ -231,6 +237,9 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
231
237
@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'"
232
238
@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'"
233
239
@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"
240
+
241
+ @non_identity_insert_sql_cross_database = "INSERT INTO #{ arunit2_database } .dbo.dogs SELECT * FROM #{ arunit_database } .dbo.dogs"
242
+ @identity_insert_sql_cross_database = "INSERT INTO #{ arunit2_database } .dbo.dogs(id) SELECT id FROM #{ arunit_database } .dbo.dogs"
234
243
end
235
244
236
245
it "return quoted table_name to #query_requires_identity_insert? when INSERT sql contains id column" do
@@ -251,20 +260,32 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
251
260
assert_equal "[test].[aliens]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_non_dbo_sp )
252
261
assert_equal "[test].[aliens]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_non_dbo_unquoted_sp )
253
262
assert_equal "[test].[aliens]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_non_dbo_unordered_sp )
263
+
264
+ assert_equal "[#{ arunit2_database } ].[dbo].[dogs]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_cross_database )
254
265
end
255
266
256
267
it "return false to #query_requires_identity_insert? for normal SQL" do
257
- [ basic_insert_sql , basic_merge_sql , basic_update_sql , basic_select_sql ] . each do |sql |
268
+ [ basic_insert_sql , basic_merge_sql , basic_update_sql , basic_select_sql , @non_identity_insert_sql_cross_database ] . each do |sql |
258
269
assert !connection . send ( :query_requires_identity_insert? , sql ) , "SQL was #{ sql } "
259
270
end
260
271
end
261
272
262
- it "find identity column using #identity_columns " do
273
+ it "find identity column" do
263
274
task_id_column = Task . columns_hash [ "id" ]
264
275
assert_equal task_id_column . name , connection . send ( :identity_columns , Task . table_name ) . first . name
265
276
assert_equal task_id_column . sql_type , connection . send ( :identity_columns , Task . table_name ) . first . sql_type
266
277
end
267
278
279
+ it "find identity column cross database" do
280
+ id_column = Dog . columns_hash [ "id" ]
281
+ assert_equal id_column . name , arunit2_connection . send ( :identity_columns , Dog . table_name ) . first . name
282
+ assert_equal id_column . sql_type , arunit2_connection . send ( :identity_columns , Dog . table_name ) . first . sql_type
283
+
284
+ id_column = OtherDog . columns_hash [ "id" ]
285
+ assert_equal id_column . name , arunit_connection . send ( :identity_columns , OtherDog . table_name ) . first . name
286
+ assert_equal id_column . sql_type , arunit_connection . send ( :identity_columns , OtherDog . table_name ) . first . sql_type
287
+ end
288
+
268
289
it "return an empty array when calling #identity_columns for a table_name with no identity" do
269
290
_ ( connection . send ( :identity_columns , Subscriber . table_name ) ) . must_equal [ ]
270
291
end
@@ -455,8 +476,8 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
455
476
assert_equal columns . size , SSTestCustomersView . columns . size
456
477
columns . each do |colname |
457
478
assert_instance_of ActiveRecord ::ConnectionAdapters ::SQLServer ::Column ,
458
- SSTestCustomersView . columns_hash [ colname ] ,
459
- "Column name #{ colname . inspect } was not found in these columns #{ SSTestCustomersView . columns . map ( &:name ) . inspect } "
479
+ SSTestCustomersView . columns_hash [ colname ] ,
480
+ "Column name #{ colname . inspect } was not found in these columns #{ SSTestCustomersView . columns . map ( &:name ) . inspect } "
460
481
end
461
482
end
462
483
@@ -482,8 +503,8 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
482
503
assert_equal columns . size , SSTestStringDefaultsView . columns . size
483
504
columns . each do |colname |
484
505
assert_instance_of ActiveRecord ::ConnectionAdapters ::SQLServer ::Column ,
485
- SSTestStringDefaultsView . columns_hash [ colname ] ,
486
- "Column name #{ colname . inspect } was not found in these columns #{ SSTestStringDefaultsView . columns . map ( &:name ) . inspect } "
506
+ SSTestStringDefaultsView . columns_hash [ colname ] ,
507
+ "Column name #{ colname . inspect } was not found in these columns #{ SSTestStringDefaultsView . columns . map ( &:name ) . inspect } "
487
508
end
488
509
end
489
510
@@ -495,7 +516,7 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
495
516
496
517
it "find default values" do
497
518
assert_equal "null" , SSTestStringDefaultsView . new . pretend_null ,
498
- SSTestStringDefaultsView . columns_hash [ "pretend_null" ] . inspect
519
+ SSTestStringDefaultsView . columns_hash [ "pretend_null" ] . inspect
499
520
end
500
521
501
522
it "respond true to data_source_exists?" do
@@ -510,7 +531,7 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
510
531
511
532
it "using alternate view definition still be able to find real default" do
512
533
assert_equal "null" , SSTestStringDefaultsBigView . new . pretend_null ,
513
- SSTestStringDefaultsBigView . columns_hash [ "pretend_null" ] . inspect
534
+ SSTestStringDefaultsBigView . columns_hash [ "pretend_null" ] . inspect
514
535
end
515
536
end
516
537
0 commit comments