From acab5c32e8e9fad8da5818ef8556791691c25360 Mon Sep 17 00:00:00 2001 From: clokep Date: Fri, 6 Oct 2023 11:24:25 +0000 Subject: deploy: 694802eecdfe18544be5252605bd427e3a5a2b2e --- develop/development/database_schema.html | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'develop/development/database_schema.html') diff --git a/develop/development/database_schema.html b/develop/development/database_schema.html index a2ef1e943d..8dffa04d7a 100644 --- a/develop/development/database_schema.html +++ b/develop/development/database_schema.html @@ -272,6 +272,55 @@ def run_upgrade( """Called whenever an existing database is to be upgraded.""" ... +

Background updates

+

It is sometimes appropriate to perform database migrations as part of a background +process (instead of blocking Synapse until the migration is done). In particular, +this is useful for migrating data when adding new columns or tables.

+

Pending background updates stored in the background_updates table and are denoted +by a unique name, the current status (stored in JSON), and some dependency information:

+ +

A new background updates needs to be added to the background_updates table:

+
INSERT INTO background_updates (ordering, update_name, depends_on, progress_json) VALUES
+  (7706, 'my_background_update', 'a_previous_background_update' '{}');
+
+

And then needs an associated handler in the appropriate datastore:

+
self.db_pool.updates.register_background_update_handler(
+    "my_background_update",
+    update_handler=self._my_background_update,
+)
+
+

There are a few types of updates that can be performed, see the BackgroundUpdater:

+ +

For register_background_update_handler, the generic handler must track progress +and then finalize the background update:

+
async def _my_background_update(self, progress: JsonDict, batch_size: int) -> int:
+    def _do_something(txn: LoggingTransaction) -> int:
+        ...
+        self.db_pool.updates._background_update_progress_txn(
+            txn, "my_background_update", {"last_processed": last_processed}
+        )
+        return last_processed - prev_last_processed
+
+    num_processed = await self.db_pool.runInteraction("_do_something", _do_something)
+    await self.db_pool.updates._end_background_update("my_background_update")
+
+    return num_processed
+
+

Synapse will attempt to rate-limit how often background updates are run via the +given batch-size and the returned number of processed entries (and how long the +function took to run). See +background update controller callbacks.

Boolean columns

Boolean columns require special treatment, since SQLite treats booleans the same as integers.

-- cgit 1.5.1