Skip to content

Commit

Permalink
Document MySQL and sub-second issue
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
owenr committed Jan 21, 2016
1 parent fd2be5e commit 2948587
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions spec/models/gadget_spec.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/models/not_on_update_spec.rb
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion spec/models/widget_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2948587

Please sign in to comment.