1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
8 class AddFileInfoToCollection < ActiveRecord::Migration
12 pdhs_str << "'" << pdh << "'" << ","
15 collections = ActiveRecord::Base.connection.exec_query(
16 "SELECT DISTINCT portable_data_hash, manifest_text FROM collections "\
17 "WHERE portable_data_hash IN (#{pdhs_str[0..-2]}) "
20 collections.rows.each do |row|
21 manifest = Keep::Manifest.new(row[1])
22 ActiveRecord::Base.connection.exec_query("BEGIN")
23 ActiveRecord::Base.connection.exec_query("UPDATE collections SET file_count=#{manifest.files_count}, "\
24 "file_size_total=#{manifest.files_size} "\
25 "WHERE portable_data_hash='#{row[0]}'")
26 ActiveRecord::Base.connection.exec_query("COMMIT")
31 add_column :collections, :file_count, :integer, default: 0, null: false
32 add_column :collections, :file_size_total, :integer, limit: 8, default: 0, null: false
34 distinct_pdh_count = ActiveRecord::Base.connection.exec_query(
35 "SELECT DISTINCT portable_data_hash FROM collections"
38 # Generator that queries for all the distinct pdhs greater than last_pdh
39 ordered_pdh_query = lambda { |last_pdh, &block|
40 pdhs = ActiveRecord::Base.connection.exec_query(
41 "SELECT DISTINCT portable_data_hash FROM collections "\
42 "WHERE portable_data_hash > '#{last_pdh}' "\
43 "ORDER BY portable_data_hash LIMIT 1000"
45 pdhs.rows.each do |row|
50 batch_size_max = 1 << 28 # 256 MiB
51 GroupPdhs.group_pdhs_for_multiple_transactions(ordered_pdh_query,
54 "AddFileInfoToCollection") do |pdhs|
60 remove_column :collections, :file_count
61 remove_column :collections, :file_size_total