1
0

database.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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 logging
  17. import os
  18. from synapse.config._base import Config, ConfigError
  19. logger = logging.getLogger(__name__)
  20. NON_SQLITE_DATABASE_PATH_WARNING = """\
  21. Ignoring 'database_path' setting: not using a sqlite3 database.
  22. --------------------------------------------------------------------------------
  23. """
  24. DEFAULT_CONFIG = """\
  25. ## Database ##
  26. # The 'database' setting defines the database that synapse uses to store all of
  27. # its data.
  28. #
  29. # 'name' gives the database engine to use: either 'sqlite3' (for SQLite) or
  30. # 'psycopg2' (for PostgreSQL).
  31. #
  32. # 'args' gives options which are passed through to the database engine,
  33. # except for options starting 'cp_', which are used to configure the Twisted
  34. # connection pool. For a reference to valid arguments, see:
  35. # * for sqlite: https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
  36. # * for postgres: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
  37. # * for the connection pool: https://twistedmatrix.com/documents/current/api/twisted.enterprise.adbapi.ConnectionPool.html#__init__
  38. #
  39. #
  40. # Example SQLite configuration:
  41. #
  42. #database:
  43. # name: sqlite3
  44. # args:
  45. # database: /path/to/homeserver.db
  46. #
  47. #
  48. # Example Postgres configuration:
  49. #
  50. #database:
  51. # name: psycopg2
  52. # args:
  53. # user: synapse
  54. # password: secretpassword
  55. # database: synapse
  56. # host: localhost
  57. # cp_min: 5
  58. # cp_max: 10
  59. #
  60. # For more information on using Synapse with Postgres, see `docs/postgres.md`.
  61. #
  62. database:
  63. name: sqlite3
  64. args:
  65. database: %(database_path)s
  66. """
  67. class DatabaseConnectionConfig:
  68. """Contains the connection config for a particular database.
  69. Args:
  70. name: A label for the database, used for logging.
  71. db_config: The config for a particular database, as per `database`
  72. section of main config. Has three fields: `name` for database
  73. module name, `args` for the args to give to the database
  74. connector, and optional `data_stores` that is a list of stores to
  75. provision on this database (defaulting to all).
  76. """
  77. def __init__(self, name: str, db_config: dict):
  78. db_engine = db_config.get("name", "sqlite3")
  79. if db_engine not in ("sqlite3", "psycopg2"):
  80. raise ConfigError("Unsupported database type %r" % (db_engine,))
  81. if db_engine == "sqlite3":
  82. db_config.setdefault("args", {}).update(
  83. {"cp_min": 1, "cp_max": 1, "check_same_thread": False}
  84. )
  85. data_stores = db_config.get("data_stores")
  86. if data_stores is None:
  87. data_stores = ["main", "state"]
  88. self.name = name
  89. self.config = db_config
  90. self.data_stores = data_stores
  91. class DatabaseConfig(Config):
  92. section = "database"
  93. def __init__(self, *args, **kwargs):
  94. super().__init__(*args, **kwargs)
  95. self.databases = []
  96. def read_config(self, config, **kwargs):
  97. # We *experimentally* support specifying multiple databases via the
  98. # `databases` key. This is a map from a label to database config in the
  99. # same format as the `database` config option, plus an extra
  100. # `data_stores` key to specify which data store goes where. For example:
  101. #
  102. # databases:
  103. # master:
  104. # name: psycopg2
  105. # data_stores: ["main"]
  106. # args: {}
  107. # state:
  108. # name: psycopg2
  109. # data_stores: ["state"]
  110. # args: {}
  111. multi_database_config = config.get("databases")
  112. database_config = config.get("database")
  113. database_path = config.get("database_path")
  114. if multi_database_config and database_config:
  115. raise ConfigError("Can't specify both 'database' and 'databases' in config")
  116. if multi_database_config:
  117. if database_path:
  118. raise ConfigError("Can't specify 'database_path' with 'databases'")
  119. self.databases = [
  120. DatabaseConnectionConfig(name, db_conf)
  121. for name, db_conf in multi_database_config.items()
  122. ]
  123. if database_config:
  124. self.databases = [DatabaseConnectionConfig("master", database_config)]
  125. if database_path:
  126. if self.databases and self.databases[0].name != "sqlite3":
  127. logger.warning(NON_SQLITE_DATABASE_PATH_WARNING)
  128. return
  129. database_config = {"name": "sqlite3", "args": {}}
  130. self.databases = [DatabaseConnectionConfig("master", database_config)]
  131. self.set_databasepath(database_path)
  132. def generate_config_section(self, data_dir_path, **kwargs):
  133. return DEFAULT_CONFIG % {
  134. "database_path": os.path.join(data_dir_path, "homeserver.db")
  135. }
  136. def read_arguments(self, args):
  137. """
  138. Cases for the cli input:
  139. - If no databases are configured and no database_path is set, raise.
  140. - No databases and only database_path available ==> sqlite3 db.
  141. - If there are multiple databases and a database_path raise an error.
  142. - If the database set in the config file is sqlite then
  143. overwrite with the command line argument.
  144. """
  145. if args.database_path is None:
  146. if not self.databases:
  147. raise ConfigError("No database config provided")
  148. return
  149. if len(self.databases) == 0:
  150. database_config = {"name": "sqlite3", "args": {}}
  151. self.databases = [DatabaseConnectionConfig("master", database_config)]
  152. self.set_databasepath(args.database_path)
  153. return
  154. if self.get_single_database().name == "sqlite3":
  155. self.set_databasepath(args.database_path)
  156. else:
  157. logger.warning(NON_SQLITE_DATABASE_PATH_WARNING)
  158. def set_databasepath(self, database_path):
  159. if database_path != ":memory:":
  160. database_path = self.abspath(database_path)
  161. self.databases[0].config["args"]["database"] = database_path
  162. @staticmethod
  163. def add_arguments(parser):
  164. db_group = parser.add_argument_group("database")
  165. db_group.add_argument(
  166. "-d",
  167. "--database-path",
  168. metavar="SQLITE_DATABASE_PATH",
  169. help="The path to a sqlite database to use.",
  170. )
  171. def get_single_database(self) -> DatabaseConnectionConfig:
  172. """Returns the database if there is only one, useful for e.g. tests
  173. """
  174. if not self.databases:
  175. raise Exception("More than one database exists")
  176. return self.databases[0]