-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Migration (PR: #2802) * Implement Site.Domain interface allowing change and expiry * Fixup seeds so they work with V2_MIGRATION_DONE=1 * Update Sites.Cache so it's capable of multi-keyed lookups * Implement worker handling domain change expiration * Implement domain change UI * Implement transition period for public APIs * Exclude v2 tests in primary test run * Update lib/plausible_web/controllers/site_controller.ex Co-authored-by: Vini Brasil <vini@hey.com> * Update lib/plausible_web/controllers/site_controller.ex Co-authored-by: Vini Brasil <vini@hey.com> * Update moduledoc * Update changelog * Remove remnant from previous implementation attempt * !fixup * !fixup * Implement domain change via Sites API cc @ukutaht * Update CHANGELOG * Credo * !fixup commit missing tests * Allow continuous domain change within the same site --------- Co-authored-by: Vini Brasil <vini@hey.com>
- Loading branch information
Showing
22 changed files
with
832 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
defmodule Plausible.Site.Domain do | ||
@expire_threshold_hours 72 | ||
|
||
@moduledoc """ | ||
Basic interface for domain changes. | ||
Once `Plausible.DataMigration.NumericIDs` schema migration is ready, | ||
domain change operation will be enabled, accessible to the users. | ||
We will set a transition period of #{@expire_threshold_hours} hours | ||
during which, both old and new domains, will be accepted as traffic | ||
identifiers to the same site. | ||
A periodic worker will call the `expire/0` function to end it where applicable. | ||
See: `Plausible.Workers.ExpireDomainChangeTransitions`. | ||
The underlying changeset for domain change (see: `Plausible.Site`) relies | ||
on database trigger installed via `Plausible.Repo.Migrations.AllowDomainChange` | ||
Postgres migration. The trigger checks if either `domain` or `domain_changed_from` | ||
exist to ensure unicity. | ||
""" | ||
|
||
alias Plausible.Site | ||
alias Plausible.Repo | ||
|
||
import Ecto.Query | ||
|
||
@spec expire_change_transitions(integer()) :: {:ok, non_neg_integer()} | ||
def expire_change_transitions(expire_threshold_hours \\ @expire_threshold_hours) do | ||
{updated, _} = | ||
Repo.update_all( | ||
from(s in Site, | ||
where: s.domain_changed_at < ago(^expire_threshold_hours, "hour") | ||
), | ||
set: [ | ||
domain_changed_from: nil, | ||
domain_changed_at: nil | ||
] | ||
) | ||
|
||
{:ok, updated} | ||
end | ||
|
||
@spec change(Site.t(), String.t(), Keyword.t()) :: | ||
{:ok, Site.t()} | {:error, Ecto.Changeset.t()} | ||
def change(%Site{} = site, new_domain, opts \\ []) do | ||
changeset = Site.update_changeset(site, %{domain: new_domain}, opts) | ||
|
||
changeset = | ||
if Enum.empty?(changeset.changes) and is_nil(changeset.errors[:domain]) do | ||
Ecto.Changeset.add_error( | ||
changeset, | ||
:domain, | ||
"New domain must be different than the current one" | ||
) | ||
else | ||
changeset | ||
end | ||
|
||
Repo.update(changeset) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.