@@ -96,6 +96,60 @@ Ensure postgres is installed, then run:
96
96
NB at the time of writing, this script predates the split into separate ` state ` /` main `
97
97
databases so will require updates to handle that correctly.
98
98
99
+ ## Delta files
100
+
101
+ Delta files define the steps required to upgrade the database from an earlier version.
102
+ They can be written as either a file containing a series of SQL statements, or a Python
103
+ module.
104
+
105
+ Synapse remembers which delta files it has applied to a database (they are stored in the
106
+ ` applied_schema_deltas ` table) and will not re-apply them (even if a given file is
107
+ subsequently updated).
108
+
109
+ Delta files should be placed in a directory named ` synapse/storage/schema/<database>/delta/<version>/ ` .
110
+ They are applied in alphanumeric order, so by convention the first two characters
111
+ of the filename should be an integer such as ` 01 ` , to put the file in the right order.
112
+
113
+ ### SQL delta files
114
+
115
+ These should be named ` *.sql ` , or — for changes which should only be applied for a
116
+ given database engine — ` *.sql.posgres ` or ` *.sql.sqlite ` . For example, a delta which
117
+ adds a new column to the ` foo ` table might be called ` 01add_bar_to_foo.sql ` .
118
+
119
+ Note that our SQL parser is a bit simple - it understands comments (` -- ` and ` /*...*/ ` ),
120
+ but complex statements which require a ` ; ` in the middle of them (such as `CREATE
121
+ TRIGGER`) are beyond it and you'll have to use a Python delta file.
122
+
123
+ ### Python delta files
124
+
125
+ For more flexibility, a delta file can take the form of a python module. These should
126
+ be named ` *.py ` . Note that database-engine-specific modules are not supported here –
127
+ instead you can write ` if isinstance(database_engine, PostgresEngine) ` or similar.
128
+
129
+ A Python delta module should define either or both of the following functions:
130
+
131
+ ``` python
132
+ import synapse.config.homeserver
133
+ import synapse.storage.engines
134
+ import synapse.storage.types
135
+
136
+
137
+ def run_create (
138
+ cur : synapse.storage.types.Cursor,
139
+ database_engine : synapse.storage.engines.BaseDatabaseEngine,
140
+ ) -> None :
141
+ """ Called whenever an existing or new database is to be upgraded"""
142
+ ...
143
+
144
+ def run_upgrade (
145
+ cur : synapse.storage.types.Cursor,
146
+ database_engine : synapse.storage.engines.BaseDatabaseEngine,
147
+ config : synapse.config.homeserver.HomeServerConfig,
148
+ ) -> None :
149
+ """ Called whenever an existing database is to be upgraded."""
150
+ ...
151
+ ```
152
+
99
153
## Boolean columns
100
154
101
155
Boolean columns require special treatment, since SQLite treats booleans the
0 commit comments