prepare_database.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 - 2016 OpenMarket Ltd
  3. # Copyright 2018 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import imp
  17. import logging
  18. import os
  19. import re
  20. from collections import Counter
  21. from typing import Optional, TextIO
  22. import attr
  23. from synapse.config.homeserver import HomeServerConfig
  24. from synapse.storage.engines import BaseDatabaseEngine
  25. from synapse.storage.engines.postgres import PostgresEngine
  26. from synapse.storage.types import Connection, Cursor
  27. from synapse.types import Collection
  28. logger = logging.getLogger(__name__)
  29. # Remember to update this number every time a change is made to database
  30. # schema files, so the users will be informed on server restarts.
  31. # XXX: If you're about to bump this to 59 (or higher) please create an update
  32. # that drops the unused `cache_invalidation_stream` table, as per #7436!
  33. # XXX: Also add an update to drop `account_data_max_stream_id` as per #7656!
  34. SCHEMA_VERSION = 58
  35. dir_path = os.path.abspath(os.path.dirname(__file__))
  36. class PrepareDatabaseException(Exception):
  37. pass
  38. class UpgradeDatabaseException(PrepareDatabaseException):
  39. pass
  40. OUTDATED_SCHEMA_ON_WORKER_ERROR = (
  41. "Expected database schema version %i but got %i: run the main synapse process to "
  42. "upgrade the database schema before starting worker processes."
  43. )
  44. EMPTY_DATABASE_ON_WORKER_ERROR = (
  45. "Uninitialised database: run the main synapse process to prepare the database "
  46. "schema before starting worker processes."
  47. )
  48. UNAPPLIED_DELTA_ON_WORKER_ERROR = (
  49. "Database schema delta %s has not been applied: run the main synapse process to "
  50. "upgrade the database schema before starting worker processes."
  51. )
  52. def prepare_database(
  53. db_conn: Connection,
  54. database_engine: BaseDatabaseEngine,
  55. config: Optional[HomeServerConfig],
  56. databases: Collection[str] = ["main", "state"],
  57. ):
  58. """Prepares a physical database for usage. Will either create all necessary tables
  59. or upgrade from an older schema version.
  60. If `config` is None then prepare_database will assert that no upgrade is
  61. necessary, *or* will create a fresh database if the database is empty.
  62. Args:
  63. db_conn:
  64. database_engine:
  65. config :
  66. application config, or None if we are connecting to an existing
  67. database which we expect to be configured already
  68. databases: The name of the databases that will be used
  69. with this physical database. Defaults to all databases.
  70. """
  71. try:
  72. cur = db_conn.cursor()
  73. # sqlite does not automatically start transactions for DDL / SELECT statements,
  74. # so we start one before running anything. This ensures that any upgrades
  75. # are either applied completely, or not at all.
  76. #
  77. # (psycopg2 automatically starts a transaction as soon as we run any statements
  78. # at all, so this is redundant but harmless there.)
  79. cur.execute("BEGIN TRANSACTION")
  80. logger.info("%r: Checking existing schema version", databases)
  81. version_info = _get_or_create_schema_state(cur, database_engine)
  82. if version_info:
  83. user_version, delta_files, upgraded = version_info
  84. logger.info(
  85. "%r: Existing schema is %i (+%i deltas)",
  86. databases,
  87. user_version,
  88. len(delta_files),
  89. )
  90. # config should only be None when we are preparing an in-memory SQLite db,
  91. # which should be empty.
  92. if config is None:
  93. raise ValueError(
  94. "config==None in prepare_database, but databse is not empty"
  95. )
  96. # if it's a worker app, refuse to upgrade the database, to avoid multiple
  97. # workers doing it at once.
  98. if config.worker_app is not None and user_version != SCHEMA_VERSION:
  99. raise UpgradeDatabaseException(
  100. OUTDATED_SCHEMA_ON_WORKER_ERROR % (SCHEMA_VERSION, user_version)
  101. )
  102. _upgrade_existing_database(
  103. cur,
  104. user_version,
  105. delta_files,
  106. upgraded,
  107. database_engine,
  108. config,
  109. databases=databases,
  110. )
  111. else:
  112. logger.info("%r: Initialising new database", databases)
  113. # if it's a worker app, refuse to upgrade the database, to avoid multiple
  114. # workers doing it at once.
  115. if config and config.worker_app is not None:
  116. raise UpgradeDatabaseException(EMPTY_DATABASE_ON_WORKER_ERROR)
  117. _setup_new_database(cur, database_engine, databases=databases)
  118. # check if any of our configured dynamic modules want a database
  119. if config is not None:
  120. _apply_module_schemas(cur, database_engine, config)
  121. cur.close()
  122. db_conn.commit()
  123. except Exception:
  124. db_conn.rollback()
  125. raise
  126. def _setup_new_database(cur, database_engine, databases):
  127. """Sets up the physical database by finding a base set of "full schemas" and
  128. then applying any necessary deltas, including schemas from the given data
  129. stores.
  130. The "full_schemas" directory has subdirectories named after versions. This
  131. function searches for the highest version less than or equal to
  132. `SCHEMA_VERSION` and executes all .sql files in that directory.
  133. The function will then apply all deltas for all versions after the base
  134. version.
  135. Example directory structure:
  136. schema/
  137. delta/
  138. ...
  139. full_schemas/
  140. 3/
  141. test.sql
  142. ...
  143. 11/
  144. foo.sql
  145. bar.sql
  146. ...
  147. In the example foo.sql and bar.sql would be run, and then any delta files
  148. for versions strictly greater than 11.
  149. Note: we apply the full schemas and deltas from the top level `schema/`
  150. folder as well those in the data stores specified.
  151. Args:
  152. cur (Cursor): a database cursor
  153. database_engine (DatabaseEngine)
  154. databases (list[str]): The names of the databases to instantiate
  155. on the given physical database.
  156. """
  157. # We're about to set up a brand new database so we check that its
  158. # configured to our liking.
  159. database_engine.check_new_database(cur)
  160. current_dir = os.path.join(dir_path, "schema", "full_schemas")
  161. directory_entries = os.listdir(current_dir)
  162. # First we find the highest full schema version we have
  163. valid_versions = []
  164. for filename in directory_entries:
  165. try:
  166. ver = int(filename)
  167. except ValueError:
  168. continue
  169. if ver <= SCHEMA_VERSION:
  170. valid_versions.append(ver)
  171. if not valid_versions:
  172. raise PrepareDatabaseException(
  173. "Could not find a suitable base set of full schemas"
  174. )
  175. max_current_ver = max(valid_versions)
  176. logger.debug("Initialising schema v%d", max_current_ver)
  177. # Now lets find all the full schema files, both in the global schema and
  178. # in data store schemas.
  179. directories = [os.path.join(current_dir, str(max_current_ver))]
  180. directories.extend(
  181. os.path.join(
  182. dir_path,
  183. "databases",
  184. database,
  185. "schema",
  186. "full_schemas",
  187. str(max_current_ver),
  188. )
  189. for database in databases
  190. )
  191. directory_entries = []
  192. for directory in directories:
  193. directory_entries.extend(
  194. _DirectoryListing(file_name, os.path.join(directory, file_name))
  195. for file_name in os.listdir(directory)
  196. )
  197. if isinstance(database_engine, PostgresEngine):
  198. specific = "postgres"
  199. else:
  200. specific = "sqlite"
  201. directory_entries.sort()
  202. for entry in directory_entries:
  203. if entry.file_name.endswith(".sql") or entry.file_name.endswith(
  204. ".sql." + specific
  205. ):
  206. logger.debug("Applying schema %s", entry.absolute_path)
  207. executescript(cur, entry.absolute_path)
  208. cur.execute(
  209. database_engine.convert_param_style(
  210. "INSERT INTO schema_version (version, upgraded) VALUES (?,?)"
  211. ),
  212. (max_current_ver, False),
  213. )
  214. _upgrade_existing_database(
  215. cur,
  216. current_version=max_current_ver,
  217. applied_delta_files=[],
  218. upgraded=False,
  219. database_engine=database_engine,
  220. config=None,
  221. databases=databases,
  222. is_empty=True,
  223. )
  224. def _upgrade_existing_database(
  225. cur,
  226. current_version,
  227. applied_delta_files,
  228. upgraded,
  229. database_engine,
  230. config,
  231. databases,
  232. is_empty=False,
  233. ):
  234. """Upgrades an existing physical database.
  235. Delta files can either be SQL stored in *.sql files, or python modules
  236. in *.py.
  237. There can be multiple delta files per version. Synapse will keep track of
  238. which delta files have been applied, and will apply any that haven't been
  239. even if there has been no version bump. This is useful for development
  240. where orthogonal schema changes may happen on separate branches.
  241. Different delta files for the same version *must* be orthogonal and give
  242. the same result when applied in any order. No guarantees are made on the
  243. order of execution of these scripts.
  244. This is a no-op of current_version == SCHEMA_VERSION.
  245. Example directory structure:
  246. schema/
  247. delta/
  248. 11/
  249. foo.sql
  250. ...
  251. 12/
  252. foo.sql
  253. bar.py
  254. ...
  255. full_schemas/
  256. ...
  257. In the example, if current_version is 11, then foo.sql will be run if and
  258. only if `upgraded` is True. Then `foo.sql` and `bar.py` would be run in
  259. some arbitrary order.
  260. Note: we apply the delta files from the specified data stores as well as
  261. those in the top-level schema. We apply all delta files across data stores
  262. for a version before applying those in the next version.
  263. Args:
  264. cur (Cursor)
  265. current_version (int): The current version of the schema.
  266. applied_delta_files (list): A list of deltas that have already been
  267. applied.
  268. upgraded (bool): Whether the current version was generated by having
  269. applied deltas or from full schema file. If `True` the function
  270. will never apply delta files for the given `current_version`, since
  271. the current_version wasn't generated by applying those delta files.
  272. database_engine (DatabaseEngine)
  273. config (synapse.config.homeserver.HomeServerConfig|None):
  274. None if we are initialising a blank database, otherwise the application
  275. config
  276. databases (list[str]): The names of the databases to instantiate
  277. on the given physical database.
  278. is_empty (bool): Is this a blank database? I.e. do we need to run the
  279. upgrade portions of the delta scripts.
  280. """
  281. if is_empty:
  282. assert not applied_delta_files
  283. else:
  284. assert config
  285. is_worker = config and config.worker_app is not None
  286. if current_version > SCHEMA_VERSION:
  287. raise ValueError(
  288. "Cannot use this database as it is too "
  289. + "new for the server to understand"
  290. )
  291. # some of the deltas assume that config.server_name is set correctly, so now
  292. # is a good time to run the sanity check.
  293. if not is_empty and "main" in databases:
  294. from synapse.storage.databases.main import check_database_before_upgrade
  295. check_database_before_upgrade(cur, database_engine, config)
  296. start_ver = current_version
  297. if not upgraded:
  298. start_ver += 1
  299. logger.debug("applied_delta_files: %s", applied_delta_files)
  300. if isinstance(database_engine, PostgresEngine):
  301. specific_engine_extension = ".postgres"
  302. else:
  303. specific_engine_extension = ".sqlite"
  304. specific_engine_extensions = (".sqlite", ".postgres")
  305. for v in range(start_ver, SCHEMA_VERSION + 1):
  306. logger.info("Applying schema deltas for v%d", v)
  307. # We need to search both the global and per data store schema
  308. # directories for schema updates.
  309. # First we find the directories to search in
  310. delta_dir = os.path.join(dir_path, "schema", "delta", str(v))
  311. directories = [delta_dir]
  312. for database in databases:
  313. directories.append(
  314. os.path.join(dir_path, "databases", database, "schema", "delta", str(v))
  315. )
  316. # Used to check if we have any duplicate file names
  317. file_name_counter = Counter()
  318. # Now find which directories have anything of interest.
  319. directory_entries = []
  320. for directory in directories:
  321. logger.debug("Looking for schema deltas in %s", directory)
  322. try:
  323. file_names = os.listdir(directory)
  324. directory_entries.extend(
  325. _DirectoryListing(file_name, os.path.join(directory, file_name))
  326. for file_name in file_names
  327. )
  328. for file_name in file_names:
  329. file_name_counter[file_name] += 1
  330. except FileNotFoundError:
  331. # Data stores can have empty entries for a given version delta.
  332. pass
  333. except OSError:
  334. raise UpgradeDatabaseException(
  335. "Could not open delta dir for version %d: %s" % (v, directory)
  336. )
  337. duplicates = {
  338. file_name for file_name, count in file_name_counter.items() if count > 1
  339. }
  340. if duplicates:
  341. # We don't support using the same file name in the same delta version.
  342. raise PrepareDatabaseException(
  343. "Found multiple delta files with the same name in v%d: %s"
  344. % (v, duplicates,)
  345. )
  346. # We sort to ensure that we apply the delta files in a consistent
  347. # order (to avoid bugs caused by inconsistent directory listing order)
  348. directory_entries.sort()
  349. for entry in directory_entries:
  350. file_name = entry.file_name
  351. relative_path = os.path.join(str(v), file_name)
  352. absolute_path = entry.absolute_path
  353. logger.debug("Found file: %s (%s)", relative_path, absolute_path)
  354. if relative_path in applied_delta_files:
  355. continue
  356. root_name, ext = os.path.splitext(file_name)
  357. if ext == ".py":
  358. # This is a python upgrade module. We need to import into some
  359. # package and then execute its `run_upgrade` function.
  360. if is_worker:
  361. raise PrepareDatabaseException(
  362. UNAPPLIED_DELTA_ON_WORKER_ERROR % relative_path
  363. )
  364. module_name = "synapse.storage.v%d_%s" % (v, root_name)
  365. with open(absolute_path) as python_file:
  366. module = imp.load_source(module_name, absolute_path, python_file)
  367. logger.info("Running script %s", relative_path)
  368. module.run_create(cur, database_engine)
  369. if not is_empty:
  370. module.run_upgrade(cur, database_engine, config=config)
  371. elif ext == ".pyc" or file_name == "__pycache__":
  372. # Sometimes .pyc files turn up anyway even though we've
  373. # disabled their generation; e.g. from distribution package
  374. # installers. Silently skip it
  375. continue
  376. elif ext == ".sql":
  377. # A plain old .sql file, just read and execute it
  378. if is_worker:
  379. raise PrepareDatabaseException(
  380. UNAPPLIED_DELTA_ON_WORKER_ERROR % relative_path
  381. )
  382. logger.info("Applying schema %s", relative_path)
  383. executescript(cur, absolute_path)
  384. elif ext == specific_engine_extension and root_name.endswith(".sql"):
  385. # A .sql file specific to our engine; just read and execute it
  386. if is_worker:
  387. raise PrepareDatabaseException(
  388. UNAPPLIED_DELTA_ON_WORKER_ERROR % relative_path
  389. )
  390. logger.info("Applying engine-specific schema %s", relative_path)
  391. executescript(cur, absolute_path)
  392. elif ext in specific_engine_extensions and root_name.endswith(".sql"):
  393. # A .sql file for a different engine; skip it.
  394. continue
  395. else:
  396. # Not a valid delta file.
  397. logger.warning(
  398. "Found directory entry that did not end in .py or .sql: %s",
  399. relative_path,
  400. )
  401. continue
  402. # Mark as done.
  403. cur.execute(
  404. database_engine.convert_param_style(
  405. "INSERT INTO applied_schema_deltas (version, file) VALUES (?,?)"
  406. ),
  407. (v, relative_path),
  408. )
  409. cur.execute("DELETE FROM schema_version")
  410. cur.execute(
  411. database_engine.convert_param_style(
  412. "INSERT INTO schema_version (version, upgraded) VALUES (?,?)"
  413. ),
  414. (v, True),
  415. )
  416. logger.info("Schema now up to date")
  417. def _apply_module_schemas(txn, database_engine, config):
  418. """Apply the module schemas for the dynamic modules, if any
  419. Args:
  420. cur: database cursor
  421. database_engine: synapse database engine class
  422. config (synapse.config.homeserver.HomeServerConfig):
  423. application config
  424. """
  425. for (mod, _config) in config.password_providers:
  426. if not hasattr(mod, "get_db_schema_files"):
  427. continue
  428. modname = ".".join((mod.__module__, mod.__name__))
  429. _apply_module_schema_files(
  430. txn, database_engine, modname, mod.get_db_schema_files()
  431. )
  432. def _apply_module_schema_files(cur, database_engine, modname, names_and_streams):
  433. """Apply the module schemas for a single module
  434. Args:
  435. cur: database cursor
  436. database_engine: synapse database engine class
  437. modname (str): fully qualified name of the module
  438. names_and_streams (Iterable[(str, file)]): the names and streams of
  439. schemas to be applied
  440. """
  441. cur.execute(
  442. database_engine.convert_param_style(
  443. "SELECT file FROM applied_module_schemas WHERE module_name = ?"
  444. ),
  445. (modname,),
  446. )
  447. applied_deltas = {d for d, in cur}
  448. for (name, stream) in names_and_streams:
  449. if name in applied_deltas:
  450. continue
  451. root_name, ext = os.path.splitext(name)
  452. if ext != ".sql":
  453. raise PrepareDatabaseException(
  454. "only .sql files are currently supported for module schemas"
  455. )
  456. logger.info("applying schema %s for %s", name, modname)
  457. execute_statements_from_stream(cur, stream)
  458. # Mark as done.
  459. cur.execute(
  460. database_engine.convert_param_style(
  461. "INSERT INTO applied_module_schemas (module_name, file) VALUES (?,?)"
  462. ),
  463. (modname, name),
  464. )
  465. def get_statements(f):
  466. statement_buffer = ""
  467. in_comment = False # If we're in a /* ... */ style comment
  468. for line in f:
  469. line = line.strip()
  470. if in_comment:
  471. # Check if this line contains an end to the comment
  472. comments = line.split("*/", 1)
  473. if len(comments) == 1:
  474. continue
  475. line = comments[1]
  476. in_comment = False
  477. # Remove inline block comments
  478. line = re.sub(r"/\*.*\*/", " ", line)
  479. # Does this line start a comment?
  480. comments = line.split("/*", 1)
  481. if len(comments) > 1:
  482. line = comments[0]
  483. in_comment = True
  484. # Deal with line comments
  485. line = line.split("--", 1)[0]
  486. line = line.split("//", 1)[0]
  487. # Find *all* semicolons. We need to treat first and last entry
  488. # specially.
  489. statements = line.split(";")
  490. # We must prepend statement_buffer to the first statement
  491. first_statement = "%s %s" % (statement_buffer.strip(), statements[0].strip())
  492. statements[0] = first_statement
  493. # Every entry, except the last, is a full statement
  494. for statement in statements[:-1]:
  495. yield statement.strip()
  496. # The last entry did *not* end in a semicolon, so we store it for the
  497. # next semicolon we find
  498. statement_buffer = statements[-1].strip()
  499. def executescript(txn, schema_path):
  500. with open(schema_path, "r") as f:
  501. execute_statements_from_stream(txn, f)
  502. def execute_statements_from_stream(cur: Cursor, f: TextIO):
  503. for statement in get_statements(f):
  504. cur.execute(statement)
  505. def _get_or_create_schema_state(txn, database_engine):
  506. # Bluntly try creating the schema_version tables.
  507. schema_path = os.path.join(dir_path, "schema", "schema_version.sql")
  508. executescript(txn, schema_path)
  509. txn.execute("SELECT version, upgraded FROM schema_version")
  510. row = txn.fetchone()
  511. current_version = int(row[0]) if row else None
  512. upgraded = bool(row[1]) if row else None
  513. if current_version:
  514. txn.execute(
  515. database_engine.convert_param_style(
  516. "SELECT file FROM applied_schema_deltas WHERE version >= ?"
  517. ),
  518. (current_version,),
  519. )
  520. applied_deltas = [d for d, in txn]
  521. return current_version, applied_deltas, upgraded
  522. return None
  523. @attr.s(slots=True)
  524. class _DirectoryListing:
  525. """Helper class to store schema file name and the
  526. absolute path to it.
  527. These entries get sorted, so for consistency we want to ensure that
  528. `file_name` attr is kept first.
  529. """
  530. file_name = attr.ib()
  531. absolute_path = attr.ib()