01_drop_user_id_constraint_profiles.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from synapse.storage.database import LoggingTransaction
  2. from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine
  3. def run_create(cur: LoggingTransaction, database_engine: BaseDatabaseEngine) -> None:
  4. """
  5. Update to drop the NOT NULL constraint on column user_id so that we can cease to
  6. write to it without inserts to other columns triggering the constraint
  7. """
  8. if isinstance(database_engine, PostgresEngine):
  9. drop_sql = """
  10. ALTER TABLE profiles ALTER COLUMN user_id DROP NOT NULL
  11. """
  12. cur.execute(drop_sql)
  13. else:
  14. # irritatingly in SQLite we need to rewrite the table to drop the constraint.
  15. cur.execute("DROP TABLE IF EXISTS temp_profiles")
  16. create_sql = """
  17. CREATE TABLE temp_profiles (
  18. full_user_id text NOT NULL,
  19. user_id text,
  20. displayname text,
  21. avatar_url text,
  22. UNIQUE (full_user_id),
  23. UNIQUE (user_id)
  24. )
  25. """
  26. cur.execute(create_sql)
  27. copy_sql = """
  28. INSERT INTO temp_profiles (
  29. user_id,
  30. displayname,
  31. avatar_url,
  32. full_user_id)
  33. SELECT user_id, displayname, avatar_url, full_user_id FROM profiles
  34. """
  35. cur.execute(copy_sql)
  36. drop_sql = """
  37. DROP TABLE profiles
  38. """
  39. cur.execute(drop_sql)
  40. rename_sql = """
  41. ALTER TABLE temp_profiles RENAME to profiles
  42. """
  43. cur.execute(rename_sql)