Rails4: replace deprecated Relation#update_all at db migrations

git-svn-id: http://svn.redmine.org/redmine/trunk@12488 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA 2014-01-07 10:03:11 +00:00
parent a5f028a242
commit 0bc3ef5014
7 changed files with 21 additions and 14 deletions

View File

@ -1,8 +1,8 @@
class UpdateEnumerationsToSti < ActiveRecord::Migration class UpdateEnumerationsToSti < ActiveRecord::Migration
def self.up def self.up
Enumeration.update_all("type = 'IssuePriority'", "opt = 'IPRI'") Enumeration.where("opt = 'IPRI'").update_all("type = 'IssuePriority'")
Enumeration.update_all("type = 'DocumentCategory'", "opt = 'DCAT'") Enumeration.where("opt = 'DCAT'").update_all("type = 'DocumentCategory'")
Enumeration.update_all("type = 'TimeEntryActivity'", "opt = 'ACTI'") Enumeration.where("opt = 'ACTI'").update_all("type = 'TimeEntryActivity'")
end end
def self.down def self.down

View File

@ -1,6 +1,6 @@
class FixMessagesStickyNull < ActiveRecord::Migration class FixMessagesStickyNull < ActiveRecord::Migration
def self.up def self.up
Message.update_all('sticky = 0', 'sticky IS NULL') Message.where('sticky IS NULL').update_all('sticky = 0')
end end
def self.down def self.down

View File

@ -1,6 +1,6 @@
class PopulateUsersType < ActiveRecord::Migration class PopulateUsersType < ActiveRecord::Migration
def self.up def self.up
Principal.update_all("type = 'User'", "type IS NULL") Principal.where("type IS NULL").update_all("type = 'User'")
end end
def self.down def self.down

View File

@ -1,9 +1,11 @@
class FixUsersCustomValues < ActiveRecord::Migration class FixUsersCustomValues < ActiveRecord::Migration
def self.up def self.up
CustomValue.update_all("customized_type = 'Principal'", "customized_type = 'User'") CustomValue.where("customized_type = 'User'").
update_all("customized_type = 'Principal'")
end end
def self.down def self.down
CustomValue.update_all("customized_type = 'User'", "customized_type = 'Principal'") CustomValue.where("customized_type = 'Principal'").
update_all("customized_type = 'User'")
end end
end end

View File

@ -2,16 +2,20 @@ class ChangeUsersMailNotificationToString < ActiveRecord::Migration
def self.up def self.up
rename_column :users, :mail_notification, :mail_notification_bool rename_column :users, :mail_notification, :mail_notification_bool
add_column :users, :mail_notification, :string, :default => '', :null => false add_column :users, :mail_notification, :string, :default => '', :null => false
User.update_all("mail_notification = 'all'", "mail_notification_bool = #{connection.quoted_true}") User.where("mail_notification_bool = #{connection.quoted_true}").
User.update_all("mail_notification = 'selected'", "EXISTS (SELECT 1 FROM #{Member.table_name} WHERE #{Member.table_name}.mail_notification = #{connection.quoted_true} AND #{Member.table_name}.user_id = #{User.table_name}.id)") update_all("mail_notification = 'all'")
User.update_all("mail_notification = 'only_my_events'", "mail_notification NOT IN ('all', 'selected')") User.where("EXISTS (SELECT 1 FROM #{Member.table_name} WHERE #{Member.table_name}.mail_notification = #{connection.quoted_true} AND #{Member.table_name}.user_id = #{User.table_name}.id)").
update_all("mail_notification = 'selected'")
User.where("mail_notification NOT IN ('all', 'selected')").
update_all("mail_notification = 'only_my_events'")
remove_column :users, :mail_notification_bool remove_column :users, :mail_notification_bool
end end
def self.down def self.down
rename_column :users, :mail_notification, :mail_notification_char rename_column :users, :mail_notification, :mail_notification_char
add_column :users, :mail_notification, :boolean, :default => true, :null => false add_column :users, :mail_notification, :boolean, :default => true, :null => false
User.update_all("mail_notification = #{connection.quoted_false}", "mail_notification_char <> 'all'") User.where("mail_notification_char <> 'all'").
update_all("mail_notification = #{connection.quoted_false}")
remove_column :users, :mail_notification_char remove_column :users, :mail_notification_char
end end
end end

View File

@ -3,7 +3,8 @@ class CopyRepositoriesLogEncoding < ActiveRecord::Migration
encoding = Setting.commit_logs_encoding.to_s.strip encoding = Setting.commit_logs_encoding.to_s.strip
encoding = encoding.blank? ? 'UTF-8' : encoding encoding = encoding.blank? ? 'UTF-8' : encoding
# encoding is NULL by default # encoding is NULL by default
Repository.update_all(["log_encoding = ?", encoding], "type IN ('Bazaar', 'Cvs', 'Darcs')") Repository.where("type IN ('Bazaar', 'Cvs', 'Darcs')").
update_all(["log_encoding = ?", encoding])
end end
def self.down def self.down

View File

@ -8,8 +8,8 @@ class ChangeAttachmentsContainerDefaults < ActiveRecord::Migration
change_column :attachments, :container_id, :integer, :default => nil, :null => true change_column :attachments, :container_id, :integer, :default => nil, :null => true
change_column :attachments, :container_type, :string, :limit => 30, :default => nil, :null => true change_column :attachments, :container_type, :string, :limit => 30, :default => nil, :null => true
Attachment.update_all "container_id = NULL", "container_id = 0" Attachment.where("container_id = 0").update_all("container_id = NULL")
Attachment.update_all "container_type = NULL", "container_type = ''" Attachment.where("container_type = ''").update_all("container_type = NULL")
add_index :attachments, [:container_id, :container_type] add_index :attachments, [:container_id, :container_type]
end end