feat(migration): add the hosts of existing remote users to instances

This commit is contained in:
Lain Soykaf 2026-10-27 20:49:07 +04:00
commit db11e5e790
No known key found for this signature in database
2 changed files with 60 additions and 0 deletions

View file

@ -0,0 +1,29 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2026 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Repo.Migrations.BackfillInstancesFromUsers do
use Ecto.Migration
# Remote users now create an instances row; add the hosts of existing ones,
# so the peer list can come from the instances table.
def up do
Pleroma.Repo.query!(
"""
INSERT INTO instances (host, inserted_at, updated_at)
SELECT DISTINCT host, now(), now()
FROM (
SELECT substring(ap_id from '^[A-Za-z][A-Za-z0-9+.-]*://([^/:?#]+)') AS host
FROM users
WHERE local = false AND ap_id IS NOT NULL
) AS hosts
WHERE host IS NOT NULL
ON CONFLICT (host) DO NOTHING
""",
[],
timeout: :infinity
)
end
def down, do: :ok
end

View file

@ -0,0 +1,31 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2026 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Repo.Migrations.BackfillInstancesFromUsersTest do
use Pleroma.DataCase
import Pleroma.Factory
import Pleroma.Tests.Helpers
alias Pleroma.Instances.Instance
setup_all do: require_migration("20261028000000_backfill_instances_from_users")
test "up/0 adds the hosts of remote users and keeps existing records", %{migration: migration} do
unreachable_since = ~N[2020-01-01 00:00:00.000000]
insert(:instance, host: "known.example", unreachable_since: unreachable_since)
insert(:user, local: false, ap_id: "https://known.example/users/a")
insert(:user, local: false, ap_id: "https://new.example/users/a")
insert(:user, local: false, ap_id: "https://new.example/users/b")
insert(:user, local: false, ap_id: "https://ported.example:8443/users/c")
insert(:user, local: true)
migration.up()
hosts = Instance |> Repo.all() |> Enum.map(& &1.host) |> Enum.sort()
assert hosts == ["known.example", "new.example", "ported.example"]
assert %{unreachable_since: ^unreachable_since} = Repo.get_by(Instance, host: "known.example")
end
end