summaryrefslogtreecommitdiff
path: root/priv/repo/migrations/20191009154608_copy_users_info_fields_to_users.exs
blob: 502b82c8b60bb3163bde8bebefdb4a257c8747a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Repo.Migrations.CopyUsersInfoFieldsToUsers do
  use Ecto.Migration

  @jsonb_array_default "'[]'::jsonb"

  @info_fields [
    :banner,
    :background,
    :source_data,
    :note_count,
    :follower_count,
    :following_count,
    :locked,
    :confirmation_pending,
    :password_reset_pending,
    :confirmation_token,
    :default_scope,
    :blocks,
    :domain_blocks,
    :mutes,
    :muted_reblogs,
    :muted_notifications,
    :subscribers,
    :deactivated,
    :no_rich_text,
    :ap_enabled,
    :is_moderator,
    :is_admin,
    :show_role,
    :settings,
    :magic_key,
    :uri,
    :hide_followers_count,
    :hide_follows_count,
    :hide_followers,
    :hide_follows,
    :hide_favorites,
    :unread_conversation_count,
    :pinned_activities,
    :email_notifications,
    :mascot,
    :emoji,
    :pleroma_settings_store,
    :fields,
    :raw_fields,
    :discoverable,
    :invisible,
    :skip_thread_containment,
    :notification_settings
  ]

  @jsonb_fields [
    :banner,
    :background,
    :source_data,
    :settings,
    :email_notifications,
    :mascot,
    :pleroma_settings_store,
    :notification_settings
  ]

  @array_jsonb_fields [:emoji, :fields, :raw_fields]

  @int_fields [:note_count, :follower_count, :following_count, :unread_conversation_count]

  @boolean_fields [
    :locked,
    :confirmation_pending,
    :password_reset_pending,
    :deactivated,
    :no_rich_text,
    :ap_enabled,
    :is_moderator,
    :is_admin,
    :show_role,
    :hide_followers_count,
    :hide_follows_count,
    :hide_followers,
    :hide_follows,
    :hide_favorites,
    :discoverable,
    :invisible,
    :skip_thread_containment
  ]

  @array_text_fields [
    :blocks,
    :domain_blocks,
    :mutes,
    :muted_reblogs,
    :muted_notifications,
    :subscribers,
    :pinned_activities
  ]

  def change do
    if direction() == :up do
      sets =
        for f <- @info_fields do
          set_field = "#{f} ="

          # Coercion of null::jsonb to NULL
          jsonb = "case when info->>'#{f}' IS NULL then null else info->'#{f}' end"

          cond do
            f in @jsonb_fields ->
              "#{set_field} #{jsonb}"

            f in @array_jsonb_fields ->
              "#{set_field} coalesce(#{jsonb}, #{@jsonb_array_default})"

            f in @int_fields ->
              "#{set_field} (info->>'#{f}')::int"

            f in @boolean_fields ->
              "#{set_field} coalesce((info->>'#{f}')::boolean, false)"

            f in @array_text_fields ->
              "#{set_field} ARRAY(SELECT jsonb_array_elements_text(#{jsonb}))"

            true ->
              "#{set_field} info->>'#{f}'"
          end
        end
        |> Enum.join(", ")

      execute("update users set " <> sets)

      for index_name <- [
            :users_deactivated_index,
            :users_is_moderator_index,
            :users_is_admin_index,
            :users_subscribers_index
          ] do
        drop_if_exists(index(:users, [], name: index_name))
      end
    end

    create_if_not_exists(index(:users, [:deactivated]))
    create_if_not_exists(index(:users, [:is_moderator]))
    create_if_not_exists(index(:users, [:is_admin]))
    create_if_not_exists(index(:users, [:subscribers]))
  end
end