summaryrefslogtreecommitdiff
path: root/priv/repo/migrations
diff options
context:
space:
mode:
Diffstat (limited to 'priv/repo/migrations')
-rw-r--r--priv/repo/migrations/20190414125034_migrate_old_bookmarks.exs6
-rw-r--r--priv/repo/migrations/20190710125158_add_following_address_from_source_data.exs8
-rw-r--r--priv/repo/migrations/20190711042024_copy_muted_to_muted_notifications.exs2
-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/20191123030554_add_activitypub_actor_type.exs9
-rw-r--r--priv/repo/migrations/20191203043610_create_report_notes.exs13
-rw-r--r--priv/repo/migrations/20191220174645_add_scopes_to_pleroma_feo_auth_records.exs17
-rw-r--r--priv/repo/migrations/20200109123126_add_counter_cache_table.exs55
-rw-r--r--priv/repo/migrations/20200307103755_fix_moderation_log_subjects.exs9
-rw-r--r--priv/repo/migrations/20200314123607_config_remove_fetch_initial_posts.exs10
-rw-r--r--priv/repo/migrations/20200315125756_delete_fetch_initial_posts_jobs.exs10
12 files changed, 221 insertions, 3 deletions
diff --git a/priv/repo/migrations/20190414125034_migrate_old_bookmarks.exs b/priv/repo/migrations/20190414125034_migrate_old_bookmarks.exs
index 99102117f..c618ea381 100644
--- a/priv/repo/migrations/20190414125034_migrate_old_bookmarks.exs
+++ b/priv/repo/migrations/20190414125034_migrate_old_bookmarks.exs
@@ -17,7 +17,11 @@ defmodule Pleroma.Repo.Migrations.MigrateOldBookmarks do
Repo.stream(query)
|> Enum.each(fn %{id: user_id, bookmarks: bookmarks} ->
Enum.each(bookmarks, fn ap_id ->
- activity = Activity.get_create_by_object_ap_id(ap_id)
+ activity =
+ ap_id
+ |> Activity.create_by_object_ap_id()
+ |> Repo.one()
+
unless is_nil(activity), do: {:ok, _} = Bookmark.create(user_id, activity.id)
end)
end)
diff --git a/priv/repo/migrations/20190710125158_add_following_address_from_source_data.exs b/priv/repo/migrations/20190710125158_add_following_address_from_source_data.exs
index a5170d521..44f9891b1 100644
--- a/priv/repo/migrations/20190710125158_add_following_address_from_source_data.exs
+++ b/priv/repo/migrations/20190710125158_add_following_address_from_source_data.exs
@@ -1,7 +1,8 @@
defmodule Pleroma.Repo.Migrations.AddFollowingAddressFromSourceData do
- use Ecto.Migration
- import Ecto.Query
alias Pleroma.User
+ import Ecto.Query
+ require Logger
+ use Ecto.Migration
def change do
query =
@@ -19,6 +20,9 @@ defmodule Pleroma.Repo.Migrations.AddFollowingAddressFromSourceData do
:following_address
])
|> Pleroma.Repo.update()
+
+ user ->
+ Logger.warn("User #{user.id} / #{user.nickname} does not seem to have source_data")
end)
end
end
diff --git a/priv/repo/migrations/20190711042024_copy_muted_to_muted_notifications.exs b/priv/repo/migrations/20190711042024_copy_muted_to_muted_notifications.exs
index fc9bf70ba..bbd502044 100644
--- a/priv/repo/migrations/20190711042024_copy_muted_to_muted_notifications.exs
+++ b/priv/repo/migrations/20190711042024_copy_muted_to_muted_notifications.exs
@@ -2,6 +2,8 @@ defmodule Pleroma.Repo.Migrations.CopyMutedToMutedNotifications do
use Ecto.Migration
def change do
+ execute("update users set info = '{}' where info is null")
+
execute(
"update users set info = safe_jsonb_set(info, '{muted_notifications}', info->'mutes', true) where local = true"
)
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/20191123030554_add_activitypub_actor_type.exs b/priv/repo/migrations/20191123030554_add_activitypub_actor_type.exs
new file mode 100644
index 000000000..76d3b32c4
--- /dev/null
+++ b/priv/repo/migrations/20191123030554_add_activitypub_actor_type.exs
@@ -0,0 +1,9 @@
+defmodule Pleroma.Repo.Migrations.AddActivitypubActorType do
+ use Ecto.Migration
+
+ def change do
+ alter table("users") do
+ add(:actor_type, :string, null: false, default: "Person")
+ end
+ end
+end
diff --git a/priv/repo/migrations/20191203043610_create_report_notes.exs b/priv/repo/migrations/20191203043610_create_report_notes.exs
new file mode 100644
index 000000000..a4f8c096d
--- /dev/null
+++ b/priv/repo/migrations/20191203043610_create_report_notes.exs
@@ -0,0 +1,13 @@
+defmodule Pleroma.Repo.Migrations.CreateReportNotes do
+ use Ecto.Migration
+
+ def change do
+ create_if_not_exists table(:report_notes) do
+ add(:user_id, references(:users, type: :uuid))
+ add(:activity_id, references(:activities, type: :uuid))
+ add(:content, :string)
+
+ timestamps()
+ end
+ end
+end
diff --git a/priv/repo/migrations/20191220174645_add_scopes_to_pleroma_feo_auth_records.exs b/priv/repo/migrations/20191220174645_add_scopes_to_pleroma_feo_auth_records.exs
new file mode 100644
index 000000000..6b160ad16
--- /dev/null
+++ b/priv/repo/migrations/20191220174645_add_scopes_to_pleroma_feo_auth_records.exs
@@ -0,0 +1,17 @@
+defmodule Pleroma.Repo.Migrations.AddScopesToPleromaFEOAuthRecords do
+ use Ecto.Migration
+
+ def up do
+ update_scopes_clause = "SET scopes = '{read,write,follow,push,admin}'"
+ apps_where = "WHERE apps.client_name like 'PleromaFE_%' or apps.client_name like 'AdminFE_%'"
+ app_id_subquery_where = "WHERE app_id IN (SELECT apps.id FROM apps #{apps_where})"
+
+ execute("UPDATE apps #{update_scopes_clause} #{apps_where}")
+
+ for table <- ["oauth_authorizations", "oauth_tokens"] do
+ execute("UPDATE #{table} #{update_scopes_clause} #{app_id_subquery_where}")
+ end
+ end
+
+ def down, do: :noop
+end
diff --git a/priv/repo/migrations/20200109123126_add_counter_cache_table.exs b/priv/repo/migrations/20200109123126_add_counter_cache_table.exs
new file mode 100644
index 000000000..df9e21193
--- /dev/null
+++ b/priv/repo/migrations/20200109123126_add_counter_cache_table.exs
@@ -0,0 +1,55 @@
+defmodule Pleroma.Repo.Migrations.AddCounterCacheTable do
+ use Ecto.Migration
+
+ def up do
+ create_if_not_exists table(:counter_cache) do
+ add(:name, :string, null: false)
+ add(:count, :bigint, null: false, default: 0)
+ end
+
+ create_if_not_exists(unique_index(:counter_cache, [:name]))
+
+ """
+ CREATE OR REPLACE FUNCTION update_status_visibility_counter_cache()
+ RETURNS TRIGGER AS
+ $$
+ DECLARE
+ BEGIN
+ IF TG_OP = 'INSERT' THEN
+ IF NEW.data->>'type' = 'Create' THEN
+ EXECUTE 'INSERT INTO counter_cache (name, count) VALUES (''status_visibility_' || activity_visibility(NEW.actor, NEW.recipients, NEW.data) || ''', 1) ON CONFLICT (name) DO UPDATE SET count = counter_cache.count + 1';
+ END IF;
+ RETURN NEW;
+ ELSIF TG_OP = 'UPDATE' THEN
+ IF (NEW.data->>'type' = 'Create') and (OLD.data->>'type' = 'Create') and activity_visibility(NEW.actor, NEW.recipients, NEW.data) != activity_visibility(OLD.actor, OLD.recipients, OLD.data) THEN
+ EXECUTE 'INSERT INTO counter_cache (name, count) VALUES (''status_visibility_' || activity_visibility(NEW.actor, NEW.recipients, NEW.data) || ''', 1) ON CONFLICT (name) DO UPDATE SET count = counter_cache.count + 1';
+ EXECUTE 'update counter_cache SET count = counter_cache.count - 1 where count > 0 and name = ''status_visibility_' || activity_visibility(OLD.actor, OLD.recipients, OLD.data) || ''';';
+ END IF;
+ RETURN NEW;
+ ELSIF TG_OP = 'DELETE' THEN
+ IF OLD.data->>'type' = 'Create' THEN
+ EXECUTE 'update counter_cache SET count = counter_cache.count - 1 where count > 0 and name = ''status_visibility_' || activity_visibility(OLD.actor, OLD.recipients, OLD.data) || ''';';
+ END IF;
+ RETURN OLD;
+ END IF;
+ END;
+ $$
+ LANGUAGE 'plpgsql';
+ """
+ |> execute()
+
+ """
+ CREATE TRIGGER status_visibility_counter_cache_trigger BEFORE INSERT OR UPDATE of recipients, data OR DELETE ON activities
+ FOR EACH ROW
+ EXECUTE PROCEDURE update_status_visibility_counter_cache();
+ """
+ |> execute()
+ end
+
+ def down do
+ execute("drop trigger if exists status_visibility_counter_cache_trigger on activities")
+ execute("drop function if exists update_status_visibility_counter_cache()")
+ drop_if_exists(unique_index(:counter_cache, [:name]))
+ drop_if_exists(table(:counter_cache))
+ end
+end
diff --git a/priv/repo/migrations/20200307103755_fix_moderation_log_subjects.exs b/priv/repo/migrations/20200307103755_fix_moderation_log_subjects.exs
new file mode 100644
index 000000000..d1c8539e1
--- /dev/null
+++ b/priv/repo/migrations/20200307103755_fix_moderation_log_subjects.exs
@@ -0,0 +1,9 @@
+defmodule Pleroma.Repo.Migrations.FixModerationLogSubjects do
+ use Ecto.Migration
+
+ def change do
+ execute(
+ "update moderation_log set data = safe_jsonb_set(data, '{subject}', safe_jsonb_set('[]'::jsonb, '{0}', data->'subject')) where jsonb_typeof(data->'subject') != 'array' and data->>'action' = ANY('{revoke,grant,activate,deactivate,delete}');"
+ )
+ end
+end
diff --git a/priv/repo/migrations/20200314123607_config_remove_fetch_initial_posts.exs b/priv/repo/migrations/20200314123607_config_remove_fetch_initial_posts.exs
new file mode 100644
index 000000000..392f531e8
--- /dev/null
+++ b/priv/repo/migrations/20200314123607_config_remove_fetch_initial_posts.exs
@@ -0,0 +1,10 @@
+defmodule Pleroma.Repo.Migrations.ConfigRemoveFetchInitialPosts do
+ use Ecto.Migration
+
+ def change do
+ execute(
+ "delete from config where config.key = ':fetch_initial_posts' and config.group = ':pleroma';",
+ ""
+ )
+ end
+end
diff --git a/priv/repo/migrations/20200315125756_delete_fetch_initial_posts_jobs.exs b/priv/repo/migrations/20200315125756_delete_fetch_initial_posts_jobs.exs
new file mode 100644
index 000000000..5b8e3ab91
--- /dev/null
+++ b/priv/repo/migrations/20200315125756_delete_fetch_initial_posts_jobs.exs
@@ -0,0 +1,10 @@
+defmodule Pleroma.Repo.Migrations.DeleteFetchInitialPostsJobs do
+ use Ecto.Migration
+
+ def change do
+ execute(
+ "delete from oban_jobs where worker = 'Pleroma.Workers.BackgroundWorker' and args->>'op' = 'fetch_initial_posts';",
+ ""
+ )
+ end
+end