Skip to content

Instantly share code, notes, and snippets.

@yrashk

yrashk/issue.rb Secret

Last active October 28, 2021 16:37
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 yrashk/29ea8a54d5770b6841a9bca6d0a46f7d to your computer and use it in GitHub Desktop.
Save yrashk/29ea8a54d5770b6841a9bca6d0a46f7d to your computer and use it in GitHub Desktop.
# 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", github: "rails/rails", tag: "v6.1.4.1"
# Same behaviour with edge
# gem "rails", github: "rails/rails", branch: "main"
gem "sqlite3"
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|
end
create_table :comments, force: true, primary_key: false do |t|
t.integer :post_id
end
execute <<-SQL
CREATE VIEW comment_views AS SELECT * FROM comments
SQL
end
class Post < ActiveRecord::Base
has_one :comment
has_one :comment_view
end
class Comment < ActiveRecord::Base
belongs_to :post, strict_loading: true
end
class CommentView < ActiveRecord::Base
belongs_to :post, strict_loading: true
end
class BugTest < Minitest::Test
def test_comment
post = Post.create!
post.create_comment!
post_ = Post.strict_loading.eager_load(:comment).first
refute_nil(post_.comment)
end
def test_comment_view
post = Post.create!
post.create_comment!
post_ = Post.strict_loading.eager_load(:comment_view).first
refute_nil(post_.comment_view)
end
def test_comment_view_reload
post = Post.create!
post.create_comment!
post_ = Post.strict_loading.eager_load(:comment_view).first
refute_nil(post_.reload.comment_view)
end
end
-- create_table(:posts, {:force=>true})
D, [2021-10-28T09:36:33.316245 #74999] DEBUG -- : (0.5ms) SELECT sqlite_version(*)
D, [2021-10-28T09:36:33.316486 #74999] DEBUG -- : (0.0ms) DROP TABLE IF EXISTS "posts"
D, [2021-10-28T09:36:33.316657 #74999] DEBUG -- : (0.1ms) CREATE TABLE "posts" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL)
-> 0.0036s
-- create_table(:comments, {:force=>true, :primary_key=>false})
D, [2021-10-28T09:36:33.316764 #74999] DEBUG -- : (0.0ms) DROP TABLE IF EXISTS "comments"
D, [2021-10-28T09:36:33.316840 #74999] DEBUG -- : (0.0ms) CREATE TABLE "comments" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "post_id" integer)
-> 0.0001s
-- execute(" CREATE VIEW comment_views AS SELECT * FROM comments\n")
D, [2021-10-28T09:36:33.316917 #74999] DEBUG -- : (0.0ms) CREATE VIEW comment_views AS SELECT * FROM comments
-> 0.0001s
D, [2021-10-28T09:36:33.328656 #74999] DEBUG -- : (0.1ms) CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL)
D, [2021-10-28T09:36:33.333860 #74999] DEBUG -- : ActiveRecord::InternalMetadata Load (0.9ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", "environment"], ["LIMIT", 1]]
D, [2021-10-28T09:36:33.336074 #74999] DEBUG -- : TRANSACTION (0.0ms) begin transaction
D, [2021-10-28T09:36:33.336189 #74999] DEBUG -- : ActiveRecord::InternalMetadata Create (0.0ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["key", "environment"], ["value", "development"], ["created_at", "2021-10-28 16:36:33.335822"], ["updated_at", "2021-10-28 16:36:33.335822"]]
D, [2021-10-28T09:36:33.336270 #74999] DEBUG -- : TRANSACTION (0.0ms) commit transaction
Run options: --seed 42796
# Running:
D, [2021-10-28T09:36:33.348951 #74999] DEBUG -- : TRANSACTION (0.0ms) begin transaction
D, [2021-10-28T09:36:33.349012 #74999] DEBUG -- : Post Create (0.0ms) INSERT INTO "posts" DEFAULT VALUES
D, [2021-10-28T09:36:33.349093 #74999] DEBUG -- : TRANSACTION (0.0ms) commit transaction
D, [2021-10-28T09:36:33.353593 #74999] DEBUG -- : TRANSACTION (0.0ms) begin transaction
D, [2021-10-28T09:36:33.353688 #74999] DEBUG -- : Comment Create (0.0ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", 1]]
D, [2021-10-28T09:36:33.353745 #74999] DEBUG -- : TRANSACTION (0.0ms) commit transaction
D, [2021-10-28T09:36:33.354169 #74999] DEBUG -- : Comment Load (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? LIMIT ? [["post_id", 1], ["LIMIT", 1]]
D, [2021-10-28T09:36:33.357204 #74999] DEBUG -- : SQL (0.1ms) SELECT "posts"."id" AS t0_r0, "comments"."id" AS t1_r0, "comments"."post_id" AS t1_r1 FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 1]]
.D, [2021-10-28T09:36:33.357532 #74999] DEBUG -- : TRANSACTION (0.0ms) begin transaction
D, [2021-10-28T09:36:33.357578 #74999] DEBUG -- : Post Create (0.0ms) INSERT INTO "posts" DEFAULT VALUES
D, [2021-10-28T09:36:33.357640 #74999] DEBUG -- : TRANSACTION (0.0ms) commit transaction
D, [2021-10-28T09:36:33.358054 #74999] DEBUG -- : TRANSACTION (0.0ms) begin transaction
D, [2021-10-28T09:36:33.358172 #74999] DEBUG -- : Comment Create (0.1ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", 2]]
D, [2021-10-28T09:36:33.358258 #74999] DEBUG -- : TRANSACTION (0.0ms) commit transaction
D, [2021-10-28T09:36:33.358440 #74999] DEBUG -- : Comment Load (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? LIMIT ? [["post_id", 2], ["LIMIT", 1]]
D, [2021-10-28T09:36:33.359506 #74999] DEBUG -- : SQL (0.0ms) SELECT "posts"."id" AS t0_r0, "comment_views"."id" AS t1_r0, "comment_views"."post_id" AS t1_r1 FROM "posts" LEFT OUTER JOIN "comment_views" ON "comment_views"."post_id" = "posts"."id" ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 1]]
F
Failure:
BugTest#test_comment_view [issue.rb:65]:
Expected nil to not be nil.
rails test issue.rb:60
D, [2021-10-28T09:36:33.359912 #74999] DEBUG -- : TRANSACTION (0.0ms) begin transaction
D, [2021-10-28T09:36:33.359954 #74999] DEBUG -- : Post Create (0.0ms) INSERT INTO "posts" DEFAULT VALUES
D, [2021-10-28T09:36:33.360013 #74999] DEBUG -- : TRANSACTION (0.0ms) commit transaction
D, [2021-10-28T09:36:33.360368 #74999] DEBUG -- : TRANSACTION (0.0ms) begin transaction
D, [2021-10-28T09:36:33.360431 #74999] DEBUG -- : Comment Create (0.0ms) INSERT INTO "comments" ("post_id") VALUES (?) [["post_id", 3]]
D, [2021-10-28T09:36:33.360515 #74999] DEBUG -- : TRANSACTION (0.0ms) commit transaction
D, [2021-10-28T09:36:33.360663 #74999] DEBUG -- : Comment Load (0.0ms) SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ? LIMIT ? [["post_id", 3], ["LIMIT", 1]]
D, [2021-10-28T09:36:33.360998 #74999] DEBUG -- : SQL (0.1ms) SELECT "posts"."id" AS t0_r0, "comment_views"."id" AS t1_r0, "comment_views"."post_id" AS t1_r1 FROM "posts" LEFT OUTER JOIN "comment_views" ON "comment_views"."post_id" = "posts"."id" ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 1]]
D, [2021-10-28T09:36:33.361208 #74999] DEBUG -- : Post Load (0.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
E
Error:
BugTest#test_comment_view_reload:
ActiveRecord::StrictLoadingViolationError: `Post` is marked for strict_loading. The `CommentView` association named `:comment_view` cannot be lazily loaded.
/Users/yrashk/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/bundler/gems/rails-0863d22660c2/activerecord/lib/active_record/core.rb:296:in `strict_loading_violation!'
/Users/yrashk/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/bundler/gems/rails-0863d22660c2/activerecord/lib/active_record/associations/association.rb:215:in `find_target'
/Users/yrashk/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/bundler/gems/rails-0863d22660c2/activerecord/lib/active_record/associations/singular_association.rb:39:in `find_target'
/Users/yrashk/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/bundler/gems/rails-0863d22660c2/activerecord/lib/active_record/associations/association.rb:174:in `load_target'
/Users/yrashk/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/bundler/gems/rails-0863d22660c2/activerecord/lib/active_record/associations/association.rb:67:in `reload'
/Users/yrashk/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/bundler/gems/rails-0863d22660c2/activerecord/lib/active_record/associations/singular_association.rb:9:in `reader'
/Users/yrashk/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/bundler/gems/rails-0863d22660c2/activerecord/lib/active_record/associations/builder/association.rb:103:in `comment_view'
issue.rb:73:in `test_comment_view_reload'
rails test issue.rb:68
Finished in 0.014470s, 207.3255 runs/s, 138.2170 assertions/s.
3 runs, 2 assertions, 1 failures, 1 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment