Merge branch '0000-ruby-client-config'
[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     act_as_system_user do
14       PipelineInstance.all.each do |pi|
15         pi.state = PipelineInstance::New
16
17         if !pi.attribute_present? :success   # success is nil
18           if pi[:active] == true
19             pi.state = PipelineInstance::RunningOnServer
20           else
21             if pi.components_look_ready?
22               pi.state = PipelineInstance::Ready
23             else
24               pi.state = PipelineInstance::New
25             end
26           end
27         elsif pi[:success] == true
28           pi.state = PipelineInstance::Complete
29         else
30           pi.state = PipelineInstance::Failed
31         end
32
33         pi.save!
34       end
35     end
36
37 # We want to perform addition of state, and removal of active and success in two phases. Hence comment these statements out.
38 =begin
39     if column_exists?(:pipeline_instances, :active)
40       remove_column :pipeline_instances, :active
41     end
42
43     if column_exists?(:pipeline_instances, :success)
44       remove_column :pipeline_instances, :success
45     end
46 =end
47   end
48
49   def down
50 # We want to perform addition of state, and removal of active and success in two phases. Hence comment these statements out.
51 =begin
52     add_column :pipeline_instances, :success, :boolean, :null => true
53     add_column :pipeline_instances, :active, :boolean, :default => false
54
55     act_as_system_user do
56       PipelineInstance.all.each do |pi|
57         case pi.state
58         when PipelineInstance::New, PipelineInstance::Ready
59           pi.active = false
60           pi.success = nil
61         when PipelineInstance::RunningOnServer
62           pi.active = true
63           pi.success = nil
64         when PipelineInstance::RunningOnClient
65           pi.active = false
66           pi.success = nil
67         when PipelineInstance::Failed
68           pi.active = false
69           pi.success = false
70         when PipelineInstance::Complete
71           pi.active = false
72           pi.success = true
73         end
74         pi.save!
75       end
76     end
77 =end
78
79     if column_exists?(:pipeline_instances, :components_summary)
80       remove_column :pipeline_instances, :components_summary
81     end
82
83     if column_exists?(:pipeline_instances, :state)
84       remove_column :pipeline_instances, :state
85     end
86   end
87 end