审计的Rails是批量创建的

时间:2017-10-26 08:44:23

标签: ruby-on-rails batch-processing acts-as-audited

我使用rails 5 /审计4.6.5 我对一次超过2000件物品进行批量操作。 为了使它可用,我需要使用updatre_all函数。

然后,我想一次创建所需的审计

我想做的是这样的事情:

Followup.where(id: ids).in_batches(of: 500) do |group|
  #quick update for user responsiveness
  group.update_all(step_id: 1)
  group.delay.newAudits(step_id: 1)
end

但经审核的宝石看起来是基本的。

之前,我确定很多人都会面临这样的问题

2 个答案:

答案 0 :(得分:1)

经过大量迭代后,我设法创建一个优化的查询。 我把它放在AditBatch类中,并添加延迟

class AuditBatch


############################################
## Init a batch update from a list of ids
## the aim is to avoid instantiations in controllers
## @param string class name (eq: 'Followup')
## @param int[] ids of object to update
## @param changet hash of changes {key1: newval, key2: newval}
############################################

def self.init_batch_creation(auditable_type, auditable_ids, changes)
    obj = Object.const_get(auditable_type)
    group = obj.where(id: auditable_ids)
    AuditBatch.delay.batch_creation(group, changes, false)
end

############################################
## insert a long list of audits in one time
## @param group array array of auditable objects
## @param changet hash of changes {key1: newval, key2: newval}
############################################
 def self.batch_creation(group, changes, delayed = true)

  sql = 'INSERT INTO audits ("action", "audited_changes", "auditable_id", "auditable_type", "created_at", "version", "request_uuid") 
  VALUES '

  total = group.size
  group.each_with_index  do |g, index|

    parameters = 'json_build_object('
    length = changes.size
    i=1
    changes.each do |key, val|
      parameters += "'#{key}',"
      parameters += "json_build_array("
      parameters += "(SELECT ((audited_changes -> '#{key}')::json->>1) FROM audits WHERE auditable_id = #{g.id} order by id desc limit 1),"
      parameters += val.is_a?(String) ? "'#{val.to_s}'" : val.to_s 
      parameters += ')'
      parameters += ',' if i < length
      i +=1
    end
    parameters += ')'

    sql += "('update', #{parameters}, #{g.id}, '#{g.class.name}', '#{Time.now}', (SELECT max(version) FROM audits where auditable_id= #{g.id})+1, '#{SecureRandom.uuid}')"
    sql += ", " if (index+1) < total
  end
  if delayed==true
    AuditBatch.delay.execute_delayed_sql(sql)
  else
    ActiveRecord::Base.connection.execute(sql)
 end

 end

def self.execute_delayed_sql(sql)
  ActiveRecord::Base.connection.execute(sql)
end
end

答案 1 :(得分:0)

使用group.update_all您的回调被跳过,并且它不会在新审核中记录更改。

您无法手动为这些记录创建审核,即使您可以手动创建审核更改,您也需要参考&#34;更改了什么?&#34; (进入audited_changes)。但是,如果您之前update_all group (`action`, `audited_changes`, `auditable_id`, `auditable_type`, `created_at`, `version`, `request_uuid`) ,则这些更改已经丢失。

line = "Elector's Name: Surpam Badurubai Elector's Name: Madavimaru Elector's Name: Madavitannubai"
[name.strip() for name in line.split("Elector's Name:") if name != '']

audited问题 - https://github.com/collectiveidea/audited/issues/352

中也记录了这一点

paper_trail,另一个保留change_logs的宝石,也有这个问题:https://github.com/airblade/paper_trail/issues/337