9875: Fix script_parameters_digest to sort hashes hiding inside arrays, too.
authorTom Clegg <tom@curoverse.com>
Fri, 2 Sep 2016 02:05:51 +0000 (22:05 -0400)
committerTom Clegg <tom@curoverse.com>
Fri, 2 Sep 2016 02:05:51 +0000 (22:05 -0400)
services/api/app/models/job.rb
services/api/db/migrate/20160901210110_repair_script_parameters_digest.rb [new file with mode: 0644]
services/api/db/structure.sql
services/api/test/unit/job_test.rb

index 248d16a4ef5c8fbb8924b2c286369468db453495..a86cc62bf2f417432b4c547bfd40563172511108 100644 (file)
@@ -281,11 +281,16 @@ class Job < ArvadosModel
     Digest::MD5.hexdigest(Oj.dump(deep_sort_hash(h)))
   end
 
-  def self.deep_sort_hash h
-    return h unless h.is_a? Hash
-    h.sort.collect do |k, v|
-      [k, deep_sort_hash(v)]
-    end.to_h
+  def self.deep_sort_hash x
+    if x.is_a? Hash
+      x.sort.collect do |k, v|
+        [k, deep_sort_hash(v)]
+      end.to_h
+    elsif x.is_a? Array
+      x.collect { |v| deep_sort_hash(v) }
+    else
+      x
+    end
   end
 
   def foreign_key_attributes
diff --git a/services/api/db/migrate/20160901210110_repair_script_parameters_digest.rb b/services/api/db/migrate/20160901210110_repair_script_parameters_digest.rb
new file mode 100644 (file)
index 0000000..18eed7a
--- /dev/null
@@ -0,0 +1,17 @@
+class RepairScriptParametersDigest < ActiveRecord::Migration
+  def up
+    Job.find_each do |j|
+      have = j.script_parameters_digest
+      want = j.update_script_parameters_digest
+      if have != want
+        # where().update_all() skips validations, event logging, and
+        # timestamp updates, and just runs SQL. (This change is
+        # invisible to clients.)
+        Job.where('id=?', j.id).update_all(script_parameters_digest: want)
+      end
+    end
+  end
+
+  def down
+  end
+end
index bda4e5de872607b4d9c29786b4054889c760d6fb..1573db232ed1b78368edadf45800bdb296aa5011 100644 (file)
@@ -2686,4 +2686,6 @@ INSERT INTO schema_migrations (version) VALUES ('20160808151559');
 
 INSERT INTO schema_migrations (version) VALUES ('20160819195557');
 
-INSERT INTO schema_migrations (version) VALUES ('20160819195725');
\ No newline at end of file
+INSERT INTO schema_migrations (version) VALUES ('20160819195725');
+
+INSERT INTO schema_migrations (version) VALUES ('20160901210110');
\ No newline at end of file
index 1bc8035db67ce414fc23a3e84c9818e266d527d7..1d3abac60a8070b9c70726e5f3ef10ba43202d6e 100644 (file)
@@ -456,4 +456,10 @@ class JobTest < ActiveSupport::TestCase
                    "wrong script_parameters_digest for #{j.uuid}")
     end
   end
+
+  test 'deep_sort_hash on array of hashes' do
+    a = {'z' => [[{'a' => 'a', 'b' => 'b'}]]}
+    b = {'z' => [[{'b' => 'b', 'a' => 'a'}]]}
+    assert_equal Job.deep_sort_hash(a).to_json, Job.deep_sort_hash(b).to_json
+  end
 end