Skip to content

Instantly share code, notes, and snippets.

@thdaraujo
Last active December 7, 2023 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thdaraujo/cdf400b0493ceee299c82930be3fc985 to your computer and use it in GitHub Desktop.
Save thdaraujo/cdf400b0493ceee299c82930be3fc985 to your computer and use it in GitHub Desktop.
timestamp loading issue on Rails 6.1
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'rails', '= 6.1'
# gem 'rails'
gem 'pg'
gem 'byebug'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
begin
config = { adapter: 'postgresql',
host: 'localhost',
port: '15432',
username: 'postgres',
database: 'pg-default-bug' }
ActiveRecord::Base.establish_connection(config.except(:database))
begin
ActiveRecord::Base.connection.drop_database(config[:database])
rescue StandardError
nil
end
ActiveRecord::Base.connection.create_database(config[:database])
end
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :invoices, force: true do |t|
t.timestamp :issued_at, null: false, default: -> { 'now()' }
t.timestamps
end
end
class Invoice < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_save
invoice = Invoice.create!
assert invoice.persisted?
puts "Ruby #{RUBY_VERSION}"
puts "Rails: #{Rails::VERSION::STRING}"
puts "issued_at: #{invoice.issued_at}"
puts "pluck issued_at: #{Invoice.pluck(:issued_at)}"
# invoice.reload solves this
assert invoice.issued_at.present?
end
end
__END__
Run with:
```
$ ruby pg_default_error.rb
```
Output on different versions:
## fails on Rails 6.1 (Ruby 3.0 or 3.3)
Ruby 3.0.6
Rails: 6.1.0
issued_at:
pluck issued_at: [2023-12-07 17:06:16.179438 UTC]
F
Failure:
BugTest#test_save [pg_default_error.rb:59]:
Expected false to be truthy.
----
Ruby 3.3.0
Rails: 6.1.0
issued_at:
pluck issued_at: [2023-12-07 17:07:34.287919 UTC]
F
Failure:
BugTest#test_save [pg_default_error.rb:59]:
Expected false to be truthy.
----
Ruby 3.3.0
Rails: 6.1.0
issued_at:
pluck issued_at: [2023-12-07 17:08:54.936266 UTC]
F
Failure:
BugTest#test_save [pg_default_error.rb:59]:
Expected false to be truthy.
----
## works on Rails 7.1.2 (Ruby 3.0 or 3.3)
Ruby 3.3.0
Rails: 7.1.2
issued_at: 2023-12-07 17:08:15 UTC
pluck issued_at: [2023-12-07 17:08:15.154946 UTC]
.
----
Ruby 3.0.6
Rails: 7.1.2
issued_at: 2023-12-07 17:18:04 UTC
pluck issued_at: [2023-12-07 17:18:04.049521 UTC]
.
@thdaraujo
Copy link
Author

related to rails/rails#34237

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment