Skip to content

Commit 855619b

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 855619b

File tree

6 files changed

+39
-6
lines changed

6 files changed

+39
-6
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ All configuration takes place in the form of environment vars. See [queue_classi
239239

240240
## JSON
241241

242-
If you are running PostgreSQL 9.2 or higher, queue_classic will use the [json](http://www.postgresql.org/docs/9.2/static/datatype-json.html) datatype for storing arguments. Versions 9.1 and lower will use the 'text' column. If you have installed queue_classic prior to version 2.1.4 and are running PostgreSQL >= 9.2, run the following to switch to using the json type:
242+
If you are running PostgreSQL 9.4 or higher, queue_classic will use the [jsonb](http://www.postgresql.org/docs/9.4/static/datatype-json.html) datatype for storing arguments. Versions 9.2 and 9.3 will use the `json` data type and versions 9.1 and lower will use the `text` data type. If you have installed queue_classic prior to version 2.1.4 and are running PostgreSQL >= 9.4, run the following to switch to using the `jsonb` type:
243243
```
244-
alter table queue_classic_jobs alter column args type json using (args::json);
244+
alter table queue_classic_jobs alter column args type jsonb using (args::jsonb);
245245
```
246246

247247
## Logging

changelog

+2
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
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

+7
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

+6-4
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

+14
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)