7709: Allow null for empty serialized fields.
authorTom Clegg <tom@curoverse.com>
Sun, 19 Mar 2017 06:46:00 +0000 (02:46 -0400)
committerTom Clegg <tom@curoverse.com>
Mon, 27 Mar 2017 16:49:32 +0000 (12:49 -0400)
services/api/app/models/arvados_model.rb
services/api/db/migrate/20170319063406_serialized_columns_accept_null.rb [new file with mode: 0644]
services/api/db/structure.sql

index 23bb71a74b4eb1cb3dcb15799b7d00f16405a612..a5aa6a42c4d36cc6b10e43271aa289e48c07b94d 100644 (file)
@@ -541,8 +541,15 @@ class ArvadosModel < ActiveRecord::Base
   end
 
   def self.where_serialized(colname, value)
-    sorted = deep_sort_hash(value)
-    where("#{colname.to_s} IN (?)", [sorted.to_yaml, SafeJSON.dump(sorted)])
+    if value.empty?
+      # rails4 stores as null, rails3 stored as serialized [] or {}
+      sql = "#{colname.to_s} is null or #{colname.to_s} IN (?)"
+      sorted = value
+    else
+      sql = "#{colname.to_s} IN (?)"
+      sorted = deep_sort_hash(value)
+    end
+    where(sql, [sorted.to_yaml, SafeJSON.dump(sorted)])
   end
 
   Serializer = {
diff --git a/services/api/db/migrate/20170319063406_serialized_columns_accept_null.rb b/services/api/db/migrate/20170319063406_serialized_columns_accept_null.rb
new file mode 100644 (file)
index 0000000..564586e
--- /dev/null
@@ -0,0 +1,5 @@
+class SerializedColumnsAcceptNull < ActiveRecord::Migration
+  def change
+    change_column :api_client_authorizations, :scopes, :text, null: true, default: '["all"]'
+  end
+end
index 87b657543b6c851232e96defd117fc1f6991c8a6..015db6ffb9a11b8a56c16e55d835b304547473be 100644 (file)
@@ -44,9 +44,7 @@ CREATE TABLE api_client_authorizations (
     created_at timestamp without time zone NOT NULL,
     updated_at timestamp without time zone NOT NULL,
     default_owner_uuid character varying(255),
-    scopes text DEFAULT '---
-- all
-'::text NOT NULL,
+    scopes text DEFAULT '["all"]'::text,
     uuid character varying(255) NOT NULL
 );
 
@@ -2744,4 +2742,7 @@ INSERT INTO schema_migrations (version) VALUES ('20170105160302');
 
 INSERT INTO schema_migrations (version) VALUES ('20170216170823');
 
-INSERT INTO schema_migrations (version) VALUES ('20170301225558');
\ No newline at end of file
+INSERT INTO schema_migrations (version) VALUES ('20170301225558');
+
+INSERT INTO schema_migrations (version) VALUES ('20170319063406');
+