Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.2KB

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