class AddColumnsToPostFiles < ActiveRecord::Migration[7.2]
  def up
    change_table :domain_post_files do |t|
      t.string :state
      t.string :url_str
      t.string :error_message
      t.integer :last_status_code
      t.integer :retry_count, default: 0
    end

    up_only { execute <<-SQL }
      UPDATE domain_post_files
      SET state = json_attributes->>'state',
          url_str = json_attributes->>'url_str',
          error_message = json_attributes->>'error_message',
          last_status_code = (json_attributes->>'last_status_code')::integer,
          retry_count = COALESCE((json_attributes->>'retry_count')::integer, 0)
    SQL

    change_column_null :domain_post_files, :state, false
  end

  def down
    raise ActiveRecord::IrreversibleMigration
  end
end
