summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erikj@jki.re>2017-06-13 11:38:44 +0100
committerGitHub <noreply@github.com>2017-06-13 11:38:44 +0100
commitfe9dc522d4639434c21a22a8276a77f1a1443de9 (patch)
tree3c46fc6f932be2d4aff41e63d8f6ab9e1c349750
parentMerge pull request #2274 from matrix-org/erikj/cache_is_host_joined (diff)
parentFix up sql (diff)
downloadsynapse-fe9dc522d4639434c21a22a8276a77f1a1443de9.tar.xz
Merge pull request #2278 from matrix-org/erikj/fix_user_dir
Fix user dir to not assume existence of user
-rw-r--r--synapse/storage/user_directory.py65
1 files changed, 41 insertions, 24 deletions
diff --git a/synapse/storage/user_directory.py b/synapse/storage/user_directory.py
index 0b874e0227..67b14cf26a 100644
--- a/synapse/storage/user_directory.py
+++ b/synapse/storage/user_directory.py
@@ -151,44 +151,61 @@ class UserDirectoryStore(SQLBaseStore):
 
     def update_profile_in_user_dir(self, user_id, display_name, avatar_url):
         def _update_profile_in_user_dir_txn(txn):
-            self._simple_update_one_txn(
+            new_entry = self._simple_upsert_txn(
                 txn,
                 table="user_directory",
                 keyvalues={"user_id": user_id},
-                updatevalues={"display_name": display_name, "avatar_url": avatar_url},
+                values={"display_name": display_name, "avatar_url": avatar_url},
+                lock=False,  # We're only inserter
             )
 
             if isinstance(self.database_engine, PostgresEngine):
                 # We weight the loclpart most highly, then display name and finally
                 # server name
-                sql = """
-                    UPDATE user_directory_search
-                    SET vector = setweight(to_tsvector('english', ?), 'A')
-                        || setweight(to_tsvector('english', ?), 'D')
-                        || setweight(to_tsvector('english', COALESCE(?, '')), 'B')
-                    WHERE user_id = ?
-                """
-                args = (
-                    get_localpart_from_id(user_id), get_domain_from_id(user_id),
-                    display_name,
-                    user_id,
-                )
+                if new_entry:
+                    sql = """
+                        INSERT INTO user_directory_search(user_id, vector)
+                        VALUES (?,
+                            setweight(to_tsvector('english', ?), 'A')
+                            || setweight(to_tsvector('english', ?), 'D')
+                            || setweight(to_tsvector('english', COALESCE(?, '')), 'B')
+                        )
+                    """
+                    txn.execute(
+                        sql,
+                        (
+                            user_id, get_localpart_from_id(user_id),
+                            get_domain_from_id(user_id), display_name,
+                        )
+                    )
+                else:
+                    sql = """
+                        UPDATE user_directory_search
+                        SET vector = setweight(to_tsvector('english', ?), 'A')
+                            || setweight(to_tsvector('english', ?), 'D')
+                            || setweight(to_tsvector('english', COALESCE(?, '')), 'B')
+                        WHERE user_id = ?
+                    """
+                    txn.execute(
+                        sql,
+                        (
+                            get_localpart_from_id(user_id), get_domain_from_id(user_id),
+                            display_name, user_id,
+                        )
+                    )
             elif isinstance(self.database_engine, Sqlite3Engine):
-                sql = """
-                    UPDATE user_directory_search
-                    set value = ?
-                    WHERE user_id = ?
-                """
-                args = (
-                    "%s %s" % (user_id, display_name,) if display_name else user_id,
-                    user_id,
+                value = "%s %s" % (user_id, display_name,) if display_name else user_id
+                self._simple_upsert_txn(
+                    txn,
+                    table="user_directory_search",
+                    keyvalues={"user_id": user_id},
+                    values={"value": value},
+                    lock=False,  # We're only inserter
                 )
             else:
                 # This should be unreachable.
                 raise Exception("Unrecognized database engine")
 
-            txn.execute(sql, args)
-
             txn.call_after(self.get_user_in_directory.invalidate, (user_id,))
 
         return self.runInteraction(