-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
rails g good_job:update
command to add idempotent migration fil…
…es, including active_job_id, concurrency_key, cron_key columns
- Loading branch information
1 parent
b3dd40d
commit 5f76275
Showing
23 changed files
with
361 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
lib/generators/good_job/templates/install/migrations/create_good_jobs.rb.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class CreateGoodJobs < ActiveRecord::Migration[5.2] | ||
def change | ||
enable_extension 'pgcrypto' | ||
|
||
create_table :good_jobs, id: :uuid do |t| | ||
t.text :queue_name | ||
t.integer :priority | ||
t.jsonb :serialized_params | ||
t.timestamp :scheduled_at | ||
t.timestamp :performed_at | ||
t.timestamp :finished_at | ||
t.text :error | ||
|
||
t.timestamps | ||
|
||
t.uuid :active_job_id | ||
t.text :concurrency_key | ||
t.text :cron_key | ||
end | ||
|
||
add_index :good_jobs, :scheduled_at, where: "(finished_at IS NULL)", name: "index_good_jobs_on_scheduled_at" | ||
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], name: :index_good_jobs_on_cron_key_and_created_at | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...templates/update/migrations/02_add_active_job_id_concurrency_key_cron_key_to_good_jobs.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class AddActiveJobIdConcurrencyKeyCronKeyToGoodJobs < ActiveRecord::Migration[5.2] | ||
def change | ||
reversible do |dir| | ||
dir.up do | ||
# Ensure this incremental update migration is idempotent | ||
# with monolithic install migration. | ||
return if connection.column_exists?(:good_jobs, :active_job_id) | ||
end | ||
end | ||
|
||
add_column :good_jobs, :active_job_id, :uuid | ||
add_column :good_jobs, :concurrency_key, :text | ||
add_column :good_jobs, :cron_key, :text | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
...es/update/migrations/03_add_active_job_id_index_and_concurrency_key_index_to_good_jobs.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class AddActiveJobIdIndexAndConcurrencyKeyIndexToGoodJobs < ActiveRecord::Migration[5.2] | ||
disable_ddl_transaction! | ||
|
||
UPDATE_BATCH_SIZE = 1_000 | ||
|
||
class GoodJobJobs < ActiveRecord::Base | ||
self.table_name = "good_jobs" | ||
end | ||
|
||
def change | ||
reversible do |dir| | ||
dir.up do | ||
# Ensure this incremental update migration is idempotent | ||
# with monolithic install migration. | ||
return if connection.index_name_exists?(:good_jobs, :index_good_jobs_on_active_job_id_and_created_at) | ||
end | ||
end | ||
|
||
add_index :good_jobs, [:active_job_id, :created_at], algorithm: :concurrently, name: :index_good_jobs_on_active_job_id_and_created_at | ||
add_index :good_jobs, :concurrency_key, where: "(finished_at IS NULL)", algorithm: :concurrently, name: :index_good_jobs_on_concurrency_key_when_unfinished | ||
add_index :good_jobs, [:cron_key, :created_at], algorithm: :concurrently, name: :index_good_jobs_on_cron_key_and_created_at | ||
|
||
reversible do |dir| | ||
dir.up do | ||
loop do | ||
break if GoodJobJobs.where(active_job_id: nil, finished_at: nil).limit(UPDATE_BATCH_SIZE).update_all("active_job_id = (serialized_params->>'job_id')::uuid").zero? | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'rails/generators' | ||
require 'rails/generators/active_record' | ||
|
||
module GoodJob | ||
# | ||
# Rails generator used for updating GoodJob in a Rails application. | ||
# Run it with +bin/rails g good_job:update+ in your console. | ||
# | ||
class UpdateGenerator < Rails::Generators::Base | ||
include Rails::Generators::Migration | ||
|
||
class << self | ||
delegate :next_migration_number, to: ActiveRecord::Generators::Base | ||
end | ||
|
||
TEMPLATES = File.join(File.dirname(__FILE__), "templates/update") | ||
source_paths << TEMPLATES | ||
|
||
# Generates incremental migration files unless they already exist. | ||
# All migrations should be idempotent e.g. +add_index+ is guarded with +if_index_exists?+ | ||
def update_migration_files | ||
migration_templates = Dir.children(File.join(TEMPLATES, 'migrations')).sort | ||
migration_templates.each do |template_file| | ||
destination_file = template_file.match(/^\d*_(.*\.rb)/)[1] # 01_create_good_jobs.rb.erb => create_good_jobs.rb | ||
migration_template "migrations/#{template_file}", "db/migrate/#{destination_file}", skip: true | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'rails_helper' | ||
require 'generators/good_job/update_generator' | ||
|
||
describe GoodJob::UpdateGenerator, type: :generator, skip_if_java: true do | ||
context 'when running the generator alone' do | ||
around do |example| | ||
within_example_app do | ||
example.run | ||
end | ||
end | ||
|
||
it 'creates migrations for good_jobs table' do | ||
quiet do | ||
run_in_example_app 'rails g good_job:update' | ||
end | ||
|
||
expect(Dir.glob("#{example_app_path}/db/migrate/[0-9]*_create_good_jobs.rb")).not_to be_empty | ||
expect(Dir.glob("#{example_app_path}/db/migrate/[0-9]*_add_active_job_id_index_and_concurrency_key_index_to_good_jobs.rb")).not_to be_empty | ||
|
||
quiet do | ||
run_in_example_app 'rails db:migrate' | ||
end | ||
end | ||
end | ||
|
||
it 'produces an idempotentent schema.rb when run with install generator' do | ||
install_schema = "" | ||
update_after_install_schema = "" | ||
only_update_schema = "" | ||
|
||
within_example_app do | ||
quiet { run_in_example_app 'rails g good_job:install; rails db:migrate' } | ||
install_schema = File.read example_app_path.join('db', 'schema.rb') | ||
|
||
quiet { run_in_example_app 'rails g good_job:update; rails db:migrate' } | ||
update_after_install_schema = File.read example_app_path.join('db', 'schema.rb') | ||
end | ||
|
||
expect(normalize_schema(update_after_install_schema)).to eq normalize_schema(install_schema) | ||
|
||
within_example_app do | ||
quiet { run_in_example_app 'rails g good_job:update; rails db:migrate' } | ||
only_update_schema = File.read example_app_path.join('db', 'schema.rb') | ||
end | ||
|
||
expect(normalize_schema(only_update_schema)).to eq normalize_schema(install_schema) | ||
end | ||
|
||
def normalize_schema(text) | ||
text.sub(/ActiveRecord::Schema\.define\(version: ([\d_]*)\)/, 'ActiveRecord::Schema.define(version: SCHEMA_VERSION)') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.