From a17b024681abd0e3cea2f0ed730369c12f775266 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 30 May 2025 14:56:58 -0400 Subject: [PATCH] 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 --- Gemfile | 1 + Gemfile.lock | 22 +++++++++++++++++++++- script/migrate-disk-service-blobs.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100755 script/migrate-disk-service-blobs.rb diff --git a/Gemfile b/Gemfile index 8c1219952..457f0790b 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/Gemfile.lock b/Gemfile.lock index 5fcb8b1ef..8afd7ccc9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/script/migrate-disk-service-blobs.rb b/script/migrate-disk-service-blobs.rb new file mode 100755 index 000000000..540787239 --- /dev/null +++ b/script/migrate-disk-service-blobs.rb @@ -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