Reorganise the database schema directories (#9932)

The hope here is that by moving all the schema files into synapse/storage/schema, it gets a bit easier for newcomers to navigate.

It certainly got easier for me to write a helpful README. There's more to do on that front, but I'll follow up with other PRs for that.
code_spécifique_watcha
Richard van der Hoff 4 years ago committed by GitHub
parent 8771b1337d
commit 25f43faa70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      changelog.d/9932.misc
  2. 21
      synapse/storage/databases/main/schema/full_schemas/README.md
  3. 48
      synapse/storage/prepare_database.py
  4. 37
      synapse/storage/schema/README.md
  5. 17
      synapse/storage/schema/__init__.py
  6. 0
      synapse/storage/schema/common/delta/25/00background_updates.sql
  7. 0
      synapse/storage/schema/common/delta/35/00background_updates_add_col.sql
  8. 0
      synapse/storage/schema/common/delta/58/00background_update_ordering.sql
  9. 0
      synapse/storage/schema/common/full_schemas/54/full.sql
  10. 0
      synapse/storage/schema/common/schema_version.sql
  11. 0
      synapse/storage/schema/main/delta/12/v12.sql
  12. 0
      synapse/storage/schema/main/delta/13/v13.sql
  13. 0
      synapse/storage/schema/main/delta/14/v14.sql
  14. 0
      synapse/storage/schema/main/delta/15/appservice_txns.sql
  15. 0
      synapse/storage/schema/main/delta/15/presence_indices.sql
  16. 0
      synapse/storage/schema/main/delta/15/v15.sql
  17. 0
      synapse/storage/schema/main/delta/16/events_order_index.sql
  18. 0
      synapse/storage/schema/main/delta/16/remote_media_cache_index.sql
  19. 0
      synapse/storage/schema/main/delta/16/remove_duplicates.sql
  20. 0
      synapse/storage/schema/main/delta/16/room_alias_index.sql
  21. 0
      synapse/storage/schema/main/delta/16/unique_constraints.sql
  22. 0
      synapse/storage/schema/main/delta/16/users.sql
  23. 0
      synapse/storage/schema/main/delta/17/drop_indexes.sql
  24. 0
      synapse/storage/schema/main/delta/17/server_keys.sql
  25. 0
      synapse/storage/schema/main/delta/17/user_threepids.sql
  26. 0
      synapse/storage/schema/main/delta/18/server_keys_bigger_ints.sql
  27. 0
      synapse/storage/schema/main/delta/19/event_index.sql
  28. 0
      synapse/storage/schema/main/delta/20/dummy.sql
  29. 0
      synapse/storage/schema/main/delta/20/pushers.py
  30. 0
      synapse/storage/schema/main/delta/21/end_to_end_keys.sql
  31. 0
      synapse/storage/schema/main/delta/21/receipts.sql
  32. 0
      synapse/storage/schema/main/delta/22/receipts_index.sql
  33. 0
      synapse/storage/schema/main/delta/22/user_threepids_unique.sql
  34. 0
      synapse/storage/schema/main/delta/24/stats_reporting.sql
  35. 0
      synapse/storage/schema/main/delta/25/fts.py
  36. 0
      synapse/storage/schema/main/delta/25/guest_access.sql
  37. 0
      synapse/storage/schema/main/delta/25/history_visibility.sql
  38. 0
      synapse/storage/schema/main/delta/25/tags.sql
  39. 0
      synapse/storage/schema/main/delta/26/account_data.sql
  40. 0
      synapse/storage/schema/main/delta/27/account_data.sql
  41. 0
      synapse/storage/schema/main/delta/27/forgotten_memberships.sql
  42. 0
      synapse/storage/schema/main/delta/27/ts.py
  43. 0
      synapse/storage/schema/main/delta/28/event_push_actions.sql
  44. 0
      synapse/storage/schema/main/delta/28/events_room_stream.sql
  45. 0
      synapse/storage/schema/main/delta/28/public_roms_index.sql
  46. 0
      synapse/storage/schema/main/delta/28/receipts_user_id_index.sql
  47. 0
      synapse/storage/schema/main/delta/28/upgrade_times.sql
  48. 0
      synapse/storage/schema/main/delta/28/users_is_guest.sql
  49. 0
      synapse/storage/schema/main/delta/29/push_actions.sql
  50. 0
      synapse/storage/schema/main/delta/30/alias_creator.sql
  51. 0
      synapse/storage/schema/main/delta/30/as_users.py
  52. 0
      synapse/storage/schema/main/delta/30/deleted_pushers.sql
  53. 0
      synapse/storage/schema/main/delta/30/presence_stream.sql
  54. 0
      synapse/storage/schema/main/delta/30/public_rooms.sql
  55. 0
      synapse/storage/schema/main/delta/30/push_rule_stream.sql
  56. 0
      synapse/storage/schema/main/delta/30/threepid_guest_access_tokens.sql
  57. 0
      synapse/storage/schema/main/delta/31/invites.sql
  58. 0
      synapse/storage/schema/main/delta/31/local_media_repository_url_cache.sql
  59. 0
      synapse/storage/schema/main/delta/31/pushers.py
  60. 0
      synapse/storage/schema/main/delta/31/pushers_index.sql
  61. 0
      synapse/storage/schema/main/delta/31/search_update.py
  62. 0
      synapse/storage/schema/main/delta/32/events.sql
  63. 0
      synapse/storage/schema/main/delta/32/openid.sql
  64. 0
      synapse/storage/schema/main/delta/32/pusher_throttle.sql
  65. 0
      synapse/storage/schema/main/delta/32/remove_indices.sql
  66. 0
      synapse/storage/schema/main/delta/32/reports.sql
  67. 0
      synapse/storage/schema/main/delta/33/access_tokens_device_index.sql
  68. 0
      synapse/storage/schema/main/delta/33/devices.sql
  69. 0
      synapse/storage/schema/main/delta/33/devices_for_e2e_keys.sql
  70. 0
      synapse/storage/schema/main/delta/33/devices_for_e2e_keys_clear_unknown_device.sql
  71. 0
      synapse/storage/schema/main/delta/33/event_fields.py
  72. 0
      synapse/storage/schema/main/delta/33/remote_media_ts.py
  73. 0
      synapse/storage/schema/main/delta/33/user_ips_index.sql
  74. 0
      synapse/storage/schema/main/delta/34/appservice_stream.sql
  75. 0
      synapse/storage/schema/main/delta/34/cache_stream.py
  76. 0
      synapse/storage/schema/main/delta/34/device_inbox.sql
  77. 0
      synapse/storage/schema/main/delta/34/push_display_name_rename.sql
  78. 0
      synapse/storage/schema/main/delta/34/received_txn_purge.py
  79. 0
      synapse/storage/schema/main/delta/35/contains_url.sql
  80. 0
      synapse/storage/schema/main/delta/35/device_outbox.sql
  81. 0
      synapse/storage/schema/main/delta/35/device_stream_id.sql
  82. 0
      synapse/storage/schema/main/delta/35/event_push_actions_index.sql
  83. 0
      synapse/storage/schema/main/delta/35/public_room_list_change_stream.sql
  84. 0
      synapse/storage/schema/main/delta/35/stream_order_to_extrem.sql
  85. 0
      synapse/storage/schema/main/delta/36/readd_public_rooms.sql
  86. 0
      synapse/storage/schema/main/delta/37/remove_auth_idx.py
  87. 0
      synapse/storage/schema/main/delta/37/user_threepids.sql
  88. 0
      synapse/storage/schema/main/delta/38/postgres_fts_gist.sql
  89. 0
      synapse/storage/schema/main/delta/39/appservice_room_list.sql
  90. 0
      synapse/storage/schema/main/delta/39/device_federation_stream_idx.sql
  91. 0
      synapse/storage/schema/main/delta/39/event_push_index.sql
  92. 0
      synapse/storage/schema/main/delta/39/federation_out_position.sql
  93. 0
      synapse/storage/schema/main/delta/39/membership_profile.sql
  94. 0
      synapse/storage/schema/main/delta/40/current_state_idx.sql
  95. 0
      synapse/storage/schema/main/delta/40/device_inbox.sql
  96. 0
      synapse/storage/schema/main/delta/40/device_list_streams.sql
  97. 0
      synapse/storage/schema/main/delta/40/event_push_summary.sql
  98. 0
      synapse/storage/schema/main/delta/40/pushers.sql
  99. 0
      synapse/storage/schema/main/delta/41/device_list_stream_idx.sql
  100. 0
      synapse/storage/schema/main/delta/41/device_outbound_index.sql
  101. Some files were not shown because too many files have changed in this diff Show More

@ -0,0 +1 @@
Move database schema files into a common directory.

@ -1,21 +0,0 @@
# Synapse Database Schemas
These schemas are used as a basis to create brand new Synapse databases, on both
SQLite3 and Postgres.
## Building full schema dumps
If you want to recreate these schemas, they need to be made from a database that
has had all background updates run.
To do so, use `scripts-dev/make_full_schema.sh`. This will produce new
`full.sql.postgres ` and `full.sql.sqlite` files.
Ensure postgres is installed and your user has the ability to run bash commands
such as `createdb`, then call
./scripts-dev/make_full_schema.sh -p postgres_username -o output_dir/
There are currently two folders with full-schema snapshots. `16` is a snapshot
from 2015, for historical reference. The other contains the most recent full
schema snapshot.

@ -26,16 +26,13 @@ from synapse.config.homeserver import HomeServerConfig
from synapse.storage.database import LoggingDatabaseConnection
from synapse.storage.engines import BaseDatabaseEngine
from synapse.storage.engines.postgres import PostgresEngine
from synapse.storage.schema import SCHEMA_VERSION
from synapse.storage.types import Cursor
logger = logging.getLogger(__name__)
# Remember to update this number every time a change is made to database
# schema files, so the users will be informed on server restarts.
SCHEMA_VERSION = 59
dir_path = os.path.abspath(os.path.dirname(__file__))
schema_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "schema")
class PrepareDatabaseException(Exception):
@ -167,7 +164,14 @@ def _setup_new_database(
Example directory structure:
schema/
schema/
common/
delta/
...
full_schemas/
11/
foo.sql
main/
delta/
...
full_schemas/
@ -175,15 +179,14 @@ def _setup_new_database(
test.sql
...
11/
foo.sql
bar.sql
...
In the example foo.sql and bar.sql would be run, and then any delta files
for versions strictly greater than 11.
Note: we apply the full schemas and deltas from the top level `schema/`
folder as well those in the data stores specified.
Note: we apply the full schemas and deltas from the `schema/common`
folder as well those in the databases specified.
Args:
cur: a database cursor
@ -195,12 +198,12 @@ def _setup_new_database(
# configured to our liking.
database_engine.check_new_database(cur)
current_dir = os.path.join(dir_path, "schema", "full_schemas")
full_schemas_dir = os.path.join(schema_path, "common", "full_schemas")
# First we find the highest full schema version we have
valid_versions = []
for filename in os.listdir(current_dir):
for filename in os.listdir(full_schemas_dir):
try:
ver = int(filename)
except ValueError:
@ -218,15 +221,13 @@ def _setup_new_database(
logger.debug("Initialising schema v%d", max_current_ver)
# Now lets find all the full schema files, both in the global schema and
# in data store schemas.
directories = [os.path.join(current_dir, str(max_current_ver))]
# Now let's find all the full schema files, both in the common schema and
# in database schemas.
directories = [os.path.join(full_schemas_dir, str(max_current_ver))]
directories.extend(
os.path.join(
dir_path,
"databases",
schema_path,
database,
"schema",
"full_schemas",
str(max_current_ver),
)
@ -357,6 +358,9 @@ def _upgrade_existing_database(
check_database_before_upgrade(cur, database_engine, config)
start_ver = current_version
# if we got to this schema version by running a full_schema rather than a series
# of deltas, we should not run the deltas for this version.
if not upgraded:
start_ver += 1
@ -385,12 +389,10 @@ def _upgrade_existing_database(
# directories for schema updates.
# First we find the directories to search in
delta_dir = os.path.join(dir_path, "schema", "delta", str(v))
delta_dir = os.path.join(schema_path, "common", "delta", str(v))
directories = [delta_dir]
for database in databases:
directories.append(
os.path.join(dir_path, "databases", database, "schema", "delta", str(v))
)
directories.append(os.path.join(schema_path, database, "delta", str(v)))
# Used to check if we have any duplicate file names
file_name_counter = Counter() # type: CounterType[str]
@ -621,8 +623,8 @@ def _get_or_create_schema_state(
txn: Cursor, database_engine: BaseDatabaseEngine
) -> Optional[Tuple[int, List[str], bool]]:
# Bluntly try creating the schema_version tables.
schema_path = os.path.join(dir_path, "schema", "schema_version.sql")
executescript(txn, schema_path)
sql_path = os.path.join(schema_path, "common", "schema_version.sql")
executescript(txn, sql_path)
txn.execute("SELECT version, upgraded FROM schema_version")
row = txn.fetchone()

@ -0,0 +1,37 @@
# Synapse Database Schemas
This directory contains the schema files used to build Synapse databases.
Synapse supports splitting its datastore across multiple physical databases (which can
be useful for large installations), and the schema files are therefore split according
to the logical database they are apply to.
At the time of writing, the following "logical" databases are supported:
* `state` - used to store Matrix room state (more specifically, `state_groups`,
their relationships and contents.)
* `main` - stores everything else.
Addionally, the `common` directory contains schema files for tables which must be
present on *all* physical databases.
## Full schema dumps
In the `full_schemas` directories, only the most recently-numbered snapshot is useful
(`54` at the time of writing). Older snapshots (eg, `16`) are present for historical
reference only.
## Building full schema dumps
If you want to recreate these schemas, they need to be made from a database that
has had all background updates run.
To do so, use `scripts-dev/make_full_schema.sh`. This will produce new
`full.sql.postgres` and `full.sql.sqlite` files.
Ensure postgres is installed, then run:
./scripts-dev/make_full_schema.sh -p postgres_username -o output_dir/
NB at the time of writing, this script predates the split into separate `state`/`main`
databases so will require updates to handle that correctly.

@ -0,0 +1,17 @@
# Copyright 2021 The Matrix.org Foundation C.I.C.
#
# 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.
# Remember to update this number every time a change is made to database
# schema files, so the users will be informed on server restarts.
SCHEMA_VERSION = 59

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save