简体   繁体   English

如何将paper_trail的whodunnit列从字符串迁移到整数?

[英]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: 所以我想采取的方法是这样的:

  • add a new integer column to the versions table, maybe named whodunnit_new ; versions表中添加一个新的整数列,可能名为whodunnit_new
  • remove 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 ; 这意味着,新的版本,paper_trail是保存idwhodunnit_new并且不保存emailwhodunnit ;
  • backfill whodunnit_new for old records by finding the user by email and saving thier id in the new column; 通过电子邮件查找用户并将其ID保存在新列中,回填whodunnit_new以获取旧记录;
  • drop the whodunnit column; 放下whodunnit列;
  • rename 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM