logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

20181218172826_users_and_activities_flake_id.exs (5977B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Repo.Migrations.UsersAndActivitiesFlakeId do
  5. use Ecto.Migration
  6. alias Pleroma.Clippy
  7. require Integer
  8. import Ecto.Query
  9. alias Pleroma.Repo
  10. # This migrates from int serial IDs to custom Flake:
  11. # 1- create a temporary uuid column
  12. # 2- fill this column with compatibility ids (see below)
  13. # 3- remove pkeys constraints
  14. # 4- update relation pkeys with the new ids
  15. # 5- rename the temporary column to id
  16. # 6- re-create the constraints
  17. def up do
  18. # Old serial int ids are transformed to 128bits with extra padding.
  19. # The application (in `Pleroma.FlakeId`) handles theses IDs properly as integers; to keep compatibility
  20. # with previously issued ids.
  21. # execute "update activities set external_id = CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid);"
  22. # execute "update users set external_id = CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid);"
  23. clippy = start_clippy_heartbeats()
  24. # Lock both tables to avoid a running server to meddling with our transaction
  25. execute("LOCK TABLE activities;")
  26. execute("LOCK TABLE users;")
  27. execute("""
  28. ALTER TABLE activities
  29. DROP CONSTRAINT activities_pkey CASCADE,
  30. ALTER COLUMN id DROP default,
  31. ALTER COLUMN id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid),
  32. ADD PRIMARY KEY (id);
  33. """)
  34. execute("""
  35. ALTER TABLE users
  36. DROP CONSTRAINT users_pkey CASCADE,
  37. ALTER COLUMN id DROP default,
  38. ALTER COLUMN id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid),
  39. ADD PRIMARY KEY (id);
  40. """)
  41. execute(
  42. "UPDATE users SET info = jsonb_set(info, '{pinned_activities}', array_to_json(ARRAY(select jsonb_array_elements_text(info->'pinned_activities')))::jsonb);"
  43. )
  44. # Fkeys:
  45. # Activities - Referenced by:
  46. # TABLE "notifications" CONSTRAINT "notifications_activity_id_fkey" FOREIGN KEY (activity_id) REFERENCES activities(id) ON DELETE CASCADE
  47. # Users - Referenced by:
  48. # TABLE "filters" CONSTRAINT "filters_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
  49. # TABLE "lists" CONSTRAINT "lists_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
  50. # TABLE "notifications" CONSTRAINT "notifications_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
  51. # TABLE "oauth_authorizations" CONSTRAINT "oauth_authorizations_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
  52. # TABLE "oauth_tokens" CONSTRAINT "oauth_tokens_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
  53. # TABLE "password_reset_tokens" CONSTRAINT "password_reset_tokens_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
  54. # TABLE "push_subscriptions" CONSTRAINT "push_subscriptions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
  55. # TABLE "websub_client_subscriptions" CONSTRAINT "websub_client_subscriptions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
  56. execute("""
  57. ALTER TABLE notifications
  58. ALTER COLUMN activity_id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(activity_id), 32, '0' ) AS uuid),
  59. ADD CONSTRAINT notifications_activity_id_fkey FOREIGN KEY (activity_id) REFERENCES activities(id) ON DELETE CASCADE;
  60. """)
  61. for table <-
  62. ~w(notifications filters lists oauth_authorizations oauth_tokens password_reset_tokens push_subscriptions websub_client_subscriptions) do
  63. execute("""
  64. ALTER TABLE #{table}
  65. ALTER COLUMN user_id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(user_id), 32, '0' ) AS uuid),
  66. ADD CONSTRAINT #{table}_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
  67. """)
  68. end
  69. flush()
  70. stop_clippy_heartbeats(clippy)
  71. end
  72. def down, do: :ok
  73. defp start_clippy_heartbeats() do
  74. count = from(a in "activities", select: count(a.id)) |> Repo.one!()
  75. if count > 5000 do
  76. heartbeat_interval = :timer.minutes(2) + :timer.seconds(30)
  77. all_tips =
  78. Clippy.tips() ++
  79. [
  80. "The migration is still running, maybe it's time for another “tea”?",
  81. "Happy rabbits practice a cute behavior known as a\n“binky:” they jump up in the air\nand twist\nand spin around!",
  82. "Nothing and everything.\n\nI still work.",
  83. "Pleroma runs on a Raspberry Pi!\n\n … but this migration will take forever if you\nactually run on a raspberry pi",
  84. "Status? Stati? Post? Note? Toot?\nRepeat? Reboost? Boost? Retweet? Retoot??\n\nI-I'm confused."
  85. ]
  86. heartbeat = fn heartbeat, runs, all_tips, tips ->
  87. tips =
  88. if Integer.is_even(runs) do
  89. tips = if tips == [], do: all_tips, else: tips
  90. [tip | tips] = Enum.shuffle(tips)
  91. Clippy.puts(tip)
  92. tips
  93. else
  94. IO.puts(
  95. "\n -- #{DateTime.to_string(DateTime.utc_now())} Migration still running, please wait…\n"
  96. )
  97. tips
  98. end
  99. :timer.sleep(heartbeat_interval)
  100. heartbeat.(heartbeat, runs + 1, all_tips, tips)
  101. end
  102. Clippy.puts([
  103. [:red, :bright, "It looks like you are running an older instance!"],
  104. [""],
  105. [:bright, "This migration may take a long time", :reset, " -- so you probably should"],
  106. ["go drink a cofe, or a tea, or a beer, a whiskey, a vodka,"],
  107. ["while it runs to deal with your temporary fediverse pause!"]
  108. ])
  109. :timer.sleep(heartbeat_interval)
  110. spawn_link(fn -> heartbeat.(heartbeat, 1, all_tips, []) end)
  111. end
  112. end
  113. defp stop_clippy_heartbeats(pid) do
  114. if pid do
  115. Process.unlink(pid)
  116. Process.exit(pid, :kill)
  117. Clippy.puts([[:green, :bright, "Hurray!!", "", "", "Migration completed!"]])
  118. end
  119. end
  120. end