activerecord-sqlserver-adapter 7.1.3 → 7.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b304aee82f2ca78fe1db797b127b31a73b95df1dfb670a7232a68d57b6d9846e
4
- data.tar.gz: bb52aecdc34253336fdeafb2fad5d26a3cb48ea7dfd2a453c3b87f07a423c0a6
3
+ metadata.gz: 4e12d59815d11d0cbecea5d206057d6251384ef3a37df229c3c20c92eb7e0312
4
+ data.tar.gz: 25644794411a6dd0e709216a5cc444571645f94f2faf07ede21a6680c9682b35
5
5
  SHA512:
6
- metadata.gz: 34a017ac93ad7e077f41e4897185ccf20820774e1b5ee3c49988fc58d3b0febf422e6989155100b06eab3b2bcfcd79fab35ec83a31d4c9b0a266fc54f06da652
7
- data.tar.gz: e9fa8fc3869bc0cc20eac834eef6737421d0ff4cfc27a1064843c89a3fd4447f02c5b35338661740c55cbd7caf687151aa469fc2092c01a048ac4967612c88ad
6
+ metadata.gz: ff6fc5d218ed61de3c5b28024f72e42023ee15e710048f797e428d00487dd3be8284fadc8a23922aacb08ae22389aa0eaf7d9e86123d1a9d23ee9f2f222cc54e
7
+ data.tar.gz: ee6cd4a68000ec209f06f39c5029b049dd870993652a9bce9125cc118efc829bced1a921631fec7a8353e7bf96790eacdd9718a606fa82dee8ff2e74143b45c5
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## v7.1.4
2
+
3
+ #### Fixed
4
+
5
+ - [#1164](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1164) Fix composite primary key with different data type with triggers
6
+
7
+ #### Changed
8
+
9
+ - [#1199](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1199) Remove ActiveRecord::Relation#calculate patch
10
+
1
11
  ## v7.1.3
2
12
 
3
13
  #### Fixed
data/README.md CHANGED
@@ -13,8 +13,8 @@ Interested in older versions? We follow a rational versioning policy that tracks
13
13
 
14
14
  | Adapter Version | Rails Version | Support | Branch |
15
15
  |-----------------|---------------|---------|--------------------------------------------------------------------------------------------------|
16
- | `7.1.3` | `7.1.x` | Active | [main](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/main) |
17
- | `7.0.5.1` | `7.0.x` | Active | [7-0-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/7-0-stable) |
16
+ | `7.1.4` | `7.1.x` | Active | [main](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/main) |
17
+ | `7.0.7` | `7.0.x` | Active | [7-0-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/7-0-stable) |
18
18
  | `6.1.3.0` | `6.1.x` | Active | [6-1-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/6-1-stable) |
19
19
  | `6.0.3` | `6.0.x` | Ended | [6-0-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/6-0-stable) |
20
20
  | `5.2.1` | `5.2.x` | Ended | [5-2-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/5-2-stable) |
@@ -49,6 +49,10 @@ adapter.exclude_output_inserted_table_names['my_table_name'] = true
49
49
 
50
50
  # Explicitly set the data type for the temporary key table.
51
51
  adapter.exclude_output_inserted_table_names['my_uuid_table_name'] = 'uniqueidentifier'
52
+
53
+
54
+ # Explicitly set data types when data type is different for composite primary keys.
55
+ adapter.exclude_output_inserted_table_names['my_composite_pk_table_name'] = { pk_col_one: "uniqueidentifier", pk_col_two: "int" }
52
56
  ```
53
57
 
54
58
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 7.1.3
1
+ 7.1.4
@@ -8,51 +8,9 @@ module ActiveRecord
8
8
  module SQLServer
9
9
  module CoreExt
10
10
  module Calculations
11
- def calculate(operation, column_name)
12
- if klass.connection.sqlserver?
13
- _calculate(operation, column_name)
14
- else
15
- super
16
- end
17
- end
18
-
11
+
19
12
  private
20
13
 
21
- # Same as original `calculate` method except we don't perform PostgreSQL hack that removes ordering.
22
- def _calculate(operation, column_name)
23
- operation = operation.to_s.downcase
24
-
25
- if @none
26
- case operation
27
- when "count", "sum"
28
- result = group_values.any? ? Hash.new : 0
29
- return @async ? Promise::Complete.new(result) : result
30
- when "average", "minimum", "maximum"
31
- result = group_values.any? ? Hash.new : nil
32
- return @async ? Promise::Complete.new(result) : result
33
- end
34
- end
35
-
36
- if has_include?(column_name)
37
- relation = apply_join_dependency
38
-
39
- if operation == "count"
40
- unless distinct_value || distinct_select?(column_name || select_for_count)
41
- relation.distinct!
42
- relation.select_values = [ klass.primary_key || table[Arel.star] ]
43
- end
44
- # PostgreSQL: ORDER BY expressions must appear in SELECT list when using DISTINCT
45
- # Start of monkey-patch
46
- # relation.order_values = [] if group_values.empty?
47
- # End of monkey-patch
48
- end
49
-
50
- relation.calculate(operation, column_name)
51
- else
52
- perform_calculation(operation, column_name)
53
- end
54
- end
55
-
56
14
  def build_count_subquery(relation, column_name, distinct)
57
15
  return super unless klass.connection.adapter_name == "SQLServer"
58
16
 
@@ -278,13 +278,17 @@ module ActiveRecord
278
278
  exclude_output_inserted = exclude_output_inserted_table_name?(table_name, sql)
279
279
 
280
280
  if exclude_output_inserted
281
- quoted_pk = Array(pk).map { |subkey| SQLServer::Utils.extract_identifiers(subkey).quoted }
281
+ pk_and_types = Array(pk).map do |subkey|
282
+ {
283
+ quoted: SQLServer::Utils.extract_identifiers(subkey).quoted,
284
+ id_sql_type: exclude_output_inserted_id_sql_type(subkey, exclude_output_inserted)
285
+ }
286
+ end
282
287
 
283
- id_sql_type = exclude_output_inserted.is_a?(TrueClass) ? "bigint" : exclude_output_inserted
284
288
  <<~SQL.squish
285
- DECLARE @ssaIdInsertTable table (#{quoted_pk.map { |subkey| "#{subkey} #{id_sql_type}"}.join(", ") });
286
- #{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/i), " OUTPUT #{ quoted_pk.map { |subkey| "INSERTED.#{subkey}" }.join(", ") } INTO @ssaIdInsertTable"}
287
- SELECT #{quoted_pk.map {|subkey| "CAST(#{subkey} AS #{id_sql_type}) #{subkey}"}.join(", ")} FROM @ssaIdInsertTable
289
+ DECLARE @ssaIdInsertTable table (#{pk_and_types.map { |pk_and_type| "#{pk_and_type[:quoted]} #{pk_and_type[:id_sql_type]}"}.join(", ") });
290
+ #{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/i), " OUTPUT #{ pk_and_types.map { |pk_and_type| "INSERTED.#{pk_and_type[:quoted]}" }.join(", ") } INTO @ssaIdInsertTable"}
291
+ SELECT #{pk_and_types.map {|pk_and_type| "CAST(#{pk_and_type[:quoted]} AS #{pk_and_type[:id_sql_type]}) #{pk_and_type[:quoted]}"}.join(", ")} FROM @ssaIdInsertTable
288
292
  SQL
289
293
  else
290
294
  returning_columns = returning || Array(pk)
@@ -382,6 +386,12 @@ module ActiveRecord
382
386
  self.class.exclude_output_inserted_table_names[table_name]
383
387
  end
384
388
 
389
+ def exclude_output_inserted_id_sql_type(pk, exclude_output_inserted)
390
+ return "bigint" if exclude_output_inserted.is_a?(TrueClass)
391
+ return exclude_output_inserted[pk.to_sym] if exclude_output_inserted.is_a?(Hash)
392
+ exclude_output_inserted
393
+ end
394
+
385
395
  def query_requires_identity_insert?(sql)
386
396
  return false unless insert_sql?(sql)
387
397
 
@@ -38,4 +38,14 @@ class SQLServerTriggerTest < ActiveRecord::TestCase
38
38
  _(obj.pk_col_two).must_equal 42
39
39
  _(obj.pk_col_one.to_s).must_equal SSTestTriggerHistory.first.id_source
40
40
  end
41
+
42
+ it "can insert into a table with composite pk with different data type with output inserted - with a hash setting for table name" do
43
+ exclude_output_inserted_table_names["sst_table_with_composite_pk_trigger_with_different_data_type"] = { pk_col_one: "uniqueidentifier", pk_col_two: "int" }
44
+ assert SSTestTriggerHistory.all.empty?
45
+ obj = SSTestTriggerCompositePkWithDefferentDataType.create! pk_col_two: 123, event_name: "test trigger"
46
+ _(obj.event_name).must_equal "test trigger"
47
+ _(obj.pk_col_one).must_be :present?
48
+ _(obj.pk_col_two).must_equal 123
49
+ _(obj.pk_col_one.to_s).must_equal SSTestTriggerHistory.first.id_source
50
+ end
41
51
  end
@@ -11,3 +11,7 @@ end
11
11
  class SSTestTriggerCompositePk < ActiveRecord::Base
12
12
  self.table_name = "sst_table_with_composite_pk_trigger"
13
13
  end
14
+
15
+ class SSTestTriggerCompositePkWithDefferentDataType < ActiveRecord::Base
16
+ self.table_name = "sst_table_with_composite_pk_trigger_with_different_data_type"
17
+ end
@@ -249,6 +249,23 @@ ActiveRecord::Schema.define do
249
249
  SELECT pk_col_one AS id_source, event_name FROM INSERTED
250
250
  SQL
251
251
 
252
+ execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sst_table_with_composite_pk_trigger_with_different_data_type') DROP TABLE sst_table_with_composite_pk_trigger_with_different_data_type"
253
+ execute <<-SQL
254
+ CREATE TABLE sst_table_with_composite_pk_trigger_with_different_data_type(
255
+ pk_col_one uniqueidentifier DEFAULT NEWID(),
256
+ pk_col_two int NOT NULL,
257
+ event_name nvarchar(255),
258
+ CONSTRAINT PK_sst_table_with_composite_pk_trigger_with_different_data_type PRIMARY KEY (pk_col_one, pk_col_two)
259
+ )
260
+ SQL
261
+ execute <<-SQL
262
+ CREATE TRIGGER sst_table_with_composite_pk_trigger_with_different_data_type_t ON sst_table_with_composite_pk_trigger_with_different_data_type
263
+ FOR INSERT
264
+ AS
265
+ INSERT INTO sst_table_with_trigger_history (id_source, event_name)
266
+ SELECT pk_col_one AS id_source, event_name FROM INSERTED
267
+ SQL
268
+
252
269
  # Another schema.
253
270
 
254
271
  create_table :sst_schema_columns, force: true do |t|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-sqlserver-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.1.3
4
+ version: 7.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Collins
@@ -15,7 +15,7 @@ authors:
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
- date: 2024-02-15 00:00:00.000000000 Z
18
+ date: 2024-07-04 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activerecord
@@ -239,8 +239,8 @@ licenses:
239
239
  - MIT
240
240
  metadata:
241
241
  bug_tracker_uri: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues
242
- changelog_uri: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/v7.1.3/CHANGELOG.md
243
- source_code_uri: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/v7.1.3
242
+ changelog_uri: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/v7.1.4/CHANGELOG.md
243
+ source_code_uri: https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/v7.1.4
244
244
  post_install_message:
245
245
  rdoc_options: []
246
246
  require_paths: