Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b4f558e
testing
serkaniyigun Oct 13, 2019
a78b6eb
sql_for_insert fix
serkaniyigun Oct 13, 2019
ec9d711
Use rails backtrace in tests
takanamito Jan 12, 2020
046f820
Prepare for Minitest6
takanamito Jan 12, 2020
1aafcc6
DateTime also returns true for .acts_like?(:date) so it must be check…
ablignaut Jan 13, 2020
9172586
corrected syntax for DEPRECATED warnings in test/cases
daveomcd Nov 7, 2019
3ba9811
corrected syntax for DEPRECATED warnings in test/cases
daveomcd Nov 7, 2019
83a607c
removed byebug
daveomcd Feb 4, 2020
4ce8c53
Merge commit 'pullrequests/daveomcd/correcting-tests' into 6-0-dev
Feb 20, 2020
3ff8afa
Merge commit 'pullrequests/ablignaut/master' into 6-0-dev
Feb 20, 2020
fe26f9a
various changes
nemesit Feb 20, 2020
8d2b737
fix column_initialization, translate_exception,...
Feb 21, 2020
c97d602
fixed tests
Feb 21, 2020
31af51a
Version -> 6.0.2.1
Feb 21, 2020
209ef6a
match activerecord changes from PR #35890
esc12-dglaser Feb 21, 2020
cfdf01b
rails removed sequence_name with commit #f21bc46
esc12-dglaser Feb 21, 2020
0aec9e8
various changes
Feb 24, 2020
319abc4
Merge pull request #718 from takanamito/fix-ci
wpolicarpo Feb 25, 2020
ed41209
Merge branch 'master' into 6-0-dev
esc12-dglaser Feb 26, 2020
e0d8a9f
Merge branch '6-0-dev' into 6-0-dev-work
esc12-dglaser Feb 26, 2020
c21c43b
Merge branch 'daveomcd' into 6-0-dev-work
esc12-dglaser Feb 26, 2020
8297397
Merge branch 'ablignaut' into 6-0-dev-work
esc12-dglaser Feb 26, 2020
c9c4b7f
Merge branch 'nemesit' into 6-0-dev-work
esc12-dglaser Feb 26, 2020
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ group :tinytds do
end

group :development do
gem 'byebug'
gem 'byebug', platform: [:mri, :mingw, :x64_mingw]
gem 'mocha'
gem 'minitest-spec-rails'
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord-sqlserver-adapter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
spec.add_dependency 'activerecord', '~> 6.0.0.beta3'
spec.add_dependency 'activerecord', '~> 6.0.2.1'
spec.add_dependency 'tiny_tds'
end
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def newsequentialid_function

protected

def sql_for_insert(sql, pk, sequence_name, binds)
def sql_for_insert(sql, pk, binds)
if pk.nil?
table_name = query_requires_identity_insert?(sql)
pk = primary_key(table_name)
Expand Down
9 changes: 5 additions & 4 deletions lib/active_record/connection_adapters/sqlserver/quoting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ def unquoted_false
end

def quoted_date(value)
if value.acts_like?(:date)
Type::Date.new.serialize(value)
else value.acts_like?(:time)
if value.acts_like?(:time)
Type::DateTime.new.serialize(value)
elsif value.acts_like?(:date)
Type::Date.new.serialize(value)
else
value
end
end


private

def _quote(value)
Expand Down
9 changes: 7 additions & 2 deletions lib/active_record/connection_adapters/sqlserver_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'active_record'
require 'arel_sqlserver'
require 'active_record/connection_adapters/abstract_adapter'
require 'active_record/connection_adapters/statement_pool'
require 'active_record/connection_adapters/sqlserver/core_ext/active_record'
require 'active_record/connection_adapters/sqlserver/core_ext/calculations'
require 'active_record/connection_adapters/sqlserver/core_ext/explain'
Expand Down Expand Up @@ -321,15 +322,15 @@ def initialize_type_map(m = type_map)
m.register_type 'timestamp', SQLServer::Type::Timestamp.new
end

def translate_exception(e, message)
def translate_exception(e, message:, sql:, binds:)
case message
when /(cannot insert duplicate key .* with unique index) | (violation of unique key constraint)/i
RecordNotUnique.new(message)
when /conflicted with the foreign key constraint/i
InvalidForeignKey.new(message)
when /has been chosen as the deadlock victim/i
DeadlockVictim.new(message)
when /database .* does not exist/i
when /'doesnotexist'((?!').)*does not exist/
NoDatabaseError.new(message)
when /data would be truncated/
ValueTooLong.new(message)
Expand Down Expand Up @@ -447,6 +448,10 @@ def version_year
def sqlserver_version
@sqlserver_version ||= _raw_select('SELECT @@version', fetch: :rows).first.first.to_s
end

def build_statement_pool
ActiveRecord::ConnectionAdapters::StatementPool.new(self.class.type_cast_config_to_integer(@config[:statement_limit]))
end
end
end
end
9 changes: 7 additions & 2 deletions lib/active_record/connection_adapters/sqlserver_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ module ActiveRecord
module ConnectionAdapters
class SQLServerColumn < Column

def initialize(name, default, sql_type_metadata = nil, null = true, table_name = nil, default_function = nil, collation = nil, comment = nil, sqlserver_options = {})
def initialize(name, default, sql_type_metadata = nil, null = true, table_name = nil, default_function = nil, collation = nil, comment = nil, **sqlserver_options)
@sqlserver_options = sqlserver_options || {}
super(name, default, sql_type_metadata, null, table_name, default_function, collation, comment: comment)
@table_name = table_name
super(name, default, sql_type_metadata, null, default_function, collation: collation, comment: comment, **sqlserver_options)
end

def table_name
@table_name
end

def is_identity?
Expand Down
10 changes: 9 additions & 1 deletion lib/arel/visitors/sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ class SQLServer < Arel::Visitors::ToSql
FETCH0 = " FETCH FIRST (SELECT 0) "
ROWS_ONLY = " ROWS ONLY"

WHERE = ' WHERE '
SPACE = ' '
COMMA = ', '
GROUP_BY = ' GROUP BY '
ORDER_BY = ' ORDER BY '
WINDOW = ' WINDOW '
AND = ' AND '
DISTINCT = 'DISTINCT'

private

Expand Down Expand Up @@ -202,7 +210,7 @@ def table_From_Statement o
elsif Arel::Nodes::SqlLiteral === core.from
Arel::Table.new(core.from)
elsif Arel::Nodes::JoinSource === core.source
Arel::Nodes::SqlLiteral === core.source.left ? Arel::Table.new(core.source.left, @engine) : core.source.left
Arel::Nodes::SqlLiteral === core.source.left ? Arel::Table.new(core.source.left, @engine) : core.source.left.left
end
end

Expand Down
36 changes: 18 additions & 18 deletions test/cases/adapter_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class AdapterTestSQLServer < ActiveRecord::TestCase

it 'has basic and non-senstive information in the adpaters inspect method' do
string = connection.inspect
string.must_match %r{ActiveRecord::ConnectionAdapters::SQLServerAdapter}
string.must_match %r{version\: \d.\d}
string.must_match %r{mode: dblib}
string.must_match %r{azure: (true|false)}
string.wont_match %r{host}
string.wont_match %r{password}
string.wont_match %r{username}
string.wont_match %r{port}
_(string).must_match %r{ActiveRecord::ConnectionAdapters::SQLServerAdapter}
_(string).must_match %r{version\: \d.\d}
_(string).must_match %r{mode: dblib}
_(string).must_match %r{azure: (true|false)}
_(string).wont_match %r{host}
_(string).wont_match %r{password}
_(string).wont_match %r{username}
_(string).wont_match %r{port}
end

it 'has a 128 max #table_alias_length' do
Expand Down Expand Up @@ -161,7 +161,7 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
end

it 'return an empty array when calling #identity_columns for a table_name with no identity' do
connection.send(:identity_columns, Subscriber.table_name).must_equal []
_(connection.send(:identity_columns, Subscriber.table_name)).must_equal []
end

end
Expand Down Expand Up @@ -303,7 +303,7 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
end

it 'find SSTestCustomersView table name' do
connection.views.must_include 'sst_customers_view'
_(connection.views).must_include 'sst_customers_view'
end

it 'work with dynamic finders' do
Expand Down Expand Up @@ -344,9 +344,9 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
end

it 'find identity column' do
SSTestCustomersView.primary_key.must_equal 'id'
connection.primary_key(SSTestCustomersView.table_name).must_equal 'id'
SSTestCustomersView.columns_hash['id'].must_be :is_identity?
_(SSTestCustomersView.primary_key).must_equal 'id'
_(connection.primary_key(SSTestCustomersView.table_name)).must_equal 'id'
_(SSTestCustomersView.columns_hash['id']).must_be :is_identity?
end

it 'find default values' do
Expand All @@ -371,9 +371,9 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
end

it 'find identity column' do
SSTestStringDefaultsView.primary_key.must_equal 'id'
connection.primary_key(SSTestStringDefaultsView.table_name).must_equal 'id'
SSTestStringDefaultsView.columns_hash['id'].must_be :is_identity?
_(SSTestStringDefaultsView.primary_key).must_equal 'id'
_(connection.primary_key(SSTestStringDefaultsView.table_name)).must_equal 'id'
_(SSTestStringDefaultsView.columns_hash['id']).must_be :is_identity?
end

it 'find default values' do
Expand Down Expand Up @@ -422,8 +422,8 @@ class AdapterTestSQLServer < ActiveRecord::TestCase

it 'in_memory_oltp' do
if ENV['IN_MEMORY_OLTP'] && connection.supports_in_memory_oltp?
SSTMemory.primary_key.must_equal 'id'
SSTMemory.columns_hash['id'].must_be :is_identity?
_(SSTMemory.primary_key).must_equal 'id'
_(SSTMemory.columns_hash['id']).must_be :is_identity?
else
skip 'supports_in_memory_oltp? => false'
end
Expand Down
8 changes: 4 additions & 4 deletions test/cases/change_column_null_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ def find_column(table, name)

describe '#change_column_null' do
it 'does not change the column limit' do
name_column.limit.must_equal 15
_(name_column.limit).must_equal 15
end

it 'does not change the column default' do
code_column.default.must_equal 'n/a'
_(code_column.default).must_equal 'n/a'
end

it 'does not change the column precision' do
value_column.precision.must_equal 32
_(value_column.precision).must_equal 32
end

it 'does not change the column scale' do
value_column.scale.must_equal 8
_(value_column.scale).must_equal 8
end
end
end
21 changes: 10 additions & 11 deletions test/cases/coerced_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ def test_should_return_decimal_average_of_integer_field_coerced
def test_limit_is_kept_coerced
queries = capture_sql_ss { Account.limit(1).count }
assert_equal 1, queries.length
queries.first.must_match %r{ORDER BY \[accounts\]\.\[id\] ASC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.*@0 = 1}
_(queries.first).must_match %r{ORDER BY \[accounts\]\.\[id\] ASC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.*@0 = 1}
end

coerce_tests! :test_limit_with_offset_is_kept
def test_limit_with_offset_is_kept_coerced
queries = capture_sql_ss { Account.limit(1).offset(1).count }
assert_equal 1, queries.length
queries.first.must_match %r{ORDER BY \[accounts\]\.\[id\] ASC OFFSET @0 ROWS FETCH NEXT @1 ROWS ONLY.*@0 = 1, @1 = 1}
_(queries.first).must_match %r{ORDER BY \[accounts\]\.\[id\] ASC OFFSET @0 ROWS FETCH NEXT @1 ROWS ONLY.*@0 = 1, @1 = 1}
end

# SQL Server needs an alias for the calculated column
Expand Down Expand Up @@ -265,7 +265,7 @@ class ColumnAttributesTest < ActiveRecord::TestCase
def test_add_column_without_limit_coerced
add_column :test_models, :description, :string, limit: nil
TestModel.reset_column_information
TestModel.columns_hash["description"].limit.must_equal 4000
_(TestModel.columns_hash["description"].limit).must_equal 4000
end
end
end
Expand All @@ -276,7 +276,7 @@ def test_add_column_without_limit_coerced

module ActiveRecord
class Migration
class ColumnsTest
class ColumnsTest < ActiveRecord::TestCase
# Our defaults are real 70000 integers vs '70000' strings.
coerce_tests! :test_rename_column_preserves_default_value_not_null
def test_rename_column_preserves_default_value_not_null_coerced
Expand Down Expand Up @@ -506,9 +506,8 @@ def test_condition_local_time_interpolation_with_default_timezone_utc_coerced
end
end
end
end


end


module ActiveRecord
Expand Down Expand Up @@ -615,14 +614,14 @@ class PersistenceTest < ActiveRecord::TestCase
coerce_tests! :test_update_all_doesnt_ignore_order
def test_update_all_doesnt_ignore_order_coerced
david, mary = authors(:david), authors(:mary)
david.id.must_equal 1
mary.id.must_equal 2
david.name.wont_equal mary.name
_(david.id).must_equal 1
_(mary.id).must_equal 2
_(david.name).wont_equal mary.name
assert_sql(/UPDATE.*\(SELECT \[authors\].\[id\] FROM \[authors\].*ORDER BY \[authors\].\[id\]/i) do
Author.where('[id] > 1').order(:id).update_all(name: 'Test')
end
david.reload.name.must_equal 'David'
mary.reload.name.must_equal 'Test'
_(david.reload.name).must_equal 'David'
_(mary.reload.name).must_equal 'Test'
end

# We can not UPDATE identity columns.
Expand Down
Loading