From 2948587a656e42ffd136f342cd243205179ae199 Mon Sep 17 00:00:00 2001 From: Owen Rodda Date: Wed, 20 Jan 2016 22:03:27 -0500 Subject: [PATCH] Document MySQL and sub-second issue See changelog at https://github.com/rails/rails/blob/master/activerecord/CHANGELOG.md: Don't run SQL if attribute value is not changed for update_attribute method. --- spec/models/gadget_spec.rb | 1 + spec/models/not_on_update_spec.rb | 1 + spec/models/widget_spec.rb | 5 ++++- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/models/gadget_spec.rb b/spec/models/gadget_spec.rb index e8055b723..ab243cc70 100644 --- a/spec/models/gadget_spec.rb +++ b/spec/models/gadget_spec.rb @@ -15,6 +15,7 @@ end it "should still generate a version when only the `updated_at` attribute is updated" do + # Plus 1 second because MySQL lacks sub-second resolution expect { gadget.update_attribute(:updated_at, Time.now + 1) }.to change{gadget.versions.size}.by(1) end end diff --git a/spec/models/not_on_update_spec.rb b/spec/models/not_on_update_spec.rb index 01f22160a..7aa4eb19a 100644 --- a/spec/models/not_on_update_spec.rb +++ b/spec/models/not_on_update_spec.rb @@ -12,6 +12,7 @@ it "increments the `:updated_at` timestamp" do before = record.updated_at + # Travel 1 second because MySQL lacks sub-second resolution Timecop.travel(1) do record.touch_with_version end diff --git a/spec/models/widget_spec.rb b/spec/models/widget_spec.rb index 078de28d2..65dbacaf9 100644 --- a/spec/models/widget_spec.rb +++ b/spec/models/widget_spec.rb @@ -43,7 +43,8 @@ subject { widget.versions.last.reify } it "should reset the value for the timestamp attrs for update so that value gets updated properly" do - Timecop.travel(5) do + # Travel 1 second because MySQL lacks sub-second resolution + Timecop.travel(1) do expect { subject.save! }.to change(subject, :updated_at) end end @@ -255,6 +256,7 @@ it "creates a version" do count = widget.versions.size + # Travel 1 second because MySQL lacks sub-second resolution Timecop.travel(1) do widget.touch_with_version end @@ -263,6 +265,7 @@ it "increments the `:updated_at` timestamp" do time_was = widget.updated_at + # Travel 1 second because MySQL lacks sub-second resolution Timecop.travel(1) do widget.touch_with_version end