Migration to confirm previously-logged-in users
authorAlex Gleason <alex@alexgleason.me>
Thu, 31 Dec 2020 20:04:51 +0000 (14:04 -0600)
committerAlex Gleason <alex@alexgleason.me>
Thu, 31 Dec 2020 20:04:51 +0000 (14:04 -0600)
priv/repo/migrations/20201231185546_confirm_logged_in_users.exs [new file with mode: 0644]
test/pleroma/repo/migrations/confirm_logged_in_users_test.exs [new file with mode: 0644]

diff --git a/priv/repo/migrations/20201231185546_confirm_logged_in_users.exs b/priv/repo/migrations/20201231185546_confirm_logged_in_users.exs
new file mode 100644 (file)
index 0000000..de2f351
--- /dev/null
@@ -0,0 +1,22 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Repo.Migrations.ConfirmLoggedInUsers do
+  use Ecto.Migration
+  import Ecto.Query
+  alias Pleroma.Repo
+  alias Pleroma.User
+  alias Pleroma.Web.OAuth.Token
+
+  def up do
+    User
+    |> where([u], u.confirmation_pending == true)
+    |> join(:inner, [u], t in Token, on: t.user_id == u.id)
+    |> Repo.update_all(set: [confirmation_pending: false])
+  end
+
+  def down do
+    :noop
+  end
+end
diff --git a/test/pleroma/repo/migrations/confirm_logged_in_users_test.exs b/test/pleroma/repo/migrations/confirm_logged_in_users_test.exs
new file mode 100644 (file)
index 0000000..f1fd891
--- /dev/null
@@ -0,0 +1,40 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Repo.Migrations.ConfirmLoggedInUsersTest do
+  alias Pleroma.Repo
+  alias Pleroma.User
+  use Pleroma.DataCase, async: true
+  import Ecto.Query
+  import Pleroma.Factory
+  import Pleroma.Tests.Helpers
+
+  setup_all do: require_migration("20201231185546_confirm_logged_in_users")
+
+  test "up/0 confirms unconfirmed but previously-logged-in users", %{migration: migration} do
+    insert_list(25, :oauth_token)
+    Repo.update_all(User, set: [confirmation_pending: true])
+    insert_list(5, :user, confirmation_pending: true)
+
+    count =
+      User
+      |> where(confirmation_pending: true)
+      |> Repo.aggregate(:count)
+
+    assert count == 30
+
+    assert {25, nil} == migration.up()
+
+    count =
+      User
+      |> where(confirmation_pending: true)
+      |> Repo.aggregate(:count)
+
+    assert count == 5
+  end
+
+  test "down/0 does nothing", %{migration: migration} do
+    assert :noop == migration.down()
+  end
+end