Merge branch '8784-dir-listings'
[arvados.git] / services / api / db / migrate / 20140918153705_add_state_to_job.rb
index 810c3a70ce157102abe466d9c1a7a5a8cb3ab2ca..77310d3ac95ac315a06134f48834fea267a43313 100644 (file)
@@ -1,22 +1,25 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 class AddStateToJob < ActiveRecord::Migration
+  include CurrentApiClient
+
   def up
-    if !column_exists?(:jobs, :state)
+    ActiveRecord::Base.transaction do
       add_column :jobs, :state, :string
-    end
-
-    Job.reset_column_information
-
-    act_as_system_user do
-      Job.all.each do |job|
-        # before_save filter will set state based on job status
-        job.save!
-      end
+      Job.reset_column_information
+      Job.update_all({state: 'Cancelled'}, ['state is null and cancelled_at is not null'])
+      Job.update_all({state: 'Failed'}, ['state is null and success = ?', false])
+      Job.update_all({state: 'Complete'}, ['state is null and success = ?', true])
+      Job.update_all({state: 'Running'}, ['state is null and running = ?', true])
+      # Locked/started, but not Running/Failed/Complete? Let's assume it failed.
+      Job.update_all({state: 'Failed'}, ['state is null and (is_locked_by_uuid is not null or started_at is not null)'])
+      Job.update_all({state: 'Queued'}, ['state is null'])
     end
   end
 
   def down
-    if column_exists?(:jobs, :state)
-      remove_column :jobs, :state
-    end
+    remove_column :jobs, :state
   end
 end