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