Skip to content

Commit de9202f

Browse files
committed
Merge branch 'main' into issues/yellowspot/rails-6-1/coerce-test-when-we-produce-more-or-less-queries
2 parents 422b28b + 2b5a37a commit de9202f

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

lib/active_record/tasks/sqlserver_database_tasks.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def initialize(configuration)
2424

2525
def create(master_established = false)
2626
establish_master_connection unless master_established
27-
connection.create_database configuration.database, configuration_hash.merge("collation" => default_collation)
27+
connection.create_database configuration.database, configuration_hash.merge(collation: default_collation)
2828
establish_connection configuration
2929
rescue ActiveRecord::StatementInvalid => e
3030
if /database .* already exists/i === e.message
@@ -54,14 +54,14 @@ def purge
5454
end
5555

5656
def structure_dump(filename, extra_flags)
57-
server_arg = "-S #{Shellwords.escape(configuration_hash['host'])}"
58-
server_arg += ":#{Shellwords.escape(configuration_hash['port'])}" if configuration_hash["port"]
57+
server_arg = "-S #{Shellwords.escape(configuration_hash[:host])}"
58+
server_arg += ":#{Shellwords.escape(configuration_hash[:port])}" if configuration_hash[:port]
5959
command = [
6060
"defncopy-ttds",
6161
server_arg,
62-
"-D #{Shellwords.escape(configuration_hash['database'])}",
63-
"-U #{Shellwords.escape(configuration_hash['username'])}",
64-
"-P #{Shellwords.escape(configuration_hash['password'])}",
62+
"-D #{Shellwords.escape(configuration_hash[:database])}",
63+
"-U #{Shellwords.escape(configuration_hash[:username])}",
64+
"-P #{Shellwords.escape(configuration_hash[:password])}",
6565
"-o #{Shellwords.escape(filename)}",
6666
]
6767
table_args = connection.tables.map { |t| Shellwords.escape(t) }
@@ -88,11 +88,11 @@ def structure_load(filename, extra_flags)
8888
attr_reader :configuration, :configuration_hash
8989

9090
def default_collation
91-
configuration_hash["collation"] || DEFAULT_COLLATION
91+
configuration_hash[:collation] || DEFAULT_COLLATION
9292
end
9393

9494
def establish_master_connection
95-
establish_connection configuration_hash.merge("database" => "master")
95+
establish_connection configuration_hash.merge(database: "master")
9696
end
9797
end
9898

test/cases/coerced_tests.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,8 +1675,34 @@ def marshal_fixture_path(file_name)
16751675
end
16761676
end
16771677

1678+
class NestedThroughAssociationsTest < ActiveRecord::TestCase
1679+
# Same as original but replace order with "order(:id)" to ensure that assert_includes_and_joins_equal doesn't raise
1680+
# "A column has been specified more than once in the order by list"
1681+
# Example: original test generate queries like "ORDER BY authors.id, [authors].[id]". We don't support duplicate columns in the order list
1682+
coerce_tests! :test_has_many_through_has_many_with_has_many_through_habtm_source_reflection_preload_via_joins, :test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection_preload_via_joins
1683+
def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection_preload_via_joins_coerced
1684+
# preload table schemas
1685+
Author.joins(:category_post_comments).first
1686+
1687+
assert_includes_and_joins_equal(
1688+
Author.where("comments.id" => comments(:does_it_hurt).id).order(:id),
1689+
[authors(:david), authors(:mary)], :category_post_comments
1690+
)
1691+
end
1692+
1693+
def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection_preload_via_joins_coerced
1694+
# preload table schemas
1695+
Category.joins(:post_comments).first
1696+
1697+
assert_includes_and_joins_equal(
1698+
Category.where("comments.id" => comments(:more_greetings).id).order(:id),
1699+
[categories(:general), categories(:technology)], :post_comments
1700+
)
1701+
end
1702+
end
1703+
16781704
class BasePreventWritesTest < ActiveRecord::TestCase
1679-
# We open one transaction, not two. Same as original but checking one query
1705+
# SQL Server does not have query for release_savepoint
16801706
coerce_tests! %r{an empty transaction does not raise if preventing writes}
16811707
test "an empty transaction does not raise if preventing writes coerced" do
16821708
ActiveRecord::Base.while_preventing_writes do
@@ -1690,7 +1716,7 @@ class BasePreventWritesTest < ActiveRecord::TestCase
16901716
end
16911717

16921718
class BasePreventWritesLegacyTest < ActiveRecord::TestCase
1693-
# We open one transaction, not two. Same as original but checking one query
1719+
# SQL Server does not have query for release_savepoint
16941720
coerce_tests! %r{an empty transaction does not raise if preventing writes}
16951721
test "an empty transaction does not raise if preventing writes coerced" do
16961722
ActiveRecord::Base.connection_handler.while_preventing_writes do

test/cases/order_test_sqlserver.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,11 @@ class OrderTestSQLServer < ActiveRecord::TestCase
143143
assert_equal post1, Post.order(Arel.sql(order)).first
144144
assert_equal post2, Post.order(Arel.sql(order)).second
145145
end
146+
147+
# Executing this kind of queries will raise "A column has been specified more than once in the order by list"
148+
# This test shows that we don't do anything to prevent this
149+
it "doesn't deduplicate semantically equal orders" do
150+
sql = Post.order(:id).order("posts.id ASC").to_sql
151+
assert_equal "SELECT [posts].* FROM [posts] ORDER BY [posts].[id] ASC, posts.id ASC", sql
152+
end
146153
end

test/cases/rake_test_sqlserver.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class SQLServerRakeTest < ActiveRecord::TestCase
1212
let(:new_database) { "activerecord_unittest_tasks" }
1313
let(:default_configuration) { ARTest.test_configuration_hashes["arunit"] }
1414
let(:configuration) { default_configuration.merge("database" => new_database) }
15+
let(:db_config) { ActiveRecord::Base.configurations.resolve(configuration) }
1516

1617
before { skip "on azure" if azure_skip }
1718
before { disconnect! unless azure_skip }
@@ -151,7 +152,7 @@ class SQLServerRakeStructureDumpLoadTest < SQLServerRakeTest
151152
_(filedata).must_match %r{CREATE TABLE dbo\.users}
152153
db_tasks.purge(configuration)
153154
_(connection.tables).wont_include "users"
154-
db_tasks.load_schema configuration, :sql, filename
155+
db_tasks.load_schema db_config, :sql, filename
155156
_(connection.tables).must_include "users"
156157
end
157158
end

0 commit comments

Comments
 (0)