From f22d602a10cbbf64e7adf79cb3d814070c27c5c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20Karsl=C4=B1?= Date: Wed, 15 Nov 2023 23:55:00 +0300 Subject: [PATCH] Add local_subscribers field to CommunityAggregates struct and schema --- crates/db_schema/src/aggregates/structs.rs | 1 + crates/db_schema/src/schema.rs | 1 + .../down.sql | 35 +++++++++++ .../up.sql | 61 +++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 migrations/2023-11-15-200123_community_aggregates_add_local_subscribers/down.sql create mode 100644 migrations/2023-11-15-200123_community_aggregates_add_local_subscribers/up.sql diff --git a/crates/db_schema/src/aggregates/structs.rs b/crates/db_schema/src/aggregates/structs.rs index 24b2d82c5..7629cb0e2 100644 --- a/crates/db_schema/src/aggregates/structs.rs +++ b/crates/db_schema/src/aggregates/structs.rs @@ -59,6 +59,7 @@ pub struct CommunityAggregates { pub users_active_half_year: i64, #[serde(skip)] pub hot_rank: f64, + pub local_subscribers: i64, } #[derive(PartialEq, Eq, Debug, Serialize, Deserialize, Clone, Default)] diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 88d468a6f..a89c00619 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -198,6 +198,7 @@ diesel::table! { users_active_month -> Int8, users_active_half_year -> Int8, hot_rank -> Float8, + local_subscribers -> Int8, } } diff --git a/migrations/2023-11-15-200123_community_aggregates_add_local_subscribers/down.sql b/migrations/2023-11-15-200123_community_aggregates_add_local_subscribers/down.sql new file mode 100644 index 000000000..6dd941011 --- /dev/null +++ b/migrations/2023-11-15-200123_community_aggregates_add_local_subscribers/down.sql @@ -0,0 +1,35 @@ +ALTER TABLE community_aggregates + DROP COLUMN local_subscribers; + +CREATE OR REPLACE FUNCTION community_aggregates_subscriber_count () + RETURNS TRIGGER + LANGUAGE plpgsql + AS $$ +BEGIN + IF (TG_OP = 'INSERT') THEN + UPDATE + community_aggregates + SET + subscribers = subscribers + 1 + FROM + community + WHERE + community.id = community_id + AND community.local + AND community_id = NEW.community_id; + ELSIF (TG_OP = 'DELETE') THEN + UPDATE + community_aggregates + SET + subscribers = subscribers - 1 + FROM + community + WHERE + community.id = community_id + AND community.local + AND community_id = OLD.community_id; + END IF; + RETURN NULL; +END +$$; + diff --git a/migrations/2023-11-15-200123_community_aggregates_add_local_subscribers/up.sql b/migrations/2023-11-15-200123_community_aggregates_add_local_subscribers/up.sql new file mode 100644 index 000000000..226b39fec --- /dev/null +++ b/migrations/2023-11-15-200123_community_aggregates_add_local_subscribers/up.sql @@ -0,0 +1,61 @@ +-- Couldn't find a way to put local_subscribers right after subscribers +ALTER TABLE community_aggregates + ADD COLUMN local_subscribers int8 NOT NULL DEFAULT 0; + +-- update initial value +UPDATE + community_aggregates +SET + local_subscribers = ( + SELECT + COUNT(*) + FROM + community_follower + WHERE + community_follower.community_id = community_aggregates.community_id + AND community_follower.person_id IN ( + SELECT + id + FROM + person + WHERE + local + ) + ) +; + + +CREATE OR REPLACE FUNCTION community_aggregates_subscriber_count () + RETURNS TRIGGER + LANGUAGE plpgsql + AS $$ +BEGIN + IF (TG_OP = 'INSERT') THEN + UPDATE + community_aggregates + SET + subscribers = subscribers + 1, + local_subscribers = local_subscribers + 1 + FROM + community + WHERE + community.id = community_id + AND community.local + AND community_id = NEW.community_id; + ELSIF (TG_OP = 'DELETE') THEN + UPDATE + community_aggregates + SET + subscribers = subscribers - 1, + local_subscribers = local_subscribers - 1 + FROM + community + WHERE + community.id = community_id + AND community.local + AND community_id = OLD.community_id; + END IF; + RETURN NULL; +END +$$; +