1 defmodule Pleroma.Repo.Migrations.UpdateCounterCacheTable do
4 @function_name "update_status_visibility_counter_cache"
5 @trigger_name "status_visibility_counter_cache_trigger"
8 execute("drop trigger if exists #{@trigger_name} on activities")
9 execute("drop function if exists #{@function_name}()")
10 drop_if_exists(unique_index(:counter_cache, [:name]))
11 drop_if_exists(table(:counter_cache))
13 create_if_not_exists table(:counter_cache) do
14 add(:instance, :string, null: false)
15 add(:direct, :bigint, null: false, default: 0)
16 add(:private, :bigint, null: false, default: 0)
17 add(:unlisted, :bigint, null: false, default: 0)
18 add(:public, :bigint, null: false, default: 0)
21 create_if_not_exists(unique_index(:counter_cache, [:instance]))
24 CREATE OR REPLACE FUNCTION #{@function_name}()
28 hostname character varying(255);
29 visibility_new character varying(64);
30 visibility_old character varying(64);
31 actor character varying(255);
33 IF TG_OP = 'DELETE' THEN
38 hostname := split_part(actor, '/', 3);
39 IF TG_OP = 'INSERT' THEN
40 visibility_new := activity_visibility(NEW.actor, NEW.recipients, NEW.data);
41 IF NEW.data->>'type' = 'Create'
42 AND visibility_new IN ('public', 'unlisted', 'private', 'direct') THEN
43 EXECUTE format('INSERT INTO "counter_cache" ("instance", %1$I) VALUES ($1, 1)
44 ON CONFLICT ("instance") DO
45 UPDATE SET %1$I = "counter_cache".%1$I + 1', visibility_new)
49 ELSIF TG_OP = 'UPDATE' THEN
50 visibility_new := activity_visibility(NEW.actor, NEW.recipients, NEW.data);
51 visibility_old := activity_visibility(OLD.actor, OLD.recipients, OLD.data);
52 IF (NEW.data->>'type' = 'Create')
53 AND (OLD.data->>'type' = 'Create')
54 AND visibility_new != visibility_old
55 AND visibility_new IN ('public', 'unlisted', 'private', 'direct') THEN
56 EXECUTE format('UPDATE "counter_cache" SET
57 %1$I = greatest("counter_cache".%1$I - 1, 0),
58 %2$I = "counter_cache".%2$I + 1
59 WHERE "instance" = $1', visibility_old, visibility_new)
63 ELSIF TG_OP = 'DELETE' THEN
64 IF OLD.data->>'type' = 'Create' THEN
65 visibility_old := activity_visibility(OLD.actor, OLD.recipients, OLD.data);
66 EXECUTE format('UPDATE "counter_cache" SET
67 %1$I = greatest("counter_cache".%1$I - 1, 0)
68 WHERE "instance" = $1', visibility_old)
79 execute("DROP TRIGGER IF EXISTS #{@trigger_name} ON activities")
82 CREATE TRIGGER #{@trigger_name}
85 OR UPDATE of recipients, data
89 EXECUTE PROCEDURE #{@function_name}();
95 execute("DROP TRIGGER IF EXISTS #{@trigger_name} ON activities")
96 execute("DROP FUNCTION IF EXISTS #{@function_name}()")
97 drop_if_exists(unique_index(:counter_cache, [:instance]))
98 drop_if_exists(table(:counter_cache))
100 create_if_not_exists table(:counter_cache) do
101 add(:name, :string, null: false)
102 add(:count, :bigint, null: false, default: 0)
105 create_if_not_exists(unique_index(:counter_cache, [:name]))
108 CREATE OR REPLACE FUNCTION #{@function_name}()
113 IF TG_OP = 'INSERT' THEN
114 IF NEW.data->>'type' = 'Create' THEN
115 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';
118 ELSIF TG_OP = 'UPDATE' THEN
119 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
120 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';
121 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) || ''';';
124 ELSIF TG_OP = 'DELETE' THEN
125 IF OLD.data->>'type' = 'Create' THEN
126 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) || ''';';
137 CREATE TRIGGER #{@trigger_name} BEFORE INSERT OR UPDATE of recipients, data OR DELETE ON activities
139 EXECUTE PROCEDURE #{@function_name}();