Skip to content

Different timestamps behaviour with InsertAll #51820

@srozen

Description

@srozen

Steps to reproduce

Try to manipulate time around insert_all or upsert_all to observe varying timestamps.

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rails"
  # If you want to test against edge Rails replace the previous line with this:
  # gem "rails", github: "rails/rails", branch: "main"

  gem "sqlite3", "~> 1.4"
  gem "timecop"
end

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

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.timestamps
  end
end

class Post < ActiveRecord::Base
  has_many :comments
end

class BugTest < Minitest::Test
  # Fails
  def test_upsert_all_timestamp
    Post.upsert_all [{ id: 101}]

    Timecop.freeze(Date.today + 3.days) do
      Post.upsert_all [{ id: 102}]
      alice = Post.find(101)
      bob = Post.find(102)
      assert (bob.created_at.to_date - alice.created_at.to_date).to_i >= 2
    end
  end

  # Succeeds
  def test_standard_creation
    alice = Post.create!(id: 103)
    Timecop.freeze(Date.today + 3.days) do
      bob = Post.create!(id: 104)
      assert (bob.created_at.to_date - alice.created_at.to_date).to_i >= 2
    end
  end
end

Expected behavior

I would expect the timestamps to be set and follow the same rules as the standard creation/update.

Actual behavior

Timestamps within insert_all, upsert_all are set using a DB function to get the current time, thus not behaving the same way nor being impacted by utilities like Timecop or travel_to.

Potential fix

If this behaviour is not intended, I think relying on the model current_time_from_proper_timezone to build timestamps in ActiveRecord::InsertAll could be a solution.
Would be happy to contribute!

System configuration

Rails version: 7.1.3.2

Ruby version: 3.3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions