Merge branch 'develop' into feature/store-statuses-data-inside-flag
[akkoma] / priv / repo / migrations / 20191009154608_copy_users_info_fields_to_users.exs
1 defmodule Pleroma.Repo.Migrations.CopyUsersInfoFieldsToUsers do
2 use Ecto.Migration
3
4 @jsonb_array_default "'[]'::jsonb"
5
6 @info_fields [
7 :banner,
8 :background,
9 :source_data,
10 :note_count,
11 :follower_count,
12 :following_count,
13 :locked,
14 :confirmation_pending,
15 :password_reset_pending,
16 :confirmation_token,
17 :default_scope,
18 :blocks,
19 :domain_blocks,
20 :mutes,
21 :muted_reblogs,
22 :muted_notifications,
23 :subscribers,
24 :deactivated,
25 :no_rich_text,
26 :ap_enabled,
27 :is_moderator,
28 :is_admin,
29 :show_role,
30 :settings,
31 :magic_key,
32 :uri,
33 :hide_followers_count,
34 :hide_follows_count,
35 :hide_followers,
36 :hide_follows,
37 :hide_favorites,
38 :unread_conversation_count,
39 :pinned_activities,
40 :email_notifications,
41 :mascot,
42 :emoji,
43 :pleroma_settings_store,
44 :fields,
45 :raw_fields,
46 :discoverable,
47 :invisible,
48 :skip_thread_containment,
49 :notification_settings
50 ]
51
52 @jsonb_fields [
53 :banner,
54 :background,
55 :source_data,
56 :settings,
57 :email_notifications,
58 :mascot,
59 :pleroma_settings_store,
60 :notification_settings
61 ]
62
63 @array_jsonb_fields [:emoji, :fields, :raw_fields]
64
65 @int_fields [:note_count, :follower_count, :following_count, :unread_conversation_count]
66
67 @boolean_fields [
68 :locked,
69 :confirmation_pending,
70 :password_reset_pending,
71 :deactivated,
72 :no_rich_text,
73 :ap_enabled,
74 :is_moderator,
75 :is_admin,
76 :show_role,
77 :hide_followers_count,
78 :hide_follows_count,
79 :hide_followers,
80 :hide_follows,
81 :hide_favorites,
82 :discoverable,
83 :invisible,
84 :skip_thread_containment
85 ]
86
87 @array_text_fields [
88 :blocks,
89 :domain_blocks,
90 :mutes,
91 :muted_reblogs,
92 :muted_notifications,
93 :subscribers,
94 :pinned_activities
95 ]
96
97 def change do
98 if direction() == :up do
99 sets =
100 for f <- @info_fields do
101 set_field = "#{f} ="
102
103 # Coercion of null::jsonb to NULL
104 jsonb = "case when info->>'#{f}' IS NULL then null else info->'#{f}' end"
105
106 cond do
107 f in @jsonb_fields ->
108 "#{set_field} #{jsonb}"
109
110 f in @array_jsonb_fields ->
111 "#{set_field} coalesce(#{jsonb}, #{@jsonb_array_default})"
112
113 f in @int_fields ->
114 "#{set_field} (info->>'#{f}')::int"
115
116 f in @boolean_fields ->
117 "#{set_field} coalesce((info->>'#{f}')::boolean, false)"
118
119 f in @array_text_fields ->
120 "#{set_field} ARRAY(SELECT jsonb_array_elements_text(#{jsonb}))"
121
122 true ->
123 "#{set_field} info->>'#{f}'"
124 end
125 end
126 |> Enum.join(", ")
127
128 execute("update users set " <> sets)
129
130 for index_name <- [
131 :users_deactivated_index,
132 :users_is_moderator_index,
133 :users_is_admin_index,
134 :users_subscribers_index
135 ] do
136 drop_if_exists(index(:users, [], name: index_name))
137 end
138 end
139
140 create_if_not_exists(index(:users, [:deactivated]))
141 create_if_not_exists(index(:users, [:is_moderator]))
142 create_if_not_exists(index(:users, [:is_admin]))
143 create_if_not_exists(index(:users, [:subscribers]))
144 end
145 end