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