From 0c5da1cc476305b304fcecce207b270d13230be7 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Wed, 25 Jun 2014 18:55:26 -0700 Subject: [PATCH] Schema migration for identity keys. --- src/main/resources/migrations.xml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/resources/migrations.xml b/src/main/resources/migrations.xml index 1c9bd9e4e..dd8c2eb66 100644 --- a/src/main/resources/migrations.xml +++ b/src/main/resources/migrations.xml @@ -142,4 +142,29 @@ + + + CREATE OR REPLACE FUNCTION "custom_json_object_set_key"( + "json" json, + "key_to_set" TEXT, + "value_to_set" anyelement + ) + RETURNS json + LANGUAGE sql + IMMUTABLE + STRICT + AS $function$ + SELECT COALESCE( + (SELECT ('{' || string_agg(to_json("key") || ':' || "value", ',') || '}') + FROM (SELECT * + FROM json_each("json") + WHERE "key" <> "key_to_set" + UNION ALL + SELECT "key_to_set", to_json("value_to_set")) AS "fields"), + '{}' + )::json + $function$; + UPDATE accounts SET data = custom_json_object_set_key(data, 'identityKey', k.identity_key) FROM keys k WHERE (data->>'identityKey')::text is null AND k.number = data->>'number' AND k.last_resort = 1; + UPDATE accounts SET data = custom_json_object_set_key(data, 'identityKey', k.identity_key) FROM keys k WHERE (data->>'identityKey')::text is null AND k.number = data->>'number'; +