Skip to content
Snippets Groups Projects
Unverified Commit 091c4a68 authored by Cal Herries's avatar Cal Herries Committed by GitHub
Browse files

Fix null characters in JSON breaking CardRevisionAddType for postgres (#41584)

parent 470df0bf
No related branches found
No related tags found
No related merge requests found
......@@ -1000,14 +1000,16 @@
(define-reversible-migration CardRevisionAddType
(case (mdb.connection/db-type)
:postgres
;; postgres doesn't allow `\u0000` in text when converting to jsonb, so we need to remove them before we can
;; parse the json. We use negative look behind to avoid matching `\\u0000` (metabase#40835)
(t2/query ["UPDATE revision
SET object = jsonb_set(
object::jsonb, '{type}',
SET object = replace(jsonb_set(
(regexp_replace(object, '(?<!\\\\)\\\\u0000', '286b707c-e895-4cd3-acfc-569147f54371', 'g'))::jsonb, '{type}',
to_jsonb(CASE
WHEN (object::jsonb->>'dataset')::boolean THEN 'model'
WHEN ((regexp_replace(object, '(?<!\\\\)\\\\u0000', '286b707c-e895-4cd3-acfc-569147f54371', 'g'))::jsonb->>'dataset')::boolean THEN 'model'
ELSE 'question'
END)::jsonb, true)
WHERE model = 'Card' AND (object::jsonb->>'dataset') IS NOT NULL;"])
END)::jsonb, true)::text, '286b707c-e895-4cd3-acfc-569147f54371', '\\u0000')
WHERE model = 'Card' AND ((regexp_replace(object, '(?<!\\\\)\\\\u0000', '286b707c-e895-4cd3-acfc-569147f54371', 'g'))::jsonb->>'dataset') IS NOT NULL;"])
:mysql
(t2/query ["UPDATE revision
......
......@@ -1600,6 +1600,32 @@
(is (not (contains? card-revision-object "type")))
(is (not (contains? model-revision-object "type"))))))))
(deftest card-revision-add-type-null-character-test
(testing "CardRevisionAddType migration works even if there's a null character in revision.object (metabase#40835)")
(impl/test-migrations "v49.2024-01-22T11:52:00" [migrate!]
(let [user-id (:id (new-instance-with-default :core_user))
db-id (:id (new-instance-with-default :metabase_database))
card (new-instance-with-default :report_card {:dataset false :creator_id user-id :database_id db-id})
viz-settings "{\"table.pivot_column\":\"\u0000..\\u0000\"}" ; note the escaped and unescaped null characters
card-revision-id (:id (new-instance-with-default :revision
{:object (json/generate-string
(assoc (dissoc card :type)
:visualization_settings viz-settings))
:model "Card"
:model_id (:id card)
:user_id user-id}))]
(testing "sanity check revision object"
(let [card-revision-object (t2/select-one-fn (comp json/parse-string :object) :revision card-revision-id)]
(testing "doesn't have type"
(is (not (contains? card-revision-object "type"))))))
(testing "after migration card revisions should have type"
(migrate!)
(let [card-revision-object (t2/select-one-fn (comp json/parse-string :object) :revision card-revision-id)]
(is (= "question" (get card-revision-object "type")))
(testing "original visualization_settings should be preserved"
(is (= viz-settings
(get card-revision-object "visualization_settings")))))))))
(deftest delete-scan-field-values-trigger-test
(testing "We should delete the triggers for DBs that are configured not to scan their field values\n"
(impl/test-migrations "v49.2024-04-09T10:00:03" [migrate!]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment