diff --git a/config/initializers/active_storage.rb b/config/initializers/active_storage.rb index 8a9b8aa5..a8606ec5 100644 --- a/config/initializers/active_storage.rb +++ b/config/initializers/active_storage.rb @@ -19,6 +19,16 @@ ActiveSupport.on_load(:active_storage_attachment) do end ActiveSupport.on_load(:active_storage_blob) do + attribute :uuid, :string, default: -> { SecureRandom.uuid } + + def uuid + super || begin + new_uuid = SecureRandom.uuid + update_columns(uuid: new_uuid) + new_uuid + end + end + def delete service.delete(key) end diff --git a/db/migrate/20240218082157_add_uuid_to_active_storage_blobs.rb b/db/migrate/20240218082157_add_uuid_to_active_storage_blobs.rb new file mode 100644 index 00000000..4d13d698 --- /dev/null +++ b/db/migrate/20240218082157_add_uuid_to_active_storage_blobs.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddUuidToActiveStorageBlobs < ActiveRecord::Migration[7.1] + def change + add_column :active_storage_blobs, :uuid, :string + + add_index :active_storage_blobs, :uuid, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 07f7930d..d25b6fe6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_02_03_113455) do +ActiveRecord::Schema[7.1].define(version: 2024_02_18_082157) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -74,7 +74,9 @@ ActiveRecord::Schema[7.1].define(version: 2024_02_03_113455) do t.bigint "byte_size", null: false t.string "checksum" t.datetime "created_at", null: false + t.string "uuid" t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true + t.index ["uuid"], name: "index_active_storage_blobs_on_uuid", unique: true end create_table "active_storage_variant_records", force: :cascade do |t|