Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

56 lignes
2.4KB

  1. defmodule Pleroma.Repo.Migrations.AddCounterCacheTable do
  2. use Ecto.Migration
  3. def up do
  4. create_if_not_exists table(:counter_cache) do
  5. add(:name, :string, null: false)
  6. add(:count, :bigint, null: false, default: 0)
  7. end
  8. create_if_not_exists(unique_index(:counter_cache, [:name]))
  9. """
  10. CREATE OR REPLACE FUNCTION update_status_visibility_counter_cache()
  11. RETURNS TRIGGER AS
  12. $$
  13. DECLARE
  14. BEGIN
  15. IF TG_OP = 'INSERT' THEN
  16. IF NEW.data->>'type' = 'Create' THEN
  17. EXECUTE 'INSERT INTO counter_cache (name, count) VALUES (''status_visibility_' || activity_visibility(NEW.actor, NEW.recipients, NEW.data) || ''', 1) ON CONFLICT (name) DO UPDATE SET count = counter_cache.count + 1';
  18. END IF;
  19. RETURN NEW;
  20. ELSIF TG_OP = 'UPDATE' THEN
  21. IF (NEW.data->>'type' = 'Create') and (OLD.data->>'type' = 'Create') and activity_visibility(NEW.actor, NEW.recipients, NEW.data) != activity_visibility(OLD.actor, OLD.recipients, OLD.data) THEN
  22. EXECUTE 'INSERT INTO counter_cache (name, count) VALUES (''status_visibility_' || activity_visibility(NEW.actor, NEW.recipients, NEW.data) || ''', 1) ON CONFLICT (name) DO UPDATE SET count = counter_cache.count + 1';
  23. EXECUTE 'update counter_cache SET count = counter_cache.count - 1 where count > 0 and name = ''status_visibility_' || activity_visibility(OLD.actor, OLD.recipients, OLD.data) || ''';';
  24. END IF;
  25. RETURN NEW;
  26. ELSIF TG_OP = 'DELETE' THEN
  27. IF OLD.data->>'type' = 'Create' THEN
  28. EXECUTE 'update counter_cache SET count = counter_cache.count - 1 where count > 0 and name = ''status_visibility_' || activity_visibility(OLD.actor, OLD.recipients, OLD.data) || ''';';
  29. END IF;
  30. RETURN OLD;
  31. END IF;
  32. END;
  33. $$
  34. LANGUAGE 'plpgsql';
  35. """
  36. |> execute()
  37. """
  38. CREATE TRIGGER status_visibility_counter_cache_trigger BEFORE INSERT OR UPDATE of recipients, data OR DELETE ON activities
  39. FOR EACH ROW
  40. EXECUTE PROCEDURE update_status_visibility_counter_cache();
  41. """
  42. |> execute()
  43. end
  44. def down do
  45. execute("drop trigger if exists status_visibility_counter_cache_trigger on activities")
  46. execute("drop function if exists update_status_visibility_counter_cache()")
  47. drop_if_exists(unique_index(:counter_cache, [:name]))
  48. drop_if_exists(table(:counter_cache))
  49. end
  50. end