Skip to content

Commit

Permalink
Strip whitespace from organisation_slug on existing BIU records
Browse files Browse the repository at this point in the history
Trello: https://trello.com/c/aO4nVCZT

I forgot to do this as part of #2371.

I've constructed the query in the migration slightly differently to
similar previous migrations, because I noticed the whitespace character
in the regular expression wasn't working as intended.

I've had to use ActiveRecord::Persistence#update_attribute in order to
skip model validation, because some of the existing BatchInvitationUser
records are invalid for other reasons.

I did consider writing the migration as a SQL UPDATE, but generating a
TRIM expression that is exactly equivalent to String#strip isn't as
trivial as it sounds!
  • Loading branch information
floehopper committed Sep 25, 2023
1 parent 2e9d06c commit 6977102
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class StripWhitespaceFromBatchInvitationUserOrganisationSlug < ActiveRecord::Migration[7.0]
def change
BatchInvitationUser.where("organisation_slug REGEXP ? OR organisation_slug REGEXP ?", "^\\s+", "\\s+$").each do |biu|
biu.update_attribute(:organisation_slug, biu.organisation_slug&.strip) # rubocop:disable Rails/SkipsModelValidations
end
end
end
2 changes: 1 addition & 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.0].define(version: 2023_09_25_093142) do
ActiveRecord::Schema[7.0].define(version: 2023_09_25_093143) do
create_table "batch_invitation_application_permissions", id: :integer, charset: "utf8mb3", force: :cascade do |t|
t.integer "batch_invitation_id", null: false
t.integer "supported_permission_id", null: false
Expand Down

0 comments on commit 6977102

Please sign in to comment.