Skip to content
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

paper_trail.update_columns is setting an incorrect Version#created_at value #1395

Closed
3 tasks done
iiwo opened this issue Aug 1, 2022 · 1 comment
Closed
3 tasks done
Labels

Comments

@iiwo
Copy link
Contributor

iiwo commented Aug 1, 2022

  • PaperTrail::RecordTrail#update_columns is setting Version#created_at as the recod.updated_at value.
  • since update_columns does not update timestamps - the previous update date is used for the Version#created_at instead of the actual update date (current)

I think this was actually addressed in the original PR, but the adjustment that fixed the value got removed during the code review


  • This is not a usage question, this is a bug report
  • This bug can be reproduced with the script I provide below
  • This bug can be reproduced in the latest release of the paper_trail gem

# frozen_string_literal: true

# Use this template to report PaperTrail bugs.
# Please include only the minimum code necessary to reproduce your issue.
require "bundler/inline"

# STEP ONE: What versions are you using?
gemfile(true) do
  ruby "3.0.2"
  source "https://rubygems.org"
  gem "activerecord", "6.1.4.1"
  gem "minitest", "5.11.3"
  gem "paper_trail", "12.3.0", require: false
  gem "sqlite3", "1.4.2"
end

require "active_record"
require "minitest/autorun"
require "logger"

# Please use sqlite for your bug reports, if possible.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = nil
ActiveRecord::Schema.define do
  # STEP TWO: Define your tables here.
  create_table :users, force: true do |t|
    t.text :first_name, null: false
    t.timestamps null: false
  end

  create_table :versions do |t|
    t.string :item_type, null: false
    t.integer :item_id, null: false
    t.string :event, null: false
    t.string :whodunnit
    t.text :object, limit: 1_073_741_823
    t.text :object_changes, limit: 1_073_741_823
    t.datetime :created_at
  end
  add_index :versions, %i[item_type item_id]
end
ActiveRecord::Base.logger = Logger.new(STDOUT)
require "paper_trail"

# STEP FOUR: Define your AR models here.
class User < ActiveRecord::Base
  has_paper_trail
end

# STEP FIVE: Please write a test that demonstrates your issue.
class UpdateColumnsCreatedAtTest < ActiveSupport::TestCase
  def test_version_created_at
    freeze_time
    user = User.create!(first_name: 'John', created_at: '2015-01-01 14:00', updated_at: '2015-01-01 15:00')

    user.paper_trail.update_columns(first_name: 'Jack')
    assert_equal Time.now, user.versions.last.created_at
  end
end


# STEP SIX: Run this script using `ruby my_bug_report.rb`
iiwo added a commit to iiwo/paper_trail that referenced this issue Aug 1, 2022
* `paper_trail.update_columns` was previously setting `version.created_at` as the `recod.updated_at` value.
* Since `update_columns` does not update timestamps - the previous update date was being used for the `version.created_at` instead of the actual update date.
* This change removes the `created_at` value from the data hash, casuing the default AR generated timestamp to be used instead for the `version.created_at`

Fixes paper-trail-gem#1395
iiwo added a commit to iiwo/paper_trail that referenced this issue Aug 1, 2022
Use the default(AR generated) timestamp to the `version.created_at` when using `paper_trail.update_columns.`

`paper_trail.update_columns` was previously setting `version.created_at` as the `recod.updated_at` value.
Since `update_columns` does not update timestamps - the previous update date was being used for the `version.created_at` instead of the actual update date.

Fixes paper-trail-gem#1395
iiwo added a commit to iiwo/paper_trail that referenced this issue Aug 1, 2022
Use the default(AR generated) timestamp to the `version.created_at` when using `paper_trail.update_columns.`

`paper_trail.update_columns` was previously setting `version.created_at` as the `recod.updated_at` value.
Since `update_columns` does not update timestamps - the previous update date was being used for the `version.created_at` instead of the actual update date.

Fixes paper-trail-gem#1395
iiwo added a commit to iiwo/paper_trail that referenced this issue Aug 1, 2022
Use the default(AR generated) timestamp for the `Version#created_at` velue when using `PaperTrail::RecordTrail#update_columns`.

`PaperTrail::RecordTrail#update_columns` was previously setting `Version#created_at` as the recod `updated_at` value.
Since `update_columns` does not update timestamps - the previous update date was being used for the `Version#created_at` instead of the actual update date.

Fixes paper-trail-gem#1395
iiwo added a commit to iiwo/paper_trail that referenced this issue Aug 1, 2022
Use the default(AR generated) timestamp for the `Version#created_at` value when using `PaperTrail::RecordTrail#update_columns`.

`PaperTrail::RecordTrail#update_columns` was previously setting `Version#created_at` as the recod `updated_at` value.
Since `update_columns` does not update timestamps - the previous update date was being used for the `Version#created_at` instead of the actual update date.

Fixes paper-trail-gem#1395
iiwo added a commit to iiwo/paper_trail that referenced this issue Aug 1, 2022
Use the default(AR generated) timestamp for the `Version#created_at` value when using `PaperTrail::RecordTrail#update_columns`.

`PaperTrail::RecordTrail#update_columns` was previously setting `Version#created_at` as the recod `updated_at` value.
Since `update_columns` does not update timestamps - the previous update date was being used for the `Version#created_at` instead of the actual update date.

Fixes paper-trail-gem#1395
@github-actions
Copy link

This issue has been automatically marked as stale due to inactivity.
The resources of our volunteers are limited.
Bug reports must provide a script that reproduces the bug, using our template. Feature suggestions must include a promise to build the feature yourself.
Thank you for all your contributions.

@github-actions github-actions bot added the Stale label Oct 31, 2022
@github-actions github-actions bot closed this as completed Nov 8, 2022
jaredbeck pushed a commit that referenced this issue Nov 12, 2022
Use the default(AR generated) timestamp for the `Version#created_at` value when using `PaperTrail::RecordTrail#update_columns`.

`PaperTrail::RecordTrail#update_columns` was previously setting `Version#created_at` as the recod `updated_at` value.
Since `update_columns` does not update timestamps - the previous update date was being used for the `Version#created_at` instead of the actual update date.

Fixes #1395
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant