15106: Adds exception handling to the trgm migration
[arvados.git] / services / api / db / migrate / 20140422011506_pipeline_instance_state.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class PipelineInstanceState < ActiveRecord::Migration[4.2]
6   include CurrentApiClient
7
8   def up
9     add_column :pipeline_instances, :state, :string
10     add_column :pipeline_instances, :components_summary, :text
11
12     PipelineInstance.reset_column_information
13
14     act_as_system_user do
15       PipelineInstance.all.each do |pi|
16         pi.state = PipelineInstance::New
17
18         if !pi.attribute_present? :success   # success is nil
19           if pi[:active] == true
20             pi.state = PipelineInstance::RunningOnServer
21           else
22             if pi.components_look_ready?
23               pi.state = PipelineInstance::Ready
24             else
25               pi.state = PipelineInstance::New
26             end
27           end
28         elsif pi[:success] == true
29           pi.state = PipelineInstance::Complete
30         else
31           pi.state = PipelineInstance::Failed
32         end
33
34         pi.save!
35       end
36     end
37
38 # We want to perform addition of state, and removal of active and success in two phases. Hence comment these statements out.
39 =begin
40     if column_exists?(:pipeline_instances, :active)
41       remove_column :pipeline_instances, :active
42     end
43
44     if column_exists?(:pipeline_instances, :success)
45       remove_column :pipeline_instances, :success
46     end
47 =end
48   end
49
50   def down
51 # We want to perform addition of state, and removal of active and success in two phases. Hence comment these statements out.
52 =begin
53     add_column :pipeline_instances, :success, :boolean, :null => true
54     add_column :pipeline_instances, :active, :boolean, :default => false
55
56     act_as_system_user do
57       PipelineInstance.all.each do |pi|
58         case pi.state
59         when PipelineInstance::New, PipelineInstance::Ready
60           pi.active = false
61           pi.success = nil
62         when PipelineInstance::RunningOnServer
63           pi.active = true
64           pi.success = nil
65         when PipelineInstance::RunningOnClient
66           pi.active = false
67           pi.success = nil
68         when PipelineInstance::Failed
69           pi.active = false
70           pi.success = false
71         when PipelineInstance::Complete
72           pi.active = false
73           pi.success = true
74         end
75         pi.save!
76       end
77     end
78 =end
79
80     if column_exists?(:pipeline_instances, :components_summary)
81       remove_column :pipeline_instances, :components_summary
82     end
83
84     if column_exists?(:pipeline_instances, :state)
85       remove_column :pipeline_instances, :state
86     end
87   end
88 end