Merge pull request #9228 from demarches-simplifiees/partition_pj_per_year_and_prefix
tech: place les pjs sous un namespace de type année/aa/bb/token
This commit is contained in:
commit
08091571f1
3 changed files with 61 additions and 0 deletions
31
app/jobs/pjs_migration_job.rb
Normal file
31
app/jobs/pjs_migration_job.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
class PjsMigrationJob < ApplicationJob
|
||||
queue_as :pj_migration_jobs
|
||||
|
||||
def perform(blob_id)
|
||||
blob = Blob.find(blob_id)
|
||||
|
||||
return if already_moved?(blob)
|
||||
|
||||
service = blob.service
|
||||
client = service.client
|
||||
container = service.container
|
||||
old_key = blob.key
|
||||
new_key = "#{blob.created_at.year}/#{old_key[0..1]}/#{old_key[2..3]}/#{old_key}"
|
||||
|
||||
excon_response = client.copy_object(container,
|
||||
old_key,
|
||||
container,
|
||||
new_key,
|
||||
{ "Content-Type" => blob.content_type })
|
||||
|
||||
if excon_response.status == 201
|
||||
blob.update_columns(key: new_key)
|
||||
client.delete_object(container, old_key)
|
||||
end
|
||||
rescue Fog::OpenStack::Storage::NotFound
|
||||
end
|
||||
|
||||
def already_moved?(blob)
|
||||
blob.key.include?('/')
|
||||
end
|
||||
end
|
|
@ -7,6 +7,11 @@ ActiveSupport.on_load(:active_storage_blob) do
|
|||
include BlobTitreIdentiteWatermarkConcern
|
||||
include BlobVirusScannerConcern
|
||||
include BlobSignedIdConcern
|
||||
|
||||
def self.generate_unique_secure_token(length: MINIMUM_TOKEN_LENGTH)
|
||||
token = super
|
||||
"#{Time.current.year}/#{token[0..1]}/#{token[2..3]}/#{token}"
|
||||
end
|
||||
end
|
||||
|
||||
ActiveSupport.on_load(:active_storage_attachment) do
|
||||
|
|
25
lib/tasks/pjs.rake
Normal file
25
lib/tasks/pjs.rake
Normal file
|
@ -0,0 +1,25 @@
|
|||
namespace :pjs do
|
||||
task stats: :environment do
|
||||
end
|
||||
|
||||
desc <<~EOD
|
||||
bin/rake 'pjs:migrate['aa']'
|
||||
bin/rake 'pjs:migrate['aa-ab-ac']'
|
||||
EOD
|
||||
task :migrate, [:prefixes] => :environment do |_t, args|
|
||||
# [a-b] => "key LIKE 'a%' or key LIKE 'b%'"
|
||||
prefix_like_query = args[:prefixes]
|
||||
.split('-')
|
||||
.map { "key LIKE '#{_1}%'" }
|
||||
.join(' or ')
|
||||
|
||||
blobs = ActiveStorage::Blob
|
||||
.where(prefix_like_query)
|
||||
.where.not("key LIKE '%/%'") # do not touch already moved blob
|
||||
|
||||
blobs_count = blobs.count
|
||||
rake_puts "targeted prefix: #{args[:prefixes]}, #{blobs_count} blobs"
|
||||
|
||||
blobs.in_batches { |batch| batch.ids.each { |id| PjsMigrationJob.perform_later(id) } }
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue