1 defmodule Pleroma.Repo.Migrations.UsersAndActivitiesFlakeId do
4 # This migrates from int serial IDs to custom Flake:
5 # 1- create a temporary uuid column
6 # 2- fill this column with compatibility ids (see below)
7 # 3- remove pkeys constraints
8 # 4- update relation pkeys with the new ids
9 # 5- rename the temporary column to id
10 # 6- re-create the constraints
12 # Old serial int ids are transformed to 128bits with extra padding.
13 # The application (in `Pleroma.FlakeId`) handles theses IDs properly as integers; to keep compatibility
14 # with previously issued ids.
15 #execute "update activities set external_id = CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid);"
16 #execute "update users set external_id = CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid);"
18 # Lock both tables to avoid a running server to meddling with our transaction
19 execute "LOCK TABLE activities;"
20 execute "LOCK TABLE users;"
22 execute "ALTER TABLE activities DROP CONSTRAINT activities_pkey CASCADE;"
23 execute "ALTER TABLE users DROP CONSTRAINT users_pkey CASCADE;"
25 execute "ALTER TABLE activities ALTER COLUMN id DROP default;"
26 execute "ALTER TABLE users ALTER COLUMN id DROP default;"
28 execute "ALTER TABLE activities ALTER COLUMN id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid);"
29 execute "ALTER TABLE users ALTER COLUMN id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid);"
31 execute "ALTER TABLE activities ADD PRIMARY KEY (id);"
32 execute "ALTER TABLE users ADD PRIMARY KEY (id);"
35 # Activities - Referenced by:
36 # TABLE "notifications" CONSTRAINT "notifications_activity_id_fkey" FOREIGN KEY (activity_id) REFERENCES activities(id) ON DELETE CASCADE
37 # Users - Referenced by:
38 # TABLE "filters" CONSTRAINT "filters_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
39 # TABLE "lists" CONSTRAINT "lists_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
40 # TABLE "notifications" CONSTRAINT "notifications_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
41 # TABLE "oauth_authorizations" CONSTRAINT "oauth_authorizations_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
42 # TABLE "oauth_tokens" CONSTRAINT "oauth_tokens_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
43 # TABLE "password_reset_tokens" CONSTRAINT "password_reset_tokens_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
44 # TABLE "push_subscriptions" CONSTRAINT "push_subscriptions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
45 # TABLE "websub_client_subscriptions" CONSTRAINT "websub_client_subscriptions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
47 execute "ALTER TABLE notifications ALTER COLUMN activity_id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(activity_id), 32, '0' ) AS uuid);"
48 execute "ALTER TABLE notifications ADD CONSTRAINT notifications_activity_id_fkey FOREIGN KEY (activity_id) REFERENCES activities(id) ON DELETE CASCADE;"
50 for table <- ~w(notifications filters lists oauth_authorizations oauth_tokens password_reset_tokens push_subscriptions websub_client_subscriptions) do
51 execute "ALTER TABLE #{table} ALTER COLUMN user_id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(user_id), 32, '0' ) AS uuid);"
52 execute "ALTER TABLE #{table} ADD CONSTRAINT #{table}_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;"