Skip to content

Commit

Permalink
Migration to allow users to be anonymised
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mec committed Dec 12, 2024
1 parent 63a700f commit 6452b40
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
23 changes: 23 additions & 0 deletions db/migrate/20241212115356_add_user_anonymisation.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 6452b40

Please sign in to comment.