Update AR::Tenanted to have tenanted blob keys

Also, a script to migrate blobs (on disk and in the database) to the
new convention.

ref: https://37s.fizzy.37signals.com/collections/7/cards/246
This commit is contained in:
Mike Dalessio
2025-05-30 14:56:58 -04:00
parent bd44d0558f
commit a17b024681
3 changed files with 50 additions and 1 deletions
+1
View File
@@ -30,6 +30,7 @@ gem "jbuilder"
gem "actiontext-lexical", bc: "actiontext-lexical"
gem "image_processing", "~> 1.14"
gem "platform_agent"
gem "aws-sdk-s3", require: false
# Telemetry and logging
gem "sentry-ruby"
+21 -1
View File
@@ -7,7 +7,7 @@ GIT
GIT
remote: https://github.com/basecamp/active_record-tenanted
revision: 523947a0f0f49f66a8a62eb93e9ba1a345b3076b
revision: f444578d6e59a1bdd65eda0ff765c9b5dac73398
specs:
active_record-tenanted (0.1.0)
activerecord (>= 8.1.alpha)
@@ -144,6 +144,24 @@ GEM
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
ast (2.4.3)
aws-eventstream (1.3.2)
aws-partitions (1.1108.0)
aws-sdk-core (3.224.1)
aws-eventstream (~> 1, >= 1.3.0)
aws-partitions (~> 1, >= 1.992.0)
aws-sigv4 (~> 1.9)
base64
jmespath (~> 1, >= 1.6.1)
logger
aws-sdk-kms (1.101.0)
aws-sdk-core (~> 3, >= 3.216.0)
aws-sigv4 (~> 1.5)
aws-sdk-s3 (1.188.0)
aws-sdk-core (~> 3, >= 3.224.1)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.5)
aws-sigv4 (1.11.0)
aws-eventstream (~> 1, >= 1.0.2)
base64 (0.2.0)
bcrypt (3.1.20)
bcrypt_pbkdf (1.1.1)
@@ -225,6 +243,7 @@ GEM
jbuilder (2.13.0)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
jmespath (1.6.2)
json (2.11.3)
kamal (2.6.1)
activesupport (>= 7.0)
@@ -470,6 +489,7 @@ PLATFORMS
DEPENDENCIES
actiontext-lexical!
active_record-tenanted!
aws-sdk-s3
bcrypt (~> 3.1.7)
bootsnap
brakeman
+28
View File
@@ -0,0 +1,28 @@
#! /usr/bin/env ruby
require_relative "../config/environment"
ApplicationRecord.with_each_tenant do |tenant|
puts "\n## #{tenant}"
report = { updated: 0, skipped: 0 }
ActiveStorage::Blob.find_each do |blob|
if blob.key.start_with?("#{tenant}/")
report[:skipped] += 1
else
blob.update_column :key, "#{tenant}/#{blob.key}"
report[:updated] += 1
end
end
pp report
disk_service = ActiveStorage::Blob.services.fetch(:local)
new_root = File.join(disk_service.root, tenant)
old_root = File.join("storage", "tenants", Rails.env, tenant, "files")
FileUtils.mkdir_p(new_root, verbose: true) unless File.directory?(new_root)
Dir.glob(File.join(old_root, "??")).each_slice(20) do |blob_dirs|
FileUtils.mv(blob_dirs, new_root, verbose: true)
end
end