Advertisement
twinfacer

migration

Apr 9th, 2014
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.22 KB | None | 0 0
  1. class ChangePageToSectionPageInSiteTemplates < ActiveRecord::Migration
  2.  
  3.   def up
  4.     remove_index :site_templates, :page_id
  5.     remove_column :site_templates, :page_id
  6.  
  7.     SiteTemplate.reset_column_information
  8.     site_section = nil
  9.     Site.all.each do |site|
  10.       site_section = site.site_sections.includes(:section).where("'sections'.'code' = 'core'").first
  11.       if site_section.nil?
  12.         core = Section.find_by_code("core")
  13.         site.site_sections.create(section: core, status: "active")
  14.         site_section = site.site_sections.includes(:section).where("'sections'.'code' = 'core'").first
  15.       end
  16.     end
  17.     SiteTemplate.all.each do |template|
  18.       template.site_section = site_section
  19.       template.save!
  20.     end
  21.  
  22.     change_column :site_templates, :site_section_id, :integer, null: false
  23.  
  24.     add_column :site_templates, :section_page_id, :integer
  25.     add_index :site_templates, :section_page_id
  26.   end
  27.  
  28.   def down
  29.     remove_index :site_templates, :section_page_id
  30.     remove_column :site_templates, :section_page_id
  31.    
  32.     change_column :site_templates, :site_section_id, :integer
  33.  
  34.     add_column :site_templates, :page_id, :integer
  35.     add_index :site_templates, :page_id
  36.   end
  37. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement