如何将 paper_trail 的 whodunnit 列从字符串迁移到整数?
How to migrate paper_trail's whodunnit column from string to integer?
在使用 paper_trail 的旧版 Rails 应用程序中,whodunnit
列设置为字符串,而不是默认的 current_user.id
,此列包含用户的电子邮件。这是通过覆盖 ApplicationController
:
中的 user_for_paper_trail
来实现的
def user_for_paper_trail
current_user.email
end
现在我想迁移到存储当前用户 ID 而不是电子邮件的默认行为。
所以我想采取的方法是这样的:
- 向
versions
添加一个新的整数列 table,可能命名为 whodunnit_new
;
- 删除应用程序控制器中的
user_for_paper_trail
方法,并告诉模型在 whodunnit_new
而不是 whodunnit
中写入 whodunnit 信息。这意味着,对于新版本,paper_trail 在 whodunnit_new
中保存 id
而不是在 whodunnit
中保存 email
;
- 通过电子邮件查找用户并将他们的 ID 保存在新列中来回填
whodunnit_new
旧记录;
- 删除
whodunnit
列;
- 将
whodunnit_new
重命名为 whodunnit
并删除模型中使用 whodunnit_new
的设置。
问题是 - 我找不到可以设置 whodunnit 列名称的设置,在我看来,它是硬编码的。我看到 whodunnit
方法有一个别名(称为 version_author
)。
我正在使用 Papertrail 10 和 Rails 5.2。
所以我的问题 - 执行所描述的迁移的正确方法应该是什么。
.. 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
。它是不可配置的。
否则你的迁移听起来不错。使用事务,您将不需要临时重命名该列。 (除非您使用 MySQL,它有 "weak" 个事务,即不受 DDL 保护)
在使用 paper_trail 的旧版 Rails 应用程序中,whodunnit
列设置为字符串,而不是默认的 current_user.id
,此列包含用户的电子邮件。这是通过覆盖 ApplicationController
:
user_for_paper_trail
来实现的
def user_for_paper_trail
current_user.email
end
现在我想迁移到存储当前用户 ID 而不是电子邮件的默认行为。
所以我想采取的方法是这样的:
- 向
versions
添加一个新的整数列 table,可能命名为whodunnit_new
; - 删除应用程序控制器中的
user_for_paper_trail
方法,并告诉模型在whodunnit_new
而不是whodunnit
中写入 whodunnit 信息。这意味着,对于新版本,paper_trail 在whodunnit_new
中保存id
而不是在whodunnit
中保存email
; - 通过电子邮件查找用户并将他们的 ID 保存在新列中来回填
whodunnit_new
旧记录; - 删除
whodunnit
列; - 将
whodunnit_new
重命名为whodunnit
并删除模型中使用whodunnit_new
的设置。
问题是 - 我找不到可以设置 whodunnit 列名称的设置,在我看来,它是硬编码的。我看到 whodunnit
方法有一个别名(称为 version_author
)。
我正在使用 Papertrail 10 和 Rails 5.2。
所以我的问题 - 执行所描述的迁移的正确方法应该是什么。
.. 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
。它是不可配置的。
否则你的迁移听起来不错。使用事务,您将不需要临时重命名该列。 (除非您使用 MySQL,它有 "weak" 个事务,即不受 DDL 保护)