From 0a3eb4a90925dada2cc564eeef446b283266ea6e Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 15 Mar 2022 11:14:45 +0100 Subject: [PATCH 1/7] reinforce preview testing --- .../controllers/administrateurs/procedures_controller_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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) From 2cac7d96be82cf8095ff9c48dbffe8791230fd8a Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 15 Mar 2022 15:10:56 +0100 Subject: [PATCH 2/7] models: fix procedure_revision comments indentation --- app/models/procedure_revision.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) 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 # From be834ba58d00fcfc35dd124c5bc4985373e4b046 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 15 Mar 2022 12:51:01 +0000 Subject: [PATCH 3/7] models: reload old_user roles before destroying it Otherwise ActiveRecord believes that old_user still has roles (although they have been migrated to the new user), and attempts to delete them. --- app/models/user.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 4da9b3382..441737c09 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -221,6 +221,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 +233,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 From 48ea53b90d508c468e362c11addce6a7039e2c37 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 8 Mar 2022 17:45:36 +0100 Subject: [PATCH 4/7] db: add foreign keys to Administrateur, Instructeur and Expert --- app/models/administrateur.rb | 1 + app/models/expert.rb | 1 + app/models/instructeur.rb | 1 + ...5113510_add_foreign_keys_to_user_related_models.rb | 11 +++++++++++ db/schema.rb | 5 ++++- 5 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20220315113510_add_foreign_keys_to_user_related_models.rb 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/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 From d2f12e518835298702ff02ea1c401a1570e1fdc1 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 9 Mar 2022 17:08:03 +0000 Subject: [PATCH 5/7] db: copy user-related ids to the respective models --- ...er_association_to_user_related_models.rake | 32 +++++++++++++++++++ lib/tasks/task_helper.rb | 4 +-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 lib/tasks/deployment/20220310115522_copy_user_association_to_user_related_models.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 From ae3e3601396ab4a97ce380b2a13530521960c561 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 8 Mar 2022 17:12:01 +0000 Subject: [PATCH 6/7] models: double-write to the newly created foreign keys This code is only used to handle a live system creating new Administrateurs, Instructeurs or Experts before the migration to the new foreign keys. --- app/models/user.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 441737c09..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 From 1b69accd107452df2c6fb60020457f561f02d91e Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 15 Mar 2022 16:20:15 +0100 Subject: [PATCH 7/7] tasks: rename after_party task to the proper name The name of the file must be exactly the same than the name of the task. --- ..._to_uuid.rake => 20210402000000_null_exports_key_to_uuid.rake} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/tasks/deployment/{20210402000000_null_export_keys_to_uuid.rake => 20210402000000_null_exports_key_to_uuid.rake} (100%) 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