update_synapse_database.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright 2019 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import argparse
  15. import logging
  16. from typing import cast
  17. import yaml
  18. from twisted.internet import defer, reactor as reactor_
  19. from synapse.config.homeserver import HomeServerConfig
  20. from synapse.metrics.background_process_metrics import run_as_background_process
  21. from synapse.server import HomeServer
  22. from synapse.storage import DataStore
  23. from synapse.types import ISynapseReactor
  24. from synapse.util import SYNAPSE_VERSION
  25. # Cast safety: Twisted does some naughty magic which replaces the
  26. # twisted.internet.reactor module with a Reactor instance at runtime.
  27. reactor = cast(ISynapseReactor, reactor_)
  28. logger = logging.getLogger("update_database")
  29. class MockHomeserver(HomeServer):
  30. DATASTORE_CLASS = DataStore # type: ignore [assignment]
  31. def __init__(self, config: HomeServerConfig):
  32. super().__init__(
  33. hostname=config.server.server_name,
  34. config=config,
  35. reactor=reactor,
  36. version_string=f"Synapse/{SYNAPSE_VERSION}",
  37. )
  38. def run_background_updates(hs: HomeServer) -> None:
  39. main = hs.get_datastores().main
  40. state = hs.get_datastores().state
  41. async def run_background_updates() -> None:
  42. await main.db_pool.updates.run_background_updates(sleep=False)
  43. if state:
  44. await state.db_pool.updates.run_background_updates(sleep=False)
  45. # Stop the reactor to exit the script once every background update is run.
  46. reactor.stop()
  47. def run() -> None:
  48. # Apply all background updates on the database.
  49. defer.ensureDeferred(
  50. run_as_background_process("background_updates", run_background_updates)
  51. )
  52. reactor.callWhenRunning(run)
  53. reactor.run()
  54. def main() -> None:
  55. parser = argparse.ArgumentParser(
  56. description=(
  57. "Updates a synapse database to the latest schema and optionally runs background updates"
  58. " on it."
  59. )
  60. )
  61. parser.add_argument("-v", action="store_true")
  62. parser.add_argument(
  63. "--database-config",
  64. type=argparse.FileType("r"),
  65. required=True,
  66. help="Synapse configuration file, giving the details of the database to be updated",
  67. )
  68. parser.add_argument(
  69. "--run-background-updates",
  70. action="store_true",
  71. required=False,
  72. help="run background updates after upgrading the database schema",
  73. )
  74. args = parser.parse_args()
  75. logging.basicConfig(
  76. level=logging.DEBUG if args.v else logging.INFO,
  77. format="%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(message)s",
  78. )
  79. # Load, process and sanity-check the config.
  80. hs_config = yaml.safe_load(args.database_config)
  81. config = HomeServerConfig()
  82. config.parse_config_dict(hs_config, "", "")
  83. # Instantiate and initialise the homeserver object.
  84. hs = MockHomeserver(config)
  85. # Setup instantiates the store within the homeserver object and updates the
  86. # DB.
  87. hs.setup()
  88. if args.run_background_updates:
  89. run_background_updates(hs)
  90. if __name__ == "__main__":
  91. main()