summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2016-10-31 14:40:14 +0000
committerErik Johnston <erik@matrix.org>2016-10-31 14:57:26 +0000
commit2fba1605893be33d7ca087b04324f9bf37ae0a7a (patch)
treeb40a26a335b8a09b62d317f2ff6e9b3c2f50a6a9
parentAdd federation profile endpoints (diff)
downloadsynapse-2fba1605893be33d7ca087b04324f9bf37ae0a7a.tar.xz
Migrate old profile data
-rw-r--r--synapse/handlers/profile.py24
-rw-r--r--synapse/storage/schema/delta/38/profile.py104
-rw-r--r--synapse/storage/schema/delta/38/profile.sql26
3 files changed, 128 insertions, 26 deletions
diff --git a/synapse/handlers/profile.py b/synapse/handlers/profile.py
index 61649aa62e..c1f6d88fa2 100644
--- a/synapse/handlers/profile.py
+++ b/synapse/handlers/profile.py
@@ -81,6 +81,18 @@ class ProfileHandler(BaseHandler):
             target_user.localpart, new_displayname
         )
 
+        if new_displayname:
+            content = {"rows": [{
+                "display_name": new_displayname
+            }]}
+        else:
+            # TODO: Delete in this case
+            content = {}
+
+        yield self.store.update_profile_key(
+            target_user.to_string(), "default", "m.display_name", content
+        )
+
         yield self._update_join_states(requester)
 
     @defer.inlineCallbacks
@@ -124,6 +136,18 @@ class ProfileHandler(BaseHandler):
             target_user.localpart, new_avatar_url
         )
 
+        if new_avatar_url:
+            content = {"rows": [{
+                "url": new_avatar_url
+            }]}
+        else:
+            # TODO: Delete in this case
+            content = {}
+
+        yield self.store.update_profile_key(
+            target_user.to_string(), "default", "m.avatar_url", content
+        )
+
         yield self._update_join_states(requester)
 
     @defer.inlineCallbacks
diff --git a/synapse/storage/schema/delta/38/profile.py b/synapse/storage/schema/delta/38/profile.py
new file mode 100644
index 0000000000..7802fb1c31
--- /dev/null
+++ b/synapse/storage/schema/delta/38/profile.py
@@ -0,0 +1,104 @@
+# Copyright 2016 OpenMarket Ltd
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from synapse.storage.prepare_database import get_statements
+from synapse.storage.engines import PostgresEngine
+
+import logging
+import ujson
+
+logger = logging.getLogger(__name__)
+
+CREATE_TABLE = """
+CREATE TABLE profiles_extended (
+    stream_id BIGINT NOT NULL,
+    user_id TEXT NOT NULL,
+    persona TEXT NOT NULL,
+    key TEXT NOT NULL,
+    content TEXT NOT NULL
+);
+
+CREATE INDEX profiles_extended_tuple ON profiles_extended(
+    user_id, persona, key, stream_id
+);
+"""
+
+UPDATE_DISPLAY_NAME = """
+INSERT INTO profiles_extended (stream_id, user_id, persona, key, content)
+SELECT
+    1,
+    '@' || user_id || ':' || %s,
+    'm.display_name',
+    '{"rows":["display_name":' || to_json(displayname) || '}]}'
+FROM profiles WHERE displayname IS NOT NULL
+"""
+
+UPDATE_AVATAR_URL = """
+INSERT INTO profiles_extended (stream_id, user_id, persona, key, content)
+SELECT
+    1,
+    '@' || user_id || ':' || %s,
+    'm.avatar_url',
+    '{"rows":[{"avatar_url":' || to_json(avatar_url) || '}]}'
+FROM profiles WHERE avatar_url IS NOT NULL
+"""
+
+
+def run_create(cur, database_engine, *args, **kwargs):
+    for statement in get_statements(CREATE_TABLE.splitlines()):
+        cur.execute(statement)
+
+
+def run_upgrade(cur, database_engine, config, *args, **kwargs):
+    if isinstance(database_engine, PostgresEngine):
+        cur.execute(UPDATE_DISPLAY_NAME, (config.server_name,))
+        cur.execute(UPDATE_AVATAR_URL, (config.server_name,))
+    else:
+        cur.execute(
+            "SELECT user_id, displayname FROM profiles WHERE displayname IS NOT NULL"
+        )
+        displaynames = []
+        for user_id, displayname in cur.fetchall():
+            displaynames.append((
+                1,
+                "@%s:%s" % (user_id, config.server_name),
+                "default",
+                "m.display_name",
+                ujson.dumps({"rows": [{"display_name": displayname}]}),
+            ))
+        cur.executemany(
+            "INSERT INTO profiles_extended"
+            " (stream_id, user_id, persona, key, content)"
+            " VALUES (?,?,?,?,?)",
+            displaynames
+        )
+
+        cur.execute(
+            "SELECT user_id, avatar_url FROM profiles WHERE avatar_url IS NOT NULL"
+        )
+        avatar_urls = []
+        for user_id, avatar_url in cur.fetchall():
+            avatar_urls.append((
+                1,
+                "@%s:%s" % (user_id, config.server_name),
+                "default",
+                "m.avatar_url",
+                ujson.dumps({"rows": [{"avatar_url": avatar_url}]}),
+            ))
+        cur.executemany(
+            "INSERT INTO profiles_extended"
+            " (stream_id, user_id, persona, key, content)"
+            " VALUES (?,?,?,?,?)",
+            avatar_urls
+        )
diff --git a/synapse/storage/schema/delta/38/profile.sql b/synapse/storage/schema/delta/38/profile.sql
deleted file mode 100644
index 6a51ef184c..0000000000
--- a/synapse/storage/schema/delta/38/profile.sql
+++ /dev/null
@@ -1,26 +0,0 @@
-/* Copyright 2016 OpenMarket Ltd
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-
-
-CREATE TABLE profiles_extended (
-    stream_id BIGINT NOT NULL,
-    user_id TEXT NOT NULL,
-    persona TEXT NOT NULL,
-    key TEXT NOT NULL,
-    content TEXT NOT NULL
-);
-
-CREATE INDEX profiles_extended_tuple ON profiles_extended(user_id, persona, key, stream_id);