Skip to content

Commit

Permalink
Change json datatype to jsonb, if available.
Browse files Browse the repository at this point in the history
Also update existing text columns to jsonb and json.

Signed-off-by: Clemens Gruber <[email protected]>
  • Loading branch information
clemensg committed May 18, 2015
1 parent e66a305 commit 821db0d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ All configuration takes place in the form of environment vars. See [queue_classi

## JSON

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:
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:
```
alter table queue_classic_jobs alter column args type json using (args::json);
alter table queue_classic_jobs alter column args type jsonb using (args::jsonb);
```

## Logging
Expand Down
2 changes: 2 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Unreleased
- Fixed a bug in the offset calculation of `.enqueue_at`.
- Use jsonb type for the args column, if available. Otherwise fall back to
json type or keep using text. Also added a migration to update old schemas.

Version 3.0.0rc
- Improved signal handling
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class UpdateQueueClassic320 < ActiveRecord::Migration
def self.up
QC::Setup.update_to_3_2_0
end

def self.down
end
end
7 changes: 7 additions & 0 deletions lib/queue_classic/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Setup
DowngradeFrom_3_0_0 = File.join(Root, "/sql/downgrade_from_3_0_0.sql")
UpgradeTo_3_1_0 = File.join(Root, "/sql/update_to_3_1_0.sql")
DowngradeFrom_3_1_0 = File.join(Root, "/sql/downgrade_from_3_1_0.sql")
UpgradeTo_3_2_0 = File.join(Root, "/sql/update_to_3_2_0.sql")

def self.create(c = QC::default_conn_adapter.connection)
conn = QC::ConnAdapter.new(c)
Expand All @@ -27,6 +28,7 @@ def self.update(c = QC::default_conn_adapter.connection)
conn = QC::ConnAdapter.new(c)
conn.execute(File.read(UpgradeTo_3_0_0))
conn.execute(File.read(UpgradeTo_3_1_0))
conn.execute(File.read(UpgradeTo_3_2_0))
conn.execute(File.read(DropSqlFunctions))
conn.execute(File.read(SqlFunctions))
end
Expand Down Expand Up @@ -54,5 +56,10 @@ def self.downgrade_from_3_1_0(c = QC::default_conn_adapter.connection)
conn = QC::ConnAdapter.new(c)
conn.execute(File.read(DowngradeFrom_3_1_0))
end

def self.update_to_3_2_0(c = QC::default_conn_adapter.connection)
conn = QC::ConnAdapter.new(c)
conn.execute(File.read(UpgradeTo_3_2_0))
end
end
end
10 changes: 6 additions & 4 deletions sql/create_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ CREATE TABLE queue_classic_jobs (
scheduled_at timestamptz default now()
);

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

end $$ language plpgsql;
Expand Down
14 changes: 14 additions & 0 deletions sql/update_to_3_2_0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
do $$ begin

-- If jsonb type is available, use it for the args column
if exists (select 1 from pg_type where typname = 'jsonb') then
alter table queue_classic_jobs alter column args type jsonb using args::jsonb;
-- Otherwise, use json type for the args column if available
elsif exists (select 1 from pg_type where typname = 'json') then
alter table queue_classic_jobs alter column args type json using args::json;
-- Keep using text type for the args column, if neither is available
else
alter table queue_classic_jobs alter column args type text using args::text;
end if;

end $$ language plpgsql;

0 comments on commit 821db0d

Please sign in to comment.