Updates only require values that will change
When updating a row, fields that don't have a provided value will not be changed
This commit is contained in:
parent
7c42693db1
commit
901a7398fa
1 changed files with 17 additions and 14 deletions
|
|
@ -25,8 +25,10 @@ module Granblue
|
||||||
|
|
||||||
def import_row(row)
|
def import_row(row)
|
||||||
attributes = build_attributes(row)
|
attributes = build_attributes(row)
|
||||||
|
# Remove nil values from attributes hash for updates
|
||||||
|
# Keep them for new records to ensure proper defaults
|
||||||
record = find_or_create_record(attributes)
|
record = find_or_create_record(attributes)
|
||||||
track_record(record, attributes) if record
|
track_record(record) if record
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_or_create_record(attributes)
|
def find_or_create_record(attributes)
|
||||||
|
|
@ -37,39 +39,40 @@ module Granblue
|
||||||
log_test_update(existing_record, attributes)
|
log_test_update(existing_record, attributes)
|
||||||
nil
|
nil
|
||||||
else
|
else
|
||||||
update_record(existing_record, attributes)
|
# For updates, only include non-nil attributes
|
||||||
|
update_attributes = attributes.compact
|
||||||
|
was_updated = update_attributes.any? { |key, value| existing_record[key] != value }
|
||||||
|
existing_record.update!(update_attributes) if was_updated
|
||||||
|
[existing_record, was_updated]
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if @test_mode
|
if @test_mode
|
||||||
log_test_creation(attributes)
|
log_test_creation(attributes)
|
||||||
nil
|
nil
|
||||||
else
|
else
|
||||||
model_class.create!(attributes)
|
# For new records, use all attributes including nil values
|
||||||
|
[model_class.create!(attributes), false]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_record(record, attributes)
|
def track_record(result)
|
||||||
changed = attributes.any? { |key, value| record[key] != value }
|
record, was_updated = result
|
||||||
record.update!(attributes) if changed
|
|
||||||
record
|
|
||||||
end
|
|
||||||
|
|
||||||
def track_record(record, attributes)
|
|
||||||
type = model_class.name.demodulize.downcase
|
type = model_class.name.demodulize.downcase
|
||||||
|
|
||||||
# For existing records, check if any attributes were actually changed
|
if was_updated
|
||||||
if record.persisted? && record.previous_changes.any?
|
|
||||||
@updated_records[type] << record.granblue_id
|
@updated_records[type] << record.granblue_id
|
||||||
log_updated_record(record) if @verbose
|
log_updated_record(record) if @verbose
|
||||||
elsif !record.persisted?
|
else
|
||||||
@new_records[type] << record.granblue_id
|
@new_records[type] << record.granblue_id
|
||||||
log_new_record(record) if @verbose
|
log_new_record(record) if @verbose
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_test_update(record, attributes)
|
def log_test_update(record, attributes)
|
||||||
@logger&.send(:log_operation, "Update #{model_class.name} #{record.granblue_id}: #{attributes.inspect}")
|
# For test mode, show only the attributes that would be updated
|
||||||
|
update_attributes = attributes.compact
|
||||||
|
@logger&.send(:log_operation, "Update #{model_class.name} #{record.granblue_id}: #{update_attributes.inspect}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_test_creation(attributes)
|
def log_test_creation(attributes)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue