Skip to content

Commit

Permalink
Adapt to current CE :fingers-crossed:
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol committed Dec 18, 2024
1 parent caa392f commit 5b7fd98
Showing 1 changed file with 59 additions and 31 deletions.
90 changes: 59 additions & 31 deletions lib/plausible/data_migration/backfill_teams.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ defmodule Plausible.DataMigration.BackfillTeams do
s in Plausible.Site,
inner_join: m in "site_memberships",
on: m.site_id == s.id,
inner_join: o in "users",
inner_join: o in Plausible.Auth.User,
on: o.id == m.user_id,
where: m.role == "owner",
where: is_nil(s.team_id)
where: is_nil(s.team_id),
select: %{s | memberships: [%{user: o, role: :owner}]}
)
|> @repo.all(timeout: :infinity)

Expand Down Expand Up @@ -181,12 +182,21 @@ defmodule Plausible.DataMigration.BackfillTeams do
from(
sm in "site_memberships",
as: :site_membership,
inner_join: s in assoc(sm, :site),
inner_join: t in assoc(s, :team),
inner_join: u in assoc(sm, :user),
inner_join: s in Plausible.Site,
on: s.id == sm.site_id,
inner_join: t in Plausible.Teams.Team,
on: t.id == s.team_id,
inner_join: u in Plausible.Auth.User,
on: u.id == sm.user_id,
where: sm.role != "owner",
where: not exists(guest_memberships_query),
preload: [user: u, site: {s, team: t}]
select: %{
user: u,
site: %{s | team: t},
inserted_at: sm.inserted_at,
updated_at: sm.updated_at,
role: sm.role
}
)
|> @repo.all(timeout: :infinity)

Expand All @@ -207,12 +217,12 @@ defmodule Plausible.DataMigration.BackfillTeams do
sm in "site_memberships",
inner_join: tm in Teams.Membership,
on: tm.user_id == sm.user_id,
inner_join: gm in assoc(tm, :guest_memberships),
inner_join: gm in Teams.GuestMembership,
on: gm.site_id == sm.site_id,
where: tm.role == :guest,
where:
(gm.role == :viewer and sm.role == :admin) or
(gm.role == :editor and sm.role == :viewer),
(gm.role == :viewer and sm.role == "admin") or
(gm.role == :editor and sm.role == "viewer"),
select: {gm, sm.role}
)
|> @repo.all(timeout: :infinity)
Expand All @@ -233,8 +243,9 @@ defmodule Plausible.DataMigration.BackfillTeams do
where: i.site_id == parent_as(:guest_invitation).site_id,
where: i.email == parent_as(:team_invitation).email,
where:
(i.role == :viewer and parent_as(:guest_invitation).role == :viewer) or
(i.role == :admin and parent_as(:guest_invitation).role == :editor)
(i.role == "viewer" and parent_as(:guest_invitation).role == :viewer) or
(i.role == "admin" and parent_as(:guest_invitation).role == :editor),
select: true
)

guest_invitations_to_remove =
Expand Down Expand Up @@ -278,12 +289,23 @@ defmodule Plausible.DataMigration.BackfillTeams do
from(
si in "invitations",
as: :site_invitation,
inner_join: s in assoc(si, :site),
inner_join: t in assoc(s, :team),
inner_join: inv in assoc(si, :inviter),
inner_join: s in Plausible.Site,
on: si.site_id == s.id,
inner_join: t in Teams.Team,
on: t.id == s.team_id,
inner_join: inv in Plausible.Auth.User,
on: inv.id == si.inviter_id,
where: si.role != "owner",
where: not exists(guest_invitations_query),
preload: [site: {s, team: t}, inviter: inv]
select: %{
inserted_at: si.inserted_at,
updated_at: si.updated_at,
role: si.role,
invitation_id: si.invitation_id,
email: si.email,
site: %{s | team: t},
inviter: inv
}
)
|> @repo.all(timeout: :infinity)

Expand All @@ -308,10 +330,10 @@ defmodule Plausible.DataMigration.BackfillTeams do
on: gi.site_id == si.site_id,
where: ti.role == :guest,
where:
(gi.role == :viewer and si.role == :admin) or
(gi.role == :editor and si.role == :viewer) or
(gi.role == :viewer and si.role == "admin") or
(gi.role == :editor and si.role == "viewer") or
is_distinct(gi.invitation_id, si.invitation_id),
select: {gi, si}
select: {gi, %{role: si.role, invitation_id: si.invitation_id}}
)
|> @repo.all(timeout: :infinity)

Expand All @@ -330,7 +352,8 @@ defmodule Plausible.DataMigration.BackfillTeams do
i in "invitations",
where: i.site_id == parent_as(:site_transfer).site_id,
where: i.email == parent_as(:site_transfer).email,
where: i.role == :owner
where: i.role == "owner",
select: true
)

site_transfers_to_remove =
Expand Down Expand Up @@ -363,11 +386,22 @@ defmodule Plausible.DataMigration.BackfillTeams do
from(
si in "invitations",
as: :site_invitation,
inner_join: s in assoc(si, :site),
inner_join: inv in assoc(si, :inviter),
where: si.role == :owner,
inner_join: s in Plausible.Site,
on: s.id == si.site_id,
inner_join: inv in Plausible.Auth.User,
on: inv.id == si.inviter_id,
where: si.role == "owner",
where: not exists(site_transfers_query),
preload: [inviter: inv, site: s]
select: %{
email: si.email,
role: si.role,
invitation_id: si.invitation_id,
inserted_at: si.inserted_at,
updated_at: si.updated_at,
site: s,
inviter: inv
}
# preload: [inviter: inv, site: s]
)
|> @repo.all(timeout: :infinity)

Expand Down Expand Up @@ -416,12 +450,6 @@ defmodule Plausible.DataMigration.BackfillTeams do
team
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:trial_expiry_date, owner.trial_expiry_date)
|> Ecto.Changeset.put_change(:accept_traffic_until, owner.accept_traffic_until)
|> Ecto.Changeset.put_change(
:allow_next_upgrade_override,
owner.allow_next_upgrade_override
)
|> Ecto.Changeset.put_embed(:grace_period, owner.grace_period)
|> Ecto.Changeset.force_change(:updated_at, owner.updated_at)
|> @repo.update!()

Expand Down Expand Up @@ -624,8 +652,8 @@ defmodule Plausible.DataMigration.BackfillTeams do
end)
end

defp translate_role(:admin), do: :editor
defp translate_role(:viewer), do: :viewer
defp translate_role("admin"), do: :editor
defp translate_role("viewer"), do: :viewer

defp log(msg) do
IO.puts("[#{NaiveDateTime.utc_now(:second)}] #{msg}")
Expand Down

0 comments on commit 5b7fd98

Please sign in to comment.