Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ end

group :development do
gem "minitest-spec-rails"
gem "mocha"
gem "pry-byebug", platform: [:mri, :mingw, :x64_mingw]
end

Expand Down
4 changes: 2 additions & 2 deletions lib/arel/visitors/sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,9 @@ def table_From_Statement(o)
if Arel::Table === core.from
core.from
elsif Arel::Nodes::SqlLiteral === core.from
Arel::Table.new(core.from)
Arel::Table.new(name: core.from)
elsif Arel::Nodes::JoinSource === core.source
(Arel::Nodes::SqlLiteral === core.source.left) ? Arel::Table.new(core.source.left, @engine) : core.source.left.left
(Arel::Nodes::SqlLiteral === core.source.left) ? Arel::Table.new(name: core.source.left) : core.source.left.left
end
end

Expand Down
13 changes: 12 additions & 1 deletion test/cases/coerced_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2491,6 +2491,17 @@ def test_in_order_of_with_unknown_enum_key_does_not_match_nulls_coerced
Book.where(author_id: nil, name: nil).delete_all
Book.lease_connection.add_index(:books, [:author_id, :name], unique: true)
end

# Need to remove index as SQL Server considers NULLs on a unique-index to be equal unlike PostgreSQL/MySQL/SQLite.
coerce_tests! :test_in_order_of_with_column_from_cte
def test_in_order_of_with_column_from_cte_coerced
connection.remove_index(:books, column: [:author_id, :name])

original_test_in_order_of_with_column_from_cte
ensure
Book.where(author_id: nil, name: nil).delete_all
Book.lease_connection.add_index(:books, [:author_id, :name], unique: true)
end
end

class QueryLogsTest < ActiveRecord::TestCase
Expand Down Expand Up @@ -2715,7 +2726,7 @@ class TableMetadataTest < ActiveSupport::TestCase
# Adapter returns an object that is subclass of what is expected in the original test.
coerce_tests! %r{#associated_table creates the right type caster for joined table with different association name}
def associated_table_creates_the_right_type_caster_for_joined_table_with_different_association_name_coerced
base_table_metadata = TableMetadata.new(AuditRequiredDeveloper, Arel::Table.new("developers"))
base_table_metadata = TableMetadata.new(AuditRequiredDeveloper, Arel::Table.new(name: "developers"))

associated_table_metadata = base_table_metadata.associated_table("audit_logs")

Expand Down
12 changes: 6 additions & 6 deletions test/cases/dbconsole_test_sqlserver.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# frozen_string_literal: true

require "cases/helper_sqlserver"

class DbConsole < ActiveRecord::TestCase
subject { ActiveRecord::ConnectionAdapters::SQLServerAdapter }

it "uses sqlcmd to connect to database" do
subject.expects(:find_cmd_and_exec).with("sqlcmd", "-d", "db", "-U", "user", "-P", "secret", "-C", "-S",
"tcp:localhost,1433")

config = make_db_config(adapter: "sqlserver", database: "db", username: "user", password: "secret",
host: "localhost", port: 1433, trust_server_certificate: true)
assert_called_with(subject, :find_cmd_and_exec, ["sqlcmd", "-d", "db", "-U", "user", "-P", "secret", "-C", "-S", "tcp:localhost,1433"]) do
config = make_db_config(adapter: "sqlserver", database: "db", username: "user", password: "secret", host: "localhost", port: 1433, trust_server_certificate: true)

subject.dbconsole(config)
subject.dbconsole(config)
end
end

private
Expand Down
16 changes: 8 additions & 8 deletions test/cases/fully_qualified_identifier_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class FullyQualifiedIdentifierTestSQLServer < ActiveRecord::TestCase
describe "local server" do
it "should use table name in select projections" do
table = Arel::Table.new(:table)
table = Arel::Table.new(name: :table)
expected_sql = "SELECT [table].[name] FROM [table]"
assert_equal expected_sql, table.project(table[:name]).to_sql
end
Expand All @@ -21,39 +21,39 @@ class FullyQualifiedIdentifierTestSQLServer < ActiveRecord::TestCase
end

it "should use fully qualified table name in select from clause" do
table = Arel::Table.new(:table)
table = Arel::Table.new(name: :table)
expected_sql = "SELECT * FROM [my.server].[db].[schema].[table]"
assert_equal expected_sql, table.project(Arel.star).to_sql
end

it "should not use fully qualified table name in select projections" do
table = Arel::Table.new(:table)
table = Arel::Table.new(name: :table)
expected_sql = "SELECT [table].[name] FROM [my.server].[db].[schema].[table]"
assert_equal expected_sql, table.project(table[:name]).to_sql
end

it "should not use fully qualified table name in where clause" do
table = Arel::Table.new(:table)
table = Arel::Table.new(name: :table)
expected_sql = "SELECT * FROM [my.server].[db].[schema].[table] WHERE [table].[id] = 42"
quietly { assert_equal expected_sql, table.project(Arel.star).where(table[:id].eq(42)).to_sql }
end

it "should not use fully qualified table name in order clause" do
table = Arel::Table.new(:table)
table = Arel::Table.new(name: :table)
expected_sql = "SELECT * FROM [my.server].[db].[schema].[table] ORDER BY [table].[name]"
assert_equal expected_sql, table.project(Arel.star).order(table[:name]).to_sql
end

it "should use fully qualified table name in insert statement" do
manager = Arel::InsertManager.new
manager.into Arel::Table.new(:table)
manager.into Arel::Table.new(name: :table)
manager.values = manager.create_values [Arel.sql("*")]
expected_sql = "INSERT INTO [my.server].[db].[schema].[table] VALUES (*)"
quietly { assert_equal expected_sql, manager.to_sql }
end

it "should use fully qualified table name in update statement" do
table = Arel::Table.new(:table)
table = Arel::Table.new(name: :table)
manager = Arel::UpdateManager.new
manager.table(table).where(table[:id].eq(42))
manager.set([[table[:name], "Bob"]])
Expand All @@ -62,7 +62,7 @@ class FullyQualifiedIdentifierTestSQLServer < ActiveRecord::TestCase
end

it "should use fully qualified table name in delete statement" do
table = Arel::Table.new(:table)
table = Arel::Table.new(name: :table)
manager = Arel::DeleteManager.new
manager.from(table).where(table[:id].eq(42))
expected_sql = "DELETE FROM [my.server].[db].[schema].[table] WHERE [table].[id] = 42"
Expand Down
1 change: 0 additions & 1 deletion test/cases/helper_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
require "support/coerceable_test_sqlserver"
require "support/connection_reflection"
require "support/query_assertions"
require "mocha/minitest"

Minitest.after_run do
puts "\n\n"
Expand Down
8 changes: 4 additions & 4 deletions test/cases/lateral_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class LateralTestSQLServer < ActiveRecord::TestCase
fixtures :posts, :authors

it "uses OUTER APPLY for OUTER JOIN LATERAL" do
post = Arel::Table.new(:posts)
author = Arel::Table.new(:authors)
post = Arel::Table.new(name: :posts)
author = Arel::Table.new(name: :authors)
subselect = post.project(Arel.star).take(1).where(post[:author_id].eq(author[:id])).where(post[:id].eq(42))

one = Arel::Nodes::Quoted.new(1)
Expand All @@ -22,8 +22,8 @@ class LateralTestSQLServer < ActiveRecord::TestCase
end

it "uses CROSS APPLY for INNER JOIN LATERAL" do
post = Arel::Table.new(:posts)
author = Arel::Table.new(:authors)
post = Arel::Table.new(name: :posts)
author = Arel::Table.new(name: :authors)
subselect = post.project(Arel.star).take(1).where(post[:author_id].eq(author[:id])).where(post[:id].eq(42))

sql = author.project(Arel.star).where(author[:name].matches("David")).join(subselect.lateral.as("bar")).to_sql
Expand Down
Loading