summaryrefslogtreecommitdiff
path: root/priv/repo/migrations/20200109123126_add_counter_cache_table.exs
diff options
context:
space:
mode:
authoreugenijm <eugenijm@protonmail.com>2020-01-09 22:18:55 +0300
committereugenijm <eugenijm@protonmail.com>2020-02-24 21:46:37 +0300
commit7ad5c51f23102e89c491a2ef731e108873a09d71 (patch)
treeedfad3b8ea92b2037ef737a97acaa87d2ee00a34 /priv/repo/migrations/20200109123126_add_counter_cache_table.exs
parentd240ce41b55a557366746fb7e313d3810bd8fe16 (diff)
downloadpleroma-7ad5c51f23102e89c491a2ef731e108873a09d71.tar.gz
pleroma-7ad5c51f23102e89c491a2ef731e108873a09d71.zip
Admin API: `GET /api/pleroma/admin/stats` to get status count by visibility scope
Diffstat (limited to 'priv/repo/migrations/20200109123126_add_counter_cache_table.exs')
-rw-r--r--priv/repo/migrations/20200109123126_add_counter_cache_table.exs55
1 files changed, 55 insertions, 0 deletions
diff --git a/priv/repo/migrations/20200109123126_add_counter_cache_table.exs b/priv/repo/migrations/20200109123126_add_counter_cache_table.exs
new file mode 100644
index 000000000..df9e21193
--- /dev/null
+++ b/priv/repo/migrations/20200109123126_add_counter_cache_table.exs
@@ -0,0 +1,55 @@
+defmodule Pleroma.Repo.Migrations.AddCounterCacheTable do
+ use Ecto.Migration
+
+ def up do
+ create_if_not_exists table(:counter_cache) do
+ add(:name, :string, null: false)
+ add(:count, :bigint, null: false, default: 0)
+ end
+
+ create_if_not_exists(unique_index(:counter_cache, [:name]))
+
+ """
+ CREATE OR REPLACE FUNCTION update_status_visibility_counter_cache()
+ RETURNS TRIGGER AS
+ $$
+ DECLARE
+ BEGIN
+ IF TG_OP = 'INSERT' THEN
+ IF NEW.data->>'type' = 'Create' THEN
+ 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';
+ END IF;
+ RETURN NEW;
+ ELSIF TG_OP = 'UPDATE' THEN
+ 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
+ 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';
+ 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) || ''';';
+ END IF;
+ RETURN NEW;
+ ELSIF TG_OP = 'DELETE' THEN
+ IF OLD.data->>'type' = 'Create' THEN
+ 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) || ''';';
+ END IF;
+ RETURN OLD;
+ END IF;
+ END;
+ $$
+ LANGUAGE 'plpgsql';
+ """
+ |> execute()
+
+ """
+ CREATE TRIGGER status_visibility_counter_cache_trigger BEFORE INSERT OR UPDATE of recipients, data OR DELETE ON activities
+ FOR EACH ROW
+ EXECUTE PROCEDURE update_status_visibility_counter_cache();
+ """
+ |> execute()
+ end
+
+ def down do
+ execute("drop trigger if exists status_visibility_counter_cache_trigger on activities")
+ execute("drop function if exists update_status_visibility_counter_cache()")
+ drop_if_exists(unique_index(:counter_cache, [:name]))
+ drop_if_exists(table(:counter_cache))
+ end
+end