Notification: Change type of `type` to an enum.
authorlain <lain@soykaf.club>
Sat, 6 Jun 2020 11:08:45 +0000 (13:08 +0200)
committerlain <lain@soykaf.club>
Sat, 6 Jun 2020 11:08:45 +0000 (13:08 +0200)
lib/pleroma/notification.ex
priv/repo/migrations/20200606105430_change_type_to_enum_for_notifications.exs [new file with mode: 0644]

index 49e27c05a4a255034c4a271e03230ed760c202c0..5c8994e358e3f4ef9069982aaef8d51f7a4ff619 100644 (file)
@@ -30,6 +30,9 @@ defmodule Pleroma.Notification do
 
   schema "notifications" do
     field(:seen, :boolean, default: false)
+    # This is an enum type in the database. If you add a new notification type,
+    # remembert to add a migration to add it to the `notifications_type` enum
+    # as well.
     field(:type, :string)
     belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
     belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
diff --git a/priv/repo/migrations/20200606105430_change_type_to_enum_for_notifications.exs b/priv/repo/migrations/20200606105430_change_type_to_enum_for_notifications.exs
new file mode 100644 (file)
index 0000000..9ea3443
--- /dev/null
@@ -0,0 +1,36 @@
+defmodule Pleroma.Repo.Migrations.ChangeTypeToEnumForNotifications do
+  use Ecto.Migration
+
+  def up do
+    """
+    create type notification_type as enum (
+      'follow',
+      'follow_request',
+      'mention',
+      'move',
+      'pleroma:emoji_reaction',
+      'pleroma:chat_mention',
+      'reblog',
+      'favourite'
+    )
+    """
+    |> execute()
+
+    """
+    alter table notifications 
+    alter column type type notification_type using (type::notification_type)
+    """
+    |> execute()
+  end
+
+  def down do
+    alter table(:notifications) do
+      modify(:type, :string)
+    end
+
+    """
+    drop type notification_type
+    """
+    |> execute()
+  end
+end