Skip to content

Commit 7793e06

Browse files
authored
Merge pull request #1 from rails-sqlserver/master
Merge upstream
2 parents 229939b + b29603b commit 7793e06

File tree

10 files changed

+105
-66
lines changed

10 files changed

+105
-66
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## v5.2.0
2+
3+
- #686 sql_for_insert set table name in case when pk is not nil
4+
5+
## v5.2.0.rc2
6+
7+
#### Fixed
8+
9+
- #681 change_column_null should not clear other column attributes. Fixes #582.
10+
- #684 Fix explain with array conditions. Fixes #673.
11+
112
## v5.2.0.rc1
213

314
#### Fixed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# ActiveRecord SQL Server Adapter. For SQL Server 2012 And Higher.
22

33
* [![TravisCI](https://travis-ci.org/rails-sqlserver/activerecord-sqlserver-adapter.svg?branch=master)](https://travis-ci.org/rails-sqlserver/activerecord-sqlserver-adapter) - TravisCI
4-
* [![CircleCI](https://circleci.com/gh/rails-sqlserver/activerecord-sqlserver-adapter/tree/master.svg?style=svg)](https://circleci.com/gh/rails-sqlserver/activerecord-sqlserver-adapter/tree/master) - CircleCI
54
* [![Build Status](https://ci.appveyor.com/api/projects/status/mtgbx8f57vr7k2qa/branch/master?svg=true)](https://ci.appveyor.com/project/rails-sqlserver/activerecord-sqlserver-adapter/branch/master) - Appveyor
65
* [![Gem Version](http://img.shields.io/gem/v/activerecord-sqlserver-adapter.svg)](https://rubygems.org/gems/activerecord-sqlserver-adapter) - Gem Version
76
* [![Dependency Status](https://dependencyci.com/github/rails-sqlserver/activerecord-sqlserver-adapter/badge)](https://dependencyci.com/github/rails-sqlserver/activerecord-sqlserver-adapter) - Dependency Status

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.2.0.rc1
1+
5.2.0

circle.yml

Lines changed: 0 additions & 38 deletions
This file was deleted.

lib/active_record/connection_adapters/sqlserver/core_ext/explain.rb

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ module CoreExt
55
module Explain
66

77
SQLSERVER_STATEMENT_PREFIX = 'EXEC sp_executesql '.freeze
8-
SQLSERVER_PARAM_MATCHER = /@\d+ = (.*)/
9-
SQLSERVER_NATIONAL_STRING_MATCHER = /N'(.*)'/m
8+
SQLSERVER_STATEMENT_REGEXP = /N'(.+)', N'(.+)', (.+)/
109

1110
def exec_explain(queries)
1211
unprepared_queries = queries.map do |(sql, binds)|
13-
[unprepare_sqlserver_statement(sql), binds]
12+
[unprepare_sqlserver_statement(sql, binds), binds]
1413
end
1514
super(unprepared_queries)
1615
end
@@ -19,22 +18,19 @@ def exec_explain(queries)
1918

2019
# This is somewhat hacky, but it should reliably reformat our prepared sql statment
2120
# which uses sp_executesql to just the first argument, then unquote it. Likewise our
22-
# `sp_executesql` method should substitude the @n args withe the quoted values.
23-
def unprepare_sqlserver_statement(sql)
24-
if sql.starts_with?(SQLSERVER_STATEMENT_PREFIX)
25-
executesql = sql.from(SQLSERVER_STATEMENT_PREFIX.length)
26-
args = executesql.split(', ')
27-
unprepared_sql = args.shift.strip.match(SQLSERVER_NATIONAL_STRING_MATCHER)[1]
28-
unprepared_sql = Utils.unquote_string(unprepared_sql)
29-
args = args.from(args.length / 2)
30-
args.each_with_index do |arg, index|
31-
value = arg.match(SQLSERVER_PARAM_MATCHER)[1]
32-
unprepared_sql.sub! "@#{index}", value
33-
end
34-
unprepared_sql
35-
else
36-
sql
21+
# `sp_executesql` method should substitude the @n args with the quoted values.
22+
def unprepare_sqlserver_statement(sql, binds)
23+
return sql unless sql.starts_with?(SQLSERVER_STATEMENT_PREFIX)
24+
25+
executesql = sql.from(SQLSERVER_STATEMENT_PREFIX.length)
26+
executesql = executesql.match(SQLSERVER_STATEMENT_REGEXP).to_a[1]
27+
28+
binds.each_with_index do |bind, index|
29+
value = connection.quote(bind)
30+
executesql = executesql.sub("@#{index}", value)
3731
end
32+
33+
executesql
3834
end
3935

4036
end

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ def sql_for_insert(sql, pk, id_value, sequence_name, binds)
228228
end
229229
sql = if pk && use_output_inserted? && !database_prefix_remote_server?
230230
quoted_pk = SQLServer::Utils.extract_identifiers(pk).quoted
231+
table_name ||= get_table_name(sql)
231232
exclude_output_inserted = exclude_output_inserted_table_name?(table_name, sql)
232233
if exclude_output_inserted
233234
id_sql_type = exclude_output_inserted.is_a?(TrueClass) ? 'bigint' : exclude_output_inserted

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def update_table_definition(table_name, base)
258258
def change_column_null(table_name, column_name, allow_null, default = nil)
259259
table_id = SQLServer::Utils.extract_identifiers(table_name)
260260
column_id = SQLServer::Utils.extract_identifiers(column_name)
261-
column = detect_column_for! table_name, column_name
261+
column = column_for(table_name, column_name)
262262
if !allow_null.nil? && allow_null == false && !default.nil?
263263
do_execute("UPDATE #{table_id} SET #{column_id}=#{quote(default)} WHERE #{column_id} IS NULL")
264264
end
@@ -492,13 +492,6 @@ def default_constraint_name(table_name, column_name)
492492
"DF_#{table_name}_#{column_name}"
493493
end
494494

495-
def detect_column_for!(table_name, column_name)
496-
unless column = schema_cache.columns(table_name).find { |c| c.name == column_name.to_s }
497-
raise ActiveRecordError, "No such column: #{table_name}.#{column_name}"
498-
end
499-
column
500-
end
501-
502495
def lowercase_schema_reflection_sql(node)
503496
lowercase_schema_reflection ? "LOWER(#{node})" : node
504497
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'cases/helper_sqlserver'
2+
require 'migrations/create_clients_and_change_column_null'
3+
4+
class ChangeColumnNullTestSqlServer < ActiveRecord::TestCase
5+
before do
6+
@old_verbose = ActiveRecord::Migration.verbose
7+
ActiveRecord::Migration.verbose = false
8+
CreateClientsAndChangeColumnNull.new.up
9+
end
10+
11+
after do
12+
CreateClientsAndChangeColumnNull.new.down
13+
ActiveRecord::Migration.verbose = @old_verbose
14+
end
15+
16+
def find_column(table, name)
17+
table.find { |column| column.name == name }
18+
end
19+
20+
let(:clients_table) { connection.columns('clients') }
21+
let(:name_column) { find_column(clients_table, 'name') }
22+
let(:code_column) { find_column(clients_table, 'code') }
23+
let(:value_column) { find_column(clients_table, 'value') }
24+
25+
describe '#change_column_null' do
26+
it 'does not change the column limit' do
27+
name_column.limit.must_equal 15
28+
end
29+
30+
it 'does not change the column default' do
31+
code_column.default.must_equal 'n/a'
32+
end
33+
34+
it 'does not change the column precision' do
35+
value_column.precision.must_equal 32
36+
end
37+
38+
it 'does not change the column scale' do
39+
value_column.scale.must_equal 8
40+
end
41+
end
42+
end

test/cases/showplan_test_sqlserver.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ class ShowplanTestSQLServer < ActiveRecord::TestCase
2626
plan.must_include "Clustered Index Scan", 'make sure we do not showplan the sp_executesql'
2727
end
2828

29+
it 'from array condition using index' do
30+
plan = Car.where(id: [1, 2]).explain
31+
plan.must_include " SELECT [cars].* FROM [cars] WHERE [cars].[id] IN (1, 2)"
32+
plan.must_include "Clustered Index Seek", 'make sure we do not showplan the sp_executesql'
33+
end
34+
35+
it 'from array condition' do
36+
plan = Car.where(name: ['honda', 'zyke']).explain
37+
plan.must_include " SELECT [cars].* FROM [cars] WHERE [cars].[name] IN (N'honda', N'zyke')"
38+
plan.must_include "Clustered Index Scan", 'make sure we do not showplan the sp_executesql'
39+
end
40+
2941
end
3042

3143
describe 'With SHOWPLAN_TEXT option' do
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class CreateClientsAndChangeColumnNull < ActiveRecord::Migration[5.2]
2+
def up
3+
create_table :clients do |t|
4+
t.string :name
5+
t.string :code
6+
t.decimal :value
7+
8+
t.timestamps
9+
end
10+
11+
change_column :clients, :name, :string, limit: 15
12+
change_column :clients, :code, :string, default: 'n/a'
13+
change_column :clients, :value, :decimal, precision: 32, scale: 8
14+
15+
change_column_null :clients, :name, false
16+
change_column_null :clients, :code, false
17+
change_column_null :clients, :value, false
18+
end
19+
20+
def down
21+
drop_table :clients
22+
end
23+
end

0 commit comments

Comments
 (0)