Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / priv / repo / migrations / 20181218172826_users_and_activities_flake_id.exs
index 69a27e0c8776566809d96ed85cb4b2090c279099..a5b4c543da9625f6dc5b2a09c3b6b6f3338bfbeb 100644 (file)
@@ -12,7 +12,7 @@ defmodule Pleroma.Repo.Migrations.UsersAndActivitiesFlakeId do
   #   4- update relation pkeys with the new ids
   #   5- rename the temporary column to id
   #   6- re-create the constraints
-  def change do
+  def up do
     # Old serial int ids are transformed to 128bits with extra padding.
     # The application (in `Pleroma.FlakeId`) handles theses IDs properly as integers; to keep compatibility
     # with previously issued ids.
@@ -25,17 +25,23 @@ defmodule Pleroma.Repo.Migrations.UsersAndActivitiesFlakeId do
     execute "LOCK TABLE activities;"
     execute "LOCK TABLE users;"
 
-    execute "ALTER TABLE activities DROP CONSTRAINT activities_pkey CASCADE;"
-    execute "ALTER TABLE users DROP CONSTRAINT users_pkey CASCADE;"
+    execute """
+      ALTER TABLE activities
+      DROP CONSTRAINT activities_pkey CASCADE,
+      ALTER COLUMN id DROP default,
+      ALTER COLUMN id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid),
+      ADD PRIMARY KEY (id);
+    """
 
-    execute "ALTER TABLE activities ALTER COLUMN id DROP default;"
-    execute "ALTER TABLE users ALTER COLUMN id DROP default;"
+    execute """
+    ALTER TABLE users
+    DROP CONSTRAINT users_pkey CASCADE,
+    ALTER COLUMN id DROP default,
+    ALTER COLUMN id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid),
+    ADD PRIMARY KEY (id);
+    """
 
-    execute "ALTER TABLE activities ALTER COLUMN id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid);"
-    execute "ALTER TABLE users ALTER COLUMN id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid);"
-
-    execute "ALTER TABLE activities ADD PRIMARY KEY (id);"
-    execute "ALTER TABLE users ADD PRIMARY KEY (id);"
+    execute "UPDATE users SET info = jsonb_set(info, '{pinned_activities}', array_to_json(ARRAY(select jsonb_array_elements_text(info->'pinned_activities')))::jsonb);"
 
     # Fkeys:
     # Activities - Referenced by:
@@ -50,24 +56,34 @@ defmodule Pleroma.Repo.Migrations.UsersAndActivitiesFlakeId do
     #  TABLE "push_subscriptions" CONSTRAINT "push_subscriptions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
     #  TABLE "websub_client_subscriptions" CONSTRAINT "websub_client_subscriptions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
 
-    execute "ALTER TABLE notifications ALTER COLUMN activity_id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(activity_id), 32, '0' ) AS uuid);"
-    execute "ALTER TABLE notifications ADD CONSTRAINT notifications_activity_id_fkey FOREIGN KEY (activity_id) REFERENCES activities(id) ON DELETE CASCADE;"
+    execute """
+    ALTER TABLE notifications
+    ALTER COLUMN activity_id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(activity_id), 32, '0' ) AS uuid),
+    ADD CONSTRAINT notifications_activity_id_fkey FOREIGN KEY (activity_id) REFERENCES activities(id) ON DELETE CASCADE;
+    """
 
     for table <- ~w(notifications filters lists oauth_authorizations oauth_tokens password_reset_tokens push_subscriptions websub_client_subscriptions) do
-      execute "ALTER TABLE #{table} ALTER COLUMN user_id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(user_id), 32, '0' ) AS uuid);"
-      execute "ALTER TABLE #{table} ADD CONSTRAINT #{table}_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;"
+      execute """
+      ALTER TABLE #{table}
+      ALTER COLUMN user_id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(user_id), 32, '0' ) AS uuid),
+      ADD CONSTRAINT #{table}_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
+      """
     end
 
+    flush()
+
     stop_clippy_heartbeats(clippy)
   end
 
+  def down, do: :ok
+
   defp start_clippy_heartbeats() do
     count = from(a in "activities", select: count(a.id)) |> Repo.one!
 
-    pid = if count > 5000 do
+    if count > 5000 do
       heartbeat_interval = :timer.minutes(2) + :timer.seconds(30)
       all_tips = Clippy.tips() ++ [
-        "The migration is still running, maybe it's time for another tea?",
+        "The migration is still running, maybe it's time for another “tea”?",
         "Happy rabbits practice a cute behavior known as a\n“binky:” they jump up in the air\nand twist\nand spin around!",
         "Nothing and everything.\n\nI still work.",
         "Pleroma runs on a Raspberry Pi!\n\n  … but this migration will take forever if you\nactually run on a raspberry pi",
@@ -92,7 +108,7 @@ defmodule Pleroma.Repo.Migrations.UsersAndActivitiesFlakeId do
         [:red, :bright, "It looks like you are running an older instance!"],
         [""],
         [:bright, "This migration may take a long time", :reset, " -- so you probably should"],
-        ["go drink a coffee, or a tea, or a beer, a whiskey, a vodka,"],
+        ["go drink a cofe, or a tea, or a beer, a whiskey, a vodka,"],
         ["while it runs to deal with your temporary fediverse pause!"]
       ]
       :timer.sleep(heartbeat_interval)