diff options
Diffstat (limited to 'priv/repo')
34 files changed, 600 insertions, 5 deletions
diff --git a/priv/repo/migrations/20170426154155_create_websub_client_subscription.exs b/priv/repo/migrations/20170426154155_create_websub_client_subscription.exs index f42782840..89d3af7ae 100644 --- a/priv/repo/migrations/20170426154155_create_websub_client_subscription.exs +++ b/priv/repo/migrations/20170426154155_create_websub_client_subscription.exs @@ -5,7 +5,7 @@ defmodule Pleroma.Repo.Migrations.CreateWebsubClientSubscription do create table(:websub_client_subscriptions) do add :topic, :string add :secret, :string - add :valid_until, :naive_datetime + add :valid_until, :naive_datetime_usec add :state, :string add :subscribers, :map diff --git a/priv/repo/migrations/20170906143140_create_o_auth_authorizations.exs b/priv/repo/migrations/20170906143140_create_o_auth_authorizations.exs index b4332870e..ead1d023e 100644 --- a/priv/repo/migrations/20170906143140_create_o_auth_authorizations.exs +++ b/priv/repo/migrations/20170906143140_create_o_auth_authorizations.exs @@ -6,7 +6,7 @@ defmodule Pleroma.Repo.Migrations.CreateOAuthAuthorizations do add :app_id, references(:apps) add :user_id, references(:users) add :token, :string - add :valid_until, :naive_datetime + add :valid_until, :naive_datetime_usec add :used, :boolean, default: false timestamps() diff --git a/priv/repo/migrations/20170906152508_create_o_auth_token.exs b/priv/repo/migrations/20170906152508_create_o_auth_token.exs index 7f8550f33..ed56bbf36 100644 --- a/priv/repo/migrations/20170906152508_create_o_auth_token.exs +++ b/priv/repo/migrations/20170906152508_create_o_auth_token.exs @@ -7,7 +7,7 @@ defmodule Pleroma.Repo.Migrations.CreateOAuthToken do add :user_id, references(:users) add :token, :string add :refresh_token, :string - add :valid_until, :naive_datetime + add :valid_until, :naive_datetime_usec timestamps() end diff --git a/priv/repo/migrations/20180813003722_create_filters.exs b/priv/repo/migrations/20180813003722_create_filters.exs index a273004ee..8e7129f34 100644 --- a/priv/repo/migrations/20180813003722_create_filters.exs +++ b/priv/repo/migrations/20180813003722_create_filters.exs @@ -8,7 +8,7 @@ defmodule Pleroma.Repo.Migrations.CreateFilters do add :hide, :boolean add :phrase, :string add :context, {:array, :string} - add :expires_at, :datetime + add :expires_at, :utc_datetime add :whole_word, :boolean timestamps() diff --git a/priv/repo/migrations/20180918182427_create_push_subscriptions.exs b/priv/repo/migrations/20180918182427_create_push_subscriptions.exs new file mode 100644 index 000000000..0cc7afa54 --- /dev/null +++ b/priv/repo/migrations/20180918182427_create_push_subscriptions.exs @@ -0,0 +1,18 @@ +defmodule Pleroma.Repo.Migrations.CreatePushSubscriptions do + use Ecto.Migration + + def change do + create table("push_subscriptions") do + add :user_id, references("users", on_delete: :delete_all) + add :token_id, references("oauth_tokens", on_delete: :delete_all) + add :endpoint, :string + add :key_p256dh, :string + add :key_auth, :string + add :data, :map + + timestamps() + end + + create index("push_subscriptions", [:user_id, :token_id], unique: true) + end +end diff --git a/priv/repo/migrations/20180919060348_users_add_last_refreshed_at.exs b/priv/repo/migrations/20180919060348_users_add_last_refreshed_at.exs index 1942e4e9c..815177e05 100644 --- a/priv/repo/migrations/20180919060348_users_add_last_refreshed_at.exs +++ b/priv/repo/migrations/20180919060348_users_add_last_refreshed_at.exs @@ -3,7 +3,7 @@ defmodule Pleroma.Repo.Migrations.UsersAddLastRefreshedAt do def change do alter table(:users) do - add :last_refreshed_at, :naive_datetime + add :last_refreshed_at, :naive_datetime_usec end end end diff --git a/priv/repo/migrations/20181206125616_add_tags_to_users.exs b/priv/repo/migrations/20181206125616_add_tags_to_users.exs new file mode 100644 index 000000000..1502f63b6 --- /dev/null +++ b/priv/repo/migrations/20181206125616_add_tags_to_users.exs @@ -0,0 +1,11 @@ +defmodule Pleroma.Repo.Migrations.AddTagsToUsers do + use Ecto.Migration + + def change do + alter table(:users) do + add :tags, {:array, :string} + end + + create index(:users, [:tags], using: :gin) + end +end diff --git a/priv/repo/migrations/20181214121049_add_bookmarks_to_users.exs b/priv/repo/migrations/20181214121049_add_bookmarks_to_users.exs new file mode 100644 index 000000000..55e97ae0e --- /dev/null +++ b/priv/repo/migrations/20181214121049_add_bookmarks_to_users.exs @@ -0,0 +1,9 @@ +defmodule Pleroma.Repo.Migrations.AddBookmarksToUsers do + use Ecto.Migration + + def change do + alter table(:users) do + add :bookmarks, {:array, :string}, null: false, default: [] + end + end +end diff --git a/priv/repo/migrations/20181218172826_users_and_activities_flake_id.exs b/priv/repo/migrations/20181218172826_users_and_activities_flake_id.exs new file mode 100644 index 000000000..47d2d02da --- /dev/null +++ b/priv/repo/migrations/20181218172826_users_and_activities_flake_id.exs @@ -0,0 +1,125 @@ +defmodule Pleroma.Repo.Migrations.UsersAndActivitiesFlakeId do + use Ecto.Migration + alias Pleroma.Clippy + require Integer + import Ecto.Query + alias Pleroma.Repo + + # This migrates from int serial IDs to custom Flake: + # 1- create a temporary uuid column + # 2- fill this column with compatibility ids (see below) + # 3- remove pkeys constraints + # 4- update relation pkeys with the new ids + # 5- rename the temporary column to id + # 6- re-create the constraints + def change do + # Old serial int ids are transformed to 128bits with extra padding. + # The application (in `Pleroma.FlakeId`) handles theses IDs properly as integers; to keep compatibility + # with previously issued ids. + #execute "update activities set external_id = CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid);" + #execute "update users set external_id = CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid);" + + clippy = start_clippy_heartbeats() + + # Lock both tables to avoid a running server to meddling with our transaction + execute "LOCK TABLE activities;" + execute "LOCK TABLE users;" + + execute """ + ALTER TABLE activities + DROP CONSTRAINT activities_pkey CASCADE, + ALTER COLUMN id DROP default, + ALTER COLUMN id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid), + ADD PRIMARY KEY (id); + """ + + execute """ + ALTER TABLE users + DROP CONSTRAINT users_pkey CASCADE, + ALTER COLUMN id DROP default, + ALTER COLUMN id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(id), 32, '0' ) AS uuid), + ADD PRIMARY KEY (id); + """ + + execute "UPDATE users SET info = jsonb_set(info, '{pinned_activities}', array_to_json(ARRAY(select jsonb_array_elements_text(info->'pinned_activities')))::jsonb);" + + # Fkeys: + # Activities - Referenced by: + # TABLE "notifications" CONSTRAINT "notifications_activity_id_fkey" FOREIGN KEY (activity_id) REFERENCES activities(id) ON DELETE CASCADE + # Users - Referenced by: + # TABLE "filters" CONSTRAINT "filters_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + # TABLE "lists" CONSTRAINT "lists_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + # TABLE "notifications" CONSTRAINT "notifications_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + # TABLE "oauth_authorizations" CONSTRAINT "oauth_authorizations_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) + # TABLE "oauth_tokens" CONSTRAINT "oauth_tokens_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) + # TABLE "password_reset_tokens" CONSTRAINT "password_reset_tokens_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) + # TABLE "push_subscriptions" CONSTRAINT "push_subscriptions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + # TABLE "websub_client_subscriptions" CONSTRAINT "websub_client_subscriptions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) + + execute """ + ALTER TABLE notifications + ALTER COLUMN activity_id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(activity_id), 32, '0' ) AS uuid), + ADD CONSTRAINT notifications_activity_id_fkey FOREIGN KEY (activity_id) REFERENCES activities(id) ON DELETE CASCADE; + """ + + for table <- ~w(notifications filters lists oauth_authorizations oauth_tokens password_reset_tokens push_subscriptions websub_client_subscriptions) do + execute """ + ALTER TABLE #{table} + ALTER COLUMN user_id SET DATA TYPE uuid USING CAST( LPAD( TO_HEX(user_id), 32, '0' ) AS uuid), + ADD CONSTRAINT #{table}_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; + """ + end + + flush() + + stop_clippy_heartbeats(clippy) + end + + defp start_clippy_heartbeats() do + count = from(a in "activities", select: count(a.id)) |> Repo.one! + + if count > 5000 do + heartbeat_interval = :timer.minutes(2) + :timer.seconds(30) + all_tips = Clippy.tips() ++ [ + "The migration is still running, maybe it's time for another “tea”?", + "Happy rabbits practice a cute behavior known as a\n“binky:” they jump up in the air\nand twist\nand spin around!", + "Nothing and everything.\n\nI still work.", + "Pleroma runs on a Raspberry Pi!\n\n … but this migration will take forever if you\nactually run on a raspberry pi", + "Status? Stati? Post? Note? Toot?\nRepeat? Reboost? Boost? Retweet? Retoot??\n\nI-I'm confused.", + ] + + heartbeat = fn(heartbeat, runs, all_tips, tips) -> + tips = if Integer.is_even(runs) do + tips = if tips == [], do: all_tips, else: tips + [tip | tips] = Enum.shuffle(tips) + Clippy.puts(tip) + tips + else + IO.puts "\n -- #{DateTime.to_string(DateTime.utc_now())} Migration still running, please wait…\n" + tips + end + :timer.sleep(heartbeat_interval) + heartbeat.(heartbeat, runs + 1, all_tips, tips) + end + + Clippy.puts [ + [:red, :bright, "It looks like you are running an older instance!"], + [""], + [:bright, "This migration may take a long time", :reset, " -- so you probably should"], + ["go drink a cofe, or a tea, or a beer, a whiskey, a vodka,"], + ["while it runs to deal with your temporary fediverse pause!"] + ] + :timer.sleep(heartbeat_interval) + spawn_link(fn() -> heartbeat.(heartbeat, 1, all_tips, []) end) + end + end + + defp stop_clippy_heartbeats(pid) do + if pid do + Process.unlink(pid) + Process.exit(pid, :kill) + Clippy.puts [[:green, :bright, "Hurray!!", "", "", "Migration completed!"]] + end + end + +end diff --git a/priv/repo/migrations/20190109152453_add_visibility_function.exs b/priv/repo/migrations/20190109152453_add_visibility_function.exs new file mode 100644 index 000000000..3aadabcd7 --- /dev/null +++ b/priv/repo/migrations/20190109152453_add_visibility_function.exs @@ -0,0 +1,48 @@ +defmodule Pleroma.Repo.Migrations.AddVisibilityFunction do + use Ecto.Migration + @disable_ddl_transaction true + + def up do + definition = """ + create or replace function activity_visibility(actor varchar, recipients varchar[], data jsonb) returns varchar as $$ + DECLARE + fa varchar; + public varchar := 'https://www.w3.org/ns/activitystreams#Public'; + BEGIN + SELECT COALESCE(users.follower_address, '') into fa from users where users.ap_id = actor; + + IF data->'to' ? public THEN + RETURN 'public'; + ELSIF data->'cc' ? public THEN + RETURN 'unlisted'; + ELSIF ARRAY[fa] && recipients THEN + RETURN 'private'; + ELSIF not(ARRAY[fa, public] && recipients) THEN + RETURN 'direct'; + ELSE + RETURN 'unknown'; + END IF; + END; + $$ LANGUAGE plpgsql IMMUTABLE; + """ + + execute(definition) + + create( + index(:activities, ["activity_visibility(actor, recipients, data)"], + name: :activities_visibility_index, + concurrently: true + ) + ) + end + + def down do + drop( + index(:activities, ["activity_visibility(actor, recipients, data)"], + name: :activities_visibility_index + ) + ) + + execute("drop function activity_visibility(actor varchar, recipients varchar[], data jsonb)") + end +end diff --git a/priv/repo/migrations/20190115085500_create_user_fts_index.exs b/priv/repo/migrations/20190115085500_create_user_fts_index.exs new file mode 100644 index 000000000..499d67113 --- /dev/null +++ b/priv/repo/migrations/20190115085500_create_user_fts_index.exs @@ -0,0 +1,17 @@ +defmodule Pleroma.Repo.Migrations.CreateUserFtsIndex do + use Ecto.Migration + + def change do + create index( + :users, + [ + """ + (setweight(to_tsvector('simple', regexp_replace(nickname, '\\W', ' ', 'g')), 'A') || + setweight(to_tsvector('simple', regexp_replace(coalesce(name, ''), '\\W', ' ', 'g')), 'B')) + """ + ], + name: :users_fts_index, + using: :gin + ) + end +end diff --git a/priv/repo/migrations/20190118074940_fix_user_trigram_index.exs b/priv/repo/migrations/20190118074940_fix_user_trigram_index.exs new file mode 100644 index 000000000..b4e8c984c --- /dev/null +++ b/priv/repo/migrations/20190118074940_fix_user_trigram_index.exs @@ -0,0 +1,22 @@ +defmodule Pleroma.Repo.Migrations.FixUserTrigramIndex do + use Ecto.Migration + + def up do + drop_if_exists(index(:users, [], name: :users_trigram_index)) + + create( + index(:users, ["(trim(nickname || ' ' || coalesce(name, ''))) gist_trgm_ops"], + name: :users_trigram_index, + using: :gist + ) + ) + end + + def down do + drop_if_exists(index(:users, [], name: :users_trigram_index)) + + create( + index(:users, ["(nickname || name) gist_trgm_ops"], name: :users_trigram_index, using: :gist) + ) + end +end diff --git a/priv/repo/migrations/20190122153157_update_activity_visibility.exs b/priv/repo/migrations/20190122153157_update_activity_visibility.exs new file mode 100644 index 000000000..30075137c --- /dev/null +++ b/priv/repo/migrations/20190122153157_update_activity_visibility.exs @@ -0,0 +1,36 @@ +defmodule Pleroma.Repo.Migrations.UpdateActivityVisibility do + use Ecto.Migration + @disable_ddl_transaction true + + def up do + definition = """ + create or replace function activity_visibility(actor varchar, recipients varchar[], data jsonb) returns varchar as $$ + DECLARE + fa varchar; + public varchar := 'https://www.w3.org/ns/activitystreams#Public'; + BEGIN + SELECT COALESCE(users.follower_address, '') into fa from users where users.ap_id = actor; + + IF data->'to' ? public THEN + RETURN 'public'; + ELSIF data->'cc' ? public THEN + RETURN 'unlisted'; + ELSIF ARRAY[fa] && recipients THEN + RETURN 'private'; + ELSIF not(ARRAY[fa, public] && recipients) THEN + RETURN 'direct'; + ELSE + RETURN 'unknown'; + END IF; + END; + $$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE SECURITY DEFINER; + """ + + execute(definition) + + end + + def down do + + end +end diff --git a/priv/repo/migrations/20190123092341_users_add_is_admin_index.exs b/priv/repo/migrations/20190123092341_users_add_is_admin_index.exs new file mode 100644 index 000000000..ba6ff78b5 --- /dev/null +++ b/priv/repo/migrations/20190123092341_users_add_is_admin_index.exs @@ -0,0 +1,7 @@ +defmodule Pleroma.Repo.Migrations.UsersAddIsAdminIndex do + use Ecto.Migration + + def change do + create(index(:users, ["(info->'is_admin')"], name: :users_is_admin_index, using: :gin)) + end +end diff --git a/priv/repo/migrations/20190123125546_create_instances.exs b/priv/repo/migrations/20190123125546_create_instances.exs new file mode 100644 index 000000000..3d23b343e --- /dev/null +++ b/priv/repo/migrations/20190123125546_create_instances.exs @@ -0,0 +1,15 @@ +defmodule Pleroma.Repo.Migrations.CreateInstances do + use Ecto.Migration + + def change do + create table(:instances) do + add :host, :string + add :unreachable_since, :naive_datetime_usec + + timestamps() + end + + create unique_index(:instances, [:host]) + create index(:instances, [:unreachable_since]) + end +end diff --git a/priv/repo/migrations/20190123125839_fix_info_ids.exs b/priv/repo/migrations/20190123125839_fix_info_ids.exs new file mode 100644 index 000000000..2b4c2b5a9 --- /dev/null +++ b/priv/repo/migrations/20190123125839_fix_info_ids.exs @@ -0,0 +1,9 @@ +defmodule Pleroma.Repo.Migrations.FixInfoIds do + use Ecto.Migration + + def change do + execute( + "update users set info = jsonb_set(info, '{id}', to_jsonb(uuid_generate_v4())) where info->'id' is null;" + ) + end +end diff --git a/priv/repo/migrations/20190124131141_update_activity_visibility_again.exs b/priv/repo/migrations/20190124131141_update_activity_visibility_again.exs new file mode 100644 index 000000000..0519a5143 --- /dev/null +++ b/priv/repo/migrations/20190124131141_update_activity_visibility_again.exs @@ -0,0 +1,37 @@ +defmodule Pleroma.Repo.Migrations.UpdateActivityVisibilityAgain do + use Ecto.Migration + @disable_ddl_transaction true + + def up do + definition = """ + create or replace function activity_visibility(actor varchar, recipients varchar[], data jsonb) returns varchar as $$ + DECLARE + fa varchar; + public varchar := 'https://www.w3.org/ns/activitystreams#Public'; + BEGIN + SELECT COALESCE(users.follower_address, '') into fa from public.users where users.ap_id = actor; + + IF data->'to' ? public THEN + RETURN 'public'; + ELSIF data->'cc' ? public THEN + RETURN 'unlisted'; + ELSIF ARRAY[fa] && recipients THEN + RETURN 'private'; + ELSIF not(ARRAY[fa, public] && recipients) THEN + RETURN 'direct'; + ELSE + RETURN 'unknown'; + END IF; + END; + $$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE SECURITY DEFINER; + """ + + execute(definition) + + end + + def down do + + end + +end diff --git a/priv/repo/migrations/20190126160540_change_push_subscriptions_varchar.exs b/priv/repo/migrations/20190126160540_change_push_subscriptions_varchar.exs new file mode 100644 index 000000000..337fed156 --- /dev/null +++ b/priv/repo/migrations/20190126160540_change_push_subscriptions_varchar.exs @@ -0,0 +1,9 @@ +defmodule Pleroma.Repo.Migrations.ChangePushSubscriptionsVarchar do + use Ecto.Migration + + def change do + alter table(:push_subscriptions) do + modify(:endpoint, :varchar) + end + end +end diff --git a/priv/repo/migrations/20190127151220_add_activities_likes_index.exs b/priv/repo/migrations/20190127151220_add_activities_likes_index.exs new file mode 100644 index 000000000..b1822d265 --- /dev/null +++ b/priv/repo/migrations/20190127151220_add_activities_likes_index.exs @@ -0,0 +1,8 @@ +defmodule Pleroma.Repo.Migrations.AddActivitiesLikesIndex do + use Ecto.Migration + @disable_ddl_transaction true + + def change do + create index(:activities, ["((data #> '{\"object\",\"likes\"}'))"], concurrently: true, name: :activities_likes, using: :gin) + end +end diff --git a/priv/repo/migrations/20190203185340_split_hide_network.exs b/priv/repo/migrations/20190203185340_split_hide_network.exs new file mode 100644 index 000000000..8b7a9151b --- /dev/null +++ b/priv/repo/migrations/20190203185340_split_hide_network.exs @@ -0,0 +1,12 @@ +defmodule Pleroma.Repo.Migrations.SplitHideNetwork do + use Ecto.Migration + + def up do + execute("UPDATE users SET info = jsonb_set(info, '{hide_network}'::text[], 'false'::jsonb) WHERE NOT(info::jsonb ? 'hide_network') AND local") + execute("UPDATE users SET info = jsonb_set(info, '{hide_followings}'::text[], info->'hide_network') WHERE local") + execute("UPDATE users SET info = jsonb_set(info, '{hide_followers}'::text[], info->'hide_network') WHERE local") + end + + def down do + end +end diff --git a/priv/repo/migrations/20190204200237_add_correct_dm_index.exs b/priv/repo/migrations/20190204200237_add_correct_dm_index.exs new file mode 100644 index 000000000..558732cd2 --- /dev/null +++ b/priv/repo/migrations/20190204200237_add_correct_dm_index.exs @@ -0,0 +1,30 @@ +defmodule Pleroma.Repo.Migrations.AddCorrectDMIndex do + use Ecto.Migration + @disable_ddl_transaction true + + def up do + drop_if_exists( + index(:activities, ["activity_visibility(actor, recipients, data)"], + name: :activities_visibility_index + ) + ) + + create( + index(:activities, ["activity_visibility(actor, recipients, data)", "id DESC NULLS LAST"], + name: :activities_visibility_index, + concurrently: true, + where: "data->>'type' = 'Create'" + ) + ) + end + + def down do + drop( + index(:activities, ["activity_visibility(actor, recipients, data)", "id DESC"], + name: :activities_visibility_index, + concurrently: true, + where: "data->>'type' = 'Create'" + ) + ) + end +end diff --git a/priv/repo/migrations/20190205114625_create_thread_mutes.exs b/priv/repo/migrations/20190205114625_create_thread_mutes.exs new file mode 100644 index 000000000..8e9eccbae --- /dev/null +++ b/priv/repo/migrations/20190205114625_create_thread_mutes.exs @@ -0,0 +1,12 @@ +defmodule Pleroma.Repo.Migrations.CreateThreadMutes do + use Ecto.Migration + + def change do + create table(:thread_mutes) do + add :user_id, references(:users, type: :uuid, on_delete: :delete_all) + add :context, :string + end + + create unique_index(:thread_mutes, [:user_id, :context], name: :unique_index) + end +end diff --git a/priv/repo/migrations/20190208131753_add_scopes_to_o_auth_entities.exs b/priv/repo/migrations/20190208131753_add_scopes_to_o_auth_entities.exs new file mode 100644 index 000000000..4efbebc4d --- /dev/null +++ b/priv/repo/migrations/20190208131753_add_scopes_to_o_auth_entities.exs @@ -0,0 +1,11 @@ +defmodule Pleroma.Repo.Migrations.AddScopeSToOAuthEntities do + use Ecto.Migration + + def change do + for t <- [:oauth_authorizations, :oauth_tokens] do + alter table(t) do + add :scopes, {:array, :string}, default: [], null: false + end + end + end +end diff --git a/priv/repo/migrations/20190213185503_change_apps_scopes_to_varchar_array.exs b/priv/repo/migrations/20190213185503_change_apps_scopes_to_varchar_array.exs new file mode 100644 index 000000000..72decd401 --- /dev/null +++ b/priv/repo/migrations/20190213185503_change_apps_scopes_to_varchar_array.exs @@ -0,0 +1,17 @@ +defmodule Pleroma.Repo.Migrations.ChangeAppsScopesToVarcharArray do + use Ecto.Migration + + @alter_apps_scopes "ALTER TABLE apps ALTER COLUMN scopes" + + def up do + execute "#{@alter_apps_scopes} TYPE varchar(255)[] USING string_to_array(scopes, ',')::varchar(255)[];" + execute "#{@alter_apps_scopes} SET DEFAULT ARRAY[]::character varying[];" + execute "#{@alter_apps_scopes} SET NOT NULL;" + end + + def down do + execute "#{@alter_apps_scopes} DROP NOT NULL;" + execute "#{@alter_apps_scopes} DROP DEFAULT;" + execute "#{@alter_apps_scopes} TYPE varchar(255) USING array_to_string(scopes, ',')::varchar(255);" + end +end diff --git a/priv/repo/migrations/20190213185600_data_migration_populate_o_auth_scopes.exs b/priv/repo/migrations/20190213185600_data_migration_populate_o_auth_scopes.exs new file mode 100644 index 000000000..7afbcbd76 --- /dev/null +++ b/priv/repo/migrations/20190213185600_data_migration_populate_o_auth_scopes.exs @@ -0,0 +1,11 @@ +defmodule Pleroma.Repo.Migrations.DataMigrationPopulateOAuthScopes do + use Ecto.Migration + + def up do + for t <- [:oauth_authorizations, :oauth_tokens] do + execute "UPDATE #{t} SET scopes = apps.scopes FROM apps WHERE #{t}.app_id = apps.id;" + end + end + + def down, do: :noop +end diff --git a/priv/repo/migrations/20190222104808_data_migration_normalize_scopes.exs b/priv/repo/migrations/20190222104808_data_migration_normalize_scopes.exs new file mode 100644 index 000000000..d44e5096b --- /dev/null +++ b/priv/repo/migrations/20190222104808_data_migration_normalize_scopes.exs @@ -0,0 +1,11 @@ +defmodule Pleroma.Repo.Migrations.DataMigrationNormalizeScopes do + use Ecto.Migration + + def up do + for t <- [:apps, :oauth_authorizations, :oauth_tokens] do + execute "UPDATE #{t} SET scopes = string_to_array(array_to_string(scopes, ' '), ' ');" + end + end + + def down, do: :noop +end diff --git a/priv/repo/migrations/20190301101154_add_default_tags_to_user.exs b/priv/repo/migrations/20190301101154_add_default_tags_to_user.exs new file mode 100644 index 000000000..faeb8f1c6 --- /dev/null +++ b/priv/repo/migrations/20190301101154_add_default_tags_to_user.exs @@ -0,0 +1,9 @@ +defmodule Pleroma.Repo.Migrations.AddDefaultTagsToUser do + use Ecto.Migration + + def up do + execute "UPDATE users SET tags = array[]::varchar[] where tags IS NULL" + end + + def down, do: :noop +end diff --git a/priv/repo/migrations/20190303120636_update_user_note_counters.exs b/priv/repo/migrations/20190303120636_update_user_note_counters.exs new file mode 100644 index 000000000..54e68f7c9 --- /dev/null +++ b/priv/repo/migrations/20190303120636_update_user_note_counters.exs @@ -0,0 +1,41 @@ +defmodule Pleroma.Repo.Migrations.UpdateUserNoteCounters do + use Ecto.Migration + + @public "https://www.w3.org/ns/activitystreams#Public" + + def up do + execute """ + WITH public_note_count AS ( + SELECT + data->>'actor' AS actor, + count(id) AS count + FROM objects + WHERE data->>'type' = 'Note' AND ( + data->'cc' ? '#{@public}' OR data->'to' ? '#{@public}' + ) + GROUP BY data->>'actor' + ) + UPDATE users AS u + SET "info" = jsonb_set(u.info, '{note_count}', o.count::varchar::jsonb, true) + FROM public_note_count AS o + WHERE u.ap_id = o.actor + """ + end + + def down do + execute """ + WITH public_note_count AS ( + SELECT + data->>'actor' AS actor, + count(id) AS count + FROM objects + WHERE data->>'type' = 'Note' + GROUP BY data->>'actor' + ) + UPDATE users AS u + SET "info" = jsonb_set(u.info, '{note_count}', o.count::varchar::jsonb, true) + FROM public_note_count AS o + WHERE u.ap_id = o.actor + """ + end +end diff --git a/priv/repo/migrations/20190315101315_create_registrations.exs b/priv/repo/migrations/20190315101315_create_registrations.exs new file mode 100644 index 000000000..6b28cbdd3 --- /dev/null +++ b/priv/repo/migrations/20190315101315_create_registrations.exs @@ -0,0 +1,18 @@ +defmodule Pleroma.Repo.Migrations.CreateRegistrations do + use Ecto.Migration + + def change do + create table(:registrations, primary_key: false) do + add :id, :uuid, primary_key: true + add :user_id, references(:users, type: :uuid, on_delete: :delete_all) + add :provider, :string + add :uid, :string + add :info, :map, default: %{} + + timestamps() + end + + create unique_index(:registrations, [:provider, :uid]) + create unique_index(:registrations, [:user_id, :provider, :uid]) + end +end diff --git a/priv/repo/migrations/20190325185009_create_notification_id_index.exs b/priv/repo/migrations/20190325185009_create_notification_id_index.exs new file mode 100644 index 000000000..a6ab38d02 --- /dev/null +++ b/priv/repo/migrations/20190325185009_create_notification_id_index.exs @@ -0,0 +1,7 @@ +defmodule Pleroma.Repo.Migrations.CreateNotificationIdIndex do + use Ecto.Migration + + def change do + create index(:notifications, ["id desc nulls last"]) + end +end diff --git a/priv/repo/migrations/20190328053912_create_scheduled_activities.exs b/priv/repo/migrations/20190328053912_create_scheduled_activities.exs new file mode 100644 index 000000000..dd737e25a --- /dev/null +++ b/priv/repo/migrations/20190328053912_create_scheduled_activities.exs @@ -0,0 +1,16 @@ +defmodule Pleroma.Repo.Migrations.CreateScheduledActivities do + use Ecto.Migration + + def change do + create table(:scheduled_activities) do + add(:user_id, references(:users, type: :uuid, on_delete: :delete_all)) + add(:scheduled_at, :naive_datetime, null: false) + add(:params, :map, null: false) + + timestamps() + end + + create(index(:scheduled_activities, [:scheduled_at])) + create(index(:scheduled_activities, [:user_id])) + end +end diff --git a/priv/repo/migrations/20190403131720_add_oauth_token_indexes.exs b/priv/repo/migrations/20190403131720_add_oauth_token_indexes.exs new file mode 100644 index 000000000..ebcd29389 --- /dev/null +++ b/priv/repo/migrations/20190403131720_add_oauth_token_indexes.exs @@ -0,0 +1,9 @@ +defmodule Pleroma.Repo.Migrations.AddOauthTokenIndexes do + use Ecto.Migration + + def change do + create(unique_index(:oauth_tokens, [:token])) + create(index(:oauth_tokens, [:app_id])) + create(index(:oauth_tokens, [:user_id])) + end +end diff --git a/priv/repo/migrations/20190404050946_add_fields_to_user_invite_tokens.exs b/priv/repo/migrations/20190404050946_add_fields_to_user_invite_tokens.exs new file mode 100644 index 000000000..211a14135 --- /dev/null +++ b/priv/repo/migrations/20190404050946_add_fields_to_user_invite_tokens.exs @@ -0,0 +1,12 @@ +defmodule Pleroma.Repo.Migrations.AddFieldsToUserInviteTokens do + use Ecto.Migration + + def change do + alter table(:user_invite_tokens) do + add(:expires_at, :date) + add(:uses, :integer, default: 0) + add(:max_use, :integer) + add(:invite_type, :string, default: "one_time") + end + end +end diff --git a/priv/repo/migrations/20190405160700_add_index_on_subscribers.exs b/priv/repo/migrations/20190405160700_add_index_on_subscribers.exs new file mode 100644 index 000000000..232f75c92 --- /dev/null +++ b/priv/repo/migrations/20190405160700_add_index_on_subscribers.exs @@ -0,0 +1,8 @@ +defmodule Pleroma.Repo.Migrations.AddIndexOnSubscribers do + use Ecto.Migration + + @disable_ddl_transaction true + def change do + create index(:users, ["(info->'subscribers')"], name: :users_subscribers_index, using: :gin, concurrently: true) + end +end |
