# coding: utf-8
class AddTimestampsToReport < ActiveRecord::Migration
  def self.up
    # add_column :reports, :created_at, :datetime#, :null => false
    # add_column :reports, :updated_at, :datetime#, :null => false
    # rs = Report.find_by_sql("SELECT r.id, IFNULL(m.date, '') new_date FROM reports r LEFT JOIN done_materials m ON m.id = (SELECT MIN(id) FROM done_materials WHERE report_id = r.id) WHERE DATE(r.date) > DATE('2019-10-01') HAVING new_date")
    # str = ""
    # rs.each do |r|
    #   str << "WHEN '#{r.id}' THEN '#{r.new_date}' "
    # end
    
    # Report.connection.execute("UPDATE reports r SET r.created_at=(CASE r.id #{str} END)")
  end
  
  def self.down
    remove_column :reports, :created_at
   remove_column :reports, :updated_at
  end
end
