Skip to content

Instantly share code, notes, and snippets.

@nshki
Last active June 21, 2020 15:16
Show Gist options
  • Save nshki/5698eb03e0427228a7aeb4fd5cc1c283 to your computer and use it in GitHub Desktop.
Save nshki/5698eb03e0427228a7aeb4fd5cc1c283 to your computer and use it in GitHub Desktop.
Script for Rails issue #39647 that replicates the reported issue.
# 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 "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 :tournament_registrations, force: true do |t|
t.datetime :deleted_at
end
end
class TournamentRegistration < ActiveRecord::Base
has_one_attached :attachment
default_scope { where(deleted_at: nil) }
end
require "minitest/autorun"
class BugTest < Minitest::Test
def test_destroy_orphaned_by_default_scope_attachments
registration = create_registration
registration.update(deleted_at: Time.zone.now)
ActiveStorage::Attachment.where(record_type: "TournamentRegistration").map(&:destroy)
assert_equal 0, ActiveStorage::Attachment.count
end
def test_purge_orphaned_by_default_scope_attachments
registration = create_registration
registration.update(deleted_at: Time.zone.now)
ActiveStorage::Attachment.where(record_type: "TournamentRegistration").map(&:purge)
assert_equal 0, ActiveStorage::Attachment.count
end
def test_destroy_orphaned_by_delete_all_attachments
create_registration
TournamentRegistration.delete_all
ActiveStorage::Attachment.where(record_type: "TournamentRegistration").map(&:destroy)
assert_equal 0, ActiveStorage::Attachment.count
end
def test_purge_orphaned_by_delete_all_attachments
create_registration
TournamentRegistration.delete_all
ActiveStorage::Attachment.where(record_type: "TournamentRegistration").map(&:purge)
assert_equal 0, ActiveStorage::Attachment.count
end
end
# Convenience method for creating a TournamentRegistration record.
#
# @return {TournamentRegistration} - Dummy record
def create_registration
TournamentRegistration.create!(
attachment: {
content_type: "text/plain",
filename: "dummy.txt",
io: ::StringIO.new("dummy"),
}
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment