From 6452b40ad888f9e540ea242162d9d50eda9615de Mon Sep 17 00:00:00 2001 From: meyric Date: Thu, 12 Dec 2024 14:40:04 +0000 Subject: [PATCH] Migration to allow users to be anonymised This is the first step in a larger piece of work. We will use DateTime to store when a user is deactivated, this will replace the `active` attribute: - when the deactivated_at is nil the user is active - when the deactivated_at has a date time the user has been deactivated We have no way to know when a user was deactivated (hence the work!) so we'll use the updated_at to be our best guess. At this point the application is broken, but we'll keep this commit separate and fix that next. --- .../20241212115356_add_user_anonymisation.rb | 23 +++++++++++++++++++ db/schema.rb | 5 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20241212115356_add_user_anonymisation.rb diff --git a/db/migrate/20241212115356_add_user_anonymisation.rb b/db/migrate/20241212115356_add_user_anonymisation.rb new file mode 100644 index 000000000..77097b52d --- /dev/null +++ b/db/migrate/20241212115356_add_user_anonymisation.rb @@ -0,0 +1,23 @@ +class AddUserAnonymisation < ActiveRecord::Migration[6.1] + def up + add_column :users, :deactivated_at, :datetime + add_column :users, :anonymised_at, :datetime + + User.where(active: false).each do |user| + user.update_column(:deactivated_at, user.updated_at) + end + + remove_column :users, :active + end + + def down + add_column :users, :active, :boolean, default: true + + User.where(deactivated_at: nil).each do |user| + user.update_column(:active, true) + end + + remove_column :users, :deactivated_at, :datetime + remove_column :users, :anonymised_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 596c0cb34..549d83b21 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: 2024_12_04_220209) do +ActiveRecord::Schema.define(version: 2024_12_12_115356) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -358,7 +358,6 @@ t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.uuid "organisation_id" - t.boolean "active", default: true t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" @@ -370,6 +369,8 @@ t.boolean "otp_required_for_login", default: true t.string "mobile_number" t.datetime "mobile_number_confirmed_at" + t.datetime "deactivated_at" + t.datetime "anonymised_at" t.index ["organisation_id"], name: "index_users_on_organisation_id" t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end