Skip to content

Commit

Permalink
Fix: create new conditional indexes _before_ dropping overlapping ind…
Browse files Browse the repository at this point in the history
…exes (#1165)

Fixes issue introduced in #1163
  • Loading branch information
bensheldon authored Dec 2, 2023
1 parent 777ddfe commit 015f45f
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 56 deletions.
27 changes: 0 additions & 27 deletions demo/db/migrate/20231128075428_recreate_good_job_cron_indexes.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

class RecreateGoodJobCronIndexesWithConditional < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

def change
reversible do |dir|
dir.up do
unless connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_created_at_cond)
add_index :good_jobs, [:cron_key, :created_at], where: "(cron_key IS NOT NULL)",
name: :index_good_jobs_on_cron_key_and_created_at_cond, algorithm: :concurrently
end
unless connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_cron_at_cond)
add_index :good_jobs, [:cron_key, :cron_at], where: "(cron_key IS NOT NULL)", unique: true,
name: :index_good_jobs_on_cron_key_and_cron_at_cond, algorithm: :concurrently
end

if connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_created_at)
remove_index :good_jobs, name: :index_good_jobs_on_cron_key_and_created_at
end
if connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_cron_at)
remove_index :good_jobs, name: :index_good_jobs_on_cron_key_and_cron_at
end
end

dir.down do
unless connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_created_at)
add_index :good_jobs, [:cron_key, :created_at],
name: :index_good_jobs_on_cron_key_and_created_at, algorithm: :concurrently
end
unless connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_cron_at)
add_index :good_jobs, [:cron_key, :cron_at], unique: true,
name: :index_good_jobs_on_cron_key_and_cron_at, algorithm: :concurrently
end

if connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_created_at_cond)
remove_index :good_jobs, name: :index_good_jobs_on_cron_key_and_created_at_cond
end
if connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_cron_at_cond)
remove_index :good_jobs, name: :index_good_jobs_on_cron_key_and_cron_at_cond
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class CreateGoodJobs < ActiveRecord::Migration<%= migration_version %>
add_index :good_jobs, [:queue_name, :scheduled_at], where: "(finished_at IS NULL)", name: :index_good_jobs_on_queue_name_and_scheduled_at
add_index :good_jobs, [:active_job_id, :created_at], name: :index_good_jobs_on_active_job_id_and_created_at
add_index :good_jobs, :concurrency_key, where: "(finished_at IS NULL)", name: :index_good_jobs_on_concurrency_key_when_unfinished
add_index :good_jobs, [:cron_key, :created_at], where: "(cron_key IS NOT NULL)", name: :index_good_jobs_on_cron_key_and_created_at
add_index :good_jobs, [:cron_key, :cron_at], where: "(cron_key IS NOT NULL)", unique: true, name: :index_good_jobs_on_cron_key_and_cron_at
add_index :good_jobs, [:cron_key, :created_at], where: "(cron_key IS NOT NULL)", name: :index_good_jobs_on_cron_key_and_created_at_cond
add_index :good_jobs, [:cron_key, :cron_at], where: "(cron_key IS NOT NULL)", unique: true, name: :index_good_jobs_on_cron_key_and_cron_at_cond
add_index :good_jobs, [:active_job_id], name: :index_good_jobs_on_active_job_id
add_index :good_jobs, [:finished_at], where: "retried_good_job_id IS NULL AND finished_at IS NOT NULL", name: :index_good_jobs_jobs_on_finished_at
add_index :good_jobs, [:priority, :created_at], order: { priority: "DESC NULLS LAST", created_at: :asc },
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

class RecreateGoodJobCronIndexesWithConditional < ActiveRecord::Migration<%= migration_version %>
disable_ddl_transaction!

def change
reversible do |dir|
dir.up do
unless connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_created_at_cond)
add_index :good_jobs, [:cron_key, :created_at], where: "(cron_key IS NOT NULL)",
name: :index_good_jobs_on_cron_key_and_created_at_cond, algorithm: :concurrently
end
unless connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_cron_at_cond)
add_index :good_jobs, [:cron_key, :cron_at], where: "(cron_key IS NOT NULL)", unique: true,
name: :index_good_jobs_on_cron_key_and_cron_at_cond, algorithm: :concurrently
end

if connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_created_at)
remove_index :good_jobs, name: :index_good_jobs_on_cron_key_and_created_at
end
if connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_cron_at)
remove_index :good_jobs, name: :index_good_jobs_on_cron_key_and_cron_at
end
end

dir.down do
unless connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_created_at)
add_index :good_jobs, [:cron_key, :created_at], where: "(cron_key IS NOT NULL)",
name: :index_good_jobs_on_cron_key_and_created_at, algorithm: :concurrently
end
unless connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_cron_at)
add_index :good_jobs, [:cron_key, :cron_at], where: "(cron_key IS NOT NULL)", unique: true,
name: :index_good_jobs_on_cron_key_and_cron_at, algorithm: :concurrently
end

if connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_created_at_cond)
remove_index :good_jobs, name: :index_good_jobs_on_cron_key_and_created_at_cond
end
if connection.index_name_exists?(:good_jobs, :index_good_jobs_on_cron_key_and_cron_at_cond)
remove_index :good_jobs, name: :index_good_jobs_on_cron_key_and_cron_at_cond
end
end
end
end
end

0 comments on commit 015f45f

Please sign in to comment.