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