Guest User

Untitled

a guest
Mar 10th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. class MergeCatalystDatabase < ActiveRecord::Migration
  2. def self.up
  3. ### Most tables can be copied directly. However, "forms", "inclusions", and "steps" are duplicated between
  4. ### the applications and are going to have to be merged in a more defined manner.
  5.  
  6. # Temporary credentials
  7. mysql_user = "hi_pastie"
  8. mysql_password = "hi_pastie"
  9. ActiveRecord::Base.establish_connection(:catalyst_migration)
  10. catalyst_db = ActiveRecord::Base.connection.current_database
  11. ActiveRecord::Base.establish_connection(:chemistry_migration)
  12. chemistry_db = ActiveRecord::Base.connection.current_database
  13.  
  14. tables_to_copy = "articles footers lists partner_pixels partnerships receipts sites source_filters"
  15. import_command = "mysql -u #{mysql_user} -p#{mysql_password} #{chemistry_db}"
  16.  
  17. ### Merge step tables
  18. # Rename steps to chemistry_steps
  19. rename_table :steps, :chemistry_steps
  20.  
  21. # Copy the catalyst form table over under a different name
  22.  
  23. # Split up the commands for readability
  24. dump_command = "mysqldump -u #{mysql_user} -p#{mysql_password} #{catalyst_db} steps"
  25.  
  26. say_with_time "create steps table from catalyst steps" do
  27. `#{dump_command} | #{import_command}`
  28. end
  29.  
  30. add_column :steps, :order_num, :integer
  31.  
  32. Step.reset_column_information
  33.  
  34. ### Merge form tables
  35. execute "create table forms_backup select * from forms"
  36.  
  37. add_column :forms, :site_id, :integer
  38. add_column :forms, :title, :string
  39. add_column :forms, :header, :string
  40. add_column :forms, :subheader, :string
  41. add_column :forms, :description, :text
  42. add_column :forms, :form_type, :string
  43. add_column :forms, :navbar, :text
  44. add_column :forms, :api_code, :string
  45.  
  46. Form.reset_column_information
  47.  
  48. # Copy the catalyst form table over under a different name
  49. rename_table :forms, :chemistry_forms
  50. dump_command = "mysqldump -u #{mysql_user} -p#{mysql_password} #{catalyst_db} forms"
  51.  
  52. say_with_time "copy forms table from catalyst" do
  53. `#{dump_command} | #{import_command}`
  54. end
  55.  
  56. rename_table :forms, :catalyst_forms
  57. rename_table :chemistry_forms, :forms
  58.  
  59. catalyst_form_fields = ['site_id', 'title', 'header', 'subheader', 'description', 'form_type', 'navbar', 'api_code']
  60. form_update_sql_fragment = catalyst_form_fields.map { |f| "forms.#{f} = catalyst_forms.#{f}" }.join(', ')
  61.  
  62. update "update forms join catalyst_forms on forms.code = catalyst_forms.api_code set #{form_update_sql_fragment}"
  63.  
  64. # Split up the commands for readability
  65. dump_command = "mysqldump -u #{mysql_user} -p#{mysql_password} #{catalyst_db} #{tables_to_copy}"
  66.  
  67. say_with_time "direct copy tables from the catalyst db into the chemistry db" do
  68. `#{dump_command} | #{import_command}`
  69. end
  70.  
  71. # All items associated with Catalyst Forms need to have their foreign key IDs replaced with the new Chemistry Form.
  72. # Lists
  73. update "update lists join catalyst_forms on catalyst_forms.id = lists.form_id join forms on forms.code = catalyst_forms.api_code set lists.form_id = forms.id"
  74. # Steps
  75. update "update steps join catalyst_forms on catalyst_forms.id = steps.form_id join forms on forms.code = catalyst_forms.api_code set steps.form_id = forms.id"
  76.  
  77. say_with_time "update step order_nums" do
  78. forms = Form.find(:all)
  79. forms.each do |form|
  80. form.steps.each do |step|
  81. step.order_num =
  82. case form.form_type
  83. when 'lead_gen'
  84. case step.name
  85. when 'index'
  86. 0
  87. when 'step2'
  88. 1
  89. when 'step3'
  90. 2
  91. when 'step4'
  92. nil
  93. when 'step4success'
  94. nil
  95. end
  96. when 'scholarships'
  97. case step.name
  98. when 'index'
  99. 0
  100. when 'step2'
  101. 1
  102. when 'step2b'
  103. 2
  104. when 'step2success'
  105. nil
  106. when 'step3'
  107. nil
  108. when 'step4'
  109. nil
  110. end
  111. else
  112. if step.name == 'index'
  113. 0
  114. else
  115. nil
  116. end
  117. end
  118. step.save!
  119. end
  120. end
  121. end
  122.  
  123. # All items associated with Chemistry Steps need to have their foreign key IDs replaced with the new Catalyst Step.
  124.  
  125. execute "create table sections_backup select * from sections"
  126.  
  127. update "update sections
  128. join chemistry_steps on chemistry_steps.id = sections.step_id
  129. join steps on steps.form_id = chemistry_steps.form_id and steps.order_num = chemistry_steps.order_num
  130. set sections.step_id = steps.id"
  131. #update "update form_attempt_steps
  132. # join chemistry_steps on chemistry_steps.id = form_attempt_steps.step_id
  133. # join steps on steps.form_id = chemistry_steps.form_id
  134. # set form_attempt_steps.step_id = steps.id"
  135.  
  136. drop_table :catalyst_forms
  137.  
  138. ### Rename Inclusions
  139. rename_table :inclusions, :question_inclusions
  140.  
  141. # Copy the catalyst inclusions table over under a different name
  142. dump_command = "mysqldump -u #{mysql_user} -p#{mysql_password} #{catalyst_db} inclusions"
  143.  
  144. say_with_time "create inclusions table from catalyst" do
  145. `#{dump_command} | #{import_command}`
  146. end
  147.  
  148. rename_table :inclusions, :article_inclusions
  149. end
  150.  
  151. def self.down
  152. drop_table :forms
  153. rename_table :forms_backup, :forms
  154. drop_table :steps
  155. rename_table :chemistry_steps, :steps
  156. rename_table :question_inclusions, :inclusions
  157. drop_table :sections
  158. rename_table :sections_backup, :sections
  159. drop_table :article_inclusions
  160. drop_table :lists
  161. drop_table :articles
  162. drop_table :footers
  163. drop_table :partner_pixels
  164. drop_table :partnerships
  165. drop_table :receipts
  166. drop_table :sites
  167. drop_table :source_filters
  168. end
  169. end
Add Comment
Please, Sign In to add comment