mirror of
https://github.com/plausible/analytics.git
synced 2024-11-26 11:44:03 +03:00
Migration: enable domain changes (upcoming feature) (#2802)
* Migration: enable domain changes (upcoming feature) * Pass domain check within the same site
This commit is contained in:
parent
70a85c3f4c
commit
94a86a17eb
49
priv/repo/migrations/20230328062644_allow_domain_change.exs
Normal file
49
priv/repo/migrations/20230328062644_allow_domain_change.exs
Normal file
@ -0,0 +1,49 @@
|
||||
defmodule Plausible.Repo.Migrations.AllowDomainChange do
|
||||
use Ecto.Migration
|
||||
|
||||
def up do
|
||||
alter table(:sites) do
|
||||
add(:domain_changed_from, :string, null: true)
|
||||
add(:domain_changed_at, :naive_datetime, null: true)
|
||||
end
|
||||
|
||||
create(unique_index(:sites, :domain_changed_from))
|
||||
create(index(:sites, :domain_changed_at))
|
||||
|
||||
execute("""
|
||||
CREATE OR REPLACE FUNCTION check_domain() RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM sites
|
||||
WHERE (NEW.domain = domain_changed_from AND NEW.id != id)
|
||||
OR (OLD IS NULL AND NEW.domain_changed_from = domain)
|
||||
) THEN
|
||||
RAISE unique_violation USING CONSTRAINT = 'domain_change_disallowed';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
""")
|
||||
|
||||
execute("""
|
||||
CREATE TRIGGER check_domain_trigger
|
||||
BEFORE INSERT OR UPDATE ON sites
|
||||
FOR EACH ROW EXECUTE FUNCTION check_domain();
|
||||
""")
|
||||
end
|
||||
|
||||
def down do
|
||||
execute("""
|
||||
DROP TRIGGER check_domain_trigger ON sites
|
||||
""")
|
||||
|
||||
execute("""
|
||||
DROP FUNCTION check_domain()
|
||||
""")
|
||||
|
||||
alter table(:sites) do
|
||||
remove(:domain_changed_from)
|
||||
remove(:domain_changed_at)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user