class AddSourceToSourceFiles < ActiveRecord::Migration[5.0]
  def up
    add_column :source_files, :creator_id, :integer
    add_column :source_files, :creator_type, :string
    connection.execute <<-SQL
      UPDATE source_files SET creator_id=user_id, creator_type='User' WHERE user_id IS NOT NULL
    SQL
    remove_column :source_files, :user_id, :integer
  end

  def down
    add_column :source_files, :user_id, :integer
    connection.execute <<-SQL
      UPDATE source_files SET user_id=creator_id WHERE creator_id IS NOT NULL AND creator_type='User'
    SQL
    remove_column :source_files, :creator_id, :integer
    remove_column :source_files, :creator_type, :string
  end
end
