logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

20191009154608_copy_users_info_fields_to_users.exs (3443B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Repo.Migrations.CopyUsersInfoFieldsToUsers do
  5. use Ecto.Migration
  6. @jsonb_array_default "'[]'::jsonb"
  7. @info_fields [
  8. :banner,
  9. :background,
  10. :source_data,
  11. :note_count,
  12. :follower_count,
  13. :following_count,
  14. :locked,
  15. :confirmation_pending,
  16. :password_reset_pending,
  17. :confirmation_token,
  18. :default_scope,
  19. :blocks,
  20. :domain_blocks,
  21. :mutes,
  22. :muted_reblogs,
  23. :muted_notifications,
  24. :subscribers,
  25. :deactivated,
  26. :no_rich_text,
  27. :ap_enabled,
  28. :is_moderator,
  29. :is_admin,
  30. :show_role,
  31. :settings,
  32. :magic_key,
  33. :uri,
  34. :hide_followers_count,
  35. :hide_follows_count,
  36. :hide_followers,
  37. :hide_follows,
  38. :hide_favorites,
  39. :unread_conversation_count,
  40. :pinned_activities,
  41. :email_notifications,
  42. :mascot,
  43. :emoji,
  44. :pleroma_settings_store,
  45. :fields,
  46. :raw_fields,
  47. :discoverable,
  48. :invisible,
  49. :skip_thread_containment,
  50. :notification_settings
  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. @array_jsonb_fields [:emoji, :fields, :raw_fields]
  63. @int_fields [:note_count, :follower_count, :following_count, :unread_conversation_count]
  64. @boolean_fields [
  65. :locked,
  66. :confirmation_pending,
  67. :password_reset_pending,
  68. :deactivated,
  69. :no_rich_text,
  70. :ap_enabled,
  71. :is_moderator,
  72. :is_admin,
  73. :show_role,
  74. :hide_followers_count,
  75. :hide_follows_count,
  76. :hide_followers,
  77. :hide_follows,
  78. :hide_favorites,
  79. :discoverable,
  80. :invisible,
  81. :skip_thread_containment
  82. ]
  83. @array_text_fields [
  84. :blocks,
  85. :domain_blocks,
  86. :mutes,
  87. :muted_reblogs,
  88. :muted_notifications,
  89. :subscribers,
  90. :pinned_activities
  91. ]
  92. def change do
  93. if direction() == :up do
  94. sets =
  95. for f <- @info_fields do
  96. set_field = "#{f} ="
  97. # Coercion of null::jsonb to NULL
  98. jsonb = "case when info->>'#{f}' IS NULL then null else info->'#{f}' end"
  99. cond do
  100. f in @jsonb_fields ->
  101. "#{set_field} #{jsonb}"
  102. f in @array_jsonb_fields ->
  103. "#{set_field} coalesce(#{jsonb}, #{@jsonb_array_default})"
  104. f in @int_fields ->
  105. "#{set_field} (info->>'#{f}')::int"
  106. f in @boolean_fields ->
  107. "#{set_field} coalesce((info->>'#{f}')::boolean, false)"
  108. f in @array_text_fields ->
  109. "#{set_field} ARRAY(SELECT jsonb_array_elements_text(#{jsonb}))"
  110. true ->
  111. "#{set_field} info->>'#{f}'"
  112. end
  113. end
  114. |> Enum.join(", ")
  115. execute("update users set " <> sets)
  116. for index_name <- [
  117. :users_deactivated_index,
  118. :users_is_moderator_index,
  119. :users_is_admin_index,
  120. :users_subscribers_index
  121. ] do
  122. drop_if_exists(index(:users, [], name: index_name))
  123. end
  124. end
  125. create_if_not_exists(index(:users, [:deactivated]))
  126. create_if_not_exists(index(:users, [:is_moderator]))
  127. create_if_not_exists(index(:users, [:is_admin]))
  128. create_if_not_exists(index(:users, [:subscribers]))
  129. end
  130. end