class AddClearanceToUsers < ActiveRecord::Migration<%= migration_version %>
  def self.up
    change_table :users do |t|
<% config[:new_columns].values.each do |column| -%>
      <%= column %>
<% end -%>
    end

<% config[:new_indexes].values.each do |index| -%>
    <%= index %>
<% end -%>

    users = select_all("SELECT id FROM users WHERE remember_token IS NULL")

    users.each do |user|
      update <<-SQL
        UPDATE users
        SET remember_token = '#{Clearance::Token.new}'
        WHERE id = '#{user['id']}'
      SQL
    end
  end

  def self.down
    change_table :users do |t|
<% if config[:new_columns].any? -%>
      t.remove <%= new_columns.keys.map { |column| ":#{column}" }.join(", ") %>
<% end -%>
    end
  end
end

class AddClearanceToUsers < ActiveRecord::Migration<%= migration_version %>
  def self.up
    change_table :users do |t|
<% config[:new_columns].values.each do |column| -%>
      <%= column %>
<% end -%>
    end

<% config[:new_indexes].values.each do |index| -%>
    <%= index %>
<% end -%>

    users = select_all("SELECT id FROM users WHERE remember_token IS NULL")

    users.each do |user|
      update <<-SQL.squish
        UPDATE users
        SET remember_token = '#{Clearance::Token.new}'
        WHERE id = '#{user['id']}'
      SQL
    end
  end

  def self.down
    change_table :users do |t|
<% if config[:new_columns].any? -%>
      t.remove <%= new_columns.keys.map { |column| ":#{column}" }.join(", ") %>
<% end -%>
    end
  end
end

Clean up sql output in generated migration
Clean up sql output in generated migration

Adding the `squish` here has the effect of slightly cleaning up the eventual SQL
generated by the migration we are helping create here. Before this change the
migration would be full of newlines (or `\n`). Post-change, each sql statement
should come out cleaner in the logs with the line breaks and tabs/spaces
replaced by just one space each.

The SQL that is run stays the same, it just looks slightly nicer in the log
output.
