Lock activities/users table during flake migration.
[akkoma] / priv / repo / migrations / 20181218172826_users_and_activities_flake_id.exs
1 defmodule Pleroma.Repo.Migrations.UsersAndActivitiesFlakeId do
2 use Ecto.Migration
3
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
11 def change do
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);"
17
18 # Lock both tables to avoid a running server to meddling with our transaction
19 execute "LOCK TABLE activities;"
20 execute "LOCK TABLE users;"
21
22 execute "ALTER TABLE activities DROP CONSTRAINT activities_pkey CASCADE;"
23 execute "ALTER TABLE users DROP CONSTRAINT users_pkey CASCADE;"
24
25 execute "ALTER TABLE activities ALTER COLUMN id DROP default;"
26 execute "ALTER TABLE users ALTER COLUMN id DROP default;"
27
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);"
30
31 execute "ALTER TABLE activities ADD PRIMARY KEY (id);"
32 execute "ALTER TABLE users ADD PRIMARY KEY (id);"
33
34 # Fkeys:
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)
46
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;"
49
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;"
53 end
54
55 end
56 end