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.
 
 
 
 
 
 

101 lines
3.0 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. import sys
  18. import yaml
  19. from twisted.internet import defer, reactor
  20. import synapse
  21. from synapse.config.homeserver import HomeServerConfig
  22. from synapse.metrics.background_process_metrics import run_as_background_process
  23. from synapse.server import HomeServer
  24. from synapse.storage import DataStore
  25. from synapse.util.versionstring import get_version_string
  26. logger = logging.getLogger("update_database")
  27. class MockHomeserver(HomeServer):
  28. DATASTORE_CLASS = DataStore
  29. def __init__(self, config, **kwargs):
  30. super(MockHomeserver, self).__init__(
  31. config.server_name, reactor=reactor, config=config, **kwargs
  32. )
  33. self.version_string = "Synapse/" + get_version_string(synapse)
  34. if __name__ == "__main__":
  35. parser = argparse.ArgumentParser(
  36. description=(
  37. "Updates a synapse database to the latest schema and runs background updates"
  38. " on it."
  39. )
  40. )
  41. parser.add_argument("-v", action="store_true")
  42. parser.add_argument(
  43. "--database-config",
  44. type=argparse.FileType("r"),
  45. required=True,
  46. help="A database config file for either a SQLite3 database or a PostgreSQL one.",
  47. )
  48. args = parser.parse_args()
  49. logging_config = {
  50. "level": logging.DEBUG if args.v else logging.INFO,
  51. "format": "%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(message)s",
  52. }
  53. logging.basicConfig(**logging_config)
  54. # Load, process and sanity-check the config.
  55. hs_config = yaml.safe_load(args.database_config)
  56. if "database" not in hs_config:
  57. sys.stderr.write("The configuration file must have a 'database' section.\n")
  58. sys.exit(4)
  59. config = HomeServerConfig()
  60. config.parse_config_dict(hs_config, "", "")
  61. # Instantiate and initialise the homeserver object.
  62. hs = MockHomeserver(config)
  63. # Setup instantiates the store within the homeserver object and updates the
  64. # DB.
  65. hs.setup()
  66. store = hs.get_datastore()
  67. async def run_background_updates():
  68. await store.db_pool.updates.run_background_updates(sleep=False)
  69. # Stop the reactor to exit the script once every background update is run.
  70. reactor.stop()
  71. def run():
  72. # Apply all background updates on the database.
  73. defer.ensureDeferred(
  74. run_as_background_process("background_updates", run_background_updates)
  75. )
  76. reactor.callWhenRunning(run)
  77. reactor.run()