Skip to content

Commit 9fb7821

Browse files
committed
Change json datatype to jsonb, if available.
Also update existing text columns to jsonb and json. Signed-off-by: Clemens Gruber <[email protected]>
1 parent e66a305 commit 9fb7821

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

changelog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Unreleased
22
- Fixed a bug in the offset calculation of `.enqueue_at`.
3+
- Use jsonb type for the args column, if available. Otherwise fall back to
4+
json type or keep using text. Also added a migration to update old schemas.
35

46
Version 3.0.0rc
57
- Improved signal handling
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class UpdateQueueClassic320 < ActiveRecord::Migration
2+
def self.up
3+
QC::Setup.update_to_3_2_0
4+
end
5+
6+
def self.down
7+
end
8+
end

lib/queue_classic/setup.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Setup
88
DowngradeFrom_3_0_0 = File.join(Root, "/sql/downgrade_from_3_0_0.sql")
99
UpgradeTo_3_1_0 = File.join(Root, "/sql/update_to_3_1_0.sql")
1010
DowngradeFrom_3_1_0 = File.join(Root, "/sql/downgrade_from_3_1_0.sql")
11+
UpgradeTo_3_2_0 = File.join(Root, "/sql/update_to_3_2_0.sql")
1112

1213
def self.create(c = QC::default_conn_adapter.connection)
1314
conn = QC::ConnAdapter.new(c)
@@ -27,6 +28,7 @@ def self.update(c = QC::default_conn_adapter.connection)
2728
conn = QC::ConnAdapter.new(c)
2829
conn.execute(File.read(UpgradeTo_3_0_0))
2930
conn.execute(File.read(UpgradeTo_3_1_0))
31+
conn.execute(File.read(UpgradeTo_3_2_0))
3032
conn.execute(File.read(DropSqlFunctions))
3133
conn.execute(File.read(SqlFunctions))
3234
end
@@ -54,5 +56,10 @@ def self.downgrade_from_3_1_0(c = QC::default_conn_adapter.connection)
5456
conn = QC::ConnAdapter.new(c)
5557
conn.execute(File.read(DowngradeFrom_3_1_0))
5658
end
59+
60+
def self.update_to_3_2_0(c = QC::default_conn_adapter.connection)
61+
conn = QC::ConnAdapter.new(c)
62+
conn.execute(File.read(UpgradeTo_3_2_0))
63+
end
5764
end
5865
end

sql/create_table.sql

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ CREATE TABLE queue_classic_jobs (
1111
scheduled_at timestamptz default now()
1212
);
1313

14-
-- If json type is available, use it for the args column.
15-
perform * from pg_type where typname = 'json';
16-
if found then
17-
alter table queue_classic_jobs alter column args type json using (args::json);
14+
-- If jsonb type is available, use it for the args column
15+
if exists (select 1 from pg_type where typname = 'jsonb') then
16+
alter table queue_classic_jobs alter column args type jsonb using args::jsonb;
17+
-- Otherwise, use json type for the args column if available
18+
elsif exists (select 1 from pg_type where typname = 'json') then
19+
alter table queue_classic_jobs alter column args type json using args::json;
1820
end if;
1921

2022
end $$ language plpgsql;

sql/update_to_3_2_0.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
do $$ begin
2+
3+
-- If jsonb type is available, use it for the args column
4+
if exists (select 1 from pg_type where typname = 'jsonb') then
5+
alter table queue_classic_jobs alter column args type jsonb using args::jsonb;
6+
-- Otherwise, use json type for the args column if available
7+
elsif exists (select 1 from pg_type where typname = 'json') then
8+
alter table queue_classic_jobs alter column args type json using args::json;
9+
-- Keep using text type for the args column, if neither is available
10+
else
11+
alter table queue_classic_jobs alter column args type text using args::text;
12+
end if;
13+
14+
end $$ language plpgsql;

0 commit comments

Comments
 (0)