From 55e1bdbf79ce2489f2dc95348eef868abf747852 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Tue, 19 Mar 2013 09:48:47 -0600 Subject: [PATCH] arel-ify migrations refs CNVS-4707 Change-Id: Iac66804015191f91e99e635571134fc575bd3ba8 Reviewed-on: https://gerrit.instructure.com/18751 Tested-by: Jenkins Reviewed-by: Jacob Fugal Product-Review: Cody Cutrer QA-Review: Cody Cutrer --- ...add_workflow_state_to_grading_standards.rb | 2 +- ...0110809193507_fix_duplicate_discussions.rb | 24 +++++++++---------- ...151806_remove_inactive_enrollment_state.rb | 2 +- ...30235857_unscribd_text_html_attachments.rb | 3 ++- ...7115901_group_categories_data_migration.rb | 14 +++++------ ...010173231_update_twitter_urls_for_https.rb | 2 +- ..._drop_deleted_unique_id_from_pseudonyms.rb | 8 +++---- ...3224_drop_sis_source_id_from_pseudonyms.rb | 2 +- ...4400_group_categories_cleanup_migration.rb | 10 ++++---- ...date_submitted_at_for_discussion_topics.rb | 2 +- ...11026201231_restore_users_sortable_name.rb | 2 +- ...0000_ensure_submissions_for_discussions.rb | 4 ++-- ...le_open_registration_for_delegated_auth.rb | 7 +++++- ...23022449_fix_enrollment_root_account_id.rb | 6 ++--- ...209171640_recalculate_muted_assignments.rb | 4 +--- ...ove_attachments_with_no_scope_from_list.rb | 2 +- ...04183918_fix_sis_communication_channels.rb | 4 ++-- ...roy_existing_anonymous_folder_downloads.rb | 4 ++-- db/migrate/20120126200026_labels_to_stars.rb | 2 +- ...purge_duplicate_deleted_sis_enrollments.rb | 6 ++--- ...41_remove_duplicate_submission_messages.rb | 2 +- ...9223909_fix_zero_point_pass_fail_scores.rb | 8 +++---- ...6_remove_irrelevant_submission_messages.rb | 2 +- ...193327_accept_pending_group_memberships.rb | 4 ++-- ...16214454_set_blank_sis_user_ids_to_null.rb | 2 +- ...emove_deleted_user_account_associations.rb | 2 +- ...847_remove_unused_notification_policies.rb | 4 ++-- ...20224194848_remove_unused_notifications.rb | 4 ++-- ...301231546_set_discussion_entry_root_ids.rb | 4 ++-- ...42_break_down_detailed_report_snapshots.rb | 2 +- ...404230916_fix_user_merge_conversations2.rb | 4 ++-- ...tion_notifications_to_separate_category.rb | 8 +++---- ...ort_notifications_to_migration_category.rb | 4 ++-- ...ault_limit_privileges_to_course_section.rb | 4 ++-- ...constrain_assignment_group_category_ids.rb | 2 +- ...705144244_add_unique_index_on_favorites.rb | 2 +- db/migrate/20120723201410_uniquify_wikis.rb | 4 ++-- ...45851_add_unique_index_on_notifications.rb | 2 +- .../20120817191623_fix_profile_pictures.rb | 15 +++++------- .../20121017165823_hash_access_tokens.rb | 2 +- ...203164800_add_unique_index_on_role_name.rb | 2 +- ...3149_clean_up_user_account_associations.rb | 12 ++++++---- ...ue_index_on_course_account_associations.rb | 8 +++++-- 43 files changed, 111 insertions(+), 102 deletions(-) diff --git a/db/migrate/20110409232339_add_workflow_state_to_grading_standards.rb b/db/migrate/20110409232339_add_workflow_state_to_grading_standards.rb index 2dba6bebfbf..2980a8359bc 100644 --- a/db/migrate/20110409232339_add_workflow_state_to_grading_standards.rb +++ b/db/migrate/20110409232339_add_workflow_state_to_grading_standards.rb @@ -1,7 +1,7 @@ class AddWorkflowStateToGradingStandards < ActiveRecord::Migration def self.up add_column :grading_standards, :workflow_state, :string - GradingStandard.update_all({:workflow_state => 'active'}) + GradingStandard.update_all(:workflow_state => 'active') end def self.down diff --git a/db/migrate/20110809193507_fix_duplicate_discussions.rb b/db/migrate/20110809193507_fix_duplicate_discussions.rb index 951027bdda9..a597dc61b9f 100644 --- a/db/migrate/20110809193507_fix_duplicate_discussions.rb +++ b/db/migrate/20110809193507_fix_duplicate_discussions.rb @@ -1,22 +1,22 @@ class FixDuplicateDiscussions < ActiveRecord::Migration def self.up DiscussionTopic.transaction do - duplicate_topics = DiscussionTopic.find(:all, - :select => 'old_assignment_id, context_id, context_type', - :conditions => "old_assignment_id IS NOT NULL AND workflow_state <> 'deleted' AND root_topic_id IS NULL AND context_type='Course'", - :group => 'old_assignment_id, context_id, context_type HAVING count(*) > 1') + duplicate_topics = DiscussionTopic. + select([:old_assignment_id, :context_id, :context_type]). + where("old_assignment_id IS NOT NULL AND workflow_state <> 'deleted' AND root_topic_id IS NULL AND context_type='Course'"). + group('old_assignment_id, context_id, context_type HAVING COUNT(*) > 1').all duplicate_topics.each do |topic| - duplicates = DiscussionTopic.find(:all, - :conditions => ["context_id=? and context_type=? and old_assignment_id=? and workflow_state<>'deleted' and root_topic_id is null", - topic.context_id, topic.context_type, topic.old_assignment_id], - :order => 'updated_at DESC') + duplicates = DiscussionTopic. + where("context_id=? AND context_type=? AND old_assignment_id=? AND workflow_state<>'deleted' AND root_topic_id is null", + topic.context_id, topic.context_type, topic.old_assignment_id). + order('updated_at DESC'). + all to_keep = duplicates.shift - duplicate_ids = duplicates.map(&:id) - DiscussionEntry.update_all("discussion_topic_id=#{to_keep.id}", :discussion_topic_id => duplicate_ids) - DiscussionTopic.update_all("root_topic_id=#{to_keep.id}", :root_topic_id => duplicate_ids) - DiscussionTopic.delete(duplicate_ids) + DiscussionEntry.where(:discussion_topic_id => duplicates).update_all(:discussion_topic_id => to_keep) + DiscussionTopic.where(:root_topic_id => duplicates).update_all(:root_topic_id => to_keep) + DiscussionTopic.delete(duplicates) end end end diff --git a/db/migrate/20110822151806_remove_inactive_enrollment_state.rb b/db/migrate/20110822151806_remove_inactive_enrollment_state.rb index fed037fdb77..cc3d486ff43 100644 --- a/db/migrate/20110822151806_remove_inactive_enrollment_state.rb +++ b/db/migrate/20110822151806_remove_inactive_enrollment_state.rb @@ -1,7 +1,7 @@ class RemoveInactiveEnrollmentState < ActiveRecord::Migration def self.up Delayed::Backend::ActiveRecord::Job.delete_all(:tag => 'EnrollmentDateRestrictions.update_restricted_enrollments') - Enrollment.update_all({:workflow_state => 'active'}, :workflow_state => 'inactive') + Enrollment.where(:workflow_state => 'inactive').update_all(:workflow_state => 'active') end def self.down diff --git a/db/migrate/20110930235857_unscribd_text_html_attachments.rb b/db/migrate/20110930235857_unscribd_text_html_attachments.rb index b8d533b0f97..1f7904cd9cc 100644 --- a/db/migrate/20110930235857_unscribd_text_html_attachments.rb +++ b/db/migrate/20110930235857_unscribd_text_html_attachments.rb @@ -1,6 +1,7 @@ class UnscribdTextHtmlAttachments < ActiveRecord::Migration def self.up - Attachment.scoped(:conditions => ["content_type IN ('text/html', 'application/xhtml+xml', 'application/xml', 'text/xml')"]).update_all(:scribd_mime_type_id => nil, :scribd_doc => nil) + Attachment.where(:content_type => ['text/html', 'application/xhtml+xml', 'application/xml', 'text/xml']). + update_all(:scribd_mime_type_id => nil, :scribd_doc => nil) end def self.down diff --git a/db/migrate/20111007115901_group_categories_data_migration.rb b/db/migrate/20111007115901_group_categories_data_migration.rb index 9a2a001bf8e..78827db4eff 100644 --- a/db/migrate/20111007115901_group_categories_data_migration.rb +++ b/db/migrate/20111007115901_group_categories_data_migration.rb @@ -20,21 +20,21 @@ class GroupCategoriesDataMigration < ActiveRecord::Migration def self.update_records_for_record(record) return unless record.context.present? and record.group_category_name.present? category_column = (record.class == Group ? 'category' : 'group_category') - records = record.class.scoped(:conditions => ["context_id=? AND context_type=? AND #{category_column}=? AND group_category_id IS NULL", + records = record.class.where("context_id=? AND context_type=? AND #{category_column}=? AND group_category_id IS NULL", record.context_id, record.context_type, - record.group_category_name]) - records.update_all({:group_category_id => group_category_id_for(record)}) + record.group_category_name) + records.update_all(:group_category_id => group_category_id_for(record)) end def self.up - Group.find(:all, :select => "DISTINCT context_id, context_type, category", - :conditions => ['context_id IS NOT NULL AND category IS NOT NULL AND group_category_id IS NULL']).each do |record| + Group.select([:context_id, :context_type, :category]).uniq. + where('context_id IS NOT NULL AND category IS NOT NULL AND group_category_id IS NULL').each do |record| update_records_for_record(record) end - Assignment.find(:all, :select => "DISTINCT context_id, context_type, group_category", - :conditions => ['context_id IS NOT NULL AND group_category IS NOT NULL AND group_category_id IS NULL']).each do |record| + Assignment.select([:context_id, :context_type, :group_category]).uniq. + where('context_id IS NOT NULL AND group_category IS NOT NULL AND group_category_id IS NULL').each do |record| update_records_for_record(record) end diff --git a/db/migrate/20111010173231_update_twitter_urls_for_https.rb b/db/migrate/20111010173231_update_twitter_urls_for_https.rb index b01529e7f18..b1d15974e9b 100644 --- a/db/migrate/20111010173231_update_twitter_urls_for_https.rb +++ b/db/migrate/20111010173231_update_twitter_urls_for_https.rb @@ -1,7 +1,7 @@ class UpdateTwitterUrlsForHttps < ActiveRecord::Migration def self.up while true - users = User.find(:all, :conditions => "avatar_image_source='twitter' AND avatar_image_url NOT LIKE 'https%'", :limit => 500).each do |u| + users = User.where("avatar_image_source='twitter' AND avatar_image_url NOT LIKE 'https%'").limit(500).each do |u| u.avatar_image = { 'type' => 'twitter' } u.save! end diff --git a/db/migrate/20111010205553_drop_deleted_unique_id_from_pseudonyms.rb b/db/migrate/20111010205553_drop_deleted_unique_id_from_pseudonyms.rb index b9d0fc559b2..8cdcd26d3c5 100644 --- a/db/migrate/20111010205553_drop_deleted_unique_id_from_pseudonyms.rb +++ b/db/migrate/20111010205553_drop_deleted_unique_id_from_pseudonyms.rb @@ -1,16 +1,16 @@ class DropDeletedUniqueIdFromPseudonyms < ActiveRecord::Migration def self.up - Pseudonym.update_all('unique_id=deleted_unique_id', "deleted_unique_id IS NOT NULL AND workflow_state='deleted'") + Pseudonym.where("deleted_unique_id IS NOT NULL AND workflow_state='deleted'").update_all('unique_id=deleted_unique_id') remove_column :pseudonyms, :deleted_unique_id end def self.down add_column :pseudonyms, :deleted_unique_id, :string - Pseudonym.update_all('deleted_unique_id=unique_id', "unique_id IS NOT NULL AND workflow_state='deleted'") + Pseudonym.where("unique_id IS NOT NULL AND workflow_state='deleted'").update_all('deleted_unique_id=unique_id') if %w{MySQL Mysql2}.include?(Pseudonym.connection.adapter_name) - Pseudonym.update_all("unique_id=unique_id || '--' || SUBSTR(RAND(), 3, 4)", "unique_id IS NOT NULL AND workflow_state='deleted'") + Pseudonym.where("unique_id IS NOT NULL AND workflow_state='deleted'").update_all("unique_id=unique_id || '--' || SUBSTR(RAND(), 3, 4)") else - Pseudonym.update_all("unique_id=unique_id || '--' || SUBSTR(CAST(RANDOM() AS varchar), 3, 4)", "unique_id IS NOT NULL AND workflow_state='deleted'") + Pseudonym.where("unique_id IS NOT NULL AND workflow_state='deleted'").update_all("unique_id=unique_id || '--' || SUBSTR(CAST(RANDOM() AS varchar), 3, 4)") end end end diff --git a/db/migrate/20111010223224_drop_sis_source_id_from_pseudonyms.rb b/db/migrate/20111010223224_drop_sis_source_id_from_pseudonyms.rb index d435713e9fb..6f502a45d98 100644 --- a/db/migrate/20111010223224_drop_sis_source_id_from_pseudonyms.rb +++ b/db/migrate/20111010223224_drop_sis_source_id_from_pseudonyms.rb @@ -5,6 +5,6 @@ class DropSisSourceIdFromPseudonyms < ActiveRecord::Migration def self.down add_column :pseudonyms, :sis_source_id, :string - Pseudonym.update_all('sis_source_id=unique_id', "sis_user_id IS NOT NULL") + Pseudonym.where("sis_user_id IS NOT NULL").update_all('sis_source_id=unique_id') end end diff --git a/db/migrate/20111017124400_group_categories_cleanup_migration.rb b/db/migrate/20111017124400_group_categories_cleanup_migration.rb index ce1429e4260..f99e12b6cfc 100644 --- a/db/migrate/20111017124400_group_categories_cleanup_migration.rb +++ b/db/migrate/20111017124400_group_categories_cleanup_migration.rb @@ -24,16 +24,16 @@ class GroupCategoriesCleanupMigration < ActiveRecord::Migration def self.update_records_for_record(record) return unless record.context.present? and record.group_category_name.present? category_column = (record.class == Group ? 'category' : 'group_category') - records = record.class.scoped(:conditions => ["context_id=? AND context_type=? AND #{category_column}=? AND group_category_id IS NULL", + records = record.class.where("context_id=? AND context_type=? AND #{category_column}=? AND group_category_id IS NULL", record.context_id, record.context_type, - record.group_category_name]) - records.update_all({:group_category_id => group_category_id_for(record)}) + record.group_category_name) + records.update_all(:group_category_id => group_category_id_for(record)) end def self.up - Assignment.find(:all, :select => "DISTINCT context_id, context_type, group_category", - :conditions => ['context_id IS NOT NULL AND group_category IS NOT NULL AND group_category_id IS NULL']).each do |record| + Assignment.select([:context_id, :context_type, :group_category]).uniq. + where('context_id IS NOT NULL AND group_category IS NOT NULL AND group_category_id IS NULL').each do |record| update_records_for_record(record) end end diff --git a/db/migrate/20111019152833_update_submitted_at_for_discussion_topics.rb b/db/migrate/20111019152833_update_submitted_at_for_discussion_topics.rb index 364da3ed040..6ab695c99eb 100644 --- a/db/migrate/20111019152833_update_submitted_at_for_discussion_topics.rb +++ b/db/migrate/20111019152833_update_submitted_at_for_discussion_topics.rb @@ -1,6 +1,6 @@ class UpdateSubmittedAtForDiscussionTopics < ActiveRecord::Migration def self.up - Submission.update_all("submitted_at = created_at", ["submission_type = ?", "discussion_topic"]) + Submission.where(:submission_type => "discussion_topic").update_all("submitted_at = created_at") end def self.down diff --git a/db/migrate/20111026201231_restore_users_sortable_name.rb b/db/migrate/20111026201231_restore_users_sortable_name.rb index 1a472299797..5f867ebeb9e 100644 --- a/db/migrate/20111026201231_restore_users_sortable_name.rb +++ b/db/migrate/20111026201231_restore_users_sortable_name.rb @@ -7,7 +7,7 @@ class RestoreUsersSortableName < ActiveRecord::Migration batch.each do |user| user.sortable_name = nil user.sortable_name - User.update_all({ :sortable_name => user.sortable_name }, :id => user.id) if user.changed? + User.where(:id => user).update_all(:sortable_name => user.sortable_name) if user.changed? end end end diff --git a/db/migrate/20111108150000_ensure_submissions_for_discussions.rb b/db/migrate/20111108150000_ensure_submissions_for_discussions.rb index 410180b31ad..50ad2b848bb 100644 --- a/db/migrate/20111108150000_ensure_submissions_for_discussions.rb +++ b/db/migrate/20111108150000_ensure_submissions_for_discussions.rb @@ -87,8 +87,8 @@ class EnsureSubmissionsForDiscussions < ActiveRecord::Migration end # touch all the courses and users - Course.update_all({:updated_at => Time.now.utc}, :id => touched_course_ids.to_a) unless touched_course_ids.empty? - User.update_all({:updated_at => Time.now.utc}, :id => touched_user_ids.to_a) unless touched_user_ids.empty? + Course.where(:id => touched_course_ids.to_a).update_all(:updated_at => Time.now.utc) unless touched_course_ids.empty? + User.where(:id => touched_user_ids.to_a).update_all(:updated_at => Time.now.utc) unless touched_user_ids.empty? end def self.down diff --git a/db/migrate/20111121175219_disable_open_registration_for_delegated_auth.rb b/db/migrate/20111121175219_disable_open_registration_for_delegated_auth.rb index 23191975d4f..90abd5d721c 100644 --- a/db/migrate/20111121175219_disable_open_registration_for_delegated_auth.rb +++ b/db/migrate/20111121175219_disable_open_registration_for_delegated_auth.rb @@ -1,6 +1,11 @@ class DisableOpenRegistrationForDelegatedAuth < ActiveRecord::Migration def self.up - Account.root_accounts.find(:all, :joins => :account_authorization_configs, :conditions => { 'account_authorization_configs.auth_type' => ['cas', 'saml']}, :readonly => false).each do |account| + if Rails.version < '3' + scope = Account.root_accounts.scoped(:joins => :account_authorization_configs, :readonly => false) + else + scope = Account.root_accounts.joins(:account_authorization_configs).readonly(false) + end + scope.where('account_authorization_configs.auth_type' => ['cas', 'saml']).each do |account| account.settings = { :open_registration => false } account.save! end diff --git a/db/migrate/20111123022449_fix_enrollment_root_account_id.rb b/db/migrate/20111123022449_fix_enrollment_root_account_id.rb index f34bb28a8f2..3f0564ca2e9 100644 --- a/db/migrate/20111123022449_fix_enrollment_root_account_id.rb +++ b/db/migrate/20111123022449_fix_enrollment_root_account_id.rb @@ -6,9 +6,9 @@ class FixEnrollmentRootAccountId < ActiveRecord::Migration when 'MySQL', 'Mysql2' execute "UPDATE enrollments e, courses c SET e.root_account_id = c.root_account_id WHERE e.course_id = c.id AND e.root_account_id != c.root_account_id" else - courses = Course.all.each do |c| - bad_enrollments = c.enrollments.select { |e| e.root_account_id != c.root_account_id }.map(&:id) - Enrollment.update_all({:root_account_id => c.root_account_id}, :id => bad_enrollments) + Course.find_each do |c| + bad_enrollments = c.enrollments.where("enrollments.root_account_id<>courses.root_account_id").pluck(:id) + Enrollment.where(:id => bad_enrollments).update_all(:root_account_id => c.root_account_id) end end end diff --git a/db/migrate/20111209171640_recalculate_muted_assignments.rb b/db/migrate/20111209171640_recalculate_muted_assignments.rb index fbac8e9705a..189babfdb01 100644 --- a/db/migrate/20111209171640_recalculate_muted_assignments.rb +++ b/db/migrate/20111209171640_recalculate_muted_assignments.rb @@ -1,9 +1,7 @@ class RecalculateMutedAssignments < ActiveRecord::Migration def self.up - course_ids = Assignment.find(:all, - :conditions => ['muted = ? AND context_type = ?', true, 'Course'], - :select => 'distinct context_id').map(&:context_id) + course_ids = Assignment.where(:muted => true, :context_type => 'Course').select(:context_id).uniq.map(&:context_id) course_ids.each do |id| c = Course.find id c.recompute_student_scores diff --git a/db/migrate/20120104170646_remove_attachments_with_no_scope_from_list.rb b/db/migrate/20120104170646_remove_attachments_with_no_scope_from_list.rb index a63108a54fd..47bf35b5843 100644 --- a/db/migrate/20120104170646_remove_attachments_with_no_scope_from_list.rb +++ b/db/migrate/20120104170646_remove_attachments_with_no_scope_from_list.rb @@ -6,7 +6,7 @@ class RemoveAttachmentsWithNoScopeFromList < ActiveRecord::Migration i = 0 # we do one extra loop to avoid race conditions while i < Attachment.maximum(:id) + 10000 - Attachment.update_all({:position => nil}, ["folder_id IS NULL AND id>? AND id <=?", i, i + 10000]) + Attachment.where("folder_id IS NULL AND id>? AND id <=?", i, i + 10000).update_all(:position => nil) sleep 1 i = i + 10000 end diff --git a/db/migrate/20120104183918_fix_sis_communication_channels.rb b/db/migrate/20120104183918_fix_sis_communication_channels.rb index 0fcad15595e..29c0e384bea 100644 --- a/db/migrate/20120104183918_fix_sis_communication_channels.rb +++ b/db/migrate/20120104183918_fix_sis_communication_channels.rb @@ -3,8 +3,8 @@ class FixSisCommunicationChannels < ActiveRecord::Migration def self.up begin - pseudonym_ids = Pseudonym.find(:all, :select => 'pseudonyms.id', :joins => :sis_communication_channel, :conditions => "pseudonyms.user_id<>communication_channels.user_id", :limit => 1000).map(&:id) - Pseudonym.update_all({:sis_communication_channel_id => nil}, :id => pseudonym_ids) + pseudonym_ids = Pseudonym.joins(:sis_communication_channel).where("pseudonyms.user_id<>communication_channels.user_id").limit(1000).pluck(:id) + Pseudonym.where(:id => pseudonym_ids).update_all(:sis_communication_channel_id => nil) sleep 1 end until pseudonym_ids.empty? end diff --git a/db/migrate/20120115222635_destroy_existing_anonymous_folder_downloads.rb b/db/migrate/20120115222635_destroy_existing_anonymous_folder_downloads.rb index 5cd2eb11b7a..3c7137bde5f 100644 --- a/db/migrate/20120115222635_destroy_existing_anonymous_folder_downloads.rb +++ b/db/migrate/20120115222635_destroy_existing_anonymous_folder_downloads.rb @@ -2,8 +2,8 @@ class DestroyExistingAnonymousFolderDownloads < ActiveRecord::Migration def self.up # There was a bug allowing locked/hidden files to be downloaded in zip files for public # courses. This migration will delete those, so that no data leaks remain from the bug. - Attachment.update_all({ :workflow_state => 'deleted', :deleted_at => Time.zone.now }, - { :context_type => 'Folder', :workflow_state => 'zipped', :user_id => nil }) + Attachment.where(:context_type => 'Folder', :workflow_state => 'zipped', :user_id => nil). + update_all(:workflow_state => 'deleted', :deleted_at => Time.zone.now) end def self.down diff --git a/db/migrate/20120126200026_labels_to_stars.rb b/db/migrate/20120126200026_labels_to_stars.rb index ac8e2c679fd..29dd8b8e11d 100644 --- a/db/migrate/20120126200026_labels_to_stars.rb +++ b/db/migrate/20120126200026_labels_to_stars.rb @@ -1,6 +1,6 @@ class LabelsToStars < ActiveRecord::Migration def self.up - ConversationParticipant.update_all("label = 'starred'", "label IS NOT NULL") + ConversationParticipant.where("label IS NOT NULL").update_all(:label => 'starred') end def self.down diff --git a/db/migrate/20120201044246_purge_duplicate_deleted_sis_enrollments.rb b/db/migrate/20120201044246_purge_duplicate_deleted_sis_enrollments.rb index d400177f4c5..3ad5e5eaec9 100644 --- a/db/migrate/20120201044246_purge_duplicate_deleted_sis_enrollments.rb +++ b/db/migrate/20120201044246_purge_duplicate_deleted_sis_enrollments.rb @@ -11,9 +11,9 @@ class PurgeDuplicateDeletedSisEnrollments < ActiveRecord::Migration HAVING count(*) > 1 LIMIT 1000") break if pairs.empty? pairs.each do |(user_id, course_section_id, type)| - scope = Enrollment.scoped(:conditions => ["user_id=? AND course_section_id=? AND type=? AND sis_source_id IS NOT NULL AND workflow_state='deleted'", user_id.to_i, course_section_id.to_i, type]) - keeper = scope.first(:select => :id) - scope.delete_all(["id<>?", keeper.id]) + scope = Enrollment.where("user_id=? AND course_section_id=? AND type=? AND sis_source_id IS NOT NULL AND workflow_state='deleted'", user_id.to_i, course_section_id.to_i, type) + keeper = scope.limit(1).pluck(:id).first + scope.where("id<>?", keeper).delete_all end end end diff --git a/db/migrate/20120208180341_remove_duplicate_submission_messages.rb b/db/migrate/20120208180341_remove_duplicate_submission_messages.rb index f47ad457445..f63e9bf610b 100644 --- a/db/migrate/20120208180341_remove_duplicate_submission_messages.rb +++ b/db/migrate/20120208180341_remove_duplicate_submission_messages.rb @@ -3,7 +3,7 @@ class RemoveDuplicateSubmissionMessages < ActiveRecord::Migration def self.up # destroy rather than delete so that callbacks happen - ConversationMessage.destroy_all(<<-CONDITIONS) + ConversationMessage.where(<<-CONDITIONS).destroy_all asset_id IS NOT NULL AND id NOT IN ( SELECT MIN(id) diff --git a/db/migrate/20120209223909_fix_zero_point_pass_fail_scores.rb b/db/migrate/20120209223909_fix_zero_point_pass_fail_scores.rb index e4e2ecdd668..a7245d7367c 100644 --- a/db/migrate/20120209223909_fix_zero_point_pass_fail_scores.rb +++ b/db/migrate/20120209223909_fix_zero_point_pass_fail_scores.rb @@ -3,10 +3,10 @@ class FixZeroPointPassFailScores < ActiveRecord::Migration # a bug allowed a few submissions to have their grade set to pass/fail, # rather than complete/incomplete. pass/fail is allowed in the api, but was # supposed to be translated to complete/incomplete in the db. - Submission.update_all({ :grade => 'complete' }, { :grade => 'pass' }) - Submission.update_all({ :grade => 'incomplete' }, { :grade => 'fail' }) - Submission.update_all({ :published_grade => 'complete' }, { :published_grade => 'pass' }) - Submission.update_all({ :published_grade => 'incomplete' }, { :published_grade => 'fail' }) + Submission.where(:grade => 'pass').update_all(:grade => 'complete') + Submission.where(:grade => 'fail').update_all(:grade => 'incomplete') + Submission.where(:published_grade => 'pass').update_all(:published_grade => 'complete') + Submission.where(:published_grade => 'fail').update_all(:published_grade => 'incomplete') end def self.down diff --git a/db/migrate/20120210173646_remove_irrelevant_submission_messages.rb b/db/migrate/20120210173646_remove_irrelevant_submission_messages.rb index 72bdc98d10c..399660bdfd2 100644 --- a/db/migrate/20120210173646_remove_irrelevant_submission_messages.rb +++ b/db/migrate/20120210173646_remove_irrelevant_submission_messages.rb @@ -5,7 +5,7 @@ class RemoveIrrelevantSubmissionMessages < ActiveRecord::Migration # destroy any submission messages where none of the commenters are # participants in the conversation. in production, this will remove about # 7k rows - ConversationMessage.destroy_all(<<-CONDITIONS) + ConversationMessage.where(<<-CONDITIONS).destroy_all asset_id IS NOT NULL AND id NOT IN ( SELECT DISTINCT cm.id diff --git a/db/migrate/20120215193327_accept_pending_group_memberships.rb b/db/migrate/20120215193327_accept_pending_group_memberships.rb index 0dd607e6167..a55960b7109 100644 --- a/db/migrate/20120215193327_accept_pending_group_memberships.rb +++ b/db/migrate/20120215193327_accept_pending_group_memberships.rb @@ -1,7 +1,7 @@ class AcceptPendingGroupMemberships < ActiveRecord::Migration def self.up - GroupMembership.update_all({ :workflow_state => 'accepted' }, { :workflow_state => 'invited' }) - GroupMembership.update_all({ :workflow_state => 'accepted' }, { :workflow_state => 'requested' }) + GroupMembership.where(:workflow_state => 'invited').update_all(:workflow_state => 'accepted') + GroupMembership.where(:workflow_state => 'requested').update_all(:workflow_state => 'accepted') end def self.down diff --git a/db/migrate/20120216214454_set_blank_sis_user_ids_to_null.rb b/db/migrate/20120216214454_set_blank_sis_user_ids_to_null.rb index d649597dfaa..b7771ee3515 100644 --- a/db/migrate/20120216214454_set_blank_sis_user_ids_to_null.rb +++ b/db/migrate/20120216214454_set_blank_sis_user_ids_to_null.rb @@ -1,6 +1,6 @@ class SetBlankSisUserIdsToNull < ActiveRecord::Migration def self.up - Pseudonym.update_all({ :sis_user_id => nil }, :sis_user_id => '') + Pseudonym.where(:sis_user_id => '').update_all(:sis_user_id => nil) end def self.down diff --git a/db/migrate/20120217214153_remove_deleted_user_account_associations.rb b/db/migrate/20120217214153_remove_deleted_user_account_associations.rb index 73fdf270303..5ec6ed0fd53 100644 --- a/db/migrate/20120217214153_remove_deleted_user_account_associations.rb +++ b/db/migrate/20120217214153_remove_deleted_user_account_associations.rb @@ -1,6 +1,6 @@ class RemoveDeletedUserAccountAssociations < ActiveRecord::Migration def self.up - UserAccountAssociation.delete_all("user_id IN (SELECT id FROM users WHERE workflow_state IN ('deleted', 'creation_pending'))") + UserAccountAssociation.where("user_id IN (SELECT id FROM users WHERE workflow_state IN ('deleted', 'creation_pending'))").delete_all end def self.down diff --git a/db/migrate/20120224194847_remove_unused_notification_policies.rb b/db/migrate/20120224194847_remove_unused_notification_policies.rb index afaffa1ffe9..6843bc496ff 100644 --- a/db/migrate/20120224194847_remove_unused_notification_policies.rb +++ b/db/migrate/20120224194847_remove_unused_notification_policies.rb @@ -2,8 +2,8 @@ class RemoveUnusedNotificationPolicies < ActiveRecord::Migration tag :postdeploy def self.up - ids = Notification.find(:all, :select => 'id', :conditions => { :category => ['Student Message', 'Files']}).map(&:id) - NotificationPolicy.delete_all(:notification_id => ids) + ids = Notification.where(:category => ['Student Message', 'Files']).pluck(:id) + NotificationPolicy.where(:notification_id => ids).delete_all end def self.down diff --git a/db/migrate/20120224194848_remove_unused_notifications.rb b/db/migrate/20120224194848_remove_unused_notifications.rb index 3a9f0489aa3..d1da2937472 100644 --- a/db/migrate/20120224194848_remove_unused_notifications.rb +++ b/db/migrate/20120224194848_remove_unused_notifications.rb @@ -3,8 +3,8 @@ class RemoveUnusedNotifications < ActiveRecord::Migration def self.up return unless Shard.current.default? - Notification.update_all({:category => 'Other'}, :category => 'Message') - Notification.delete_all(:category => ['Files', 'Student Message']) + Notification.where(:category => 'Message').update_all(:category => 'Other') + Notification.where(:category => ['Files', 'Student Message']).delete_all end def self.down diff --git a/db/migrate/20120301231546_set_discussion_entry_root_ids.rb b/db/migrate/20120301231546_set_discussion_entry_root_ids.rb index f40adca1358..31362c5979e 100644 --- a/db/migrate/20120301231546_set_discussion_entry_root_ids.rb +++ b/db/migrate/20120301231546_set_discussion_entry_root_ids.rb @@ -4,11 +4,11 @@ class SetDiscussionEntryRootIds < ActiveRecord::Migration def self.up # fix up parent_id, which was getting set to 0 for root-level entries # also set root_entry_id to parent_id for all existing entries - DiscussionEntry.update_all("parent_id = CASE parent_id WHEN 0 THEN null ELSE parent_id END, root_entry_id = CASE parent_id WHEN 0 THEN null ELSE parent_id END") + DiscussionEntry.update_all("parent_id = CASE parent_id WHEN 0 THEN NULL ELSE parent_id END, root_entry_id = CASE parent_id WHEN 0 THEN NULL ELSE parent_id END") end def self.down - DiscussionEntry.update_all("parent_id = 0", "parent_id IS NULL") + DiscussionEntry.where(:parent_id => nil).update_all(:parent_id => 0) # previous migration drops the root_entry_id column here end end diff --git a/db/migrate/20120322184742_break_down_detailed_report_snapshots.rb b/db/migrate/20120322184742_break_down_detailed_report_snapshots.rb index 2e6811dce09..a1496393127 100644 --- a/db/migrate/20120322184742_break_down_detailed_report_snapshots.rb +++ b/db/migrate/20120322184742_break_down_detailed_report_snapshots.rb @@ -3,7 +3,7 @@ class BreakDownDetailedReportSnapshots < ActiveRecord::Migration self.transactional = false def self.do_report_type(scope) - detailed = scope.find(:last, :conditions => { :account_id => nil }) + detailed = scope.where(:account_id => nil).last return unless detailed detailed.data['detailed'].each do |(account_id, data)| new_detailed = detailed.clone diff --git a/db/migrate/20120404230916_fix_user_merge_conversations2.rb b/db/migrate/20120404230916_fix_user_merge_conversations2.rb index 312652a7184..8a6515db56f 100644 --- a/db/migrate/20120404230916_fix_user_merge_conversations2.rb +++ b/db/migrate/20120404230916_fix_user_merge_conversations2.rb @@ -9,7 +9,7 @@ class FixUserMergeConversations2 < ActiveRecord::Migration # the previous merging was done incorrectly due to a scoping issue # there are only about 100 that need to be fixed, so we just load them all - convos = ConversationParticipant.find(:all, :conditions => "NOT EXISTS (SELECT 1 FROM conversations WHERE id = conversation_id)") + convos = ConversationParticipant.where("NOT EXISTS (SELECT 1 FROM conversations WHERE id = conversation_id)") convos.group_by(&:conversation_id).each do |conversation_id, cps| private_hash = Conversation.private_hash_for(cps.map(&:user_id)) if target = Conversation.find_by_private_hash(private_hash) @@ -17,7 +17,7 @@ class FixUserMergeConversations2 < ActiveRecord::Migration cps.each do |cp| if new_cp = new_participants[cp.user_id] new_cp.update_attribute(:workflow_state, cp.workflow_state) if cp.unread? || new_cp.archived? - cp.conversation_message_participants.update_all(["conversation_participant_id = ?", new_cp.id]) + cp.conversation_message_participants.update_all(:conversation_participant_id => new_cp) cp.destroy else $stderr.puts "couldn't find a target ConversationParticipant for id #{cp.id}" diff --git a/db/migrate/20120427162634_move_migration_notifications_to_separate_category.rb b/db/migrate/20120427162634_move_migration_notifications_to_separate_category.rb index 1b2e5f561da..1b06594cd32 100644 --- a/db/migrate/20120427162634_move_migration_notifications_to_separate_category.rb +++ b/db/migrate/20120427162634_move_migration_notifications_to_separate_category.rb @@ -2,12 +2,12 @@ class MoveMigrationNotificationsToSeparateCategory < ActiveRecord::Migration tag :postdeploy def self.up - Notification.update_all({ :category => 'Migration' }, - { :name => ['Migration Export Ready', 'Migration Import Finished', 'Migration Import Failed'] }) + Notification.where(:name => ['Migration Export Ready', 'Migration Import Finished', 'Migration Import Failed']). + update_all(:category => 'Migration') end def self.down - Notification.update_all({ :category => 'Other' }, - { :name => ['Migration Export Ready', 'Migration Import Finished', 'Migration Import Failed'] }) + Notification.where(:name => ['Migration Export Ready', 'Migration Import Finished', 'Migration Import Failed']). + update_all(:category => 'Other') end end diff --git a/db/migrate/20120603222842_move_content_export_notifications_to_migration_category.rb b/db/migrate/20120603222842_move_content_export_notifications_to_migration_category.rb index 368d1f12a21..c54a04e42d0 100644 --- a/db/migrate/20120603222842_move_content_export_notifications_to_migration_category.rb +++ b/db/migrate/20120603222842_move_content_export_notifications_to_migration_category.rb @@ -6,7 +6,7 @@ class MoveContentExportNotificationsToMigrationCategory < ActiveRecord::Migratio end def self.down - Notification.update_all({ :category => 'Other' }, - { :name => ['Content Export Finished', 'Content Export Failed'] }) + Notification.where(:name => ['Content Export Finished', 'Content Export Failed']). + update_all(:category => 'Other') end end diff --git a/db/migrate/20120620190441_fix_default_limit_privileges_to_course_section.rb b/db/migrate/20120620190441_fix_default_limit_privileges_to_course_section.rb index 137cb5e6b95..e465ff1404c 100644 --- a/db/migrate/20120620190441_fix_default_limit_privileges_to_course_section.rb +++ b/db/migrate/20120620190441_fix_default_limit_privileges_to_course_section.rb @@ -4,8 +4,8 @@ class FixDefaultLimitPrivilegesToCourseSection < ActiveRecord::Migration def self.up Enrollment.find_ids_in_ranges do |(start_id, end_id)| - Enrollment.update_all({ :limit_privileges_to_course_section => false }, ["type IN ('StudentEnrollment', 'ObserverEnrollment', 'StudentViewEnrollment', 'DesignerEnrollment') AND id>=? AND id <=?", start_id, end_id]) - Enrollment.update_all({ :limit_privileges_to_course_section => false }, ["type IN ('TeacherEnrollment', 'TaEnrollment') AND limit_privileges_to_course_section IS NULL AND id>=? AND id <=?", start_id, end_id]) + Enrollment.where("type IN ('StudentEnrollment', 'ObserverEnrollment', 'StudentViewEnrollment', 'DesignerEnrollment') AND id>=? AND id <=?", start_id, end_id).update_all(:limit_privileges_to_course_section => false) + Enrollment.where("type IN ('TeacherEnrollment', 'TaEnrollment') AND limit_privileges_to_course_section IS NULL AND id>=? AND id <=?", start_id, end_id).update_all(:limit_privileges_to_course_section => false) end end diff --git a/db/migrate/20120626174816_constrain_assignment_group_category_ids.rb b/db/migrate/20120626174816_constrain_assignment_group_category_ids.rb index f6d6ef3a3b6..9250e8c8481 100644 --- a/db/migrate/20120626174816_constrain_assignment_group_category_ids.rb +++ b/db/migrate/20120626174816_constrain_assignment_group_category_ids.rb @@ -2,7 +2,7 @@ class ConstrainAssignmentGroupCategoryIds < ActiveRecord::Migration tag :postdeploy def self.up - Assignment.update_all({:group_category_id => nil}, :group_category_id => 0) + Assignment.where(:group_category_id => 0).update_all(:group_category_id => nil) add_foreign_key :assignments, :group_categories end diff --git a/db/migrate/20120705144244_add_unique_index_on_favorites.rb b/db/migrate/20120705144244_add_unique_index_on_favorites.rb index ea8f62b35ea..9ba6a114723 100644 --- a/db/migrate/20120705144244_add_unique_index_on_favorites.rb +++ b/db/migrate/20120705144244_add_unique_index_on_favorites.rb @@ -4,7 +4,7 @@ class AddUniqueIndexOnFavorites < ActiveRecord::Migration def self.up # cleanup must happen synchronously in order to create the unique index # the extra subquery is necessary to avoid error 1093 on mysql - Favorite.delete_all("id NOT IN (SELECT * FROM (SELECT MIN(id) FROM favorites GROUP BY user_id, context_id, context_type) x)") + Favorite.where("id NOT IN (SELECT * FROM (SELECT MIN(id) FROM favorites GROUP BY user_id, context_id, context_type) x)").delete_all add_index :favorites, [:user_id, :context_id, :context_type], :unique => true, :name => "index_favorites_unique_user_object" end diff --git a/db/migrate/20120723201410_uniquify_wikis.rb b/db/migrate/20120723201410_uniquify_wikis.rb index 8e6a4ea5752..60dfa4290bf 100644 --- a/db/migrate/20120723201410_uniquify_wikis.rb +++ b/db/migrate/20120723201410_uniquify_wikis.rb @@ -9,8 +9,8 @@ class UniquifyWikis < ActiveRecord::Migration wikis = connection.select_rows("SELECT wiki_id FROM wiki_namespaces WHERE namespace='default' AND context_type='#{context_type}' AND context_id=#{context_id}").map(&:first).map(&:to_i) to_keep = context.wiki_id wikis.delete(to_keep) - WikiPage.update_all({:wiki_id => to_keep}, {:wiki_id => wikis}) - Wiki.delete_all(:id => wikis) + WikiPage.where(:wiki_id => wikis).update_all(:wiki_id => to_keep) + Wiki.where(:id => wikis).delete_all connection.execute("DELETE FROM wiki_namespaces WHERE wiki_id IN (#{wikis.join(',')})") end end diff --git a/db/migrate/20120727145851_add_unique_index_on_notifications.rb b/db/migrate/20120727145851_add_unique_index_on_notifications.rb index ff69c04b543..8865913982d 100644 --- a/db/migrate/20120727145851_add_unique_index_on_notifications.rb +++ b/db/migrate/20120727145851_add_unique_index_on_notifications.rb @@ -4,7 +4,7 @@ class AddUniqueIndexOnNotifications < ActiveRecord::Migration def self.up return unless Shard.current.default? # the excess subquery is necessary to avoid error 1093 on mysql - Notification.delete_all("id NOT IN (SELECT * FROM (SELECT MIN(id) FROM notifications GROUP BY name) x)") + Notification.where("id NOT IN (SELECT * FROM (SELECT MIN(id) FROM notifications GROUP BY name) x)").delete_all add_index :notifications, [:name], :unique => true, :name => "index_notifications_unique_on_name" end diff --git a/db/migrate/20120817191623_fix_profile_pictures.rb b/db/migrate/20120817191623_fix_profile_pictures.rb index 7d9ebae6848..fc78ccb61d4 100644 --- a/db/migrate/20120817191623_fix_profile_pictures.rb +++ b/db/migrate/20120817191623_fix_profile_pictures.rb @@ -8,15 +8,12 @@ class FixProfilePictures < ActiveRecord::Migration def self.up # we don't support twitter or linked in profile pictures anymore, because # they are too small - ids = User.where(:avatar_image_source => %w(twitter linkedin)).map(&:id) - now = Time.now - User.update_all( - { - :avatar_image_source => 'no_pic', - :avatar_image_url => nil, - :avatar_image_updated_at => now - }, - :id => ids) + User.where(:avatar_image_source => ['twitter', 'linkedin']). + update_all( + :avatar_image_source => 'no_pic', + :avatar_image_url => nil, + :avatar_image_updated_at => Time.now.utc + ) DataFixup::RegenerateUserThumbnails.send_later_if_production(:run) end diff --git a/db/migrate/20121017165823_hash_access_tokens.rb b/db/migrate/20121017165823_hash_access_tokens.rb index af9afe2e080..331bfa1c467 100644 --- a/db/migrate/20121017165823_hash_access_tokens.rb +++ b/db/migrate/20121017165823_hash_access_tokens.rb @@ -15,7 +15,7 @@ class HashAccessTokens < ActiveRecord::Migration :token_hint => at['token'][0,5], :crypted_token => AccessToken.hashed_token(at['token']), } - AccessToken.update_all(updates, { :id => at['id'] }) + AccessToken.where(:id => at['id']).update_all(updates) end end end diff --git a/db/migrate/20121203164800_add_unique_index_on_role_name.rb b/db/migrate/20121203164800_add_unique_index_on_role_name.rb index cb70793a407..6dfcb3228ac 100644 --- a/db/migrate/20121203164800_add_unique_index_on_role_name.rb +++ b/db/migrate/20121203164800_add_unique_index_on_role_name.rb @@ -8,7 +8,7 @@ class AddUniqueIndexOnRoleName < ActiveRecord::Migration # same name and different base role types; that can't be cleaned up automatically # (but should not happen because an existing validation prevents this case) # note 2: the extra subquery is necessary to avoid error 1093 on mysql - Role.delete_all("id NOT IN (SELECT * FROM (SELECT MAX(id) FROM roles GROUP BY account_id, name, base_role_type) x)") + Role.where("id NOT IN (SELECT * FROM (SELECT MAX(id) FROM roles GROUP BY account_id, name, base_role_type) x)").delete_all add_index :roles, [:account_id, :name], :unique => true, :name => "index_roles_unique_account_name" end diff --git a/db/migrate/20130124203149_clean_up_user_account_associations.rb b/db/migrate/20130124203149_clean_up_user_account_associations.rb index 76bdf05b2e9..72168395a98 100644 --- a/db/migrate/20130124203149_clean_up_user_account_associations.rb +++ b/db/migrate/20130124203149_clean_up_user_account_associations.rb @@ -4,13 +4,17 @@ class CleanUpUserAccountAssociations < ActiveRecord::Migration def self.up # clean up garbage data - UserAccountAssociation.delete_all(:user_id => nil) + UserAccountAssociation.where(:user_id => nil).delete_all # we don't have any of these in production, but just in case... - UserAccountAssociation.delete_all(:account_id => nil) + UserAccountAssociation.where(:account_id => nil).delete_all # clean up dups by recalculating - user_ids = UserAccountAssociation.find(:all, :select => 'DISTINCT user_id', - :group => 'user_id, account_id', :having => 'COUNT(*) > 1').map(&:user_id) + user_ids = UserAccountAssociation. + select(:user_id). + uniq. + group(:user_id, :account_id). + having("COUNT(*)>1"). + map(&:user_id) User.update_account_associations(user_ids) # add a unique index diff --git a/db/migrate/20130128221236_add_unique_index_on_course_account_associations.rb b/db/migrate/20130128221236_add_unique_index_on_course_account_associations.rb index b5580eac495..f620fb0b6fa 100644 --- a/db/migrate/20130128221236_add_unique_index_on_course_account_associations.rb +++ b/db/migrate/20130128221236_add_unique_index_on_course_account_associations.rb @@ -4,8 +4,12 @@ class AddUniqueIndexOnCourseAccountAssociations < ActiveRecord::Migration def self.up # clean up any dups first - course_ids = CourseAccountAssociation.find(:all, :select => 'DISTINCT course_id', - :group => 'course_id, course_section_id, account_id', :having => 'COUNT(*) > 1').map(&:course_id) + course_ids = CourseAccountAssociation. + select(:course_id). + uniq. + group(:course_id, :course_section_id, :account_id). + having("COUNT(*)>1"). + map(&:course_id) Course.update_account_associations(course_ids) add_index :course_account_associations, [:course_id, :course_section_id, :account_id], :unique => true, :concurrently => true, :name => 'index_caa_on_course_id_and_section_id_and_account_id'