Sha256: 4d874579288ca5f865e57e5f801f77f834f31ae46c3bd3681f2969d8ea1cac9c
Contents?: true
Size: 951 Bytes
Versions: 11
Compression:
Stored size: 951 Bytes
Contents
# frozen_string_literal: true 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 /blog_id/.match?(index.name) 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
11 entries across 11 versions & 1 rubygems