簡體   English   中英

Rails遷移:可以使用動態代碼進行update_all嗎?

[英]Rails migration: update_all with dynamic code is possible?

我想在表格中添加一個新字段。

我的用戶模型中新的“ secret_code”字段應等於Digest :: SHA1.hexdigest([Time.now,rand] .join)[1..12]。

我想做的是生成一個遷移,該遷移會將字段添加到表中,並用(某種)唯一的“ secret_code”填充我的現有用戶。

class AddSecretCodeToUsers < ActiveRecord::Migration
  def self.up
    add_column :users, :secret_code, :string
    User.update_all ["secret_code =?", Digest::SHA1.hexdigest([Time.now, rand].join)[1..12]]
  end

  def self.down
    remove_column :users, :secret_code
  end
end

問題在於此遷移將使用相同的密碼填充所有現有用戶!

一種解決方案是不使用update_all並運行循環以獲取每個用戶並向每個用戶發送更新,但是在這種情況下,我的遷移將非常緩慢。

有沒有一種方法可以將“唯一”隨機值發送到update_all方法?

謝謝,奧古斯托

嘗試將其更改為Digest::SHA1.hexdigest([Time.now, rand].to_s)但就我個人而言,我將在上面創建一個rake任務,因為它實際上不是遷移。

你的耙子任務會做

User.all.each do |u|
  u.update_attribute(:secret_code, Digest::SHA1.hexdigest([Time.now, rand].to_s))
end

但是,對於您的遷移,我還要將t.string :secret_code, :default => Digest::SHA1.hexdigest([Time.now, rand].to_s)到屬性,以便將其添加到新創建的記錄中。

對於MySQL,您可以在self.up此權限:

connection.execute(%Q{
    update users
    set secret_code = substring(sha1(rand()) from 1 for 12)
})

PostgreSQL默認不支持SHA1,但是它確實具有MD5,這可能已經足夠了:

connection.execute(%Q{
    update users
    set secret_code = substring(md5(random()::varchar) from 1 for 12)
})

如果您安裝了pgcrypto軟件包,則可以同時使用SHA1。

兩者都將使數據庫完成所有工作,並避免了往返整個表的所有開銷。 如果您想將時間混合在以下內容中,也可以使用一些哈希值:

md5(random()::varchar || now()::varchar) -- PostgreSQL
sha(rand()            || now()         ) -- MySQL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM