【发布时间】:2020-10-08 14:18:05
【问题描述】:
根据 Rails (edge6.0) 指南,我们可以分别调用以下语句对System Test 和Integration Test 中使用的ActiveStorage 进行维护工作
# System Test
FileUtils.rm_rf("#{Rails.root}/storage_test")
# Integration Test
FileUtils.rm_rf(Rails.root.join('tmp', 'storage'))
我想知道-
是否有任何 Rails 内置函数或 rake 命令或 gem 来执行以下操作?
- 删除孤立 blob(
ActiveStorage::Blob记录不再与任何ActiveStorage::Attachment记录关联) - 删除孤立文件(不再与任何
ActiveStorage::Blob记录关联的文件)
我没有看到任何与 rails --tasks 相关的 rake 任务。
目前,我正在使用
# remove blob not associated with any attachment
ActiveStorage::Blob.where.not(id: ActiveStorage::Attachment.select(:blob_id)).find_each do |blob|
blob.purge # or purge_later
end
这个脚本来清理孤立文件(通过rails console)
# run these ruby statement in project rails console
# to remove the orphan file
include ActionView::Helpers::NumberHelper
dry_run = true
files = Dir['storage/??/??/*']
orphan = files.select do |f|
!ActiveStorage::Blob.exists?(key: File.basename(f))
end
sum = 0
orphan.each do |f|
sum += File.size(f)
FileUtils.remove(f) unless dry_run
end
puts "Size: #{number_to_human_size(sum)}"
【问题讨论】:
-
看起来你做对了。我也想知道,环顾四周,我没有看到任何开箱即用的东西。
标签: ruby-on-rails rails-activestorage