Skip to content

Instantly share code, notes, and snippets.

@soulcutter
Created March 8, 2019 18:36
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 soulcutter/347f5a4b46e5f8af7bb61508eddd206c to your computer and use it in GitHub Desktop.
Save soulcutter/347f5a4b46e5f8af7bb61508eddd206c to your computer and use it in GitHub Desktop.
create_or_find_by sequence exhaustion
# 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"
gem "pg"
end
require "active_record"
require "minitest/autorun"
require "logger"
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "rails")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_primary_key_growth
post = Post.create!
2_500_000_000.times { Post.create_or_find_by(id: post.id) }
# This fails
# Failure:
# BugTest#test_primary_key_growth [create_or_find_by.rb:56]:
# Expected: 2
# Actual: #<Comment id: 3, post_id: 2>
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment