[英]How to migrate paper_trail's whodunnit column from string to integer?
In a legacy Rails app using paper_trail, the whodunnit
column is set to string and instead of the default current_user.id
, this column holds the user's email. 在传统的Rails应用程序使用paper_trail,该
whodunnit
列设置为字符串,而不是默认current_user.id
,此列保存用户的电子邮件。 This is achieved by overriding user_for_paper_trail
in ApplicationController
: 这是通过在
ApplicationController
重写user_for_paper_trail
来实现的:
def user_for_paper_trail
current_user.email
end
Now I want to migrate to the default behavior of storing the id of the current user instead of the email. 现在,我想迁移到默认行为,即存储当前用户的ID而不是电子邮件。
So the approach I was thinking to take was something like this: 所以我想采取的方法是这样的:
versions
table, maybe named whodunnit_new
; versions
表中添加一个新的整数列,可能名为whodunnit_new
; user_for_paper_trail
method in application controller and tell the models to write whodunnit info in whodunnit_new
instead of whodunnit
. user_for_paper_trail
在应用控制器方法告诉模型写whodunnit信息在whodunnit_new
代替whodunnit
。 This means, that for new versions, paper_trail is saving id
in whodunnit_new
and is not saving email
in whodunnit
; id
在whodunnit_new
并且不保存email
中whodunnit
; whodunnit_new
for old records by finding the user by email and saving thier id in the new column; whodunnit_new
以获取旧记录; whodunnit
column; whodunnit
列; whodunnit_new
to whodunnit
and remove the setting in the models for using whodunnit_new
. whodunnit_new
重命名为whodunnit
并删除模型中使用whodunnit_new
的设置。 The problem is - I couldn't find a setting, which can set the name of the whodunnit column, it seems to me, that it is hard-coded. 问题是-我找不到设置可以设置whodunnit列的名称的设置,在我看来,它是硬编码的。 I saw that there is an alias for the
whodunnit
method (called version_author
). 我看到了
whodunnit
方法的别名(称为version_author
)。
I'm using Papertrail 10 and Rails 5.2. 我正在使用Papertrail 10和Rails 5.2。
So my question - what should be the proper way of doing the described migration. 所以我的问题是-进行所述迁移的正确方法应该是什么。
.. I couldn't find a setting, which can set the name of the whodunnit column, it seems to me, that it is hard-coded.
..我找不到设置,它可以设置whodunnit列的名称,在我看来,它是硬编码的。
Correct, the column must be named whodunnit
. 正确,该列必须命名为
whodunnit
。 It is not configurable. 它是不可配置的。
Otherwise your migration sounds good. 否则,您的迁移听起来不错。 Use a transaction and you won't need to temporarily rename the column.
使用事务,您将不需要临时重命名该列。 (Unless you're using MySQL, which has "weak" transactions, ie. not protected against DDL)
(除非您使用的MySQL具有“弱”事务,即不受DDL保护)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.