Skip to content

Commit

Permalink
Split processes' name migration into two
Browse files Browse the repository at this point in the history
As it won't be possible to start new processes after the column
is made NOT NULL and before deploying the code that uses that column.
  • Loading branch information
rosa committed Aug 21, 2024
1 parent 9fb89f1 commit 3945042
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
16 changes: 10 additions & 6 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Upgrading to version 0.6.x

## New migration in 3 steps
This version adds a new migration to the `solid_queue_processes` table. This migration adds a new column that needs to be `NOT NULL`. It will run in three steps:
1. Add the new column, nullable
2. Backfill existing rows that would have the column as NULL
3. Make the column not nullable and add a new index
This version adds two new migrations to modify the `solid_queue_processes` table. The goal of that migration is to add a new column that needs to be `NOT NULL`. This needs to be done with two migrations and the following steps to ensure it happens without downtime and with new processes being able to register just fine:
1. Run the first migration that adds the new column, nullable
2. Deploy the updated Solid Queue code that uses this column
2. Run the second migration. This migration does two things:
- Backfill existing rows that would have the column as NULL
- Make the column not nullable and add a new index

To install it:
Besides, it adds another migration with no effects to the `solid_queue_recurring_tasks` table. This one can be run just fine whenever, as the column affected is not used.

To install the migrations:
```bash
$ bin/rails solid_queue:install:migrations
```
Expand All @@ -17,7 +21,7 @@ Or, if you're using a different database for Solid Queue:
$ bin/rails solid_queue:install:migrations DATABASE=<the_name_of_your_solid_queue_db>
```

And then just run it.
And then follow the steps above, running first one, then deploying the code, then running the second one.

## New behaviour when workers are killed
From this version onwards, when a worker is killed and the supervisor can detect that, it'll fail in-progress jobs claimed by that worker. For this to work correctly, you need to run the above migration and ensure you restart any supervisors you'd have.
Expand Down
15 changes: 1 addition & 14 deletions db/migrate/20240811173327_add_name_to_processes.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
class AddNameToProcesses < ActiveRecord::Migration[7.1]
def up
def change
add_column :solid_queue_processes, :name, :string

SolidQueue::Process.find_each do |process|
process.name ||= [ process.kind.downcase, SecureRandom.hex(10) ].join("-")
process.save!
end

add_index :solid_queue_processes, [ :name, :supervisor_id ], unique: true
change_column :solid_queue_processes, :name, :string, null: false
end

def down
remove_index :solid_queue_processes, [ :name, :supervisor_id ]
remove_column :solid_queue_processes, :name
end
end
16 changes: 16 additions & 0 deletions db/migrate/20240813160053_make_name_not_null.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class MakeNameNotNull < ActiveRecord::Migration[7.1]
def up
SolidQueue::Process.where(name: nil).find_each do |process|
process.name ||= [ process.kind.downcase, SecureRandom.hex(10) ].join("-")
process.save!
end

change_column :solid_queue_processes, :name, :string, null: false
add_index :solid_queue_processes, [ :name, :supervisor_id ], unique: true
end

def down
remove_index :solid_queue_processes, [ :name, :supervisor_id ]
change_column :solid_queue_processes, :name, :string, null: false
end
end

0 comments on commit 3945042

Please sign in to comment.