From 7efb7fc426cec636eba96c7d160cfaf622abe942 Mon Sep 17 00:00:00 2001 From: Artur Trofimov Date: Fri, 23 Sep 2016 21:20:43 +1000 Subject: [PATCH] fix first migration for migrate users to redmine_2fa SMS --- db/migrate/001_add_sms_auth.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/db/migrate/001_add_sms_auth.rb b/db/migrate/001_add_sms_auth.rb index 4aba22d..43b7991 100644 --- a/db/migrate/001_add_sms_auth.rb +++ b/db/migrate/001_add_sms_auth.rb @@ -4,9 +4,24 @@ def up end def down - AuthSource.where(type: 'AuthSourceSms').destroy_all + if Redmine::Plugin.installed?(:redmine_2fa) - Redmine2FA::AuthSourceSms.create name: 'SMS', onthefly_register: false, tls: false + old_auth_source = AuthSource.where(type: 'AuthSourceSms').first + old_auth_source_id = if old_auth_source + old_auth_source.id + else + (User.all.pluck(:auth_source_id).compact.uniq - AuthSource.pluck(:id)).first + end + + AuthSource.where(type: 'AuthSourceSms').destroy_all + + new_auth_source = Redmine2FA::AuthSourceSms.create name: 'SMS', onthefly_register: false, tls: false + + if old_auth_source_id + User.where(auth_source_id: old_auth_source_id).update_all(auth_source_id: new_auth_source.id) + end + else + AuthSource.where(type: 'AuthSourceSms').destroy_all end end end