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
def self.up
Enumeration.update_all("type = 'IssuePriority'", "opt = 'IPRI'")
Enumeration.update_all("type = 'DocumentCategory'", "opt = 'DCAT'")
Enumeration.update_all("type = 'TimeEntryActivity'", "opt = 'ACTI'")
Enumeration.where("opt = 'IPRI'").update_all("type = 'IssuePriority'")
Enumeration.where("opt = 'DCAT'").update_all("type = 'DocumentCategory'")
Enumeration.where("opt = 'ACTI'").update_all("type = 'TimeEntryActivity'")
end
def self.down

View File

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

View File

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

View File

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

View File

@ -2,16 +2,20 @@ class ChangeUsersMailNotificationToString < ActiveRecord::Migration
def self.up
rename_column :users, :mail_notification, :mail_notification_bool
add_column :users, :mail_notification, :string, :default => '', :null => false
User.update_all("mail_notification = 'all'", "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)")
User.update_all("mail_notification = 'only_my_events'", "mail_notification NOT IN ('all', 'selected')")
User.where("mail_notification_bool = #{connection.quoted_true}").
update_all("mail_notification = 'all'")
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
end
def self.down
rename_column :users, :mail_notification, :mail_notification_char
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
end
end

View File

@ -3,7 +3,8 @@ class CopyRepositoriesLogEncoding < ActiveRecord::Migration
encoding = Setting.commit_logs_encoding.to_s.strip
encoding = encoding.blank? ? 'UTF-8' : encoding
# 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
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_type, :string, :limit => 30, :default => nil, :null => true
Attachment.update_all "container_id = NULL", "container_id = 0"
Attachment.update_all "container_type = NULL", "container_type = ''"
Attachment.where("container_id = 0").update_all("container_id = NULL")
Attachment.where("container_type = ''").update_all("container_type = NULL")
add_index :attachments, [:container_id, :container_type]
end