1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class AddFileInfoToCollection < ActiveRecord::Migration
9 pdhs_str << "'" << pdh << "'" << ','
12 collections = ActiveRecord::Base.connection.exec_query(
13 'SELECT DISTINCT portable_data_hash, manifest_text FROM collections '\
14 "WHERE portable_data_hash IN (#{pdhs_str[0..-2]}) "
17 collections.rows.each do |row|
20 row[1].scan(/\S+/) do |token|
21 is_file = token.match(/^[[:digit:]]+:[[:digit:]]+:/)
23 _, filesize, filename = token.split(':', 3)
25 # Avoid counting empty dir placeholders
26 break if filename == '.' && filesize.zero?
29 file_size_total += filesize.to_i
32 ActiveRecord::Base.connection.exec_query('BEGIN')
33 ActiveRecord::Base.connection.exec_query("UPDATE collections SET file_count=#{file_count}, "\
34 "file_size_total=#{file_size_total} "\
35 "WHERE portable_data_hash='#{row[0]}'")
36 ActiveRecord::Base.connection.exec_query('COMMIT')
41 add_column :collections, :file_count, :integer, default: 0, null: false
42 add_column :collections, :file_size_total, :integer, default: 0, null: false
44 Container.group_pdhs_for_multiple_transactions('AddFileInfoToCollection') do |pdhs|
50 remove_column :collections, :file_count
51 remove_column :collections, :file_size_total