Sha256: c65d4ea2cf6f41c2312e5e70ac8459c6eb68e1568f8373ba0a4c1cfa2f7cf504

Contents?: true

Size: 915 Bytes

Versions: 1

Compression:

Stored size: 915 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
      begin
        remove_index :contents, :blog_id
      rescue
        nil
      end
    end
    remove_column :contents, :blog_id
    remove_column :sidebars, :blog_id
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
publify_core-9.1.0 db/migrate/20150808052637_add_blog_ids.rb