User info migration improvements

- Move column additions into a separate migration, so postgres doesn't
need an exclusive lock on the table for the main part
- Fill in columns by using one big update statement instead of a bunch
of small ones because it's much faster (the migration took 140s on
patch.cx database)
This commit is contained in:
rinpatch 2019-10-26 03:45:24 +03:00
parent 653054d301
commit 12ab7b3280
2 changed files with 75 additions and 64 deletions

View File

@ -0,0 +1,53 @@
defmodule Pleroma.Repo.Migrations.AddUsersInfoColumns do
use Ecto.Migration
@jsonb_array_default "'[]'::jsonb"
def change do
alter table(:users) do
add_if_not_exists(:banner, :map, default: %{})
add_if_not_exists(:background, :map, default: %{})
add_if_not_exists(:source_data, :map, default: %{})
add_if_not_exists(:note_count, :integer, default: 0)
add_if_not_exists(:follower_count, :integer, default: 0)
add_if_not_exists(:following_count, :integer, default: nil)
add_if_not_exists(:locked, :boolean, default: false, null: false)
add_if_not_exists(:confirmation_pending, :boolean, default: false, null: false)
add_if_not_exists(:password_reset_pending, :boolean, default: false, null: false)
add_if_not_exists(:confirmation_token, :text, default: nil)
add_if_not_exists(:default_scope, :string, default: "public")
add_if_not_exists(:blocks, {:array, :text}, default: [])
add_if_not_exists(:domain_blocks, {:array, :text}, default: [])
add_if_not_exists(:mutes, {:array, :text}, default: [])
add_if_not_exists(:muted_reblogs, {:array, :text}, default: [])
add_if_not_exists(:muted_notifications, {:array, :text}, default: [])
add_if_not_exists(:subscribers, {:array, :text}, default: [])
add_if_not_exists(:deactivated, :boolean, default: false, null: false)
add_if_not_exists(:no_rich_text, :boolean, default: false, null: false)
add_if_not_exists(:ap_enabled, :boolean, default: false, null: false)
add_if_not_exists(:is_moderator, :boolean, default: false, null: false)
add_if_not_exists(:is_admin, :boolean, default: false, null: false)
add_if_not_exists(:show_role, :boolean, default: true, null: false)
add_if_not_exists(:settings, :map, default: nil)
add_if_not_exists(:magic_key, :text, default: nil)
add_if_not_exists(:uri, :text, default: nil)
add_if_not_exists(:hide_followers_count, :boolean, default: false, null: false)
add_if_not_exists(:hide_follows_count, :boolean, default: false, null: false)
add_if_not_exists(:hide_followers, :boolean, default: false, null: false)
add_if_not_exists(:hide_follows, :boolean, default: false, null: false)
add_if_not_exists(:hide_favorites, :boolean, default: true, null: false)
add_if_not_exists(:unread_conversation_count, :integer, default: 0)
add_if_not_exists(:pinned_activities, {:array, :text}, default: [])
add_if_not_exists(:email_notifications, :map, default: %{"digest" => false})
add_if_not_exists(:mascot, :map, default: nil)
add_if_not_exists(:emoji, :map, default: fragment(@jsonb_array_default))
add_if_not_exists(:pleroma_settings_store, :map, default: %{})
add_if_not_exists(:fields, :map, default: fragment(@jsonb_array_default))
add_if_not_exists(:raw_fields, :map, default: fragment(@jsonb_array_default))
add_if_not_exists(:discoverable, :boolean, default: false, null: false)
add_if_not_exists(:invisible, :boolean, default: false, null: false)
add_if_not_exists(:notification_settings, :map, default: %{})
add_if_not_exists(:skip_thread_containment, :boolean, default: false, null: false)
end
end
end

View File

@ -95,79 +95,37 @@ defmodule Pleroma.Repo.Migrations.CopyUsersInfoFieldsToUsers do
] ]
def change do def change do
alter table(:users) do
add(:banner, :map, default: %{})
add(:background, :map, default: %{})
add(:source_data, :map, default: %{})
add(:note_count, :integer, default: 0)
add(:follower_count, :integer, default: 0)
add(:following_count, :integer, default: nil)
add(:locked, :boolean, default: false, null: false)
add(:confirmation_pending, :boolean, default: false, null: false)
add(:password_reset_pending, :boolean, default: false, null: false)
add(:confirmation_token, :text, default: nil)
add(:default_scope, :string, default: "public")
add(:blocks, {:array, :text}, default: [])
add(:domain_blocks, {:array, :text}, default: [])
add(:mutes, {:array, :text}, default: [])
add(:muted_reblogs, {:array, :text}, default: [])
add(:muted_notifications, {:array, :text}, default: [])
add(:subscribers, {:array, :text}, default: [])
add(:deactivated, :boolean, default: false, null: false)
add(:no_rich_text, :boolean, default: false, null: false)
add(:ap_enabled, :boolean, default: false, null: false)
add(:is_moderator, :boolean, default: false, null: false)
add(:is_admin, :boolean, default: false, null: false)
add(:show_role, :boolean, default: true, null: false)
add(:settings, :map, default: nil)
add(:magic_key, :text, default: nil)
add(:uri, :text, default: nil)
add(:hide_followers_count, :boolean, default: false, null: false)
add(:hide_follows_count, :boolean, default: false, null: false)
add(:hide_followers, :boolean, default: false, null: false)
add(:hide_follows, :boolean, default: false, null: false)
add(:hide_favorites, :boolean, default: true, null: false)
add(:unread_conversation_count, :integer, default: 0)
add(:pinned_activities, {:array, :text}, default: [])
add(:email_notifications, :map, default: %{"digest" => false})
add(:mascot, :map, default: nil)
add(:emoji, :map, default: fragment(@jsonb_array_default))
add(:pleroma_settings_store, :map, default: %{})
add(:fields, :map, default: fragment(@jsonb_array_default))
add(:raw_fields, :map, default: fragment(@jsonb_array_default))
add(:discoverable, :boolean, default: false, null: false)
add(:invisible, :boolean, default: false, null: false)
add(:notification_settings, :map, default: %{})
add(:skip_thread_containment, :boolean, default: false, null: false)
end
if direction() == :up do if direction() == :up do
sets =
for f <- @info_fields do for f <- @info_fields do
set_field = "update users set #{f} =" set_field = "#{f} ="
# Coercion of null::jsonb to NULL # Coercion of null::jsonb to NULL
jsonb = "case when info->>'#{f}' IS NULL then null else info->'#{f}' end" jsonb = "case when info->>'#{f}' IS NULL then null else info->'#{f}' end"
cond do cond do
f in @jsonb_fields -> f in @jsonb_fields ->
execute("#{set_field} #{jsonb}") "#{set_field} #{jsonb}"
f in @array_jsonb_fields -> f in @array_jsonb_fields ->
execute("#{set_field} coalesce(#{jsonb}, #{@jsonb_array_default})") "#{set_field} coalesce(#{jsonb}, #{@jsonb_array_default})"
f in @int_fields -> f in @int_fields ->
execute("#{set_field} (info->>'#{f}')::int") "#{set_field} (info->>'#{f}')::int"
f in @boolean_fields -> f in @boolean_fields ->
execute("#{set_field} coalesce((info->>'#{f}')::boolean, false)") "#{set_field} coalesce((info->>'#{f}')::boolean, false)"
f in @array_text_fields -> f in @array_text_fields ->
execute("#{set_field} ARRAY(SELECT jsonb_array_elements_text(#{jsonb}))") "#{set_field} ARRAY(SELECT jsonb_array_elements_text(#{jsonb}))"
true -> true ->
execute("#{set_field} info->>'#{f}'") "#{set_field} info->>'#{f}'"
end end
end end
|> Enum.join(", ")
execute("update users set " <> sets)
for index_name <- [ for index_name <- [
:users_deactivated_index, :users_deactivated_index,