diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index 7bdaa90fa..6fa4f929a 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -7,6 +7,7 @@ # encrypted_token :string # created_at :datetime # updated_at :datetime +# user_id :bigint # class Administrateur < ApplicationRecord include ActiveRecord::SecureToken diff --git a/app/models/expert.rb b/app/models/expert.rb index d7f80e2ac..19ccddcc9 100644 --- a/app/models/expert.rb +++ b/app/models/expert.rb @@ -5,6 +5,7 @@ # id :bigint not null, primary key # created_at :datetime not null # updated_at :datetime not null +# user_id :bigint # class Expert < ApplicationRecord has_one :user diff --git a/app/models/instructeur.rb b/app/models/instructeur.rb index 77682fe5c..66a6e3fb6 100644 --- a/app/models/instructeur.rb +++ b/app/models/instructeur.rb @@ -9,6 +9,7 @@ # created_at :datetime # updated_at :datetime # agent_connect_id :string +# user_id :bigint # class Instructeur < ApplicationRecord has_and_belongs_to_many :administrateurs diff --git a/app/models/procedure_revision.rb b/app/models/procedure_revision.rb index 458dd0c38..09d425847 100644 --- a/app/models/procedure_revision.rb +++ b/app/models/procedure_revision.rb @@ -2,11 +2,11 @@ # # Table name: procedure_revisions # -# id :bigint not null, primary key -# published_at :datetime -# created_at :datetime not null -# updated_at :datetime not null -# attestation_template_id :bigint +# id :bigint not null, primary key +# published_at :datetime +# created_at :datetime not null +# updated_at :datetime not null +# attestation_template_id :bigint # dossier_submitted_message_id :bigint # procedure_id :bigint not null # diff --git a/app/models/user.rb b/app/models/user.rb index 4da9b3382..9c0467b13 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -65,6 +65,21 @@ class User < ApplicationRecord validate :does_not_merge_on_self, if: :requested_merge_into_id_changed? + # Temporary code for double writing the admin, instructeur and expert id to the foreign key + after_save do + if saved_change_to_attribute?(:administrateur_id) && administrateur_id.present? + Administrateur.find(administrateur_id).update!(user_id: id) + end + + if saved_change_to_attribute?(:instructeur_id) && instructeur_id.present? + Instructeur.find(instructeur_id).update!(user_id: id) + end + + if saved_change_to_attribute?(:expert_id) && expert_id.present? + Expert.find(expert_id).update!(user_id: id) + end + end + def validate_password_complexity? administrateur? end @@ -221,6 +236,7 @@ class User < ApplicationRecord old_user.invites.update_all(user_id: id) old_user.merge_logs.update_all(user_id: id) + # Move or merge old user's roles to the user [ [old_user.instructeur, instructeur], [old_user.expert, expert], @@ -232,6 +248,8 @@ class User < ApplicationRecord targeted_role.merge(old_role) end end + # (Ensure the old user doesn't reference its former roles anymore) + old_user.reload merge_logs.create(from_user_id: old_user.id, from_user_email: old_user.email) old_user.destroy diff --git a/db/migrate/20220315113510_add_foreign_keys_to_user_related_models.rb b/db/migrate/20220315113510_add_foreign_keys_to_user_related_models.rb new file mode 100644 index 000000000..07cd3a0ee --- /dev/null +++ b/db/migrate/20220315113510_add_foreign_keys_to_user_related_models.rb @@ -0,0 +1,11 @@ +class AddForeignKeysToUserRelatedModels < ActiveRecord::Migration[6.1] + def change + # options = { type: :bigint, index: { unique: true }, foreign_key: true, null: true } + # add_reference :administrateurs, :user, **options + # add_reference :instructeurs, :user, **options + # add_reference :experts, :user, **options + add_column :administrateurs, :user_id, :bigint + add_column :instructeurs, :user_id, :bigint + add_column :experts, :user_id, :bigint + end +end diff --git a/db/schema.rb b/db/schema.rb index 3e2d4a1f7..870305058 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.define(version: 2022_03_08_110720) do +ActiveRecord::Schema.define(version: 2022_03_15_113510) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -60,6 +60,7 @@ ActiveRecord::Schema.define(version: 2022_03_08_110720) do t.datetime "updated_at" t.boolean "active", default: false t.string "encrypted_token" + t.bigint "user_id" end create_table "administrateurs_instructeurs", id: false, force: :cascade do |t| @@ -411,6 +412,7 @@ ActiveRecord::Schema.define(version: 2022_03_08_110720) do create_table "experts", force: :cascade do |t| t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.bigint "user_id" end create_table "experts_procedures", force: :cascade do |t| @@ -539,6 +541,7 @@ ActiveRecord::Schema.define(version: 2022_03_08_110720) do t.datetime "login_token_created_at" t.boolean "bypass_email_login_token", default: false, null: false t.string "agent_connect_id" + t.bigint "user_id" t.index ["agent_connect_id"], name: "index_instructeurs_on_agent_connect_id", unique: true end diff --git a/lib/tasks/deployment/20210402000000_null_export_keys_to_uuid.rake b/lib/tasks/deployment/20210402000000_null_exports_key_to_uuid.rake similarity index 100% rename from lib/tasks/deployment/20210402000000_null_export_keys_to_uuid.rake rename to lib/tasks/deployment/20210402000000_null_exports_key_to_uuid.rake diff --git a/lib/tasks/deployment/20220310115522_copy_user_association_to_user_related_models.rake b/lib/tasks/deployment/20220310115522_copy_user_association_to_user_related_models.rake new file mode 100644 index 000000000..135019792 --- /dev/null +++ b/lib/tasks/deployment/20220310115522_copy_user_association_to_user_related_models.rake @@ -0,0 +1,32 @@ +namespace :after_party do + desc 'Deployment task: copy user id to administrateurs.user_id, instructeurs.user_id and experts.user_id' + task copy_user_association_to_user_related_models: :environment do + rake_puts "Running deploy task 'copy_user_association_to_user_related_models'" + + copy_user_id_to(Administrateur) + copy_user_id_to(Instructeur) + copy_user_id_to(Expert) + + # 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 + + def copy_user_id_to(model) + table_name = model.table_name + rake_print "Copying user.#{table_name.singularize}_id to #{table_name}.user_id" + + records_to_update = model.where(user_id: nil) + progress = ProgressReport.new(records_to_update.count) + + records_to_update.where(user_id: nil).in_batches do |relation| + count = relation.update_all("user_id = (SELECT id FROM users WHERE #{table_name}.id = users.#{table_name.singularize}_id)") + progress.inc(count) + sleep(0.01) # throttle + end + + progress.finish + rake_puts + end +end diff --git a/lib/tasks/task_helper.rb b/lib/tasks/task_helper.rb index 8323773d7..0057a21be 100644 --- a/lib/tasks/task_helper.rb +++ b/lib/tasks/task_helper.rb @@ -33,8 +33,8 @@ class ProgressReport set_progress(total: total, count: 0) end - def inc - set_progress(count: @count + 1) + def inc(amount = 1) + set_progress(count: @count + amount) if @per_10_000 % 10 == 0 print_progress end diff --git a/spec/controllers/administrateurs/procedures_controller_spec.rb b/spec/controllers/administrateurs/procedures_controller_spec.rb index feae0df8b..a53d204fc 100644 --- a/spec/controllers/administrateurs/procedures_controller_spec.rb +++ b/spec/controllers/administrateurs/procedures_controller_spec.rb @@ -14,7 +14,9 @@ describe Administrateurs::ProceduresController, type: :controller do let(:lien_site_web) { 'http://mon-site.gouv.fr' } describe '#apercu' do - let(:procedure) { create(:procedure) } + render_views + + let(:procedure) { create(:procedure, :with_all_champs) } before do sign_in(admin.user)