From 516053313e9850c8d732fcaadab6de4de306340a Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 28 Feb 2023 14:56:51 +0100 Subject: [PATCH] Update `account_summaries` database view --- ...3_update_account_summaries_to_version_3.rb | 26 +++++++++++++++++++ db/schema.rb | 4 +-- db/views/account_summaries_v03.sql | 24 +++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20240327145403_update_account_summaries_to_version_3.rb create mode 100644 db/views/account_summaries_v03.sql diff --git a/db/migrate/20240327145403_update_account_summaries_to_version_3.rb b/db/migrate/20240327145403_update_account_summaries_to_version_3.rb new file mode 100644 index 00000000000..45320097d6c --- /dev/null +++ b/db/migrate/20240327145403_update_account_summaries_to_version_3.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class UpdateAccountSummariesToVersion3 < ActiveRecord::Migration[6.1] + def up + reapplication_global_follow_recommendations_v1 do + drop_view :account_summaries, materialized: true + create_view :account_summaries, version: 3, materialized: { no_data: true } + safety_assured { add_index :account_summaries, :account_id, unique: true } + end + end + + def down + reapplication_global_follow_recommendations_v1 do + drop_view :account_summaries, materialized: true + create_view :account_summaries, version: 2, materialized: { no_data: true } + safety_assured { add_index :account_summaries, :account_id, unique: true } + end + end + + def reapplication_global_follow_recommendations_v1 + drop_view :global_follow_recommendations, materialized: true + yield + create_view :global_follow_recommendations, version: 1, materialized: { no_data: true } + safety_assured { add_index :global_follow_recommendations, :account_id, unique: true } + end +end diff --git a/db/schema.rb b/db/schema.rb index 79221faa8d6..b05fb61be1c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_03_27_124057) do +ActiveRecord::Schema[7.1].define(version: 2024_03_27_145403) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -1450,7 +1450,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_03_27_124057) do WHERE ((statuses.account_id = accounts.id) AND (statuses.deleted_at IS NULL) AND (statuses.reblog_of_id IS NULL)) ORDER BY statuses.id DESC LIMIT 20) t0) - WHERE ((accounts.suspended_at IS NULL) AND (accounts.silenced_at IS NULL) AND (accounts.moved_to_account_id IS NULL) AND (accounts.discoverable = true) AND (accounts.locked = false)) + WHERE ((accounts.suspended_at IS NULL) AND (accounts.deleted_at IS NULL) AND (accounts.silenced_at IS NULL) AND (accounts.moved_to_account_id IS NULL) AND (accounts.discoverable = true) AND (accounts.locked = false)) GROUP BY accounts.id; SQL add_index "account_summaries", ["account_id", "language", "sensitive"], name: "idx_on_account_id_language_sensitive_250461e1eb" diff --git a/db/views/account_summaries_v03.sql b/db/views/account_summaries_v03.sql new file mode 100644 index 00000000000..1a961c6d81d --- /dev/null +++ b/db/views/account_summaries_v03.sql @@ -0,0 +1,24 @@ +SELECT + accounts.id AS account_id, + mode() WITHIN GROUP (ORDER BY language ASC) AS language, + mode() WITHIN GROUP (ORDER BY sensitive ASC) AS sensitive +FROM accounts +CROSS JOIN LATERAL ( + SELECT + statuses.account_id, + statuses.language, + statuses.sensitive + FROM statuses + WHERE statuses.account_id = accounts.id + AND statuses.deleted_at IS NULL + AND statuses.reblog_of_id IS NULL + ORDER BY statuses.id DESC + LIMIT 20 +) t0 +WHERE accounts.suspended_at IS NULL + AND accounts.deleted_at IS NULL + AND accounts.silenced_at IS NULL + AND accounts.moved_to_account_id IS NULL + AND accounts.discoverable = 't' + AND accounts.locked = 'f' +GROUP BY accounts.id