From ab75744a6fef0032250784edbb5a52c9321accd5 Mon Sep 17 00:00:00 2001 From: James Williams Date: Tue, 16 Feb 2016 06:54:44 -0700 Subject: [PATCH] fix folders with leading spaces as well as trailing ones closes #CNVS-27257 Change-Id: I21f88c87400cda576f233c57b88026995bf391b5 Reviewed-on: https://gerrit.instructure.com/72224 Tested-by: Jenkins Reviewed-by: Jeremy Stanley Product-Review: James Williams QA-Review: James Williams --- app/models/folder.rb | 2 +- ...names.rb => 20160216135203_fix_folder_names_again.rb} | 2 +- lib/data_fixup/fix_folder_names.rb | 9 +++++++-- 3 files changed, 9 insertions(+), 4 deletions(-) rename db/migrate/{20151222173024_fix_folder_names.rb => 20160216135203_fix_folder_names_again.rb} (78%) diff --git a/app/models/folder.rb b/app/models/folder.rb index eeebcc3a6c6..bada015e124 100644 --- a/app/models/folder.rb +++ b/app/models/folder.rb @@ -147,7 +147,7 @@ class Folder < ActiveRecord::Base # TODO i18n t :default_folder_name, 'New Folder' self.name = 'New Folder' if self.name.blank? - self.name = self.name.rstrip.gsub(/\//, "_") + self.name = self.name.strip.gsub(/\//, "_") folder = self @update_sub_folders = false self.parent_folder_id = nil if !self.parent_folder || self.parent_folder.context != self.context || self.parent_folder_id == self.id diff --git a/db/migrate/20151222173024_fix_folder_names.rb b/db/migrate/20160216135203_fix_folder_names_again.rb similarity index 78% rename from db/migrate/20151222173024_fix_folder_names.rb rename to db/migrate/20160216135203_fix_folder_names_again.rb index 1f6eaf131a1..31acd78acdf 100644 --- a/db/migrate/20151222173024_fix_folder_names.rb +++ b/db/migrate/20160216135203_fix_folder_names_again.rb @@ -1,4 +1,4 @@ -class FixFolderNames < ActiveRecord::Migration +class FixFolderNamesAgain < ActiveRecord::Migration tag :postdeploy def up DataFixup::FixFolderNames.send_later_if_production_enqueue_args(:run, diff --git a/lib/data_fixup/fix_folder_names.rb b/lib/data_fixup/fix_folder_names.rb index 1fcfd925eda..063bb15cd35 100644 --- a/lib/data_fixup/fix_folder_names.rb +++ b/lib/data_fixup/fix_folder_names.rb @@ -1,9 +1,14 @@ module DataFixup module FixFolderNames def self.run + wildcards = [] + [" ", "\t", "\r", "\n"].each do |ws| + wildcards += ["#{ws}%", "%#{ws}"] + end + Folder.find_ids_in_ranges(:batch_size => 10000) do |min_id, max_id| - Folder.active.where(:id => min_id..max_id).where("name LIKE ?", "% ").each do |f| - f.name = f.name.rstrip + Folder.active.where(:id => min_id..max_id).where((["(name LIKE ?)"] * wildcards.count).join(" OR "), *wildcards).each do |f| + f.name = f.name.strip f.save end end