demarches-normaliennes/lib/tasks/deployment/20221129162903_migrate_api_tokens.rake
2024-08-22 09:26:48 +02:00

32 lines
1 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
namespace :after_party do
desc 'Deployment task: migrate_api_tokens'
task migrate_api_tokens: :environment do
puts "Running deploy task 'migrate_api_tokens'"
administrateurs = Administrateur
.where.not(encrypted_token: nil)
.where.missing(:api_tokens)
progress = ProgressReport.new(administrateurs.count)
administrateurs.find_each do |administrateur|
administrateur.transaction do
administrateur
.api_tokens
.create!(name: administrateur.updated_at.strftime('Jeton dAPI généré le %d/%m/%Y'),
encrypted_token: administrateur.encrypted_token,
version: 1)
administrateur.update_column(:encrypted_token, nil)
end
progress.inc
end
progress.finish
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end