Skip to content

Commit

Permalink
Change jobseekers email in db to Citext. Part 1.
Browse files Browse the repository at this point in the history
To achieve a smooth non-case sensitive "search user by email", while
keeping the indexed search instead of table scanning, the ideal
solution seems to be using citext as the DB type, allowing postgresql to
do the non-case sensitive matches.

Changing the column type directly would lock the Jobseekers table. So,
following "Online Migrations" guidance (and its useful helpers), we're
doing the change in stages to avoid causing service downtime.

The first stages to be deployed are:

1 - Create a new email column of citext type that is kept in sync with
email column using DB triggers.

2 - Backfill this column for all the existing data.

After deploying and testing this, we will follow up with copying the
constraints, indexes, foreign keys, etc and swapping the columns.

Once that is deployed we will remove the copy trigger and the old
column.

Reference:

https://github.com/fatkodima/online_migrations?tab=readme-ov-file#bullettrain_side-concrete-steps-for-active-record
  • Loading branch information
scruti committed Oct 11, 2024
1 parent 3fd9f7d commit dc8f6e8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class InitializeChangeJobseekersEmailType < ActiveRecord::Migration[7.1]
def change
enable_extension("citext")

initialize_column_type_change :jobseekers, :email, :citext
end
end
11 changes: 11 additions & 0 deletions db/migrate/20241011114125_backfill_change_jobseekers_email_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class BackfillChangeJobseekersEmailType < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def up
backfill_column_for_type_change :jobseekers, :email
end

def down
# no op
end
end
3 changes: 2 additions & 1 deletion 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[7.1].define(version: 2024_09_24_144355) do
ActiveRecord::Schema[7.1].define(version: 2024_10_11_114125) do
# These are extensions that must be enabled in order to support this database
enable_extension "btree_gist"
enable_extension "citext"
Expand Down Expand Up @@ -320,6 +320,7 @@
t.text "last_sign_in_ip_ciphertext"
t.string "account_merge_confirmation_code"
t.datetime "account_merge_confirmation_code_generated_at"
t.citext "email_for_type_change", default: "", null: false
t.index ["confirmation_token"], name: "index_jobseekers_on_confirmation_token", unique: true
t.index ["email"], name: "index_jobseekers_on_email", unique: true
t.index ["reset_password_token"], name: "index_jobseekers_on_reset_password_token", unique: true
Expand Down

0 comments on commit dc8f6e8

Please sign in to comment.