Sha256: 64d4e3427fec88fa4b0840a100e423ba91522d164744c3e59aa66c90852c7a42

Contents?: true

Size: 877 Bytes

Versions: 2

Compression:

Stored size: 877 Bytes

Contents

class AddBlogIds < ActiveRecord::Migration[4.2]
  class Blog < ActiveRecord::Base; end
  class Content < ActiveRecord::Base; end
  class Sidebar < ActiveRecord::Base; end

  def up
    add_column :contents, :blog_id, :integer
    add_column :sidebars, :blog_id, :integer

    if Content.any? || Sidebar.any?
      default_blog_id = Blog.order(:id).first.id

      Content.update_all("blog_id = #{default_blog_id}")
      Sidebar.update_all("blog_id = #{default_blog_id}")
    end

    change_column :sidebars, :blog_id, :integer, null: false
  end

  def down
    if adapter_name == 'PostgreSQL'
      indexes(:contents).each do |index|
        remove_index(:contents, name: index.name) if index.name =~ /blog_id/
      end
    else
      remove_index :contents, :blog_id rescue nil
    end
    remove_column :contents, :blog_id
    remove_column :sidebars, :blog_id
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
publify_core-9.0.1 db/migrate/20150808052637_add_blog_ids.rb
publify_core-9.0.0 db/migrate/20150808052637_add_blog_ids.rb