Skip to content

Commit 12c109e

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 12c109e

File tree

8 files changed

+61
-6
lines changed

8 files changed

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

lib/generators/queue_classic/install_generator.rb

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def create_migration_file
3131
if self.class.migration_exists?('db/migrate', 'update_queue_classic_3_1_0').nil?
3232
migration_template 'update_queue_classic_3_1_0.rb', 'db/migrate/update_queue_classic_3_1_0.rb'
3333
end
34+
35+
if self.class.migration_exists?('db/migrate', 'update_queue_classic_3_2_0').nil?
36+
migration_template 'update_queue_classic_3_2_0.rb', 'db/migrate/update_queue_classic_3_2_0.rb'
37+
end
3438
end
3539
end
3640
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
QC::Setup.downgrade_from_3_2_0
8+
end
9+
end

lib/queue_classic/setup.rb

+13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ 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")
12+
DowngradeFrom_3_2_0 = File.join(Root, "/sql/downgrade_from_3_2_0.sql")
1113

1214
def self.create(c = QC::default_conn_adapter.connection)
1315
conn = QC::ConnAdapter.new(c)
@@ -27,6 +29,7 @@ def self.update(c = QC::default_conn_adapter.connection)
2729
conn = QC::ConnAdapter.new(c)
2830
conn.execute(File.read(UpgradeTo_3_0_0))
2931
conn.execute(File.read(UpgradeTo_3_1_0))
32+
conn.execute(File.read(UpgradeTo_3_2_0))
3033
conn.execute(File.read(DropSqlFunctions))
3134
conn.execute(File.read(SqlFunctions))
3235
end
@@ -54,5 +57,15 @@ def self.downgrade_from_3_1_0(c = QC::default_conn_adapter.connection)
5457
conn = QC::ConnAdapter.new(c)
5558
conn.execute(File.read(DowngradeFrom_3_1_0))
5659
end
60+
61+
def self.update_to_3_2_0(c = QC::default_conn_adapter.connection)
62+
conn = QC::ConnAdapter.new(c)
63+
conn.execute(File.read(UpgradeTo_3_2_0))
64+
end
65+
66+
def self.downgrade_from_3_2_0(c = QC::default_conn_adapter.connection)
67+
conn = QC::ConnAdapter.new(c)
68+
conn.execute(File.read(DowngradeFrom_3_2_0))
69+
end
5770
end
5871
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/downgrade_from_3_2_0.sql

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
do $$ begin
2+
3+
-- If json type is available, use it for the args column
4+
if exists (select 1 from pg_type where typname = 'json') then
5+
alter table queue_classic_jobs alter column args type json using args::json;
6+
-- Otherwise use the text type for the args column
7+
else
8+
alter table queue_classic_jobs alter column args type text using args::text;
9+
end if;
10+
11+
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)