From 9fb78218eb958f431a56f545e501274d0859ef5d Mon Sep 17 00:00:00 2001 From: Clemens Gruber Date: Thu, 14 May 2015 17:19:45 +0200 Subject: [PATCH] Change json datatype to jsonb, if available. Also update existing text columns to jsonb and json. Signed-off-by: Clemens Gruber --- changelog | 2 ++ .../templates/update_queue_classic_3_2_0.rb | 8 ++++++++ lib/queue_classic/setup.rb | 7 +++++++ sql/create_table.sql | 10 ++++++---- sql/update_to_3_2_0.sql | 14 ++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 lib/generators/queue_classic/templates/update_queue_classic_3_2_0.rb create mode 100644 sql/update_to_3_2_0.sql diff --git a/changelog b/changelog index 0d405103..c607d4a0 100644 --- a/changelog +++ b/changelog @@ -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 diff --git a/lib/generators/queue_classic/templates/update_queue_classic_3_2_0.rb b/lib/generators/queue_classic/templates/update_queue_classic_3_2_0.rb new file mode 100644 index 00000000..9bce993e --- /dev/null +++ b/lib/generators/queue_classic/templates/update_queue_classic_3_2_0.rb @@ -0,0 +1,8 @@ +class UpdateQueueClassic320 < ActiveRecord::Migration + def self.up + QC::Setup.update_to_3_2_0 + end + + def self.down + end +end diff --git a/lib/queue_classic/setup.rb b/lib/queue_classic/setup.rb index 89d1480c..04670f66 100644 --- a/lib/queue_classic/setup.rb +++ b/lib/queue_classic/setup.rb @@ -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) @@ -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 @@ -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 diff --git a/sql/create_table.sql b/sql/create_table.sql index 52a24797..8ab53a39 100644 --- a/sql/create_table.sql +++ b/sql/create_table.sql @@ -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; diff --git a/sql/update_to_3_2_0.sql b/sql/update_to_3_2_0.sql new file mode 100644 index 00000000..870cb57f --- /dev/null +++ b/sql/update_to_3_2_0.sql @@ -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;