[#1033] Fix database migrations

This commit is contained in:
Maksim 2019-07-01 01:08:07 +00:00 committed by feld
parent 66380d36d5
commit 85c7916c94
70 changed files with 151 additions and 103 deletions

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreatePleroma.User do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:users) do create_if_not_exists table(:users) do
add :email, :string add :email, :string
add :password_hash, :string add :password_hash, :string
add :name, :string add :name, :string

View file

@ -2,13 +2,13 @@ defmodule Pleroma.Repo.Migrations.CreatePleroma.Activity do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:activities) do create_if_not_exists table(:activities) do
add :data, :map add :data, :map
timestamps() timestamps()
end end
create index(:activities, [:data], using: :gin) create_if_not_exists index(:activities, [:data], using: :gin)
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreatePleroma.Object do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:objects) do create_if_not_exists table(:objects) do
add :data, :map add :data, :map
timestamps() timestamps()

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.AddIndexToObjects do
use Ecto.Migration use Ecto.Migration
def change do def change do
create index(:objects, [:data], using: :gin) create_if_not_exists index(:objects, [:data], using: :gin)
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.AddUniqueIndexToEmailAndNickname do
use Ecto.Migration use Ecto.Migration
def change do def change do
create unique_index(:users, [:email]) create_if_not_exists unique_index(:users, [:email])
create unique_index(:users, [:nickname]) create_if_not_exists unique_index(:users, [:nickname])
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreateWebsubServerSubscription do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:websub_server_subscriptions) do create_if_not_exists table(:websub_server_subscriptions) do
add :topic, :string add :topic, :string
add :callback, :string add :callback, :string
add :secret, :string add :secret, :string

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreateWebsubClientSubscription do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:websub_client_subscriptions) do create_if_not_exists table(:websub_client_subscriptions) do
add :topic, :string add :topic, :string
add :secret, :string add :secret, :string
add :valid_until, :naive_datetime_usec add :valid_until, :naive_datetime_usec

View file

@ -1,10 +1,12 @@
defmodule Pleroma.Repo.Migrations.AddIdContraintsToActivitiesAndObjectsPartTwo do defmodule Pleroma.Repo.Migrations.AddIdContraintsToActivitiesAndObjectsPartTwo do
use Ecto.Migration use Ecto.Migration
def change do def up do
drop_if_exists index(:objects, ["(data->>\"id\")"], name: :objects_unique_apid_index) drop_if_exists index(:objects, ["(data->>\"id\")"], name: :objects_unique_apid_index)
drop_if_exists index(:activities, ["(data->>\"id\")"], name: :activities_unique_apid_index) drop_if_exists index(:activities, ["(data->>\"id\")"], name: :activities_unique_apid_index)
create unique_index(:objects, ["(data->>'id')"], name: :objects_unique_apid_index) create_if_not_exists unique_index(:objects, ["(data->>'id')"], name: :objects_unique_apid_index)
create unique_index(:activities, ["(data->>'id')"], name: :activities_unique_apid_index) create_if_not_exists unique_index(:activities, ["(data->>'id')"], name: :activities_unique_apid_index)
end end
def down, do: :ok
end end

View file

@ -6,6 +6,6 @@ defmodule Pleroma.Repo.Migrations.AddLocalFieldToActivities do
add :local, :boolean, default: true add :local, :boolean, default: true
end end
create index(:activities, [:local]) create_if_not_exists index(:activities, [:local])
end end
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.AddUniqueIndexToAPID do
use Ecto.Migration use Ecto.Migration
def change do def change do
create unique_index(:users, [:ap_id]) create_if_not_exists unique_index(:users, [:ap_id])
end end
end end

View file

@ -1,9 +1,16 @@
defmodule Pleroma.Repo.Migrations.LongerBios do defmodule Pleroma.Repo.Migrations.LongerBios do
use Ecto.Migration use Ecto.Migration
def change do def up do
alter table(:users) do alter table(:users) do
modify :bio, :text modify :bio, :text
end end
end end
def down do
alter table(:users) do
modify :bio, :string
end
end
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.RemoveActivitiesIndex do
use Ecto.Migration use Ecto.Migration
def change do def change do
drop index(:activities, [:data]) drop_if_exists index(:activities, [:data])
end end
end end

View file

@ -3,6 +3,6 @@ defmodule Pleroma.Repo.Migrations.AddObjectActivityIndex do
def change do def change do
# This was wrong, now a noop # This was wrong, now a noop
# create index(:objects, ["(data->'object'->>'id')", "(data->>'type')"], name: :activities_create_objects_index) # create_if_not_exists index(:objects, ["(data->'object'->>'id')", "(data->>'type')"], name: :activities_create_objects_index)
end end
end end

View file

@ -3,6 +3,6 @@ defmodule Pleroma.Repo.Migrations.AddObjectActivityIndexPartTwo do
def change do def change do
drop_if_exists index(:objects, ["(data->'object'->>'id')", "(data->>'type')"], name: :activities_create_objects_index) drop_if_exists index(:objects, ["(data->'object'->>'id')", "(data->>'type')"], name: :activities_create_objects_index)
create index(:activities, ["(data->'object'->>'id')", "(data->>'type')"], name: :activities_create_objects_index) create_if_not_exists index(:activities, ["(data->'object'->>'id')", "(data->>'type')"], name: :activities_create_objects_index)
end end
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.AddActorIndexToActivity do
use Ecto.Migration use Ecto.Migration
def change do def change do
create index(:activities, ["(data->>'actor')", "inserted_at desc"], name: :activities_actor_index) create_if_not_exists index(:activities, ["(data->>'actor')", "inserted_at desc"], name: :activities_actor_index)
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.AddMastodonApps do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:apps) do create_if_not_exists table(:apps) do
add :client_name, :string add :client_name, :string
add :redirect_uris, :string add :redirect_uris, :string
add :scopes, :string add :scopes, :string

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreateOAuthAuthorizations do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:oauth_authorizations) do create_if_not_exists table(:oauth_authorizations) do
add :app_id, references(:apps) add :app_id, references(:apps)
add :user_id, references(:users) add :user_id, references(:users)
add :token, :string add :token, :string

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreateOAuthToken do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:oauth_tokens) do create_if_not_exists table(:oauth_tokens) do
add :app_id, references(:apps) add :app_id, references(:apps)
add :user_id, references(:users) add :user_id, references(:users)
add :token, :string add :token, :string

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreateNotifications do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:notifications) do create_if_not_exists table(:notifications) do
add :user_id, references(:users, on_delete: :delete_all) add :user_id, references(:users, on_delete: :delete_all)
add :activity_id, references(:activities, on_delete: :delete_all) add :activity_id, references(:activities, on_delete: :delete_all)
add :seen, :boolean, default: false add :seen, :boolean, default: false
@ -10,6 +10,6 @@ defmodule Pleroma.Repo.Migrations.CreateNotifications do
timestamps() timestamps()
end end
create index(:notifications, [:user_id]) create_if_not_exists index(:notifications, [:user_id])
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreatePasswordResetTokens do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:password_reset_tokens) do create_if_not_exists table(:password_reset_tokens) do
add :token, :string add :token, :string
add :user_id, references(:users) add :user_id, references(:users)
add :used, :boolean, default: false add :used, :boolean, default: false

View file

@ -12,7 +12,7 @@ defmodule Pleroma.Repo.Migrations.AddActorToActivity do
end end
def down do def down do
drop index(:activities, [:actor, "id DESC NULLS LAST"]) drop_if_exists index(:activities, [:actor, "id DESC NULLS LAST"])
alter table(:activities) do alter table(:activities) do
remove :actor remove :actor
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.AddLocalIndexToUser do
use Ecto.Migration use Ecto.Migration
def change do def change do
create index(:users, [:local]) create_if_not_exists index(:users, [:local])
end end
end end

View file

@ -6,6 +6,6 @@ defmodule Pleroma.Repo.Migrations.AddRecipientsToActivities do
add :recipients, {:array, :string} add :recipients, {:array, :string}
end end
create index(:activities, [:recipients], using: :gin) create_if_not_exists index(:activities, [:recipients], using: :gin)
end end
end end

View file

@ -18,4 +18,6 @@ defmodule Pleroma.Repo.Migrations.FillRecipientsInActivities do
end) end)
end end
end end
def down, do: :ok
end end

View file

@ -1,7 +1,7 @@
defmodule Pleroma.Repo.Migrations.MakeFollowingPostgresArray do defmodule Pleroma.Repo.Migrations.MakeFollowingPostgresArray do
use Ecto.Migration use Ecto.Migration
def change do def up do
alter table(:users) do alter table(:users) do
add :following_temp, {:array, :string} add :following_temp, {:array, :string}
end end
@ -15,4 +15,6 @@ defmodule Pleroma.Repo.Migrations.MakeFollowingPostgresArray do
end end
rename table(:users), :following_temp, to: :following rename table(:users), :following_temp, to: :following
end end
def down, do: :ok
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.DropLocalIndexOnActivities do
use Ecto.Migration use Ecto.Migration
def change do def change do
drop index(:users, [:local]) drop_if_exists index(:users, [:local])
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.ActuallyDropLocalIndex do
use Ecto.Migration use Ecto.Migration
def change do def change do
create index(:users, [:local]) create_if_not_exists index(:users, [:local])
drop_if_exists index("activities", :local) drop_if_exists index("activities", :local)
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreateLists do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:lists) do create_if_not_exists table(:lists) do
add :user_id, references(:users, on_delete: :delete_all) add :user_id, references(:users, on_delete: :delete_all)
add :title, :string add :title, :string
add :following, {:array, :string} add :following, {:array, :string}
@ -10,6 +10,6 @@ defmodule Pleroma.Repo.Migrations.CreateLists do
timestamps() timestamps()
end end
create index(:lists, [:user_id]) create_if_not_exists index(:lists, [:user_id])
end end
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.CreateUserTrigramIndex do
use Ecto.Migration use Ecto.Migration
def change do def change do
create index(:users, ["(nickname || name) gist_trgm_ops"], name: :users_trigram_index, using: :gist) create_if_not_exists index(:users, ["(nickname || name) gist_trgm_ops"], name: :users_trigram_index, using: :gist)
end end
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.AddListFollowIndex do
use Ecto.Migration use Ecto.Migration
def change do def change do
create index(:lists, [:following]) create_if_not_exists index(:lists, [:following])
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreateUserInviteTokens do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:user_invite_tokens) do create_if_not_exists table(:user_invite_tokens) do
add :token, :string add :token, :string
add :used, :boolean, default: false add :used, :boolean, default: false

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreateFilters do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:filters) do create_if_not_exists table(:filters) do
add :user_id, references(:users, on_delete: :delete_all) add :user_id, references(:users, on_delete: :delete_all)
add :filter_id, :integer add :filter_id, :integer
add :hide, :boolean add :hide, :boolean
@ -14,7 +14,7 @@ defmodule Pleroma.Repo.Migrations.CreateFilters do
timestamps() timestamps()
end end
create index(:filters, [:user_id]) create_if_not_exists index(:filters, [:user_id])
create index(:filters, [:phrase], where: "hide = true", name: :hided_phrases_index) create_if_not_exists index(:filters, [:phrase], where: "hide = true", name: :hided_phrases_index)
end end
end end

View file

@ -7,7 +7,7 @@ defmodule Pleroma.Repo.Migrations.AddRecipientsToAndCcFieldsToActivities do
add :recipients_cc, {:array, :string} add :recipients_cc, {:array, :string}
end end
create index(:activities, [:recipients_to], using: :gin) create_if_not_exists index(:activities, [:recipients_to], using: :gin)
create index(:activities, [:recipients_cc], using: :gin) create_if_not_exists index(:activities, [:recipients_cc], using: :gin)
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.ActivitiesAddToCcIndices do
use Ecto.Migration use Ecto.Migration
def change do def change do
create index(:activities, ["(data->'to')"], name: :activities_to_index, using: :gin) create_if_not_exists index(:activities, ["(data->'to')"], name: :activities_to_index, using: :gin)
create index(:activities, ["(data->'cc')"], name: :activities_cc_index, using: :gin) create_if_not_exists index(:activities, ["(data->'cc')"], name: :activities_cc_index, using: :gin)
end end
end end

View file

@ -1,10 +1,17 @@
defmodule Pleroma.Repo.Migrations.RemoveRecipientsToAndCcFieldsFromActivities do defmodule Pleroma.Repo.Migrations.RemoveRecipientsToAndCcFieldsFromActivities do
use Ecto.Migration use Ecto.Migration
def change do def up do
alter table(:activities) do alter table(:activities) do
remove :recipients_to remove :recipients_to
remove :recipients_cc remove :recipients_cc
end end
end end
def down do
alter table(:activities) do
add :recipients_to, {:array, :string}
add :recipients_cc, {:array, :string}
end
end
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.UsersAddIsModeratorIndex do
use Ecto.Migration use Ecto.Migration
def change do def change do
create index(:users, ["(info->'is_moderator')"], name: :users_is_moderator_index, using: :gin) create_if_not_exists index(:users, ["(info->'is_moderator')"], name: :users_is_moderator_index, using: :gin)
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreatePushSubscriptions do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table("push_subscriptions") do create_if_not_exists table("push_subscriptions") do
add :user_id, references("users", on_delete: :delete_all) add :user_id, references("users", on_delete: :delete_all)
add :token_id, references("oauth_tokens", on_delete: :delete_all) add :token_id, references("oauth_tokens", on_delete: :delete_all)
add :endpoint, :string add :endpoint, :string
@ -13,6 +13,6 @@ defmodule Pleroma.Repo.Migrations.CreatePushSubscriptions do
timestamps() timestamps()
end end
create index("push_subscriptions", [:user_id, :token_id], unique: true) create_if_not_exists index("push_subscriptions", [:user_id, :token_id], unique: true)
end end
end end

View file

@ -1,7 +1,9 @@
defmodule Pleroma.Repo.Migrations.AddUUIDExtension do defmodule Pleroma.Repo.Migrations.AddUUIDExtension do
use Ecto.Migration use Ecto.Migration
def change do def up do
execute("create extension if not exists \"uuid-ossp\"") execute("create extension if not exists \"uuid-ossp\"")
end end
def down, do: :ok
end end

View file

@ -1,7 +1,9 @@
defmodule Pleroma.Repo.Migrations.AddUUIDsToUserInfo do defmodule Pleroma.Repo.Migrations.AddUUIDsToUserInfo do
use Ecto.Migration use Ecto.Migration
def change do def up do
execute("update users set info = jsonb_set(info, '{\"id\"}', to_jsonb(uuid_generate_v4()))") execute("update users set info = jsonb_set(info, '{\"id\"}', to_jsonb(uuid_generate_v4()))")
end end
def down, do: :ok
end end

View file

@ -6,6 +6,6 @@ defmodule Pleroma.Repo.Migrations.AddTagsToUsers do
add :tags, {:array, :string} add :tags, {:array, :string}
end end
create index(:users, [:tags], using: :gin) create_if_not_exists index(:users, [:tags], using: :gin)
end end
end end

View file

@ -12,7 +12,7 @@ defmodule Pleroma.Repo.Migrations.UsersAndActivitiesFlakeId do
# 4- update relation pkeys with the new ids # 4- update relation pkeys with the new ids
# 5- rename the temporary column to id # 5- rename the temporary column to id
# 6- re-create the constraints # 6- re-create the constraints
def change do def up do
# Old serial int ids are transformed to 128bits with extra padding. # 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 # The application (in `Pleroma.FlakeId`) handles theses IDs properly as integers; to keep compatibility
# with previously issued ids. # with previously issued ids.
@ -75,6 +75,8 @@ defmodule Pleroma.Repo.Migrations.UsersAndActivitiesFlakeId do
stop_clippy_heartbeats(clippy) stop_clippy_heartbeats(clippy)
end end
def down, do: :ok
defp start_clippy_heartbeats() do defp start_clippy_heartbeats() do
count = from(a in "activities", select: count(a.id)) |> Repo.one! count = from(a in "activities", select: count(a.id)) |> Repo.one!

View file

@ -37,12 +37,12 @@ defmodule Pleroma.Repo.Migrations.AddVisibilityFunction do
end end
def down do def down do
drop( drop_if_exists(
index(:activities, ["activity_visibility(actor, recipients, data)"], index(:activities, ["activity_visibility(actor, recipients, data)"],
name: :activities_visibility_index name: :activities_visibility_index
) )
) )
execute("drop function activity_visibility(actor varchar, recipients varchar[], data jsonb)") execute("drop function if exists activity_visibility(actor varchar, recipients varchar[], data jsonb)")
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreateUserFtsIndex do
use Ecto.Migration use Ecto.Migration
def change do def change do
create index( create_if_not_exists index(
:users, :users,
[ [
""" """

View file

@ -4,7 +4,7 @@ defmodule Pleroma.Repo.Migrations.FixUserTrigramIndex do
def up do def up do
drop_if_exists(index(:users, [], name: :users_trigram_index)) drop_if_exists(index(:users, [], name: :users_trigram_index))
create( create_if_not_exists(
index(:users, ["(trim(nickname || ' ' || coalesce(name, ''))) gist_trgm_ops"], index(:users, ["(trim(nickname || ' ' || coalesce(name, ''))) gist_trgm_ops"],
name: :users_trigram_index, name: :users_trigram_index,
using: :gist using: :gist
@ -15,7 +15,7 @@ defmodule Pleroma.Repo.Migrations.FixUserTrigramIndex do
def down do def down do
drop_if_exists(index(:users, [], name: :users_trigram_index)) drop_if_exists(index(:users, [], name: :users_trigram_index))
create( create_if_not_exists(
index(:users, ["(nickname || name) gist_trgm_ops"], name: :users_trigram_index, using: :gist) index(:users, ["(nickname || name) gist_trgm_ops"], name: :users_trigram_index, using: :gist)
) )
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.UsersAddIsAdminIndex do
use Ecto.Migration use Ecto.Migration
def change do def change do
create(index(:users, ["(info->'is_admin')"], name: :users_is_admin_index, using: :gin)) create_if_not_exists(index(:users, ["(info->'is_admin')"], name: :users_is_admin_index, using: :gin))
end end
end end

View file

@ -2,14 +2,14 @@ defmodule Pleroma.Repo.Migrations.CreateInstances do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:instances) do create_if_not_exists table(:instances) do
add :host, :string add :host, :string
add :unreachable_since, :naive_datetime_usec add :unreachable_since, :naive_datetime_usec
timestamps() timestamps()
end end
create unique_index(:instances, [:host]) create_if_not_exists unique_index(:instances, [:host])
create index(:instances, [:unreachable_since]) create_if_not_exists index(:instances, [:unreachable_since])
end end
end end

View file

@ -1,9 +1,11 @@
defmodule Pleroma.Repo.Migrations.FixInfoIds do defmodule Pleroma.Repo.Migrations.FixInfoIds do
use Ecto.Migration use Ecto.Migration
def change do def up do
execute( execute(
"update users set info = jsonb_set(info, '{id}', to_jsonb(uuid_generate_v4())) where info->'id' is null;" "update users set info = jsonb_set(info, '{id}', to_jsonb(uuid_generate_v4())) where info->'id' is null;"
) )
end end
def down, do: :ok
end end

View file

@ -1,9 +1,15 @@
defmodule Pleroma.Repo.Migrations.ChangePushSubscriptionsVarchar do defmodule Pleroma.Repo.Migrations.ChangePushSubscriptionsVarchar do
use Ecto.Migration use Ecto.Migration
def change do def up do
alter table(:push_subscriptions) do alter table(:push_subscriptions) do
modify(:endpoint, :varchar) modify(:endpoint, :varchar)
end end
end end
def down do
alter table(:push_subscriptions) do
modify(:endpoint, :string)
end
end
end end

View file

@ -19,7 +19,7 @@ defmodule Pleroma.Repo.Migrations.AddCorrectDMIndex do
end end
def down do def down do
drop( drop_if_exists(
index(:activities, ["activity_visibility(actor, recipients, data)", "id DESC"], index(:activities, ["activity_visibility(actor, recipients, data)", "id DESC"],
name: :activities_visibility_index, name: :activities_visibility_index,
concurrently: true, concurrently: true,

View file

@ -2,11 +2,11 @@ defmodule Pleroma.Repo.Migrations.CreateThreadMutes do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:thread_mutes) do create_if_not_exists table(:thread_mutes) do
add :user_id, references(:users, type: :uuid, on_delete: :delete_all) add :user_id, references(:users, type: :uuid, on_delete: :delete_all)
add :context, :string add :context, :string
end end
create unique_index(:thread_mutes, [:user_id, :context], name: :unique_index) create_if_not_exists unique_index(:thread_mutes, [:user_id, :context], name: :unique_index)
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreateRegistrations do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:registrations, primary_key: false) do create_if_not_exists table(:registrations, primary_key: false) do
add :id, :uuid, primary_key: true add :id, :uuid, primary_key: true
add :user_id, references(:users, type: :uuid, on_delete: :delete_all) add :user_id, references(:users, type: :uuid, on_delete: :delete_all)
add :provider, :string add :provider, :string
@ -12,7 +12,7 @@ defmodule Pleroma.Repo.Migrations.CreateRegistrations do
timestamps() timestamps()
end end
create unique_index(:registrations, [:provider, :uid]) create_if_not_exists unique_index(:registrations, [:provider, :uid])
create unique_index(:registrations, [:user_id, :provider, :uid]) create_if_not_exists unique_index(:registrations, [:user_id, :provider, :uid])
end end
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.CreateNotificationIdIndex do
use Ecto.Migration use Ecto.Migration
def change do def change do
create index(:notifications, ["id desc nulls last"]) create_if_not_exists index(:notifications, ["id desc nulls last"])
end end
end end

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.CreateScheduledActivities do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:scheduled_activities) do create_if_not_exists table(:scheduled_activities) do
add(:user_id, references(:users, type: :uuid, on_delete: :delete_all)) add(:user_id, references(:users, type: :uuid, on_delete: :delete_all))
add(:scheduled_at, :naive_datetime, null: false) add(:scheduled_at, :naive_datetime, null: false)
add(:params, :map, null: false) add(:params, :map, null: false)
@ -10,7 +10,7 @@ defmodule Pleroma.Repo.Migrations.CreateScheduledActivities do
timestamps() timestamps()
end end
create(index(:scheduled_activities, [:scheduled_at])) create_if_not_exists(index(:scheduled_activities, [:scheduled_at]))
create(index(:scheduled_activities, [:user_id])) create_if_not_exists(index(:scheduled_activities, [:user_id]))
end end
end end

View file

@ -2,8 +2,8 @@ defmodule Pleroma.Repo.Migrations.AddOauthTokenIndexes do
use Ecto.Migration use Ecto.Migration
def change do def change do
create(unique_index(:oauth_tokens, [:token])) create_if_not_exists(unique_index(:oauth_tokens, [:token]))
create(index(:oauth_tokens, [:app_id])) create_if_not_exists(index(:oauth_tokens, [:app_id]))
create(index(:oauth_tokens, [:user_id])) create_if_not_exists(index(:oauth_tokens, [:user_id]))
end end
end end

View file

@ -1,6 +1,6 @@
defmodule Pleroma.Repo.Migrations.AddIndexOnSubscribers do defmodule Pleroma.Repo.Migrations.AddIndexOnSubscribers do
use Ecto.Migration use Ecto.Migration
@disable_ddl_transaction true @disable_ddl_transaction true
def change do def change do
create index(:users, ["(info->'subscribers')"], name: :users_subscribers_index, using: :gin, concurrently: true) create index(:users, ["(info->'subscribers')"], name: :users_subscribers_index, using: :gin, concurrently: true)

View file

@ -6,12 +6,12 @@ defmodule Pleroma.Repo.Migrations.CreateConversations do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:conversations) do create_if_not_exists table(:conversations) do
add(:ap_id, :string, null: false) add(:ap_id, :string, null: false)
timestamps() timestamps()
end end
create table(:conversation_participations) do create_if_not_exists table(:conversation_participations) do
add(:user_id, references(:users, type: :uuid, on_delete: :delete_all)) add(:user_id, references(:users, type: :uuid, on_delete: :delete_all))
add(:conversation_id, references(:conversations, on_delete: :delete_all)) add(:conversation_id, references(:conversations, on_delete: :delete_all))
add(:read, :boolean, default: false) add(:read, :boolean, default: false)
@ -19,8 +19,8 @@ defmodule Pleroma.Repo.Migrations.CreateConversations do
timestamps() timestamps()
end end
create index(:conversation_participations, [:conversation_id]) create_if_not_exists index(:conversation_participations, [:conversation_id])
create unique_index(:conversation_participations, [:user_id, :conversation_id]) create_if_not_exists unique_index(:conversation_participations, [:user_id, :conversation_id])
create unique_index(:conversations, [:ap_id]) create_if_not_exists unique_index(:conversations, [:ap_id])
end end
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.AddParticipationUpdatedAtIndex do
use Ecto.Migration use Ecto.Migration
def change do def change do
create index(:conversation_participations, ["updated_at desc"]) create_if_not_exists index(:conversation_participations, ["updated_at desc"])
end end
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.AddIndexOnUserInfoDeactivated do
use Ecto.Migration use Ecto.Migration
def change do def change do
create(index(:users, ["(info->'deactivated')"], name: :users_deactivated_index, using: :gin)) create_if_not_exists(index(:users, ["(info->'deactivated')"], name: :users_deactivated_index, using: :gin))
end end
end end

View file

@ -2,13 +2,13 @@ defmodule Pleroma.Repo.Migrations.CreateBookmarks do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:bookmarks) do create_if_not_exists table(:bookmarks) do
add(:user_id, references(:users, type: :uuid, on_delete: :delete_all)) add(:user_id, references(:users, type: :uuid, on_delete: :delete_all))
add(:activity_id, references(:activities, type: :uuid, on_delete: :delete_all)) add(:activity_id, references(:activities, type: :uuid, on_delete: :delete_all))
timestamps() timestamps()
end end
create(unique_index(:bookmarks, [:user_id, :activity_id])) create_if_not_exists(unique_index(:bookmarks, [:user_id, :activity_id]))
end end
end end

View file

@ -6,7 +6,7 @@ defmodule Pleroma.Repo.Migrations.MigrateOldBookmarks do
alias Pleroma.User alias Pleroma.User
alias Pleroma.Repo alias Pleroma.Repo
def change do def up do
query = query =
from(u in User, from(u in User,
where: u.local == true, where: u.local == true,
@ -18,7 +18,7 @@ defmodule Pleroma.Repo.Migrations.MigrateOldBookmarks do
|> Enum.each(fn %{id: user_id, bookmarks: bookmarks} -> |> Enum.each(fn %{id: user_id, bookmarks: bookmarks} ->
Enum.each(bookmarks, fn ap_id -> Enum.each(bookmarks, fn ap_id ->
activity = Activity.get_create_by_object_ap_id(ap_id) activity = Activity.get_create_by_object_ap_id(ap_id)
unless is_nil(activity), do: {:ok, _} = Bookmark.create(user_id, activity.id) unless is_nil(activity), do: {:ok, _} = Bookmark.create(user_id, activity.id)
end) end)
end) end)
@ -26,4 +26,10 @@ defmodule Pleroma.Repo.Migrations.MigrateOldBookmarks do
remove(:bookmarks) remove(:bookmarks)
end end
end end
def down do
alter table(:users) do
add :bookmarks, {:array, :string}, null: false, default: []
end
end
end end

View file

@ -3,6 +3,6 @@ defmodule Pleroma.Repo.Migrations.AddFTSIndexToObjects do
def change do def change do
drop_if_exists index(:activities, ["(to_tsvector('english', data->'object'->>'content'))"], using: :gin, name: :activities_fts) drop_if_exists index(:activities, ["(to_tsvector('english', data->'object'->>'content'))"], using: :gin, name: :activities_fts)
create index(:objects, ["(to_tsvector('english', data->>'content'))"], using: :gin, name: :objects_fts) create_if_not_exists index(:objects, ["(to_tsvector('english', data->>'content'))"], using: :gin, name: :objects_fts)
end end
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.AddRefreshTokenIndexToToken do
use Ecto.Migration use Ecto.Migration
def change do def change do
create(unique_index(:oauth_tokens, [:refresh_token])) create_if_not_exists(unique_index(:oauth_tokens, [:refresh_token]))
end end
end end

View file

@ -1,9 +1,15 @@
defmodule Pleroma.Repo.Migrations.ChangeHideColumnInFilterTable do defmodule Pleroma.Repo.Migrations.ChangeHideColumnInFilterTable do
use Ecto.Migration use Ecto.Migration
def change do def up do
alter table(:filters) do alter table(:filters) do
modify :hide, :boolean, default: false modify :hide, :boolean, default: false
end end
end end
def down do
alter table(:filters) do
modify :hide, :boolean
end
end
end end

View file

@ -68,6 +68,6 @@ defmodule Pleroma.Repo.Migrations.AddThreadVisibilityFunction do
end end
def down do def down do
execute("drop function thread_visibility(actor varchar, activity_id varchar)") execute("drop function if exists thread_visibility(actor varchar, activity_id varchar)")
end end
end end

View file

@ -2,12 +2,12 @@ defmodule Pleroma.Repo.Migrations.CreateConfig do
use Ecto.Migration use Ecto.Migration
def change do def change do
create table(:config) do create_if_not_exists table(:config) do
add(:key, :string) add(:key, :string)
add(:value, :binary) add(:value, :binary)
timestamps() timestamps()
end end
create(unique_index(:config, :key)) create_if_not_exists(unique_index(:config, :key))
end end
end end

View file

@ -1,10 +1,12 @@
defmodule Pleroma.Repo.Migrations.AddNonFollowsAndNonFollowersFieldsToNotificationSettings do defmodule Pleroma.Repo.Migrations.AddNonFollowsAndNonFollowersFieldsToNotificationSettings do
use Ecto.Migration use Ecto.Migration
def change do def up do
execute(""" execute("""
update users set info = jsonb_set(info, '{notification_settings}', '{"local": true, "remote": true, "follows": true, "followers": true, "non_follows": true, "non_followers": true}') update users set info = jsonb_set(info, '{notification_settings}', '{"local": true, "remote": true, "follows": true, "followers": true, "non_follows": true, "non_followers": true}')
where local=true where local=true
""") """)
end end
def down, do: :ok
end end

View file

@ -2,6 +2,6 @@ defmodule Pleroma.Repo.Migrations.AddIndexOnActivitiesLocal do
use Ecto.Migration use Ecto.Migration
def change do def change do
create(index("activities", [:local])) create_if_not_exists(index("activities", [:local]))
end end
end end

View file

@ -3,6 +3,6 @@ defmodule Pleroma.Repo.Migrations.AddTagIndexToObjects do
def change do def change do
drop_if_exists index(:activities, ["(data #> '{\"object\",\"tag\"}')"], using: :gin, name: :activities_tags) drop_if_exists index(:activities, ["(data #> '{\"object\",\"tag\"}')"], using: :gin, name: :activities_tags)
create index(:objects, ["(data->'tag')"], using: :gin, name: :objects_tags) create_if_not_exists index(:objects, ["(data->'tag')"], using: :gin, name: :objects_tags)
end end
end end

View file

@ -6,7 +6,7 @@ defmodule Pleroma.Repo.Migrations.AddGroupKeyToConfig do
add(:group, :string) add(:group, :string)
end end
drop(unique_index("config", :key)) drop_if_exists(unique_index("config", :key))
create(unique_index("config", [:group, :key])) create_if_not_exists(unique_index("config", [:group, :key]))
end end
end end

View file

@ -14,7 +14,7 @@ defmodule Pleroma.Repo.Migrations.AddFtsIndexToObjectsTwo do
return new; return new;
end end
$$ LANGUAGE plpgsql") $$ LANGUAGE plpgsql")
execute("create index objects_fts on objects using RUM (fts_content rum_tsvector_addon_ops, inserted_at) with (attach = 'inserted_at', to = 'fts_content');") execute("create index if not exists objects_fts on objects using RUM (fts_content rum_tsvector_addon_ops, inserted_at) with (attach = 'inserted_at', to = 'fts_content');")
execute("CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE ON objects execute("CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE ON objects
FOR EACH ROW EXECUTE PROCEDURE objects_fts_update()") FOR EACH ROW EXECUTE PROCEDURE objects_fts_update()")
@ -23,12 +23,12 @@ defmodule Pleroma.Repo.Migrations.AddFtsIndexToObjectsTwo do
end end
def down do def down do
execute "drop index objects_fts" execute "drop index if exists objects_fts"
execute "drop trigger tsvectorupdate on objects" execute "drop trigger if exists tsvectorupdate on objects"
execute "drop function objects_fts_update()" execute "drop function if exists objects_fts_update()"
alter table(:objects) do alter table(:objects) do
remove(:fts_content, :tsvector) remove(:fts_content, :tsvector)
end end
create index(:objects, ["(to_tsvector('english', data->>'content'))"], using: :gin, name: :objects_fts) create_if_not_exists index(:objects, ["(to_tsvector('english', data->>'content'))"], using: :gin, name: :objects_fts)
end end
end end