Created
March 11, 2021 19:26
-
-
Save reshleman/25edc7854e9d8a262e6fff9e5a8f26ab to your computer and use it in GitHub Desktop.
ActiveStorage bug w/ multiple `#attach` calls in a transaction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
# Activate the gem you are reporting the issue against. | |
gem "rails", "6.1.3" | |
gem "sqlite3" | |
end | |
require "active_record/railtie" | |
require "active_storage/engine" | |
require "tmpdir" | |
class TestApp < Rails::Application | |
config.root = __dir__ | |
config.hosts << "example.org" | |
config.eager_load = false | |
config.session_store :cookie_store, key: "cookie_store_key" | |
secrets.secret_key_base = "secret_key_base" | |
config.logger = Logger.new($stdout) | |
Rails.logger = config.logger | |
config.active_storage.service = :local | |
config.active_storage.service_configurations = { | |
local: { | |
root: Dir.tmpdir, | |
service: "Disk" | |
} | |
} | |
end | |
ENV["DATABASE_URL"] = "sqlite3::memory:" | |
Rails.application.initialize! | |
require ActiveStorage::Engine.root.join("db/migrate/20170806125915_create_active_storage_tables.rb").to_s | |
ActiveRecord::Schema.define do | |
CreateActiveStorageTables.new.change | |
create_table :users, force: true | |
end | |
class User < ActiveRecord::Base | |
has_many_attached :attachments | |
end | |
require "minitest/autorun" | |
class BugTest < Minitest::Test | |
# Passes | |
def test_attach_files_together_no_transaction | |
user = User.create! | |
user.attachments.attach([ | |
{ | |
content_type: "text/plain", | |
filename: "dummy.txt", | |
io: ::StringIO.new("dummy"), | |
}, | |
{ | |
content_type: "text/plain", | |
filename: "dummy2.txt", | |
io: ::StringIO.new("dummy2"), | |
} | |
]) | |
assert_equal 2, user.attachments.count | |
assert user.attachments.first.service.exist?(user.attachments.first.key) | |
assert user.attachments.second.service.exist?(user.attachments.second.key) | |
end | |
# Passes | |
def test_attach_files_separately_no_transaction | |
user = User.create! | |
user.attachments.attach({ | |
content_type: "text/plain", | |
filename: "dummy.txt", | |
io: ::StringIO.new("dummy"), | |
}) | |
user.attachments.attach({ | |
content_type: "text/plain", | |
filename: "dummy2.txt", | |
io: ::StringIO.new("dummy2"), | |
}) | |
assert_equal 2, user.attachments.count | |
assert user.attachments.first.service.exist?(user.attachments.first.key) | |
assert user.attachments.second.service.exist?(user.attachments.second.key) | |
end | |
# Passes | |
def test_attach_files_together_in_transaction | |
user = User.create! | |
ActiveRecord::Base.transaction do | |
user.attachments.attach([ | |
{ | |
content_type: "text/plain", | |
filename: "dummy.txt", | |
io: ::StringIO.new("dummy"), | |
}, | |
{ | |
content_type: "text/plain", | |
filename: "dummy2.txt", | |
io: ::StringIO.new("dummy2"), | |
} | |
]) | |
end | |
assert_equal 2, user.attachments.count | |
assert user.attachments.first.service.exist?(user.attachments.first.key) | |
assert user.attachments.second.service.exist?(user.attachments.second.key) | |
end | |
# Fails | |
def test_attach_files_separately_in_transaction | |
user = User.create! | |
ActiveRecord::Base.transaction do | |
user.attachments.attach({ | |
content_type: "text/plain", | |
filename: "dummy.txt", | |
io: ::StringIO.new("dummy"), | |
}) | |
user.attachments.attach({ | |
content_type: "text/plain", | |
filename: "dummy2.txt", | |
io: ::StringIO.new("dummy2"), | |
}) | |
end | |
assert_equal 2, user.attachments.count | |
assert user.attachments.first.service.exist?(user.attachments.first.key) # Fails | |
assert user.attachments.second.service.exist?(user.attachments.second.key) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment