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.
 
 
 
 
 
 

95 lines
3.3 KiB

  1. # Copyright 2023 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. from twisted.test.proto_helpers import MemoryReactor
  15. from synapse.server import HomeServer
  16. from synapse.storage.database import LoggingTransaction
  17. from synapse.storage.engines import PostgresEngine
  18. from synapse.util import Clock
  19. from tests import unittest
  20. class UserFiltersStoreTestCase(unittest.HomeserverTestCase):
  21. """
  22. Test background migration that copies entries from column user_id to full_user_id, adding
  23. the hostname in the process.
  24. """
  25. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  26. self.store = hs.get_datastores().main
  27. def test_bg_migration(self) -> None:
  28. updater = self.hs.get_datastores().main.db_pool.updates
  29. # drop the constraint so we can insert nulls in full_user_id to populate the test
  30. if isinstance(self.store.database_engine, PostgresEngine):
  31. def f(txn: LoggingTransaction) -> None:
  32. txn.execute(
  33. "ALTER TABLE user_filters DROP CONSTRAINT full_user_id_not_null"
  34. )
  35. self.get_success(self.store.db_pool.runInteraction("", f))
  36. for i in range(70):
  37. self.get_success(
  38. self.store.db_pool.simple_insert(
  39. "user_filters",
  40. {
  41. "user_id": f"hello{i:02}",
  42. "filter_id": i,
  43. "filter_json": bytearray(i),
  44. },
  45. )
  46. )
  47. # re-add the constraint so that when it's validated it actually exists
  48. if isinstance(self.store.database_engine, PostgresEngine):
  49. def f(txn: LoggingTransaction) -> None:
  50. txn.execute(
  51. "ALTER TABLE user_filters ADD CONSTRAINT full_user_id_not_null CHECK (full_user_id IS NOT NULL) NOT VALID"
  52. )
  53. self.get_success(self.store.db_pool.runInteraction("", f))
  54. self.get_success(
  55. self.store.db_pool.simple_insert(
  56. "background_updates",
  57. values={
  58. "update_name": "populate_full_user_id_user_filters",
  59. "progress_json": "{}",
  60. },
  61. )
  62. )
  63. self.get_success(
  64. updater.run_background_updates(False),
  65. )
  66. expected_values = []
  67. for i in range(70):
  68. expected_values.append((f"@hello{i:02}:{self.hs.hostname}",))
  69. res = self.get_success(
  70. self.store.db_pool.execute(
  71. "", None, "SELECT full_user_id from user_filters ORDER BY full_user_id"
  72. )
  73. )
  74. self.assertEqual(len(res), len(expected_values))
  75. self.assertEqual(res, expected_values)