summaryrefslogtreecommitdiff
path: root/priv/repo/migrations
diff options
context:
space:
mode:
Diffstat (limited to 'priv/repo/migrations')
-rw-r--r--priv/repo/migrations/20191025081729_add_move_support_to_users.exs10
-rw-r--r--priv/repo/migrations/20191104133100_set_visible_service_actors.exs22
-rw-r--r--priv/repo/migrations/20191118084425_create_user_relationships.exs17
-rw-r--r--priv/repo/migrations/20191118084500_data_migration_populate_user_relationships.exs68
-rw-r--r--priv/repo/migrations/20191123103423_remove_info_from_users.exs9
-rw-r--r--priv/repo/migrations/20191128153944_fix_missing_following_count.exs53
6 files changed, 179 insertions, 0 deletions
diff --git a/priv/repo/migrations/20191025081729_add_move_support_to_users.exs b/priv/repo/migrations/20191025081729_add_move_support_to_users.exs
new file mode 100644
index 000000000..580b9eb0f
--- /dev/null
+++ b/priv/repo/migrations/20191025081729_add_move_support_to_users.exs
@@ -0,0 +1,10 @@
+defmodule Pleroma.Repo.Migrations.AddMoveSupportToUsers do
+ use Ecto.Migration
+
+ def change do
+ alter table(:users) do
+ add(:also_known_as, {:array, :string}, default: [], null: false)
+ add(:allow_following_move, :boolean, default: true, null: false)
+ end
+ end
+end
diff --git a/priv/repo/migrations/20191104133100_set_visible_service_actors.exs b/priv/repo/migrations/20191104133100_set_visible_service_actors.exs
new file mode 100644
index 000000000..62907093c
--- /dev/null
+++ b/priv/repo/migrations/20191104133100_set_visible_service_actors.exs
@@ -0,0 +1,22 @@
+defmodule Pleroma.Repo.Migrations.SetVisibleServiceActors do
+ use Ecto.Migration
+ import Ecto.Query
+ alias Pleroma.Repo
+
+ def up do
+ user_nicknames = ["relay", "internal.fetch"]
+
+ from(
+ u in "users",
+ where: u.nickname in ^user_nicknames,
+ update: [
+ set: [invisible: true]
+ ]
+ )
+ |> Repo.update_all([])
+ end
+
+ def down do
+ :ok
+ end
+end
diff --git a/priv/repo/migrations/20191118084425_create_user_relationships.exs b/priv/repo/migrations/20191118084425_create_user_relationships.exs
new file mode 100644
index 000000000..c281f887d
--- /dev/null
+++ b/priv/repo/migrations/20191118084425_create_user_relationships.exs
@@ -0,0 +1,17 @@
+defmodule Pleroma.Repo.Migrations.CreateUserRelationships do
+ use Ecto.Migration
+
+ def change do
+ create_if_not_exists table(:user_relationships) do
+ add(:source_id, references(:users, type: :uuid, on_delete: :delete_all))
+ add(:target_id, references(:users, type: :uuid, on_delete: :delete_all))
+ add(:relationship_type, :integer, null: false)
+
+ timestamps(updated_at: false)
+ end
+
+ create_if_not_exists(
+ unique_index(:user_relationships, [:source_id, :relationship_type, :target_id])
+ )
+ end
+end
diff --git a/priv/repo/migrations/20191118084500_data_migration_populate_user_relationships.exs b/priv/repo/migrations/20191118084500_data_migration_populate_user_relationships.exs
new file mode 100644
index 000000000..990e9f3b8
--- /dev/null
+++ b/priv/repo/migrations/20191118084500_data_migration_populate_user_relationships.exs
@@ -0,0 +1,68 @@
+defmodule Pleroma.Repo.Migrations.DataMigrationPopulateUserRelationships do
+ use Ecto.Migration
+
+ alias Ecto.Adapters.SQL
+ alias Pleroma.Repo
+
+ require Logger
+
+ def up do
+ Enum.each(
+ [blocks: 1, mutes: 2, muted_reblogs: 3, muted_notifications: 4, subscribers: 5],
+ fn {field, relationship_type_code} ->
+ migrate(field, relationship_type_code)
+
+ if field == :subscribers do
+ drop_if_exists(index(:users, [:subscribers]))
+ end
+ end
+ )
+ end
+
+ def down, do: :noop
+
+ defp migrate(field, relationship_type_code) do
+ Logger.info("Processing users.#{field}...")
+
+ {:ok, %{rows: field_rows}} =
+ SQL.query(Repo, "SELECT id, #{field} FROM users WHERE #{field} != '{}'")
+
+ target_ap_ids =
+ Enum.flat_map(
+ field_rows,
+ fn [_, ap_ids] -> ap_ids end
+ )
+ |> Enum.uniq()
+
+ # Selecting ids of all targets at once in order to reduce the number of SELECT queries
+ {:ok, %{rows: target_ap_id_id}} =
+ SQL.query(Repo, "SELECT ap_id, id FROM users WHERE ap_id = ANY($1)", [target_ap_ids])
+
+ target_id_by_ap_id = Enum.into(target_ap_id_id, %{}, fn [k, v] -> {k, v} end)
+
+ Enum.each(
+ field_rows,
+ fn [source_id, target_ap_ids] ->
+ source_uuid = Ecto.UUID.cast!(source_id)
+
+ for target_ap_id <- target_ap_ids do
+ target_id = target_id_by_ap_id[target_ap_id]
+
+ with {:ok, target_uuid} <- target_id && Ecto.UUID.cast(target_id) do
+ execute("""
+ INSERT INTO user_relationships(
+ source_id, target_id, relationship_type, inserted_at
+ )
+ VALUES(
+ '#{source_uuid}'::uuid, '#{target_uuid}'::uuid, #{relationship_type_code}, now()
+ )
+ ON CONFLICT (source_id, relationship_type, target_id) DO NOTHING
+ """)
+ else
+ _ -> Logger.warn("Unresolved #{field} reference: (#{source_uuid}, #{target_id})")
+ end
+ end
+ end
+ )
+ end
+end
diff --git a/priv/repo/migrations/20191123103423_remove_info_from_users.exs b/priv/repo/migrations/20191123103423_remove_info_from_users.exs
new file mode 100644
index 000000000..b251255ea
--- /dev/null
+++ b/priv/repo/migrations/20191123103423_remove_info_from_users.exs
@@ -0,0 +1,9 @@
+defmodule Pleroma.Repo.Migrations.RemoveInfoFromUsers do
+ use Ecto.Migration
+
+ def change do
+ alter table(:users) do
+ remove(:info, :map, default: %{})
+ end
+ end
+end
diff --git a/priv/repo/migrations/20191128153944_fix_missing_following_count.exs b/priv/repo/migrations/20191128153944_fix_missing_following_count.exs
new file mode 100644
index 000000000..3236de7a4
--- /dev/null
+++ b/priv/repo/migrations/20191128153944_fix_missing_following_count.exs
@@ -0,0 +1,53 @@
+defmodule Pleroma.Repo.Migrations.FixMissingFollowingCount do
+ use Ecto.Migration
+
+ def up do
+ """
+ UPDATE
+ users
+ SET
+ following_count = sub.count
+ FROM
+ (
+ SELECT
+ users.id AS sub_id
+ ,COUNT (following_relationships.id)
+ FROM
+ following_relationships
+ ,users
+ WHERE
+ users.id = following_relationships.follower_id
+ AND following_relationships.state = 'accept'
+ GROUP BY
+ users.id
+ ) AS sub
+ WHERE
+ users.id = sub.sub_id
+ AND users.local = TRUE
+ ;
+ """
+ |> execute()
+
+ """
+ UPDATE
+ users
+ SET
+ following_count = 0
+ WHERE
+ following_count IS NULL
+ """
+ |> execute()
+
+ execute("ALTER TABLE users
+ ALTER COLUMN following_count SET DEFAULT 0,
+ ALTER COLUMN following_count SET NOT NULL
+ ")
+ end
+
+ def down do
+ execute("ALTER TABLE users
+ ALTER COLUMN following_count DROP DEFAULT,
+ ALTER COLUMN following_count DROP NOT NULL
+ ")
+ end
+end