You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

118 lines
3.6 KiB

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