mirror of
https://github.com/plausible/analytics.git
synced 2024-11-22 02:27:57 +03:00
WIP: Clean up personal and site segments on user leaving a team/sites
This commit is contained in:
parent
9004a02f30
commit
7199b2cf21
32
priv/repo/migrations/20241112100000_cleanup_segments.exs
Normal file
32
priv/repo/migrations/20241112100000_cleanup_segments.exs
Normal file
@ -0,0 +1,32 @@
|
||||
defmodule Plausible.Repo.Migrations.SegmentsCleanup do
|
||||
use Ecto.Migration
|
||||
|
||||
def up do
|
||||
execute """
|
||||
CREATE OR REPLACE FUNCTION clean_segments() RETURNS trigger AS $$
|
||||
BEGIN
|
||||
UPDATE segments
|
||||
SET owner_id = null
|
||||
WHERE site_id in (SELECT id FROM sites WHERE sites.team_id = OLD.team_id) AND owner_id = OLD.user_id AND type = 'site';
|
||||
|
||||
DELETE FROM segments
|
||||
WHERE site_id in (SELECT id FROM sites WHERE sites.team_id = OLD.team_id) AND owner_id = OLD.user_id AND type = 'personal';
|
||||
|
||||
RETURN OLD;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
"""
|
||||
|
||||
execute """
|
||||
CREATE TRIGGER user_deassociated_from_team
|
||||
AFTER DELETE ON team_memberships
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION clean_segments();
|
||||
"""
|
||||
end
|
||||
|
||||
def down do
|
||||
execute "DROP TRIGGER IF EXISTS user_deassociated_from_team ON team_memberships;"
|
||||
execute "DROP FUNCTION IF EXISTS clean_segments();"
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user