@@ -48,6 +48,26 @@ def test_value_limit_violations_are_translated_to_specific_exception_coerced
4848 assert_not_nil error . cause
4949 end
5050 end
51+
52+ # SQL Server does not allow truncation of tables that are referenced by foreign key
53+ # constraints. So manually remove/add foreign keys in test.
54+ coerce_tests! test_truncate_tables
55+ def test_truncate_tables_coerced
56+ @connection . remove_foreign_key :authors , :author_addresses
57+
58+ assert_operator Post . count , :> , 0
59+ assert_operator Author . count , :> , 0
60+ assert_operator AuthorAddress . count , :> , 0
61+
62+ @connection . truncate_tables ( "author_addresses" , "authors" , "posts" )
63+
64+ assert_equal 0 , Post . count
65+ assert_equal 0 , Author . count
66+ assert_equal 0 , AuthorAddress . count
67+ ensure
68+ @connection . add_foreign_key :authors , :author_addresses
69+ reset_fixtures ( "posts" , "authors" , "author_addresses" )
70+ end
5171 end
5272end
5373
@@ -1181,3 +1201,41 @@ class EagerLoadingTooManyIdsTest < ActiveRecord::TestCase
11811201 # Temporarily coerce this test due to https://github.com/rails/rails/issues/34945
11821202 coerce_tests! :test_eager_loading_too_may_ids
11831203end
1204+
1205+
1206+ module ActiveRecord
1207+ class DatabaseTasksTruncateAllTest < ActiveRecord ::TestCase
1208+
1209+ # SQL Server does not allow truncation of tables that are referenced by foreign key
1210+ # constraints. So manually remove/add foreign keys in test.
1211+ coerce_tests! test_truncate_tables
1212+ def test_truncate_tables_coerced
1213+ connection = ActiveRecord ::Base . connection
1214+ connection . remove_foreign_key :authors , :author_addresses
1215+
1216+ assert_operator SchemaMigration . count , :> , 0
1217+ assert_operator InternalMetadata . count , :> , 0
1218+ assert_operator Author . count , :> , 0
1219+ assert_operator AuthorAddress . count , :> , 0
1220+
1221+ old_configurations = ActiveRecord ::Base . configurations
1222+ configurations = { development : ActiveRecord ::Base . configurations [ "arunit" ] }
1223+ ActiveRecord ::Base . configurations = configurations
1224+
1225+ ActiveRecord ::Tasks ::DatabaseTasks . stub ( :root , nil ) do
1226+ ActiveRecord ::Tasks ::DatabaseTasks . truncate_all (
1227+ ActiveSupport ::StringInquirer . new ( "development" )
1228+ )
1229+ end
1230+
1231+ assert_operator SchemaMigration . count , :> , 0
1232+ assert_operator InternalMetadata . count , :> , 0
1233+ assert_equal 0 , Author . count
1234+ assert_equal 0 , AuthorAddress . count
1235+ ensure
1236+ connection . add_foreign_key :authors , :author_addresses
1237+ ActiveRecord ::Base . configurations = old_configurations
1238+ end
1239+
1240+ end
1241+ end
0 commit comments