ensl.org/db/migrate/20200401031518_update_passwords_to_scrypt.rb

22 lines
655 B
Ruby
Raw Normal View History

2020-04-02 00:15:28 +00:00
ENV['SCRYPT_MAX_TIME'] ||= "1"
2020-04-02 00:08:19 +00:00
class UpdatePasswordsToScrypt < ActiveRecord::Migration[6.0]
require 'scrypt'
def up
puts("SCRYPT_MAX_TIME=%s" % ENV['SCRYPT_MAX_TIME'])
2020-04-06 02:52:39 +00:00
puts("Migration takes about %0.0f seconds." % ENV['SCRYPT_MAX_TIME'].to_f*User.all.count*3)
2020-04-02 02:08:58 +00:00
SCrypt::Engine.calibrate!(max_time: ENV['SCRYPT_MAX_TIME'].to_f)
2020-04-02 00:08:19 +00:00
ActiveRecord::Base.transaction do
User.all.order(:id).each do |user|
2020-04-02 00:15:28 +00:00
user.team = nil unless user&.team&.present?
if user.valid?
user.update_password
user.save!
puts("User %s (%d) updated" % [user.username, user.id])
2020-04-02 00:15:28 +00:00
end
2020-04-02 00:08:19 +00:00
end
end
end
end