maintenance_tasks: CSV task fails if uuid is used for active_storage_attachments.record_id

Background

We use uuid for primary keys, thus active_storage_attachments.record_id is uuid. On the other hand, maintenance_tasks_runs.id is bigint. Therefore, when a CSV task tries to upload a CSV as an ActiveStorage::Attachment, the attachment fails to reference the CSV task’s run record (MaintenanceTasks::Run) because of the type inconsistency.

Possible solutions

  • Document that the generated migration file must be manually edited when uuid is used.
  • Respect g.orm :active_record, primary_key_type: :uuid when generating the migration file.

Thanks in advance.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 5
  • Comments: 18 (12 by maintainers)

Most upvoted comments

I found myself in this situation and decided to hack it out with some monkeypatching and I thought I’d just offer this to anyone else who has a similar short-sighted need.

I’m using Zeitwerk overrides as described here: https://guides.rubyonrails.org/classic_to_zeitwerk_howto.html#decorating-classes-and-modules-from-engines

It uses ActiveRecordExtended for a window function and CTE because I was already using it, I’m sure we wouldn’t want to pull this dependency into the gem but it could certainly be written without it.

# app/overrides/maintenance_tasks/run_override.rb

module MaintenanceTasks
  class Run < ApplicationRecord
    scope :last_completed_per_task, -> do
      window_cte = define_window(:ended_at_window)
        .partition_by(:task_name, order_by: {ended_at: :desc})
        .select(:id)
        .select_window(:row_number, over: :ended_at_window, as: :ended_position)

      completed
        .with(run_ended_positions: window_cte)
        .joins("JOIN run_ended_positions ON run_ended_positions.id = maintenance_tasks_runs.id")
        .where(run_ended_positions: {ended_position: 1})
    end
  end
end
# app/overrides/maintenance_tasks/task_data_index_override.rb

module MaintenanceTasks
  class TaskDataIndex
    class << self
      def available_tasks
        tasks = []

        task_names = Task.available_tasks.map(&:name)

        active_runs = Run.with_attached_csv.active.where(task_name: task_names)
        active_runs.each do |run|
          tasks << TaskDataIndex.new(run.task_name, run)
          task_names.delete(run.task_name)
        end

        last_runs = Run.with_attached_csv.last_completed_per_task.where(task_name: task_names)
        task_names.map do |task_name|
          last_run = last_runs.find { |run| run.task_name == task_name }
          tasks << TaskDataIndex.new(task_name, last_run)
        end

        # We add an additional sorting key (status) to avoid possible
        # inconsistencies across database adapters when a Task has
        # multiple active Runs.
        tasks.sort_by! { |task| [task.name, task.status] }
      end
    end
  end
end

The meat of the change above is substituting the old:

        completed_runs = Run.completed.where(task_name: task_names)
        last_runs = Run.with_attached_csv.where(id: completed_runs.select("MAX(id) as id").group(:task_name))

for the new scope:

        last_runs = Run.with_attached_csv.last_completed_per_task.where(task_name: task_names)

With no other changes

# db/migrate/###_maintenance_tasks_runs_uuid_pkey.rb

class MaintenanceTasksRunsUuidPkey < ActiveRecord::Migration[7.0]
  def change
    add_column :maintenance_tasks_runs, :uuid, :uuid, default: "gen_random_uuid()", null: false
    change_table :maintenance_tasks_runs do |t|
      t.remove :id
      t.rename :uuid, :id
    end
    execute "ALTER TABLE maintenance_tasks_runs ADD PRIMARY KEY (id);"
  end
end

And now I can run CSV tasks. No tests, no promises, works for me.

This is a similar patch based on the version posted above but without the additional gem dependency. Still need to convert columns to UUID as above. I put this into an an initializer. Use at own risk

Rails.application.config.to_prepare do
  MaintenanceTasks::Run.class_eval do
    scope :last_completed_per_task, -> {
      subquery = completed
                   .select(:id, Arel.sql("ROW_NUMBER() OVER (PARTITION BY task_name ORDER BY ended_at DESC) AS ended_position"))
                   .to_sql

      # Join this subquery as a derived table and filter to where ended_position = 1
      joins("INNER JOIN (#{subquery}) run_ended_positions ON run_ended_positions.id = maintenance_tasks_runs.id")
        .where("run_ended_positions.ended_position = 1")
    }
  end

  module MaintenanceTasks
    class TaskDataIndex
      class << self
        def available_tasks
          tasks = []

          task_names = Task.available_tasks.map(&:name)

          active_runs = Run.with_attached_csv.active.where(task_name: task_names)
          active_runs.each do |run|
            tasks << TaskDataIndex.new(run.task_name, run)
            task_names.delete(run.task_name)
          end

          last_runs = Run.with_attached_csv.last_completed_per_task.where(task_name: task_names)
          task_names.map do |task_name|
            last_run = last_runs.find { |run| run.task_name == task_name }
            tasks << TaskDataIndex.new(task_name, last_run)
          end

          # We add additional sorting key (status) to avoid possible
          # inconsistencies across database adapters when a Task has
          # multiple active Runs.
          tasks.sort_by! { |task| [task.name, task.status] }
        end
      end
    end
  end
end

My team is currently pretty heads down, but we could potentially look at it later this year. If this issue gets closed as stale, can we reopen it later in the year when we’re able to better contribute?