Skip to content

Fix SQL statement to calculate updated_at when upserting #1320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
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: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
#### Fixed

- [#1313](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1313) Correctly retrieve the SQL Server database version.
- [#1320](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1320) Fix SQL statement to calculate `updated_at` when upserting

Please check [8-0-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/8-0-stable/CHANGELOG.md) for previous changes.
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def joining_on_columns_with_uniqueness_constraints(columns_with_uniqueness_const
def build_sql_for_recording_timestamps_when_updating(insert:)
insert.model.timestamp_attributes_for_update_in_model.filter_map do |column_name|
if insert.send(:touch_timestamp_attribute?, column_name)
"target.#{quote_column_name(column_name)}=CASE WHEN (#{insert.updatable_columns.map { |column| "(COALESCE(target.#{quote_column_name(column)}, 'NULL') = COALESCE(source.#{quote_column_name(column)}, 'NULL'))" }.join(" AND ")}) THEN target.#{quote_column_name(column_name)} ELSE #{high_precision_current_timestamp} END,"
"target.#{quote_column_name(column_name)}=CASE WHEN (#{insert.updatable_columns.map { |column| "(source.#{quote_column_name(column)} = target.#{quote_column_name(column)} OR (source.#{quote_column_name(column)} IS NULL AND target.#{quote_column_name(column)} IS NULL))" }.join(" AND ")}) THEN target.#{quote_column_name(column_name)} ELSE #{high_precision_current_timestamp} END,"
end
end.join
end
Expand Down
20 changes: 20 additions & 0 deletions test/cases/insert_all_test_sqlserver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require "cases/helper_sqlserver"
require "models/sqlserver/recurring_task"

class InsertAllTestSQLServer < ActiveRecord::TestCase
test "upsert_all recording of timestamps works with mixed datatypes" do
task = RecurringTask.create!(
key: "abcdef",
priority: 5
)

RecurringTask.upsert_all([{
id: task.id,
priority: nil
}])

assert_not_equal task.updated_at, RecurringTask.find(task.id).updated_at
end
end
3 changes: 3 additions & 0 deletions test/models/sqlserver/recurring_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class RecurringTask < ActiveRecord::Base
self.table_name = "recurring_tasks"
end
8 changes: 8 additions & 0 deletions test/schema/sqlserver_specific_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,12 @@
name varchar(255)
)
TABLE_IN_OTHER_SCHEMA_USED_BY_MODEL

create_table "recurring_tasks", force: true do |t|
t.string :key
t.integer :priority, default: 0

t.datetime2 :created_at
t.datetime2 :updated_at
end
end
Loading