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 9fb7821
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
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 9fb7821

Please sign in to comment.