ts.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright 2015, 2016 OpenMarket Ltd
  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 logging
  15. from synapse.storage.prepare_database import get_statements
  16. import simplejson
  17. logger = logging.getLogger(__name__)
  18. ALTER_TABLE = (
  19. "ALTER TABLE events ADD COLUMN origin_server_ts BIGINT;"
  20. "CREATE INDEX events_ts ON events(origin_server_ts, stream_ordering);"
  21. )
  22. def run_create(cur, database_engine, *args, **kwargs):
  23. for statement in get_statements(ALTER_TABLE.splitlines()):
  24. cur.execute(statement)
  25. cur.execute("SELECT MIN(stream_ordering) FROM events")
  26. rows = cur.fetchall()
  27. min_stream_id = rows[0][0]
  28. cur.execute("SELECT MAX(stream_ordering) FROM events")
  29. rows = cur.fetchall()
  30. max_stream_id = rows[0][0]
  31. if min_stream_id is not None and max_stream_id is not None:
  32. progress = {
  33. "target_min_stream_id_inclusive": min_stream_id,
  34. "max_stream_id_exclusive": max_stream_id + 1,
  35. "rows_inserted": 0,
  36. }
  37. progress_json = simplejson.dumps(progress)
  38. sql = (
  39. "INSERT into background_updates (update_name, progress_json)"
  40. " VALUES (?, ?)"
  41. )
  42. sql = database_engine.convert_param_style(sql)
  43. cur.execute(sql, ("event_origin_server_ts", progress_json))
  44. def run_upgrade(*args, **kwargs):
  45. pass