-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_top_container_barcodes.rb
105 lines (81 loc) · 2.4 KB
/
update_top_container_barcodes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
require 'bundler/setup'
Bundler.require
require 'csv'
require 'pp'
class AddTopContainerBarcodes
@@csv_path = './ASX_Satellite_2022-02-03-2.csv'
@@batch_size = 50
# Specify the CSV column header containing the new barcodes
@@barcode_column = 'updated_barcode'
# NOTE: This script only works if you are using ArchivesSpace with an external MySQL database.
# You will need these connection details:
# * MySQL host
# * MySQL username + password
# * MySQL port (probably 3306)
# * ArchivesSpace database name
# If you have multiple environments (e.g. staging and production)
# you can specify the environment here and set different connection options for each below.
@@environment = :production
case @@environment
when :production
@@mysql_client_config = { :host => "your.database.host", :username => "archivesspace",
:password => "password", :database => "archivesspace", :port => 3306 }
when :staging
@@mysql_client_config = { :host => "your.database.host", :username => "archivesspace",
:password => "password", :database => "archivesspace_staging", :port => 3306 }
end
def self.call
object= new
object.call
end
def initialize
@mysql_client = Mysql2::Client.new(@@mysql_client_config)
end
def call
execute
end
private
def execute
get_data_from_csv
# check_barcodes
update_top_containers
puts
end
def get_data_from_csv
@data = []
puts "Processing CSV..."
CSV.foreach(@@csv_path, headers: true) do |row|
if row[@@barcode_column] && row[@@barcode_column].length > 1
uri = row['top_container_uri']
id = Pathname.new(uri).basename.to_s.to_i
row_data = { id: id, barcode: row[@@barcode_column] }
@data << row_data
# puts row_data.inspect
end
end
puts @data.length
@data
end
def check_barcodes
@data.each do |d|
q = "SELECT barcode from top_container WHERE id = #{d[:id]}"
result = @mysql_client.query(q)
if result.count == 1
b = result.first['barcode']
print b != d[:barcode] ? '+' : '.'
else
print result.count
end
end
puts
end
def update_top_containers
@data.each do |d|
q = "UPDATE top_container SET barcode = '#{d[:barcode]}' WHERE id = #{d[:id]}"
# puts q
@mysql_client.query(q)
print '.'
end
end
end
AddTopContainerBarcodes.call