Skip to content

Instantly share code, notes, and snippets.

@ioquatix
Last active May 14, 2024 05:20
Show Gist options
  • Save ioquatix/2bbcc5a4dd2fdf171e585b69f9c5f914 to your computer and use it in GitHub Desktop.
Save ioquatix/2bbcc5a4dd2fdf171e585b69f9c5f914 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails", git: "https://github.com/rails/rails.git"
# 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"
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.permanent_connection_checkout = :disallowed
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_bare_connection
assert_raises(ActiveRecord::ActiveRecordError) do
post = Post.create!
post.comments << Comment.create!
end
end
end
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails", git: "https://github.com/rails/rails.git"
# 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"
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 do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_with_connection
Post.with_connection do
post = Post.create!
post.comments << Comment.create!
assert_equal 1, post.comments.count
assert_equal 1, Comment.count
assert_equal post.id, Comment.first.post.id
end
assert_equal 0, Post.connection_pool.stat[:busy]
end
def test_with_connection_prevent_permanent_checkout
Post.with_connection(prevent_permanent_checkout: true) do
post = Post.create!
post.comments << Comment.create!
assert_equal 1, post.comments.count
assert_equal 1, Comment.count
assert_equal post.id, Comment.first.post.id
end
assert_equal 0, Post.connection_pool.stat[:busy]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment