Adding SQL format checking via `pg_format` / pgFormatter (#3740)
authorDessalines <dessalines@users.noreply.github.com>
Wed, 2 Aug 2023 16:44:51 +0000 (12:44 -0400)
committerGitHub <noreply@github.com>
Wed, 2 Aug 2023 16:44:51 +0000 (12:44 -0400)
* SQL format checking, 1.

* SQL format checking, 2.

* SQL format checking, 3.

* SQL format checking, 4.

* SQL format checking, 5.

* Running pg_format

* Getting rid of comment.

* Upping pg_format version.

* Using git ls-files for sql format check.

* Fixing sql lints.

* Addressing PR comments.

341 files changed:
.woodpecker.yml
migrations/00000000000000_diesel_initial_setup/down.sql
migrations/00000000000000_diesel_initial_setup/up.sql
migrations/2019-02-26-002946_create_user/down.sql
migrations/2019-02-26-002946_create_user/up.sql
migrations/2019-02-27-170003_create_community/down.sql
migrations/2019-02-27-170003_create_community/up.sql
migrations/2019-03-03-163336_create_post/down.sql
migrations/2019-03-03-163336_create_post/up.sql
migrations/2019-03-05-233828_create_comment/down.sql
migrations/2019-03-05-233828_create_comment/up.sql
migrations/2019-03-30-212058_create_post_view/down.sql
migrations/2019-03-30-212058_create_post_view/up.sql
migrations/2019-04-03-155205_create_community_view/down.sql
migrations/2019-04-03-155205_create_community_view/up.sql
migrations/2019-04-03-155309_create_comment_view/down.sql
migrations/2019-04-03-155309_create_comment_view/up.sql
migrations/2019-04-07-003142_create_moderation_logs/down.sql
migrations/2019-04-07-003142_create_moderation_logs/up.sql
migrations/2019-04-08-015947_create_user_view/down.sql
migrations/2019-04-08-015947_create_user_view/up.sql
migrations/2019-04-11-144915_create_mod_views/down.sql
migrations/2019-04-11-144915_create_mod_views/up.sql
migrations/2019-04-29-175834_add_delete_columns/down.sql
migrations/2019-04-29-175834_add_delete_columns/up.sql
migrations/2019-05-02-051656_community_view_hot_rank/down.sql
migrations/2019-05-02-051656_community_view_hot_rank/up.sql
migrations/2019-06-01-222649_remove_admin/down.sql
migrations/2019-06-01-222649_remove_admin/up.sql
migrations/2019-08-11-000918_add_nsfw_columns/down.sql
migrations/2019-08-11-000918_add_nsfw_columns/up.sql
migrations/2019-08-29-040006_add_community_count/down.sql
migrations/2019-08-29-040006_add_community_count/up.sql
migrations/2019-09-05-230317_add_mod_ban_views/down.sql
migrations/2019-09-05-230317_add_mod_ban_views/up.sql
migrations/2019-09-09-042010_add_stickied_posts/down.sql
migrations/2019-09-09-042010_add_stickied_posts/up.sql
migrations/2019-10-15-181630_add_themes/down.sql
migrations/2019-10-15-181630_add_themes/up.sql
migrations/2019-10-19-052737_create_user_mention/down.sql
migrations/2019-10-19-052737_create_user_mention/up.sql
migrations/2019-10-21-011237_add_default_sorts/down.sql
migrations/2019-10-21-011237_add_default_sorts/up.sql
migrations/2019-10-24-002614_create_password_reset_request/down.sql
migrations/2019-10-24-002614_create_password_reset_request/up.sql
migrations/2019-12-09-060754_add_lang/down.sql
migrations/2019-12-09-060754_add_lang/up.sql
migrations/2019-12-11-181820_add_site_fields/down.sql
migrations/2019-12-11-181820_add_site_fields/up.sql
migrations/2019-12-29-164820_add_avatar/down.sql
migrations/2019-12-29-164820_add_avatar/up.sql
migrations/2020-01-01-200418_add_email_to_user_view/down.sql
migrations/2020-01-01-200418_add_email_to_user_view/up.sql
migrations/2020-01-02-172755_add_show_avatar_and_email_notifications_to_user/down.sql
migrations/2020-01-02-172755_add_show_avatar_and_email_notifications_to_user/up.sql
migrations/2020-01-11-012452_add_indexes/down.sql
migrations/2020-01-11-012452_add_indexes/up.sql
migrations/2020-01-13-025151_create_materialized_views/down.sql
migrations/2020-01-13-025151_create_materialized_views/up.sql
migrations/2020-01-21-001001_create_private_message/down.sql
migrations/2020-01-21-001001_create_private_message/up.sql
migrations/2020-01-29-011901_create_reply_materialized_view/down.sql
migrations/2020-01-29-011901_create_reply_materialized_view/up.sql
migrations/2020-01-29-030825_create_user_mention_materialized_view/down.sql
migrations/2020-01-29-030825_create_user_mention_materialized_view/up.sql
migrations/2020-02-02-004806_add_case_insensitive_usernames/down.sql
migrations/2020-02-02-004806_add_case_insensitive_usernames/up.sql
migrations/2020-02-06-165953_change_post_title_length/down.sql
migrations/2020-02-06-165953_change_post_title_length/up.sql
migrations/2020-02-07-210055_add_comment_subscribed/down.sql
migrations/2020-02-07-210055_add_comment_subscribed/up.sql
migrations/2020-02-08-145624_add_post_newest_activity_time/down.sql
migrations/2020-02-08-145624_add_post_newest_activity_time/up.sql
migrations/2020-03-06-202329_add_post_iframely_data/down.sql
migrations/2020-03-06-202329_add_post_iframely_data/up.sql
migrations/2020-03-26-192410_add_activitypub_tables/down.sql
migrations/2020-03-26-192410_add_activitypub_tables/up.sql
migrations/2020-04-03-194936_add_activitypub_for_posts_and_comments/down.sql
migrations/2020-04-03-194936_add_activitypub_for_posts_and_comments/up.sql
migrations/2020-04-07-135912_add_user_community_apub_constraints/down.sql
migrations/2020-04-07-135912_add_user_community_apub_constraints/up.sql
migrations/2020-04-14-163701_update_views_for_activitypub/down.sql
migrations/2020-04-14-163701_update_views_for_activitypub/up.sql
migrations/2020-04-21-123957_remove_unique_user_constraints/down.sql
migrations/2020-04-21-123957_remove_unique_user_constraints/up.sql
migrations/2020-05-05-210233_add_activitypub_for_private_messages/down.sql
migrations/2020-05-05-210233_add_activitypub_for_private_messages/up.sql
migrations/2020-06-30-135809_remove_mat_views/down.sql
migrations/2020-06-30-135809_remove_mat_views/up.sql
migrations/2020-07-08-202609_add_creator_published/down.sql
migrations/2020-07-08-202609_add_creator_published/up.sql
migrations/2020-07-12-100442_add_post_title_to_comments_view/down.sql
migrations/2020-07-12-100442_add_post_title_to_comments_view/up.sql
migrations/2020-07-18-234519_add_unique_community_user_actor_ids/down.sql
migrations/2020-07-18-234519_add_unique_community_user_actor_ids/up.sql
migrations/2020-08-03-000110_add_preferred_usernames_banners_and_icons/down.sql
migrations/2020-08-03-000110_add_preferred_usernames_banners_and_icons/up.sql
migrations/2020-08-06-205355_update_community_post_count/down.sql
migrations/2020-08-06-205355_update_community_post_count/up.sql
migrations/2020-08-25-132005_add_unique_ap_ids/down.sql
migrations/2020-08-25-132005_add_unique_ap_ids/up.sql
migrations/2020-09-07-231141_add_migration_utils/down.sql
migrations/2020-09-07-231141_add_migration_utils/up.sql
migrations/2020-10-07-234221_fix_fast_triggers/down.sql
migrations/2020-10-07-234221_fix_fast_triggers/up.sql
migrations/2020-10-10-035723_fix_fast_triggers_2/down.sql
migrations/2020-10-10-035723_fix_fast_triggers_2/up.sql
migrations/2020-10-13-212240_create_report_tables/down.sql
migrations/2020-10-13-212240_create_report_tables/up.sql
migrations/2020-10-23-115011_activity_ap_id_column/down.sql
migrations/2020-10-23-115011_activity_ap_id_column/up.sql
migrations/2020-11-05-152724_activity_remove_user_id/down.sql
migrations/2020-11-05-152724_activity_remove_user_id/up.sql
migrations/2020-11-10-150835_community_follower_pending/down.sql
migrations/2020-11-10-150835_community_follower_pending/up.sql
migrations/2020-11-26-134531_delete_user/down.sql
migrations/2020-11-26-134531_delete_user/up.sql
migrations/2020-12-02-152437_create_site_aggregates/down.sql
migrations/2020-12-02-152437_create_site_aggregates/up.sql
migrations/2020-12-03-035643_create_user_aggregates/down.sql
migrations/2020-12-03-035643_create_user_aggregates/up.sql
migrations/2020-12-04-183345_create_community_aggregates/down.sql
migrations/2020-12-04-183345_create_community_aggregates/up.sql
migrations/2020-12-10-152350_create_post_aggregates/down.sql
migrations/2020-12-10-152350_create_post_aggregates/up.sql
migrations/2020-12-14-020038_create_comment_aggregates/down.sql
migrations/2020-12-14-020038_create_comment_aggregates/up.sql
migrations/2020-12-17-030456_create_alias_views/down.sql
migrations/2020-12-17-030456_create_alias_views/up.sql
migrations/2020-12-17-031053_remove_fast_tables_and_views/down.sql
migrations/2020-12-17-031053_remove_fast_tables_and_views/up.sql
migrations/2021-01-05-200932_add_hot_rank_indexes/down.sql
migrations/2021-01-05-200932_add_hot_rank_indexes/up.sql
migrations/2021-01-26-173850_default_actor_id/down.sql
migrations/2021-01-26-173850_default_actor_id/up.sql
migrations/2021-01-27-202728_active_users_monthly/down.sql
migrations/2021-01-27-202728_active_users_monthly/up.sql
migrations/2021-01-31-050334_add_forum_sort_index/down.sql
migrations/2021-01-31-050334_add_forum_sort_index/up.sql
migrations/2021-02-02-153240_apub_columns/down.sql
migrations/2021-02-02-153240_apub_columns/up.sql
migrations/2021-02-10-164051_add_new_comments_sort_index/down.sql
migrations/2021-02-10-164051_add_new_comments_sort_index/up.sql
migrations/2021-02-13-210612_set_correct_aggregates_time_columns/down.sql
migrations/2021-02-13-210612_set_correct_aggregates_time_columns/up.sql
migrations/2021-02-25-112959_remove-categories/down.sql
migrations/2021-02-25-112959_remove-categories/up.sql
migrations/2021-02-28-162616_clean_empty_post_urls/down.sql
migrations/2021-02-28-162616_clean_empty_post_urls/up.sql
migrations/2021-03-04-040229_clean_icon_urls/down.sql
migrations/2021-03-04-040229_clean_icon_urls/up.sql
migrations/2021-03-09-171136_split_user_table_2/down.sql
migrations/2021-03-09-171136_split_user_table_2/up.sql
migrations/2021-03-19-014144_add_col_local_user_validator_time/down.sql
migrations/2021-03-19-014144_add_col_local_user_validator_time/up.sql
migrations/2021-03-20-185321_move_matrix_id_to_person/down.sql
migrations/2021-03-20-185321_move_matrix_id_to_person/up.sql
migrations/2021-03-31-103917_add_show_score_setting/down.sql
migrations/2021-03-31-103917_add_show_score_setting/up.sql
migrations/2021-03-31-105915_add_bot_account/down.sql
migrations/2021-03-31-105915_add_bot_account/up.sql
migrations/2021-03-31-144349_add_site_short_description/down.sql
migrations/2021-03-31-144349_add_site_short_description/up.sql
migrations/2021-04-01-173552_rename_preferred_username_to_display_name/down.sql
migrations/2021-04-01-173552_rename_preferred_username_to_display_name/up.sql
migrations/2021-04-01-181826_add_community_agg_active_monthly_index/down.sql
migrations/2021-04-01-181826_add_community_agg_active_monthly_index/up.sql
migrations/2021-04-02-021422_remove_community_creator/down.sql
migrations/2021-04-02-021422_remove_community_creator/up.sql
migrations/2021-04-20-155001_limit-admins-create-community/down.sql
migrations/2021-04-20-155001_limit-admins-create-community/up.sql
migrations/2021-04-24-174047_add_show_read_post_setting/down.sql
migrations/2021-04-24-174047_add_show_read_post_setting/up.sql
migrations/2021-07-19-130929_add_show_new_post_notifs_setting/down.sql
migrations/2021-07-19-130929_add_show_new_post_notifs_setting/up.sql
migrations/2021-07-20-102033_actor_name_length/down.sql
migrations/2021-07-20-102033_actor_name_length/up.sql
migrations/2021-08-02-002342_comment_count_fixes/down.sql
migrations/2021-08-02-002342_comment_count_fixes/up.sql
migrations/2021-08-04-223559_create_user_community_block/down.sql
migrations/2021-08-04-223559_create_user_community_block/up.sql
migrations/2021-08-16-004209_fix_remove_bots_from_aggregates/down.sql
migrations/2021-08-16-004209_fix_remove_bots_from_aggregates/up.sql
migrations/2021-08-17-210508_create_mod_transfer_community/down.sql
migrations/2021-08-17-210508_create_mod_transfer_community/up.sql
migrations/2021-09-20-112945_jwt-secret/down.sql
migrations/2021-09-20-112945_jwt-secret/up.sql
migrations/2021-10-01-141650_create_admin_purge/down.sql
migrations/2021-10-01-141650_create_admin_purge/up.sql
migrations/2021-11-22-135324_add_activity_ap_id_index/down.sql
migrations/2021-11-22-135324_add_activity_ap_id_index/up.sql
migrations/2021-11-22-143904_add_required_public_key/down.sql
migrations/2021-11-22-143904_add_required_public_key/up.sql
migrations/2021-11-23-031528_add_report_published_index/down.sql
migrations/2021-11-23-031528_add_report_published_index/up.sql
migrations/2021-11-23-132840_email_verification/down.sql
migrations/2021-11-23-132840_email_verification/up.sql
migrations/2021-11-23-153753_add_invite_only_columns/down.sql
migrations/2021-11-23-153753_add_invite_only_columns/up.sql
migrations/2021-12-09-225529_add_published_to_email_verification/down.sql
migrations/2021-12-09-225529_add_published_to_email_verification/up.sql
migrations/2021-12-14-181537_add_temporary_bans/down.sql
migrations/2021-12-14-181537_add_temporary_bans/up.sql
migrations/2022-01-04-034553_add_hidden_column/down.sql
migrations/2022-01-04-034553_add_hidden_column/up.sql
migrations/2022-01-20-160328_remove_site_creator/down.sql
migrations/2022-01-20-160328_remove_site_creator/up.sql
migrations/2022-01-28-104106_instance-actor/down.sql
migrations/2022-01-28-104106_instance-actor/up.sql
migrations/2022-02-01-154240_add_community_title_index/down.sql
migrations/2022-02-01-154240_add_community_title_index/up.sql
migrations/2022-02-18-210946_default_theme/down.sql
migrations/2022-02-18-210946_default_theme/up.sql
migrations/2022-04-04-183652_update_community_aggregates_on_soft_delete/down.sql
migrations/2022-04-04-183652_update_community_aggregates_on_soft_delete/up.sql
migrations/2022-04-11-210137_fix_unique_changeme/down.sql
migrations/2022-04-11-210137_fix_unique_changeme/up.sql
migrations/2022-04-12-114352_default_post_listing_type/down.sql
migrations/2022-04-12-114352_default_post_listing_type/up.sql
migrations/2022-04-12-185205_change_default_listing_type_to_local/down.sql
migrations/2022-04-12-185205_change_default_listing_type_to_local/up.sql
migrations/2022-04-19-111004_default_require_application/down.sql
migrations/2022-04-19-111004_default_require_application/up.sql
migrations/2022-04-26-105145_only_mod_can_post/down.sql
migrations/2022-04-26-105145_only_mod_can_post/up.sql
migrations/2022-05-19-153931_legal-information/down.sql
migrations/2022-05-19-153931_legal-information/up.sql
migrations/2022-05-20-135341_embed-url/down.sql
migrations/2022-05-20-135341_embed-url/up.sql
migrations/2022-06-12-012121_add_site_hide_modlog_names/down.sql
migrations/2022-06-12-012121_add_site_hide_modlog_names/up.sql
migrations/2022-06-13-124806_post_report_name_length/down.sql
migrations/2022-06-13-124806_post_report_name_length/up.sql
migrations/2022-06-21-123144_language-tags/down.sql
migrations/2022-06-21-123144_language-tags/up.sql
migrations/2022-07-07-182650_comment_ltrees/down.sql
migrations/2022-07-07-182650_comment_ltrees/up.sql
migrations/2022-08-04-150644_add_application_email_admins/down.sql
migrations/2022-08-04-150644_add_application_email_admins/up.sql
migrations/2022-08-04-214722_add_distinguished_comment/down.sql
migrations/2022-08-04-214722_add_distinguished_comment/up.sql
migrations/2022-08-05-203502_add_person_post_aggregates/down.sql
migrations/2022-08-05-203502_add_person_post_aggregates/up.sql
migrations/2022-08-22-193848_comment-language-tags/down.sql
migrations/2022-08-22-193848_comment-language-tags/up.sql
migrations/2022-09-07-113813_drop_ccnew_indexes_function/down.sql
migrations/2022-09-07-113813_drop_ccnew_indexes_function/up.sql
migrations/2022-09-07-114618_pm-reports/down.sql
migrations/2022-09-07-114618_pm-reports/up.sql
migrations/2022-09-08-102358_site-and-community-languages/down.sql
migrations/2022-09-08-102358_site-and-community-languages/up.sql
migrations/2022-09-24-161829_remove_table_aliases/down.sql
migrations/2022-09-24-161829_remove_table_aliases/up.sql
migrations/2022-10-06-183632_move_blocklist_to_db/down.sql
migrations/2022-10-06-183632_move_blocklist_to_db/up.sql
migrations/2022-11-13-181529_create_taglines/down.sql
migrations/2022-11-13-181529_create_taglines/up.sql
migrations/2022-11-20-032430_sticky_local/down.sql
migrations/2022-11-20-032430_sticky_local/up.sql
migrations/2022-11-21-143249_remove-federation-settings/down.sql
migrations/2022-11-21-143249_remove-federation-settings/up.sql
migrations/2022-11-21-204256_user-following/down.sql
migrations/2022-11-21-204256_user-following/up.sql
migrations/2022-12-05-110642_registration_mode/down.sql
migrations/2022-12-05-110642_registration_mode/up.sql
migrations/2023-01-17-165819_cleanup_post_aggregates_indexes/down.sql
migrations/2023-01-17-165819_cleanup_post_aggregates_indexes/up.sql
migrations/2023-02-01-012747_fix_active_index/down.sql
migrations/2023-02-01-012747_fix_active_index/up.sql
migrations/2023-02-05-102549_drop-site-federation-debug/down.sql
migrations/2023-02-05-102549_drop-site-federation-debug/up.sql
migrations/2023-02-07-030958_community-collections/down.sql
migrations/2023-02-07-030958_community-collections/up.sql
migrations/2023-02-11-173347_custom_emojis/down.sql
migrations/2023-02-11-173347_custom_emojis/up.sql
migrations/2023-02-13-172528_add_report_email_admins/down.sql
migrations/2023-02-13-172528_add_report_email_admins/up.sql
migrations/2023-02-13-221303_add_instance_software_and_version/down.sql
migrations/2023-02-13-221303_add_instance_software_and_version/up.sql
migrations/2023-02-15-212546_add_post_comment_saved_indexes/down.sql
migrations/2023-02-15-212546_add_post_comment_saved_indexes/up.sql
migrations/2023-02-16-194139_add_totp_secret/down.sql
migrations/2023-02-16-194139_add_totp_secret/up.sql
migrations/2023-04-14-175955_add_listingtype_sorttype_enums/down.sql
migrations/2023-04-14-175955_add_listingtype_sorttype_enums/up.sql
migrations/2023-04-23-164732_add_person_details_indexes/down.sql
migrations/2023-04-23-164732_add_person_details_indexes/up.sql
migrations/2023-05-10-095739_force_enable_undetermined_language/up.sql
migrations/2023-06-06-104440_index_post_url/down.sql
migrations/2023-06-06-104440_index_post_url/up.sql
migrations/2023-06-07-105918_add_hot_rank_columns/down.sql
migrations/2023-06-07-105918_add_hot_rank_columns/up.sql
migrations/2023-06-17-175955_add_listingtype_sorttype_hour_enums/down.sql
migrations/2023-06-17-175955_add_listingtype_sorttype_hour_enums/up.sql
migrations/2023-06-19-055530_add_retry_worker_setting/down.sql
migrations/2023-06-19-055530_add_retry_worker_setting/up.sql
migrations/2023-06-19-120700_no_double_deletion/down.sql
migrations/2023-06-19-120700_no_double_deletion/up.sql
migrations/2023-06-20-191145_add_listingtype_sorttype_3_6_9_months_enums/down.sql
migrations/2023-06-20-191145_add_listingtype_sorttype_3_6_9_months_enums/up.sql
migrations/2023-06-21-153242_add_captcha/down.sql
migrations/2023-06-21-153242_add_captcha/up.sql
migrations/2023-06-22-051755_fix_local_communities_marked_non_local/down.sql
migrations/2023-06-22-051755_fix_local_communities_marked_non_local/up.sql
migrations/2023-06-22-101245_increase_user_theme_column_size/down.sql
migrations/2023-06-22-101245_increase_user_theme_column_size/up.sql
migrations/2023-06-24-072904_add_open_links_in_new_tab_setting/down.sql
migrations/2023-06-24-072904_add_open_links_in_new_tab_setting/up.sql
migrations/2023-06-24-185942_aggegates_published_indexes/down.sql
migrations/2023-06-24-185942_aggegates_published_indexes/up.sql
migrations/2023-06-27-065106_add_ui_settings/down.sql
migrations/2023-06-27-065106_add_ui_settings/up.sql
migrations/2023-07-04-153335_add_optimized_indexes/down.sql
migrations/2023-07-04-153335_add_optimized_indexes/up.sql
migrations/2023-07-05-000058_person-admin/down.sql
migrations/2023-07-05-000058_person-admin/up.sql
migrations/2023-07-06-151124_hot-rank-future/down.sql
migrations/2023-07-06-151124_hot-rank-future/up.sql
migrations/2023-07-08-101154_fix_soft_delete_aggregates/down.sql
migrations/2023-07-08-101154_fix_soft_delete_aggregates/up.sql
migrations/2023-07-10-075550_add-infinite-scroll-setting/down.sql
migrations/2023-07-10-075550_add-infinite-scroll-setting/up.sql
migrations/2023-07-11-084714_receive_activity_table/down.sql
migrations/2023-07-11-084714_receive_activity_table/up.sql
migrations/2023-07-14-154840_add_optimized_indexes_published/down.sql
migrations/2023-07-14-154840_add_optimized_indexes_published/up.sql
migrations/2023-07-14-215339_aggregates_nonzero_indexes/down.sql
migrations/2023-07-14-215339_aggregates_nonzero_indexes/up.sql
migrations/2023-07-18-082614_post_aggregates_community_id/down.sql
migrations/2023-07-18-082614_post_aggregates_community_id/up.sql
migrations/2023-07-19-163511_comment_sort_hot_rank_then_score/down.sql
migrations/2023-07-19-163511_comment_sort_hot_rank_then_score/up.sql
migrations/2023-07-24-232635_trigram-index/up.sql
migrations/2023-07-26-000217_create_controversial_indexes/down.sql
migrations/2023-07-26-000217_create_controversial_indexes/up.sql
migrations/2023-07-26-222023_site-aggregates-one/down.sql
migrations/2023-07-26-222023_site-aggregates-one/up.sql
migrations/2023-07-27-134652_remove-expensive-broken-trigger/down.sql
migrations/2023-07-27-134652_remove-expensive-broken-trigger/up.sql
scripts/lint.sh [moved from scripts/fix-clippy.sh with 87% similarity]
scripts/sql_format_check.sh [new file with mode: 0755]

index 91779122a2c6e2e92b1125ae0439cf3d963bd63a..a3c1f1194ab1b506de8acb9d022b420070a3c5b0 100644 (file)
@@ -83,6 +83,18 @@ pipeline:
       - rustup component add rustfmt --toolchain nightly-2023-07-10
       - cargo +nightly-2023-07-10 fmt -- --check
 
+  sql_fmt:
+    image: alpine:3
+    commands:
+      - apk add bash wget perl make git
+      - wget https://github.com/darold/pgFormatter/archive/refs/tags/v5.5.tar.gz
+      - tar xzf v5.5.tar.gz
+      - cd pgFormatter-5.5
+      - perl Makefile.PL
+      - make && make install
+      - cd ..
+      - ./scripts/./sql_format_check.sh
+
   # make sure api builds with default features (used by other crates relying on lemmy api)
   check_api_common_default_features:
     image: *muslrust_image
@@ -145,7 +157,7 @@ pipeline:
     environment:
       CARGO_HOME: .cargo
     commands:
-      # when adding new clippy lints, make sure to also add them in scripts/fix-clippy.sh
+      # when adding new clippy lints, make sure to also add them in scripts/lint.sh
       - rustup component add clippy
       - cargo clippy --workspace --tests --all-targets --features console --
         -D warnings -D deprecated -D clippy::perf -D clippy::complexity
index a9f526091194b70e312bd1b30084ea34e0df9670..5e29318c8bea936054e430d47bb7281da0898c51 100644 (file)
@@ -1,6 +1,7 @@
 -- This file was automatically created by Diesel to setup helper functions
 -- and other internal bookkeeping. This file is safe to edit, any future
 -- changes will be added to existing projects as new migrations.
+DROP FUNCTION IF EXISTS diesel_manage_updated_at (_tbl regclass);
+
+DROP FUNCTION IF EXISTS diesel_set_updated_at ();
 
-DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
-DROP FUNCTION IF EXISTS diesel_set_updated_at();
index d68895b1a7b7dbbd699eb627b23b01746eb80222..246f5e474c085ab7be8251843a18bbabf0c6ba1f 100644 (file)
@@ -1,10 +1,6 @@
 -- This file was automatically created by Diesel to setup helper functions
 -- and other internal bookkeeping. This file is safe to edit, any future
 -- changes will be added to existing projects as new migrations.
-
-
-
-
 -- Sets up a trigger for the given table to automatically set a column called
 -- `updated_at` whenever the row is modified (unless `updated_at` was included
 -- in the modified columns)
 --
 -- SELECT diesel_manage_updated_at('users');
 -- ```
-CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
+CREATE OR REPLACE FUNCTION diesel_manage_updated_at (_tbl regclass)
+    RETURNS VOID
+    AS $$
 BEGIN
     EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
                     FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
 END;
-$$ LANGUAGE plpgsql;
+$$
+LANGUAGE plpgsql;
 
-CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
+CREATE OR REPLACE FUNCTION diesel_set_updated_at ()
+    RETURNS TRIGGER
+    AS $$
 BEGIN
-    IF (
-        NEW IS DISTINCT FROM OLD AND
-        NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
-    ) THEN
-        NEW.updated_at := current_timestamp;
+    IF (NEW IS DISTINCT FROM OLD AND NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at) THEN
+        NEW.updated_at := CURRENT_TIMESTAMP;
     END IF;
     RETURN NEW;
 END;
-$$ LANGUAGE plpgsql;
+$$
+LANGUAGE plpgsql;
+
index 67a280d62463d3d0abce0f6b5765c28248a1fd88..a9bfa3fefa4837d55f1dae93d93f24aa5e9db9e6 100644 (file)
@@ -1,2 +1,4 @@
-drop table user_ban;
-drop table user_;
+DROP TABLE user_ban;
+
+DROP TABLE user_;
+
index ea2c9234f3718d741eed6fbe1f9c1be7617e0cc7..251938fee53dd934e2b8961f8974bda79889523a 100644 (file)
@@ -1,23 +1,25 @@
-create table user_ (
-  id serial primary key,
-  name varchar(20) not null,
-  fedi_name varchar(40) not null,
-  preferred_username varchar(20),
-  password_encrypted text not null,
-  email text unique,
-  icon bytea,
-  admin boolean default false not null,
-  banned boolean default false not null,
-  published timestamp not null default now(),
-  updated timestamp,
-  unique(name, fedi_name)
+CREATE TABLE user_ (
+    id serial PRIMARY KEY,
+    name varchar(20) NOT NULL,
+    fedi_name varchar(40) NOT NULL,
+    preferred_username varchar(20),
+    password_encrypted text NOT NULL,
+    email text UNIQUE,
+    icon bytea,
+    admin boolean DEFAULT FALSE NOT NULL,
+    banned boolean DEFAULT FALSE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp,
+    UNIQUE (name, fedi_name)
 );
 
-create table user_ban (
-  id serial primary key,
-  user_id int references user_ on update cascade on delete cascade not null,
-  published timestamp not null default now(),
-  unique (user_id)
+CREATE TABLE user_ban (
+    id serial PRIMARY KEY,
+    user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (user_id)
 );
 
-insert into user_ (name, fedi_name, password_encrypted) values ('admin', 'TBD', 'TBD');
+INSERT INTO user_ (name, fedi_name, password_encrypted)
+    VALUES ('admin', 'TBD', 'TBD');
+
index 219588d8f09f1475af4c91f46505ec5e4f5d1229..d5ecfc52ddcdce6a6eac2ebb78d32093d30ea4cd 100644 (file)
@@ -1,6 +1,14 @@
-drop table site;
-drop table community_user_ban;;
-drop table community_moderator;
-drop table community_follower;
-drop table community;
-drop table category;
+DROP TABLE site;
+
+DROP TABLE community_user_ban;
+
+;
+
+DROP TABLE community_moderator;
+
+DROP TABLE community_follower;
+
+DROP TABLE community;
+
+DROP TABLE category;
+
index 363f99f27ca2ecff5515472b9f7d23cdc7985a62..be8513957f7b7d3d87c87671d1db72caf2dfa532 100644 (file)
@@ -1,79 +1,81 @@
-create table category (
-  id serial primary key,
-  name varchar(100) not null unique
+CREATE TABLE category (
+    id serial PRIMARY KEY,
+    name varchar(100) NOT NULL UNIQUE
 );
 
-insert into category (name) values
-('Discussion'),
-('Humor/Memes'),
-('Gaming'),
-('Movies'),
-('TV'),
-('Music'),
-('Literature'),
-('Comics'),
-('Photography'),
-('Art'),
-('Learning'),
-('DIY'),
-('Lifestyle'),
-('News'),
-('Politics'),
-('Society'),
-('Gender/Identity/Sexuality'),
-('Race/Colonisation'),
-('Religion'),
-('Science/Technology'),
-('Programming/Software'),
-('Health/Sports/Fitness'),
-('Porn'),
-('Places'),
-('Meta'),
-('Other');
+INSERT INTO category (name)
+    VALUES ('Discussion'),
+    ('Humor/Memes'),
+    ('Gaming'),
+    ('Movies'),
+    ('TV'),
+    ('Music'),
+    ('Literature'),
+    ('Comics'),
+    ('Photography'),
+    ('Art'),
+    ('Learning'),
+    ('DIY'),
+    ('Lifestyle'),
+    ('News'),
+    ('Politics'),
+    ('Society'),
+    ('Gender/Identity/Sexuality'),
+    ('Race/Colonisation'),
+    ('Religion'),
+    ('Science/Technology'),
+    ('Programming/Software'),
+    ('Health/Sports/Fitness'),
+    ('Porn'),
+    ('Places'),
+    ('Meta'),
+    ('Other');
 
-create table community (
-  id serial primary key,
-  name varchar(20) not null unique,
-  title varchar(100) not null,
-  description text,
-  category_id int references category on update cascade on delete cascade not null,
-  creator_id int references user_ on update cascade on delete cascade not null,
-  removed boolean default false not null,
-  published timestamp not null default now(),
-  updated timestamp
+CREATE TABLE community (
+    id serial PRIMARY KEY,
+    name varchar(20) NOT NULL UNIQUE,
+    title varchar(100) NOT NULL,
+    description text,
+    category_id int REFERENCES category ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    removed boolean DEFAULT FALSE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp
 );
 
-create table community_moderator (
-  id serial primary key,
-  community_id int references community on update cascade on delete cascade not null,
-  user_id int references user_ on update cascade on delete cascade not null,
-  published timestamp not null default now(),
-  unique (community_id, user_id)
+CREATE TABLE community_moderator (
+    id serial PRIMARY KEY,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (community_id, user_id)
 );
 
-create table community_follower (
-  id serial primary key,
-  community_id int references community on update cascade on delete cascade not null,
-  user_id int references user_ on update cascade on delete cascade not null,
-  published timestamp not null default now(),
-  unique (community_id, user_id)
+CREATE TABLE community_follower (
+    id serial PRIMARY KEY,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (community_id, user_id)
 );
 
-create table community_user_ban (
-  id serial primary key,
-  community_id int references community on update cascade on delete cascade not null,
-  user_id int references user_ on update cascade on delete cascade not null,
-  published timestamp not null default now(),
-  unique (community_id, user_id)
+CREATE TABLE community_user_ban (
+    id serial PRIMARY KEY,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (community_id, user_id)
 );
 
-insert into community (name, title, category_id, creator_id) values ('main', 'The Default Community', 1, 1);
+INSERT INTO community (name, title, category_id, creator_id)
+    VALUES ('main', 'The Default Community', 1, 1);
 
-create table site (
-  id serial primary key,
-  name varchar(20) not null unique,
-  description text,
-  creator_id int references user_ on update cascade on delete cascade not null,
-  published timestamp not null default now(),
-  updated timestamp
+CREATE TABLE site (
+    id serial PRIMARY KEY,
+    name varchar(20) NOT NULL UNIQUE,
+    description text,
+    creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp
 );
+
index a671c2e76c79c212be8263346935c8ad8723e631..193f1c1d4350d62f68eb727afb9291c1404752ab 100644 (file)
@@ -1,4 +1,8 @@
-drop table post_read;
-drop table post_saved;
-drop table post_like;
-drop table post;
+DROP TABLE post_read;
+
+DROP TABLE post_saved;
+
+DROP TABLE post_like;
+
+DROP TABLE post;
+
index 9073781298ac4788cc44cd87c22ea6b7d2cf22ae..dfdd15c2c93f54fd7d694611a0c0532d74e8fa0b 100644 (file)
@@ -1,37 +1,38 @@
-create table post (
-  id serial primary key,
-  name varchar(100) not null,
-  url text, -- These are both optional, a post can just have a title
-  body text,
-  creator_id int references user_ on update cascade on delete cascade not null,
-  community_id int references community on update cascade on delete cascade not null,
-  removed boolean default false not null,
-  locked boolean default false not null,
-  published timestamp not null default now(),
-  updated timestamp
+CREATE TABLE post (
+    id serial PRIMARY KEY,
+    name varchar(100) NOT NULL,
+    url text, -- These are both optional, a post can just have a title
+    body text,
+    creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    removed boolean DEFAULT FALSE NOT NULL,
+    locked boolean DEFAULT FALSE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp
 );
 
-create table post_like (
-  id serial primary key,
-  post_id int references post on update cascade on delete cascade not null,
-  user_id int references user_ on update cascade on delete cascade not null,
-  score smallint not null, -- -1, or 1 for dislike, like, no row for no opinion
-  published timestamp not null default now(),
-  unique(post_id, user_id)
+CREATE TABLE post_like (
+    id serial PRIMARY KEY,
+    post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    score smallint NOT NULL, -- -1, or 1 for dislike, like, no row for no opinion
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (post_id, user_id)
 );
 
-create table post_saved (
-  id serial primary key,
-  post_id int references post on update cascade on delete cascade not null,
-  user_id int references user_ on update cascade on delete cascade not null,
-  published timestamp not null default now(),
-  unique(post_id, user_id)
+CREATE TABLE post_saved (
+    id serial PRIMARY KEY,
+    post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (post_id, user_id)
 );
 
-create table post_read (
-  id serial primary key,
-  post_id int references post on update cascade on delete cascade not null,
-  user_id int references user_ on update cascade on delete cascade not null,
-  published timestamp not null default now(),
-  unique(post_id, user_id)
+CREATE TABLE post_read (
+    id serial PRIMARY KEY,
+    post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (post_id, user_id)
 );
+
index 80fe0b1f5234f49f1a6604f5550a7238b12d6316..4c4a13cdd393885dac9f10e0100e09a56c3e3954 100644 (file)
@@ -1,3 +1,6 @@
-drop table comment_saved;
-drop table comment_like;
-drop table comment;
+DROP TABLE comment_saved;
+
+DROP TABLE comment_like;
+
+DROP TABLE comment;
+
index 4b754ece1a13ec66a97acd1ef342f0ab54377ecb..ed7ea17cd8be03ec2cbf1f5cfc6907918229daa8 100644 (file)
@@ -1,29 +1,30 @@
-create table comment (
-  id serial primary key,
-  creator_id int references user_ on update cascade on delete cascade not null,
-  post_id int references post on update cascade on delete cascade not null,
-  parent_id int references comment on update cascade on delete cascade,
-  content text not null,
-  removed boolean default false not null,
-  read boolean default false not null,
-  published timestamp not null default now(),
-  updated timestamp
+CREATE TABLE comment (
+    id serial PRIMARY KEY,
+    creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    parent_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE,
+    content text NOT NULL,
+    removed boolean DEFAULT FALSE NOT NULL,
+    read boolean DEFAULT FALSE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp
 );
 
-create table comment_like (
-  id serial primary key,
-  user_id int references user_ on update cascade on delete cascade not null,
-  comment_id int references comment on update cascade on delete cascade not null,
-  post_id int references post on update cascade on delete cascade not null,
-  score smallint not null, -- -1, or 1 for dislike, like, no row for no opinion
-  published timestamp not null default now(),
-  unique(comment_id, user_id)
+CREATE TABLE comment_like (
+    id serial PRIMARY KEY,
+    user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    score smallint NOT NULL, -- -1, or 1 for dislike, like, no row for no opinion
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (comment_id, user_id)
 );
 
-create table comment_saved (
-  id serial primary key,
-  comment_id int references comment on update cascade on delete cascade not null,
-  user_id int references user_ on update cascade on delete cascade not null,
-  published timestamp not null default now(),
-  unique(comment_id, user_id)
+CREATE TABLE comment_saved (
+    id serial PRIMARY KEY,
+    comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (comment_id, user_id)
 );
+
index 37c54d9110ae5d9196ea1f9e448a7ab033f986f3..75f9dc992d72f7133fded54e9badb92578b87b98 100644 (file)
@@ -1,2 +1,4 @@
-drop view post_view;
-drop function hot_rank;
+DROP VIEW post_view;
+
+DROP FUNCTION hot_rank;
+
index 4a4fd146e56cb1c84e951debfa874fad4e69f5ef..7b5a8574b7f83abc5410b1ecc03284738c613ff6 100644 (file)
 -- Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
-create or replace function hot_rank(
-  score numeric,
-  published timestamp without time zone)
-returns integer as $$
-begin
-  -- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
-  return floor(10000*log(greatest(1,score+3)) / power(((EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600) + 2), 1.8))::integer;
-end; $$
+CREATE OR REPLACE FUNCTION hot_rank (score numeric, published timestamp without time zone)
+    RETURNS integer
+    AS $$
+BEGIN
+    -- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
+    RETURN floor(10000 * log(greatest (1, score + 3)) / power(((EXTRACT(EPOCH FROM (timezone('utc', now()) - published)) / 3600) + 2), 1.8))::integer;
+END;
+$$
 LANGUAGE plpgsql;
 
-create view post_view as
-with all_post as
-(
-  select        
-  p.*,
-  (select name from user_ where p.creator_id = user_.id) as creator_name,
-  (select name from community where p.community_id = community.id) as community_name,
-  (select removed from community c where p.community_id = c.id) as community_removed,
-  (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-  coalesce(sum(pl.score), 0) as score,
-  count (case when pl.score = 1 then 1 else null end) as upvotes,
-  count (case when pl.score = -1 then 1 else null end) as downvotes,
-  hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-  from post p
-  left join post_like pl on p.id = pl.post_id
-  group by p.id
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        p.*,
+        (
+            SELECT
+                name
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                community
+            WHERE
+                p.community_id = community.id) AS community_name,
+        (
+            SELECT
+                removed
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_removed,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment
+            WHERE
+                comment.post_id = p.id) AS number_of_comments,
+        coalesce(sum(pl.score), 0) AS score,
+        count(
+            CASE WHEN pl.score = 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS upvotes,
+        count(
+            CASE WHEN pl.score = - 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS downvotes,
+        hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+    FROM
+        post p
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        p.id
 )
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
index 67d12f6fe1f491ca633a5b3f2eb61d3546058489..494b8257877fb4f1615e04e7e5ab4c26354b625a 100644 (file)
@@ -1,5 +1,10 @@
-drop view community_view;
-drop view community_moderator_view;
-drop view community_follower_view;
-drop view community_user_ban_view;
-drop view site_view;
+DROP VIEW community_view;
+
+DROP VIEW community_moderator_view;
+
+DROP VIEW community_follower_view;
+
+DROP VIEW community_user_ban_view;
+
+DROP VIEW site_view;
+
index 7d38dbfa3e2f8e47f26e2a356de8a0a9354e04b8..7f86e38ade86d1e021cf9467f377ddacca17c516 100644 (file)
-create view community_view as 
-with all_community as
-(
-  select *,
-  (select name from user_ u where c.creator_id = u.id) as creator_name,
-  (select name from category ct where c.category_id = ct.id) as category_name,
-  (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-  (select count(*) from post p where p.community_id = c.id) as number_of_posts,
-  (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
-  from community c
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        *,
+        (
+            SELECT
+                name
+            FROM
+                user_ u
+            WHERE
+                c.creator_id = u.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                category ct
+            WHERE
+                c.category_id = ct.id) AS category_name,
+        (
+            SELECT
+                count(*)
+            FROM
+                community_follower cf
+            WHERE
+                cf.community_id = c.id) AS number_of_subscribers,
+        (
+            SELECT
+                count(*)
+            FROM
+                post p
+            WHERE
+                p.community_id = c.id) AS number_of_posts,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment co,
+                post p
+            WHERE
+                c.id = p.community_id
+                AND p.id = co.post_id) AS number_of_comments
+    FROM
+        community c
 )
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
+CREATE VIEW community_moderator_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_name,
+    (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_name
+FROM
+    community_moderator cm;
 
-union all
+CREATE VIEW community_follower_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cf.user_id = u.id) AS user_name,
+    (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cf.community_id = c.id) AS community_name
+FROM
+    community_follower cf;
 
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
+CREATE VIEW community_user_ban_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_name,
+    (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_name
+FROM
+    community_user_ban cm;
 
-create view community_moderator_view as 
-select *,
-(select name from user_ u where cm.user_id = u.id) as user_name,
-(select name from community c where cm.community_id = c.id) as community_name
-from community_moderator cm;
+CREATE VIEW site_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            s.creator_id = u.id) AS creator_name,
+    (
+        SELECT
+            count(*)
+        FROM
+            user_) AS number_of_users,
+    (
+        SELECT
+            count(*)
+        FROM
+            post) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment) AS number_of_comments
+FROM
+    site s;
 
-create view community_follower_view as 
-select *,
-(select name from user_ u where cf.user_id = u.id) as user_name,
-(select name from community c where cf.community_id = c.id) as community_name
-from community_follower cf;
-
-create view community_user_ban_view as 
-select *,
-(select name from user_ u where cm.user_id = u.id) as user_name,
-(select name from community c where cm.community_id = c.id) as community_name
-from community_user_ban cm;
-
-create view site_view as 
-select *,
-(select name from user_ u where s.creator_id = u.id) as creator_name,
-(select count(*) from user_) as number_of_users,
-(select count(*) from post) as number_of_posts,
-(select count(*) from comment) as number_of_comments
-from site s;
index c19d5ff7e01ffa008b113533837c85fd04fee4e8..22313223579b7d982987d489f4da772c4f2707c1 100644 (file)
@@ -1,2 +1,4 @@
-drop view reply_view;
-drop view comment_view;
+DROP VIEW reply_view;
+
+DROP VIEW comment_view;
+
index 24ce98fcc70be6333b53ecf6803c5540e2942aee..dee4b97bfa346c63bcaa14caf796e08acdc4e5f1 100644 (file)
-create view comment_view as
-with all_comment as
-(
-  select        
-  c.*,
-  (select community_id from post p where p.id = c.post_id),
-  (select u.banned from user_ u where c.creator_id = u.id) as banned,
-  (select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
-  (select name from user_ where c.creator_id = user_.id) as creator_name,
-  coalesce(sum(cl.score), 0) as score,
-  count (case when cl.score = 1 then 1 else null end) as upvotes,
-  count (case when cl.score = -1 then 1 else null end) as downvotes
-  from comment c
-  left join comment_like cl on c.id = cl.comment_id
-  group by c.id
+CREATE VIEW comment_view AS
+with all_comment AS (
+    SELECT
+        c.*,
+        (
+            SELECT
+                community_id
+            FROM
+                post p
+            WHERE
+                p.id = c.post_id),
+            (
+                SELECT
+                    u.banned
+                FROM
+                    user_ u
+                WHERE
+                    c.creator_id = u.id) AS banned,
+                (
+                    SELECT
+                        cb.id::bool
+                    FROM
+                        community_user_ban cb,
+                        post p
+                    WHERE
+                        c.creator_id = cb.user_id
+                        AND p.id = c.post_id
+                        AND p.community_id = cb.community_id) AS banned_from_community,
+                    (
+                        SELECT
+                            name
+                        FROM
+                            user_
+                        WHERE
+                            c.creator_id = user_.id) AS creator_name,
+                        coalesce(sum(cl.score), 0) AS score,
+                    count(
+                        CASE WHEN cl.score = 1 THEN
+                            1
+                        ELSE
+                            NULL
+                        END) AS upvotes,
+                    count(
+                        CASE WHEN cl.score = - 1 THEN
+                            1
+                        ELSE
+                            NULL
+                        END) AS downvotes
+                FROM
+                    comment c
+                LEFT JOIN comment_like cl ON c.id = cl.comment_id
+            GROUP BY
+                c.id
 )
+    SELECT
+        ac.*,
+        u.id AS user_id,
+        coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS saved
+    FROM
+        all_comment ac;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-
-union all
-
-select 
-    ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as saved
-from all_comment ac
-;
-
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
index 888a87feb014dc2a83008e3f3e9f5dd354531282..aa2fd81593b8329a23eeea14136317cd061b20e9 100644 (file)
@@ -1,8 +1,16 @@
-drop table mod_remove_post;
-drop table mod_lock_post;
-drop table mod_remove_comment;
-drop table mod_remove_community;
-drop table mod_ban;
-drop table mod_ban_from_community;
-drop table mod_add;
-drop table mod_add_community;
+DROP TABLE mod_remove_post;
+
+DROP TABLE mod_lock_post;
+
+DROP TABLE mod_remove_comment;
+
+DROP TABLE mod_remove_community;
+
+DROP TABLE mod_ban;
+
+DROP TABLE mod_ban_from_community;
+
+DROP TABLE mod_add;
+
+DROP TABLE mod_add_community;
+
index 3b320d810d70a3ba664767ecfd39088c9dc41ecb..718723c9fd6832506dc6466a974399a6f3d77bb6 100644 (file)
@@ -1,76 +1,76 @@
-create table mod_remove_post (
-  id serial primary key,
-  mod_user_id int references user_ on update cascade on delete cascade not null,
-  post_id int references post on update cascade on delete cascade not null,
-  reason text,
-  removed boolean default true,
-  when_ timestamp not null default now()
+CREATE TABLE mod_remove_post (
+    id serial PRIMARY KEY,
+    mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    reason text,
+    removed boolean DEFAULT TRUE,
+    when_ timestamp NOT NULL DEFAULT now()
 );
 
-create table mod_lock_post (
-  id serial primary key,
-  mod_user_id int references user_ on update cascade on delete cascade not null,
-  post_id int references post on update cascade on delete cascade not null,
-  locked boolean default true,
-  when_ timestamp not null default now()
+CREATE TABLE mod_lock_post (
+    id serial PRIMARY KEY,
+    mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    locked boolean DEFAULT TRUE,
+    when_ timestamp NOT NULL DEFAULT now()
 );
 
-create table mod_remove_comment (
-  id serial primary key,
-  mod_user_id int references user_ on update cascade on delete cascade not null,
-  comment_id int references comment on update cascade on delete cascade not null,
-  reason text,
-  removed boolean default true,
-  when_ timestamp not null default now()
+CREATE TABLE mod_remove_comment (
+    id serial PRIMARY KEY,
+    mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    reason text,
+    removed boolean DEFAULT TRUE,
+    when_ timestamp NOT NULL DEFAULT now()
 );
 
-create table mod_remove_community (
-  id serial primary key,
-  mod_user_id int references user_ on update cascade on delete cascade not null,
-  community_id int references community on update cascade on delete cascade not null,
-  reason text,
-  removed boolean default true,
-  expires timestamp,
-  when_ timestamp not null default now()
+CREATE TABLE mod_remove_community (
+    id serial PRIMARY KEY,
+    mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    reason text,
+    removed boolean DEFAULT TRUE,
+    expires timestamp,
+    when_ timestamp NOT NULL DEFAULT now()
 );
 
 -- TODO make sure you can't ban other mods
-create table mod_ban_from_community (
-  id serial primary key,
-  mod_user_id int references user_ on update cascade on delete cascade not null,
-  other_user_id int references user_ on update cascade on delete cascade not null,
-  community_id int references community on update cascade on delete cascade not null,
-  reason text,
-  banned boolean default true,
-  expires timestamp,
-  when_ timestamp not null default now()
+CREATE TABLE mod_ban_from_community (
+    id serial PRIMARY KEY,
+    mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    other_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    reason text,
+    banned boolean DEFAULT TRUE,
+    expires timestamp,
+    when_ timestamp NOT NULL DEFAULT now()
 );
 
-create table mod_ban (
-  id serial primary key,
-  mod_user_id int references user_ on update cascade on delete cascade not null,
-  other_user_id int references user_ on update cascade on delete cascade not null,
-  reason text,
-  banned boolean default true,
-  expires timestamp,
-  when_ timestamp not null default now()
+CREATE TABLE mod_ban (
+    id serial PRIMARY KEY,
+    mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    other_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    reason text,
+    banned boolean DEFAULT TRUE,
+    expires timestamp,
+    when_ timestamp NOT NULL DEFAULT now()
 );
 
-create table mod_add_community (
-  id serial primary key,
-  mod_user_id int references user_ on update cascade on delete cascade not null,
-  other_user_id int references user_ on update cascade on delete cascade not null,
-  community_id int references community on update cascade on delete cascade not null,
-  removed boolean default false,
-  when_ timestamp not null default now()
+CREATE TABLE mod_add_community (
+    id serial PRIMARY KEY,
+    mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    other_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    removed boolean DEFAULT FALSE,
+    when_ timestamp NOT NULL DEFAULT now()
 );
 
 -- When removed is false that means kicked
-create table mod_add (
-  id serial primary key,
-  mod_user_id int references user_ on update cascade on delete cascade not null,
-  other_user_id int references user_ on update cascade on delete cascade not null,
-  removed boolean default false,
-  when_ timestamp not null default now()
+CREATE TABLE mod_add (
+    id serial PRIMARY KEY,
+    mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    other_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    removed boolean DEFAULT FALSE,
+    when_ timestamp NOT NULL DEFAULT now()
 );
 
index c94d94c47d1cc051b16331eb7c6a0573ceeba3c4..eec12284888db71cdfad1fdce9106c3920f1abb2 100644 (file)
@@ -1 +1,2 @@
-drop view user_view;
+DROP VIEW user_view;
+
index 08eb56ca08dc6046b4a7002715b05138fc23aabf..a65905935a8c6d69ecb01f56e98978be45a96d79 100644 (file)
@@ -1,12 +1,43 @@
-create view user_view as 
-select id,
-name,
-fedi_name,
-admin,
-banned,
-published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
+CREATE VIEW user_view AS
+SELECT
+    id,
+    name,
+    fedi_name,
+    admin,
+    banned,
+    published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
+
index 95018f35acf3f5d4000f899e0ef9c7805564e2a8..bf3b8bbf6a75bc75cc24c6bcfe38d3e46bddd310 100644 (file)
@@ -1,8 +1,16 @@
-drop view mod_remove_post_view;
-drop view mod_lock_post_view;
-drop view mod_remove_comment_view;
-drop view mod_remove_community_view;
-drop view mod_ban_from_community_view;
-drop view mod_ban_view;
-drop view mod_add_community_view;
-drop view mod_add_view;
+DROP VIEW mod_remove_post_view;
+
+DROP VIEW mod_lock_post_view;
+
+DROP VIEW mod_remove_comment_view;
+
+DROP VIEW mod_remove_community_view;
+
+DROP VIEW mod_ban_from_community_view;
+
+DROP VIEW mod_ban_view;
+
+DROP VIEW mod_add_community_view;
+
+DROP VIEW mod_add_view;
+
index 70a33e469ca4bcb59103a8fcb5cc3acabe8667c6..b252a4ba9af9a25cf8059120feb61938b7074c56 100644 (file)
-create view mod_remove_post_view as 
-select mrp.*,
-(select name from user_ u where mrp.mod_user_id = u.id) as mod_user_name,
-(select name from post p where mrp.post_id = p.id) as post_name,
-(select c.id from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_id,
-(select c.name from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_name
-from mod_remove_post mrp;
-
-create view mod_lock_post_view as 
-select mlp.*,
-(select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
-(select name from post p where mlp.post_id = p.id) as post_name,
-(select c.id from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_id,
-(select c.name from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_name
-from mod_lock_post mlp;
-
-create view mod_remove_comment_view as 
-select mrc.*,
-(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
-(select c.id from comment c where mrc.comment_id = c.id) as comment_user_id,
-(select name from user_ u, comment c where mrc.comment_id = c.id and u.id = c.creator_id) as comment_user_name,
-(select content from comment c where mrc.comment_id = c.id) as comment_content,
-(select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
-(select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
-(select co.id from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_id, 
-(select co.name from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_name
-from mod_remove_comment mrc;
-
-create view mod_remove_community_view as 
-select mrc.*,
-(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
-(select c.name from community c where mrc.community_id = c.id) as community_name
-from mod_remove_community mrc;
-
-create view mod_ban_from_community_view as 
-select mb.*,
-(select name from user_ u where mb.mod_user_id = u.id) as mod_user_name,
-(select name from user_ u where mb.other_user_id = u.id) as other_user_name,
-(select name from community c where mb.community_id = c.id) as community_name
-from mod_ban_from_community mb;
-
-create view mod_ban_view as 
-select mb.*,
-(select name from user_ u where mb.mod_user_id = u.id) as mod_user_name,
-(select name from user_ u where mb.other_user_id = u.id) as other_user_name
-from mod_ban mb;
-
-create view mod_add_community_view as 
-select ma.*,
-(select name from user_ u where ma.mod_user_id = u.id) as mod_user_name,
-(select name from user_ u where ma.other_user_id = u.id) as other_user_name,
-(select name from community c where ma.community_id = c.id) as community_name
-from mod_add_community ma;
-
-create view mod_add_view as 
-select ma.*,
-(select name from user_ u where ma.mod_user_id = u.id) as mod_user_name,
-(select name from user_ u where ma.other_user_id = u.id) as other_user_name
-from mod_add ma;
+CREATE VIEW mod_remove_post_view AS
+SELECT
+    mrp.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mrp.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            post p
+        WHERE
+            mrp.post_id = p.id) AS post_name,
+    (
+        SELECT
+            c.id
+        FROM
+            post p,
+            community c
+        WHERE
+            mrp.post_id = p.id
+            AND p.community_id = c.id) AS community_id,
+    (
+        SELECT
+            c.name
+        FROM
+            post p,
+            community c
+        WHERE
+            mrp.post_id = p.id
+            AND p.community_id = c.id) AS community_name
+FROM
+    mod_remove_post mrp;
+
+CREATE VIEW mod_lock_post_view AS
+SELECT
+    mlp.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mlp.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            post p
+        WHERE
+            mlp.post_id = p.id) AS post_name,
+    (
+        SELECT
+            c.id
+        FROM
+            post p,
+            community c
+        WHERE
+            mlp.post_id = p.id
+            AND p.community_id = c.id) AS community_id,
+    (
+        SELECT
+            c.name
+        FROM
+            post p,
+            community c
+        WHERE
+            mlp.post_id = p.id
+            AND p.community_id = c.id) AS community_name
+FROM
+    mod_lock_post mlp;
+
+CREATE VIEW mod_remove_comment_view AS
+SELECT
+    mrc.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mrc.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            c.id
+        FROM
+            comment c
+        WHERE
+            mrc.comment_id = c.id) AS comment_user_id,
+    (
+        SELECT
+            name
+        FROM
+            user_ u,
+            comment c
+        WHERE
+            mrc.comment_id = c.id
+            AND u.id = c.creator_id) AS comment_user_name,
+    (
+        SELECT
+            content
+        FROM
+            comment c
+        WHERE
+            mrc.comment_id = c.id) AS comment_content,
+    (
+        SELECT
+            p.id
+        FROM
+            post p,
+            comment c
+        WHERE
+            mrc.comment_id = c.id
+            AND c.post_id = p.id) AS post_id,
+    (
+        SELECT
+            p.name
+        FROM
+            post p,
+            comment c
+        WHERE
+            mrc.comment_id = c.id
+            AND c.post_id = p.id) AS post_name,
+    (
+        SELECT
+            co.id
+        FROM
+            comment c,
+            post p,
+            community co
+        WHERE
+            mrc.comment_id = c.id
+            AND c.post_id = p.id
+            AND p.community_id = co.id) AS community_id,
+    (
+        SELECT
+            co.name
+        FROM
+            comment c,
+            post p,
+            community co
+        WHERE
+            mrc.comment_id = c.id
+            AND c.post_id = p.id
+            AND p.community_id = co.id) AS community_name
+FROM
+    mod_remove_comment mrc;
+
+CREATE VIEW mod_remove_community_view AS
+SELECT
+    mrc.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mrc.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            c.name
+        FROM
+            community c
+        WHERE
+            mrc.community_id = c.id) AS community_name
+FROM
+    mod_remove_community mrc;
+
+CREATE VIEW mod_ban_from_community_view AS
+SELECT
+    mb.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mb.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mb.other_user_id = u.id) AS other_user_name,
+    (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            mb.community_id = c.id) AS community_name
+FROM
+    mod_ban_from_community mb;
+
+CREATE VIEW mod_ban_view AS
+SELECT
+    mb.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mb.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mb.other_user_id = u.id) AS other_user_name
+FROM
+    mod_ban mb;
+
+CREATE VIEW mod_add_community_view AS
+SELECT
+    ma.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            ma.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            ma.other_user_id = u.id) AS other_user_name,
+    (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            ma.community_id = c.id) AS community_name
+FROM
+    mod_add_community ma;
+
+CREATE VIEW mod_add_view AS
+SELECT
+    ma.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            ma.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            ma.other_user_id = u.id) AS other_user_name
+FROM
+    mod_add ma;
+
index 5e13295b2a2f496f24997dd927776eeeff799f2d..2cb9fda3d8ec824ba61980fda6e0b5285344e505 100644 (file)
-drop view reply_view;
-drop view comment_view;
-drop view community_view;
-drop view post_view;
-alter table community drop column deleted;
-alter table post drop column deleted;
-alter table comment drop column deleted;
+DROP VIEW reply_view;
 
-create view community_view as 
-with all_community as
-(
-  select *,
-  (select name from user_ u where c.creator_id = u.id) as creator_name,
-  (select name from category ct where c.category_id = ct.id) as category_name,
-  (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-  (select count(*) from post p where p.community_id = c.id) as number_of_posts,
-  (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
-  from community c
-)
-
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
+DROP VIEW comment_view;
 
-union all
+DROP VIEW community_view;
 
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
+DROP VIEW post_view;
 
-create or replace view post_view as
-with all_post as
-(
-  select        
-  p.*,
-  (select name from user_ where p.creator_id = user_.id) as creator_name,
-  (select name from community where p.community_id = community.id) as community_name,
-  (select removed from community c where p.community_id = c.id) as community_removed,
-  (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-  coalesce(sum(pl.score), 0) as score,
-  count (case when pl.score = 1 then 1 else null end) as upvotes,
-  count (case when pl.score = -1 then 1 else null end) as downvotes,
-  hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-  from post p
-  left join post_like pl on p.id = pl.post_id
-  group by p.id
-)
+ALTER TABLE community
+    DROP COLUMN deleted;
 
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+ALTER TABLE post
+    DROP COLUMN deleted;
 
-union all
+ALTER TABLE comment
+    DROP COLUMN deleted;
 
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
-
-create view comment_view as
-with all_comment as
-(
-  select        
-  c.*,
-  (select community_id from post p where p.id = c.post_id),
-  (select u.banned from user_ u where c.creator_id = u.id) as banned,
-  (select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
-  (select name from user_ where c.creator_id = user_.id) as creator_name,
-  coalesce(sum(cl.score), 0) as score,
-  count (case when cl.score = 1 then 1 else null end) as upvotes,
-  count (case when cl.score = -1 then 1 else null end) as downvotes
-  from comment c
-  left join comment_like cl on c.id = cl.comment_id
-  group by c.id
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        *,
+        (
+            SELECT
+                name
+            FROM
+                user_ u
+            WHERE
+                c.creator_id = u.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                category ct
+            WHERE
+                c.category_id = ct.id) AS category_name,
+        (
+            SELECT
+                count(*)
+            FROM
+                community_follower cf
+            WHERE
+                cf.community_id = c.id) AS number_of_subscribers,
+        (
+            SELECT
+                count(*)
+            FROM
+                post p
+            WHERE
+                p.community_id = c.id) AS number_of_posts,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment co,
+                post p
+            WHERE
+                c.id = p.community_id
+                AND p.id = co.post_id) AS number_of_comments
+    FROM
+        community c
 )
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-
-union all
+CREATE OR REPLACE VIEW post_view AS
+with all_post AS (
+    SELECT
+        p.*,
+        (
+            SELECT
+                name
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                community
+            WHERE
+                p.community_id = community.id) AS community_name,
+        (
+            SELECT
+                removed
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_removed,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment
+            WHERE
+                comment.post_id = p.id) AS number_of_comments,
+        coalesce(sum(pl.score), 0) AS score,
+        count(
+            CASE WHEN pl.score = 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS upvotes,
+        count(
+            CASE WHEN pl.score = - 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS downvotes,
+        hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+    FROM
+        post p
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        p.id
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
-select 
-    ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as saved
-from all_comment ac
-;
+CREATE VIEW comment_view AS
+with all_comment AS (
+    SELECT
+        c.*,
+        (
+            SELECT
+                community_id
+            FROM
+                post p
+            WHERE
+                p.id = c.post_id),
+            (
+                SELECT
+                    u.banned
+                FROM
+                    user_ u
+                WHERE
+                    c.creator_id = u.id) AS banned,
+                (
+                    SELECT
+                        cb.id::bool
+                    FROM
+                        community_user_ban cb,
+                        post p
+                    WHERE
+                        c.creator_id = cb.user_id
+                        AND p.id = c.post_id
+                        AND p.community_id = cb.community_id) AS banned_from_community,
+                    (
+                        SELECT
+                            name
+                        FROM
+                            user_
+                        WHERE
+                            c.creator_id = user_.id) AS creator_name,
+                        coalesce(sum(cl.score), 0) AS score,
+                    count(
+                        CASE WHEN cl.score = 1 THEN
+                            1
+                        ELSE
+                            NULL
+                        END) AS upvotes,
+                    count(
+                        CASE WHEN cl.score = - 1 THEN
+                            1
+                        ELSE
+                            NULL
+                        END) AS downvotes
+                FROM
+                    comment c
+                LEFT JOIN comment_like cl ON c.id = cl.comment_id
+            GROUP BY
+                c.id
+)
+    SELECT
+        ac.*,
+        u.id AS user_id,
+        coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS saved
+    FROM
+        all_comment ac;
 
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
index 88432dda4d33dc86fea7f309785c4fbd5b0ec02c..05ac13896bf887411f0715dde93d6cb0ce381d71 100644 (file)
-alter table community add column deleted boolean default false not null;
-alter table post add column deleted boolean default false not null;
-alter table comment add column deleted boolean default false not null;
+ALTER TABLE community
+    ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
 
--- The views
-drop view community_view;
-
-create view community_view as 
-with all_community as
-(
-  select *,
-  (select name from user_ u where c.creator_id = u.id) as creator_name,
-  (select name from category ct where c.category_id = ct.id) as category_name,
-  (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-  (select count(*) from post p where p.community_id = c.id) as number_of_posts,
-  (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
-  from community c
-)
+ALTER TABLE post
+    ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
 
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
-
-union all
-
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
+ALTER TABLE comment
+    ADD COLUMN deleted boolean DEFAULT FALSE NOT NULL;
 
+-- The views
+DROP VIEW community_view;
 
-drop view post_view;
-create view post_view as
-with all_post as
-(
-  select        
-  p.*,
-  (select name from user_ where p.creator_id = user_.id) as creator_name,
-  (select name from community where p.community_id = community.id) as community_name,
-  (select removed from community c where p.community_id = c.id) as community_removed,
-  (select deleted from community c where p.community_id = c.id) as community_deleted,
-  (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-  coalesce(sum(pl.score), 0) as score,
-  count (case when pl.score = 1 then 1 else null end) as upvotes,
-  count (case when pl.score = -1 then 1 else null end) as downvotes,
-  hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-  from post p
-  left join post_like pl on p.id = pl.post_id
-  group by p.id
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        *,
+        (
+            SELECT
+                name
+            FROM
+                user_ u
+            WHERE
+                c.creator_id = u.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                category ct
+            WHERE
+                c.category_id = ct.id) AS category_name,
+        (
+            SELECT
+                count(*)
+            FROM
+                community_follower cf
+            WHERE
+                cf.community_id = c.id) AS number_of_subscribers,
+        (
+            SELECT
+                count(*)
+            FROM
+                post p
+            WHERE
+                p.community_id = c.id) AS number_of_posts,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment co,
+                post p
+            WHERE
+                c.id = p.community_id
+                AND p.id = co.post_id) AS number_of_comments
+    FROM
+        community c
 )
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+DROP VIEW post_view;
 
-drop view reply_view;
-drop view comment_view;
-create view comment_view as
-with all_comment as
-(
-  select        
-  c.*,
-  (select community_id from post p where p.id = c.post_id),
-  (select u.banned from user_ u where c.creator_id = u.id) as banned,
-  (select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
-  (select name from user_ where c.creator_id = user_.id) as creator_name,
-  coalesce(sum(cl.score), 0) as score,
-  count (case when cl.score = 1 then 1 else null end) as upvotes,
-  count (case when cl.score = -1 then 1 else null end) as downvotes
-  from comment c
-  left join comment_like cl on c.id = cl.comment_id
-  group by c.id
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        p.*,
+        (
+            SELECT
+                name
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                community
+            WHERE
+                p.community_id = community.id) AS community_name,
+        (
+            SELECT
+                removed
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_removed,
+        (
+            SELECT
+                deleted
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_deleted,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment
+            WHERE
+                comment.post_id = p.id) AS number_of_comments,
+        coalesce(sum(pl.score), 0) AS score,
+        count(
+            CASE WHEN pl.score = 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS upvotes,
+        count(
+            CASE WHEN pl.score = - 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS downvotes,
+        hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+    FROM
+        post p
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        p.id
 )
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
+DROP VIEW reply_view;
 
-union all
+DROP VIEW comment_view;
 
-select 
-    ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as saved
-from all_comment ac
-;
+CREATE VIEW comment_view AS
+with all_comment AS (
+    SELECT
+        c.*,
+        (
+            SELECT
+                community_id
+            FROM
+                post p
+            WHERE
+                p.id = c.post_id),
+            (
+                SELECT
+                    u.banned
+                FROM
+                    user_ u
+                WHERE
+                    c.creator_id = u.id) AS banned,
+                (
+                    SELECT
+                        cb.id::bool
+                    FROM
+                        community_user_ban cb,
+                        post p
+                    WHERE
+                        c.creator_id = cb.user_id
+                        AND p.id = c.post_id
+                        AND p.community_id = cb.community_id) AS banned_from_community,
+                    (
+                        SELECT
+                            name
+                        FROM
+                            user_
+                        WHERE
+                            c.creator_id = user_.id) AS creator_name,
+                        coalesce(sum(cl.score), 0) AS score,
+                    count(
+                        CASE WHEN cl.score = 1 THEN
+                            1
+                        ELSE
+                            NULL
+                        END) AS upvotes,
+                    count(
+                        CASE WHEN cl.score = - 1 THEN
+                            1
+                        ELSE
+                            NULL
+                        END) AS downvotes
+                FROM
+                    comment c
+                LEFT JOIN comment_like cl ON c.id = cl.comment_id
+            GROUP BY
+                c.id
+)
+    SELECT
+        ac.*,
+        u.id AS user_id,
+        coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS saved
+    FROM
+        all_comment ac;
 
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
index 0f3a58a8b537812d85789dc8678e1deeee1c93b2..7d3af2500c8712caf0501b38fbf8db687dcd329b 100644 (file)
@@ -1,28 +1,68 @@
-drop view community_view;
-create view community_view as 
-with all_community as
-(
-  select *,
-  (select name from user_ u where c.creator_id = u.id) as creator_name,
-  (select name from category ct where c.category_id = ct.id) as category_name,
-  (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-  (select count(*) from post p where p.community_id = c.id) as number_of_posts,
-  (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments
-  from community c
-)
-
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
+DROP VIEW community_view;
 
-union all
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        *,
+        (
+            SELECT
+                name
+            FROM
+                user_ u
+            WHERE
+                c.creator_id = u.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                category ct
+            WHERE
+                c.category_id = ct.id) AS category_name,
+        (
+            SELECT
+                count(*)
+            FROM
+                community_follower cf
+            WHERE
+                cf.community_id = c.id) AS number_of_subscribers,
+        (
+            SELECT
+                count(*)
+            FROM
+                post p
+            WHERE
+                p.community_id = c.id) AS number_of_posts,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment co,
+                post p
+            WHERE
+                c.id = p.community_id
+                AND p.id = co.post_id) AS number_of_comments
+    FROM
+        community c
+)
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
index e7e753665f6d99aa9859d127e557d563762fac77..fdd79e4c9d6b2e86ac9bda516a89717c00133dce 100644 (file)
@@ -1,29 +1,74 @@
-drop view community_view;
-create view community_view as 
-with all_community as
-(
-  select *,
-  (select name from user_ u where c.creator_id = u.id) as creator_name,
-  (select name from category ct where c.category_id = ct.id) as category_name,
-  (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-  (select count(*) from post p where p.community_id = c.id) as number_of_posts,
-  (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
-  hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
-  from community c
-)
-
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
+DROP VIEW community_view;
 
-union all
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        *,
+        (
+            SELECT
+                name
+            FROM
+                user_ u
+            WHERE
+                c.creator_id = u.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                category ct
+            WHERE
+                c.category_id = ct.id) AS category_name,
+        (
+            SELECT
+                count(*)
+            FROM
+                community_follower cf
+            WHERE
+                cf.community_id = c.id) AS number_of_subscribers,
+        (
+            SELECT
+                count(*)
+            FROM
+                post p
+            WHERE
+                p.community_id = c.id) AS number_of_posts,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment co,
+                post p
+            WHERE
+                c.id = p.community_id
+                AND p.id = co.post_id) AS number_of_comments,
+        hot_rank ((
+            SELECT
+                count(*)
+            FROM community_follower cf
+            WHERE
+                cf.community_id = c.id), c.published) AS hot_rank
+FROM
+    community c
+)
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
index 6178857fa9c20a0a96347786d6ec32a5705681f1..926c18afc3186b2c2c78822031e870ccd37a33b0 100644 (file)
@@ -1 +1,3 @@
-insert into user_ (name, fedi_name, password_encrypted) values ('admin', 'TBD', 'TBD');
+INSERT INTO user_ (name, fedi_name, password_encrypted)
+    VALUES ('admin', 'TBD', 'TBD');
+
index 7cec88705728ddfd8ff77363fb3b910380e05ea3..cf3d20d4264f58b329fe32ffbcabf634794e2d54 100644 (file)
@@ -1 +1,3 @@
-delete from user_ where name like 'admin';
+DELETE FROM user_
+WHERE name LIKE 'admin';
+
index 2eefece4e5993bf6f5c66ff8f54e618f714e428f..12c46b7fe791236b61c611e18693afb2fade240c 100644 (file)
-drop view community_view;
-drop view post_view;
-alter table community drop column nsfw;
-alter table post drop column nsfw;
-alter table user_ drop column show_nsfw;
+DROP VIEW community_view;
 
--- the views
-create view community_view as 
-with all_community as
-(
-  select *,
-  (select name from user_ u where c.creator_id = u.id) as creator_name,
-  (select name from category ct where c.category_id = ct.id) as category_name,
-  (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-  (select count(*) from post p where p.community_id = c.id) as number_of_posts,
-  (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
-  hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
-  from community c
-)
+DROP VIEW post_view;
 
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
+ALTER TABLE community
+    DROP COLUMN nsfw;
 
-union all
+ALTER TABLE post
+    DROP COLUMN nsfw;
 
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
+ALTER TABLE user_
+    DROP COLUMN show_nsfw;
 
+-- the views
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        *,
+        (
+            SELECT
+                name
+            FROM
+                user_ u
+            WHERE
+                c.creator_id = u.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                category ct
+            WHERE
+                c.category_id = ct.id) AS category_name,
+        (
+            SELECT
+                count(*)
+            FROM
+                community_follower cf
+            WHERE
+                cf.community_id = c.id) AS number_of_subscribers,
+        (
+            SELECT
+                count(*)
+            FROM
+                post p
+            WHERE
+                p.community_id = c.id) AS number_of_posts,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment co,
+                post p
+            WHERE
+                c.id = p.community_id
+                AND p.id = co.post_id) AS number_of_comments,
+        hot_rank ((
+            SELECT
+                count(*)
+            FROM community_follower cf
+            WHERE
+                cf.community_id = c.id), c.published) AS hot_rank
+FROM
+    community c
+)
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
 -- Post view
-create view post_view as
-with all_post as
-(
-  select        
-  p.*,
-  (select name from user_ where p.creator_id = user_.id) as creator_name,
-  (select name from community where p.community_id = community.id) as community_name,
-  (select removed from community c where p.community_id = c.id) as community_removed,
-  (select deleted from community c where p.community_id = c.id) as community_deleted,
-  (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-  coalesce(sum(pl.score), 0) as score,
-  count (case when pl.score = 1 then 1 else null end) as upvotes,
-  count (case when pl.score = -1 then 1 else null end) as downvotes,
-  hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-  from post p
-  left join post_like pl on p.id = pl.post_id
-  group by p.id
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        p.*,
+        (
+            SELECT
+                name
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                community
+            WHERE
+                p.community_id = community.id) AS community_name,
+        (
+            SELECT
+                removed
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_removed,
+        (
+            SELECT
+                deleted
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_deleted,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment
+            WHERE
+                comment.post_id = p.id) AS number_of_comments,
+        coalesce(sum(pl.score), 0) AS score,
+        count(
+            CASE WHEN pl.score = 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS upvotes,
+        count(
+            CASE WHEN pl.score = - 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS downvotes,
+        hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+    FROM
+        post p
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        p.id
 )
-
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
index cc1e00740c06af808f02682871c9e469d8985468..a43718187d6248f709374a7e02e96371ffdf94c2 100644 (file)
-alter table community add column nsfw boolean default false not null;
-alter table post add column nsfw boolean default false not null;
-alter table user_ add column show_nsfw boolean default false not null;
+ALTER TABLE community
+    ADD COLUMN nsfw boolean DEFAULT FALSE NOT NULL;
 
--- The views
-drop view community_view;
-create view community_view as 
-with all_community as
-(
-  select *,
-  (select name from user_ u where c.creator_id = u.id) as creator_name,
-  (select name from category ct where c.category_id = ct.id) as category_name,
-  (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-  (select count(*) from post p where p.community_id = c.id) as number_of_posts,
-  (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
-  hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
-  from community c
-)
+ALTER TABLE post
+    ADD COLUMN nsfw boolean DEFAULT FALSE NOT NULL;
 
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
+ALTER TABLE user_
+    ADD COLUMN show_nsfw boolean DEFAULT FALSE NOT NULL;
 
-union all
+-- The views
+DROP VIEW community_view;
 
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
--- Post view
-drop view post_view;
-create view post_view as
-with all_post as
-(
-  select        
-  p.*,
-  (select name from user_ where p.creator_id = user_.id) as creator_name,
-  (select name from community where p.community_id = community.id) as community_name,
-  (select removed from community c where p.community_id = c.id) as community_removed,
-  (select deleted from community c where p.community_id = c.id) as community_deleted,
-  (select nsfw from community c where p.community_id = c.id) as community_nsfw,
-  (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-  coalesce(sum(pl.score), 0) as score,
-  count (case when pl.score = 1 then 1 else null end) as upvotes,
-  count (case when pl.score = -1 then 1 else null end) as downvotes,
-  hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-  from post p
-  left join post_like pl on p.id = pl.post_id
-  group by p.id
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        *,
+        (
+            SELECT
+                name
+            FROM
+                user_ u
+            WHERE
+                c.creator_id = u.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                category ct
+            WHERE
+                c.category_id = ct.id) AS category_name,
+        (
+            SELECT
+                count(*)
+            FROM
+                community_follower cf
+            WHERE
+                cf.community_id = c.id) AS number_of_subscribers,
+        (
+            SELECT
+                count(*)
+            FROM
+                post p
+            WHERE
+                p.community_id = c.id) AS number_of_posts,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment co,
+                post p
+            WHERE
+                c.id = p.community_id
+                AND p.id = co.post_id) AS number_of_comments,
+        hot_rank ((
+            SELECT
+                count(*)
+            FROM community_follower cf
+            WHERE
+                cf.community_id = c.id), c.published) AS hot_rank
+FROM
+    community c
 )
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+-- Post view
+DROP VIEW post_view;
 
-union all
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        p.*,
+        (
+            SELECT
+                name
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                community
+            WHERE
+                p.community_id = community.id) AS community_name,
+        (
+            SELECT
+                removed
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_removed,
+        (
+            SELECT
+                deleted
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_deleted,
+        (
+            SELECT
+                nsfw
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_nsfw,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment
+            WHERE
+                comment.post_id = p.id) AS number_of_comments,
+        coalesce(sum(pl.score), 0) AS score,
+        count(
+            CASE WHEN pl.score = 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS upvotes,
+        count(
+            CASE WHEN pl.score = - 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS downvotes,
+        hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+    FROM
+        post p
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        p.id
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
index 6302f2675a50d07a4eb38fd2626225046c85fd9d..a016508f3bc256735037e9fefe4f9be2daf123f5 100644 (file)
@@ -1,9 +1,30 @@
-drop view site_view;
+DROP VIEW site_view;
+
+CREATE VIEW site_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            s.creator_id = u.id) AS creator_name,
+    (
+        SELECT
+            count(*)
+        FROM
+            user_) AS number_of_users,
+    (
+        SELECT
+            count(*)
+        FROM
+            post) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment) AS number_of_comments
+FROM
+    site s;
 
-create view site_view as 
-select *,
-(select name from user_ u where s.creator_id = u.id) as creator_name,
-(select count(*) from user_) as number_of_users,
-(select count(*) from post) as number_of_posts,
-(select count(*) from comment) as number_of_comments
-from site s;
index 0ec1c9c33fd9a1283964c6e15776dd48ff7965dc..9747ac846d9da3a0e0743860b2b23edae6d0dab4 100644 (file)
@@ -1,10 +1,35 @@
-drop view site_view;
+DROP VIEW site_view;
+
+CREATE VIEW site_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            s.creator_id = u.id) AS creator_name,
+    (
+        SELECT
+            count(*)
+        FROM
+            user_) AS number_of_users,
+    (
+        SELECT
+            count(*)
+        FROM
+            post) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment) AS number_of_comments,
+    (
+        SELECT
+            count(*)
+        FROM
+            community) AS number_of_communities
+FROM
+    site s;
 
-create view site_view as 
-select *,
-(select name from user_ u where s.creator_id = u.id) as creator_name,
-(select count(*) from user_) as number_of_users,
-(select count(*) from post) as number_of_posts,
-(select count(*) from comment) as number_of_comments,
-(select count(*) from community) as number_of_communities
-from site s;
index c60b672c29306982b80d130303f6df62c25c06d9..2b5782e0359b0dfde6c91e4d8b2958b50ceae2e7 100644 (file)
 -- Post view
-drop view post_view;
-create view post_view as
-with all_post as
-(
-  select        
-  p.*,
-  (select name from user_ where p.creator_id = user_.id) as creator_name,
-  (select name from community where p.community_id = community.id) as community_name,
-  (select removed from community c where p.community_id = c.id) as community_removed,
-  (select deleted from community c where p.community_id = c.id) as community_deleted,
-  (select nsfw from community c where p.community_id = c.id) as community_nsfw,
-  (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-  coalesce(sum(pl.score), 0) as score,
-  count (case when pl.score = 1 then 1 else null end) as upvotes,
-  count (case when pl.score = -1 then 1 else null end) as downvotes,
-  hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-  from post p
-  left join post_like pl on p.id = pl.post_id
-  group by p.id
-)
-
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+DROP VIEW post_view;
 
-union all
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        p.*,
+        (
+            SELECT
+                name
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                community
+            WHERE
+                p.community_id = community.id) AS community_name,
+        (
+            SELECT
+                removed
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_removed,
+        (
+            SELECT
+                deleted
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_deleted,
+        (
+            SELECT
+                nsfw
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_nsfw,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment
+            WHERE
+                comment.post_id = p.id) AS number_of_comments,
+        coalesce(sum(pl.score), 0) AS score,
+        count(
+            CASE WHEN pl.score = 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS upvotes,
+        count(
+            CASE WHEN pl.score = - 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS downvotes,
+        hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+    FROM
+        post p
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        p.id
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
index d73b3720845fd5fae4ad1f252f1f2ee299062a0d..94fc5754a0e489a45b730c4c71f890f45d8e4119 100644 (file)
--- Create post view, adding banned_from_community 
+-- Create post view, adding banned_from_community
+DROP VIEW post_view;
 
-drop view post_view;
-create view post_view as
-with all_post as
-(
-  select        
-  p.*,
-  (select u.banned from user_ u where p.creator_id = u.id) as banned,
-  (select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-  (select name from user_ where p.creator_id = user_.id) as creator_name,
-  (select name from community where p.community_id = community.id) as community_name,
-  (select removed from community c where p.community_id = c.id) as community_removed,
-  (select deleted from community c where p.community_id = c.id) as community_deleted,
-  (select nsfw from community c where p.community_id = c.id) as community_nsfw,
-  (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-  coalesce(sum(pl.score), 0) as score,
-  count (case when pl.score = 1 then 1 else null end) as upvotes,
-  count (case when pl.score = -1 then 1 else null end) as downvotes,
-  hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-  from post p
-  left join post_like pl on p.id = pl.post_id
-  group by p.id
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        p.*,
+        (
+            SELECT
+                u.banned
+            FROM
+                user_ u
+            WHERE
+                p.creator_id = u.id) AS banned,
+        (
+            SELECT
+                cb.id::bool
+            FROM
+                community_user_ban cb
+            WHERE
+                p.creator_id = cb.user_id
+                AND p.community_id = cb.community_id) AS banned_from_community,
+        (
+            SELECT
+                name
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                community
+            WHERE
+                p.community_id = community.id) AS community_name,
+        (
+            SELECT
+                removed
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_removed,
+        (
+            SELECT
+                deleted
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_deleted,
+        (
+            SELECT
+                nsfw
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_nsfw,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment
+            WHERE
+                comment.post_id = p.id) AS number_of_comments,
+        coalesce(sum(pl.score), 0) AS score,
+        count(
+            CASE WHEN pl.score = 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS upvotes,
+        count(
+            CASE WHEN pl.score = - 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS downvotes,
+        hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+    FROM
+        post p
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        p.id
 )
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
index fb8eac8fd66d4537446a39b07d56e01712e22324..838959b35999e6d8bc6240d032d2d2c21f7e300b 100644 (file)
-drop view post_view;
-drop view mod_sticky_post_view;
-alter table post drop column stickied;
+DROP VIEW post_view;
 
-drop table mod_sticky_post;
+DROP VIEW mod_sticky_post_view;
 
-create view post_view as
-with all_post as
-(
-  select        
-  p.*,
-  (select u.banned from user_ u where p.creator_id = u.id) as banned,
-  (select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-  (select name from user_ where p.creator_id = user_.id) as creator_name,
-  (select name from community where p.community_id = community.id) as community_name,
-  (select removed from community c where p.community_id = c.id) as community_removed,
-  (select deleted from community c where p.community_id = c.id) as community_deleted,
-  (select nsfw from community c where p.community_id = c.id) as community_nsfw,
-  (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-  coalesce(sum(pl.score), 0) as score,
-  count (case when pl.score = 1 then 1 else null end) as upvotes,
-  count (case when pl.score = -1 then 1 else null end) as downvotes,
-  hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-  from post p
-  left join post_like pl on p.id = pl.post_id
-  group by p.id
-)
+ALTER TABLE post
+    DROP COLUMN stickied;
 
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+DROP TABLE mod_sticky_post;
 
-union all
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        p.*,
+        (
+            SELECT
+                u.banned
+            FROM
+                user_ u
+            WHERE
+                p.creator_id = u.id) AS banned,
+        (
+            SELECT
+                cb.id::bool
+            FROM
+                community_user_ban cb
+            WHERE
+                p.creator_id = cb.user_id
+                AND p.community_id = cb.community_id) AS banned_from_community,
+        (
+            SELECT
+                name
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                community
+            WHERE
+                p.community_id = community.id) AS community_name,
+        (
+            SELECT
+                removed
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_removed,
+        (
+            SELECT
+                deleted
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_deleted,
+        (
+            SELECT
+                nsfw
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_nsfw,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment
+            WHERE
+                comment.post_id = p.id) AS number_of_comments,
+        coalesce(sum(pl.score), 0) AS score,
+        count(
+            CASE WHEN pl.score = 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS upvotes,
+        count(
+            CASE WHEN pl.score = - 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS downvotes,
+        hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+    FROM
+        post p
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        p.id
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
index 0848f86cad7f564324542bf8a6dbc539fcd5d61a..555d287448523e988c7b3f05410737c0076a848a 100644 (file)
 -- Add the column
-alter table post add column stickied boolean default false not null;
+ALTER TABLE post
+    ADD COLUMN stickied boolean DEFAULT FALSE NOT NULL;
 
 -- Add the mod table
-create table mod_sticky_post (
-  id serial primary key,
-  mod_user_id int references user_ on update cascade on delete cascade not null,
-  post_id int references post on update cascade on delete cascade not null,
-  stickied boolean default true,
-  when_ timestamp not null default now()
+CREATE TABLE mod_sticky_post (
+    id serial PRIMARY KEY,
+    mod_user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    stickied boolean DEFAULT TRUE,
+    when_ timestamp NOT NULL DEFAULT now()
 );
 
 -- Add mod view
-create view mod_sticky_post_view as 
-select msp.*,
-(select name from user_ u where msp.mod_user_id = u.id) as mod_user_name,
-(select name from post p where msp.post_id = p.id) as post_name,
-(select c.id from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_id,
-(select c.name from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_name
-from mod_sticky_post msp;
+CREATE VIEW mod_sticky_post_view AS
+SELECT
+    msp.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            msp.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            post p
+        WHERE
+            msp.post_id = p.id) AS post_name,
+    (
+        SELECT
+            c.id
+        FROM
+            post p,
+            community c
+        WHERE
+            msp.post_id = p.id
+            AND p.community_id = c.id) AS community_id,
+    (
+        SELECT
+            c.name
+        FROM
+            post p,
+            community c
+        WHERE
+            msp.post_id = p.id
+            AND p.community_id = c.id) AS community_name
+FROM
+    mod_sticky_post msp;
 
 -- Recreate the view
-drop view post_view;
-create view post_view as
-with all_post as
-(
-  select        
-  p.*,
-  (select u.banned from user_ u where p.creator_id = u.id) as banned,
-  (select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-  (select name from user_ where p.creator_id = user_.id) as creator_name,
-  (select name from community where p.community_id = community.id) as community_name,
-  (select removed from community c where p.community_id = c.id) as community_removed,
-  (select deleted from community c where p.community_id = c.id) as community_deleted,
-  (select nsfw from community c where p.community_id = c.id) as community_nsfw,
-  (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-  coalesce(sum(pl.score), 0) as score,
-  count (case when pl.score = 1 then 1 else null end) as upvotes,
-  count (case when pl.score = -1 then 1 else null end) as downvotes,
-  hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-  from post p
-  left join post_like pl on p.id = pl.post_id
-  group by p.id
-)
-
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+DROP VIEW post_view;
 
-union all
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        p.*,
+        (
+            SELECT
+                u.banned
+            FROM
+                user_ u
+            WHERE
+                p.creator_id = u.id) AS banned,
+        (
+            SELECT
+                cb.id::bool
+            FROM
+                community_user_ban cb
+            WHERE
+                p.creator_id = cb.user_id
+                AND p.community_id = cb.community_id) AS banned_from_community,
+        (
+            SELECT
+                name
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                community
+            WHERE
+                p.community_id = community.id) AS community_name,
+        (
+            SELECT
+                removed
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_removed,
+        (
+            SELECT
+                deleted
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_deleted,
+        (
+            SELECT
+                nsfw
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_nsfw,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment
+            WHERE
+                comment.post_id = p.id) AS number_of_comments,
+        coalesce(sum(pl.score), 0) AS score,
+        count(
+            CASE WHEN pl.score = 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS upvotes,
+        count(
+            CASE WHEN pl.score = - 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS downvotes,
+        hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+    FROM
+        post p
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        p.id
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
index 67a5e17bfa312e6d75e9ab4842821a2ae065aa91..a310630262b5e80b8e0fdd1ec1dc075d8799e685 100644 (file)
@@ -1 +1,3 @@
-alter table user_ drop column theme;
+ALTER TABLE user_
+    DROP COLUMN theme;
+
index e9c3666bc6176040ac447d6c766250e61f1a8928..00b3d10af0f270e243f1d59db2d145f7603db325 100644 (file)
@@ -1 +1,3 @@
-alter table user_ add column theme varchar(20) default 'darkly' not null;
+ALTER TABLE user_
+    ADD COLUMN theme varchar(20) DEFAULT 'darkly' NOT NULL;
+
index 7165bc86dddff2a9fffdd9859b9802bfc7a7a49b..e21425c59ff6c922cfd9f71c9a9506ec8855c5fd 100644 (file)
@@ -1,2 +1,4 @@
-drop view user_mention_view;
-drop table user_mention;
+DROP VIEW user_mention_view;
+
+DROP TABLE user_mention;
+
index 81fef0082f29ec0e2553ba9c4a11a3de7dfa68ad..b02c57529efe605b2c8834ec724109760479231d 100644 (file)
@@ -1,16 +1,16 @@
-create table user_mention (
-  id serial primary key,
-  recipient_id int references user_ on update cascade on delete cascade not null,
-  comment_id int references comment on update cascade on delete cascade not null,
-  read boolean default false not null,
-  published timestamp not null default now(),
-  unique(recipient_id, comment_id)
+CREATE TABLE user_mention (
+    id serial PRIMARY KEY,
+    recipient_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    read boolean DEFAULT FALSE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (recipient_id, comment_id)
 );
 
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.post_id,
     c.parent_id,
@@ -31,5 +31,9 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id
-from user_mention um, comment_view c
-where um.comment_id = c.id;
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
+
index 238c9e7966944e23062e886e7d0684bdecdd5d79..3763b5a070c22c7736cd0b823b607d71fb9aa422 100644 (file)
@@ -1,2 +1,6 @@
-alter table user_ drop column default_sort_type;
-alter table user_ drop column default_listing_type;
+ALTER TABLE user_
+    DROP COLUMN default_sort_type;
+
+ALTER TABLE user_
+    DROP COLUMN default_listing_type;
+
index 4bb9600948843091ccd2fe10eeff3d1b525df2d3..62d04141e8209a0255c91cdade279da0fb789ce8 100644 (file)
@@ -1,2 +1,6 @@
-alter table user_ add column default_sort_type smallint default 0 not null;
-alter table user_ add column default_listing_type smallint default 1 not null;
+ALTER TABLE user_
+    ADD COLUMN default_sort_type smallint DEFAULT 0 NOT NULL;
+
+ALTER TABLE user_
+    ADD COLUMN default_listing_type smallint DEFAULT 1 NOT NULL;
+
index 33500dfeb4117870037900b6e31b98bd3980f2e2..54d4ba823f10d947904d6d7f7a509aca47f79d72 100644 (file)
@@ -1 +1,2 @@
-drop table password_reset_request;
+DROP TABLE password_reset_request;
+
index 15cfaa59302c3093a939b7d0ca28e9e0f792e252..f883d1503f4a74a5763610a7817f5cbb8f39bd3d 100644 (file)
@@ -1,6 +1,7 @@
-create table password_reset_request (
-  id serial primary key,
-  user_id int references user_ on update cascade on delete cascade not null,
-  token_encrypted text not null,
-  published timestamp not null default now()
+CREATE TABLE password_reset_request (
+    id serial PRIMARY KEY,
+    user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    token_encrypted text NOT NULL,
+    published timestamp NOT NULL DEFAULT now()
 );
+
index c1335599958c49a84cb25a1a11a83a0c140df574..eb64acf8dae3f1546a71c025304f1bb918bebd37 100644 (file)
@@ -1 +1,3 @@
-alter table user_ drop column lang;
+ALTER TABLE user_
+    DROP COLUMN lang;
+
index 98a72ad90732508dfe96f6c5eb5748bed00e5edf..a1279da638f517d9eb8093ea76841d27d778468f 100644 (file)
@@ -1 +1,3 @@
-alter table user_ add column lang varchar(20) default 'browser' not null;
+ALTER TABLE user_
+    ADD COLUMN lang varchar(20) DEFAULT 'browser' NOT NULL;
+
index 72eedba45eb4c193e0b62c3ed8bddfc4b22d92d0..107d2256333c13918381be49c6f064e66b3d247c 100644 (file)
@@ -1,16 +1,46 @@
 -- Drop the columns
-drop view site_view;
-alter table site drop column enable_downvotes;
-alter table site drop column open_registration;
-alter table site drop column enable_nsfw;
+DROP VIEW site_view;
+
+ALTER TABLE site
+    DROP COLUMN enable_downvotes;
+
+ALTER TABLE site
+    DROP COLUMN open_registration;
+
+ALTER TABLE site
+    DROP COLUMN enable_nsfw;
 
 -- Rebuild the views
+CREATE VIEW site_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            s.creator_id = u.id) AS creator_name,
+    (
+        SELECT
+            count(*)
+        FROM
+            user_) AS number_of_users,
+    (
+        SELECT
+            count(*)
+        FROM
+            post) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment) AS number_of_comments,
+    (
+        SELECT
+            count(*)
+        FROM
+            community) AS number_of_communities
+FROM
+    site s;
 
-create view site_view as 
-select *,
-(select name from user_ u where s.creator_id = u.id) as creator_name,
-(select count(*) from user_) as number_of_users,
-(select count(*) from post) as number_of_posts,
-(select count(*) from comment) as number_of_comments,
-(select count(*) from community) as number_of_communities
-from site s;
index e107b1ac3cae37fcaedd64531d143795823c39c8..319aca2dde92eee4838444738b86d4b88746ba97 100644 (file)
@@ -1,16 +1,46 @@
 -- Add the column
-alter table site add column enable_downvotes boolean default true not null;
-alter table site add column open_registration boolean default true not null;
-alter table site add column enable_nsfw boolean default true not null;
+ALTER TABLE site
+    ADD COLUMN enable_downvotes boolean DEFAULT TRUE NOT NULL;
+
+ALTER TABLE site
+    ADD COLUMN open_registration boolean DEFAULT TRUE NOT NULL;
+
+ALTER TABLE site
+    ADD COLUMN enable_nsfw boolean DEFAULT TRUE NOT NULL;
 
 -- Reload the view
-drop view site_view;
+DROP VIEW site_view;
+
+CREATE VIEW site_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            s.creator_id = u.id) AS creator_name,
+    (
+        SELECT
+            count(*)
+        FROM
+            user_) AS number_of_users,
+    (
+        SELECT
+            count(*)
+        FROM
+            post) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment) AS number_of_comments,
+    (
+        SELECT
+            count(*)
+        FROM
+            community) AS number_of_communities
+FROM
+    site s;
 
-create view site_view as 
-select *,
-(select name from user_ u where s.creator_id = u.id) as creator_name,
-(select count(*) from user_) as number_of_users,
-(select count(*) from post) as number_of_posts,
-(select count(*) from comment) as number_of_comments,
-(select count(*) from community) as number_of_communities
-from site s;
index 74b4146d916e1e0f29a717e0779cbad25c8927fd..bd062b53b61ab990cf4a537e32e12a0f9661e42a 100644 (file)
 -- the views
-drop view user_mention_view;
-drop view reply_view;
-drop view comment_view;
-drop view user_view;
+DROP VIEW user_mention_view;
+
+DROP VIEW reply_view;
+
+DROP VIEW comment_view;
+
+DROP VIEW user_view;
 
 -- user
-create view user_view as 
-select id,
-name,
-fedi_name,
-admin,
-banned,
-published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
+CREATE VIEW user_view AS
+SELECT
+    id,
+    name,
+    fedi_name,
+    admin,
+    banned,
+    published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
 
 -- post
 -- Recreate the view
-drop view post_view;
-create view post_view as
-with all_post as
-(
-  select        
-  p.*,
-  (select u.banned from user_ u where p.creator_id = u.id) as banned,
-  (select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-  (select name from user_ where p.creator_id = user_.id) as creator_name,
-  (select name from community where p.community_id = community.id) as community_name,
-  (select removed from community c where p.community_id = c.id) as community_removed,
-  (select deleted from community c where p.community_id = c.id) as community_deleted,
-  (select nsfw from community c where p.community_id = c.id) as community_nsfw,
-  (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-  coalesce(sum(pl.score), 0) as score,
-  count (case when pl.score = 1 then 1 else null end) as upvotes,
-  count (case when pl.score = -1 then 1 else null end) as downvotes,
-  hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-  from post p
-  left join post_like pl on p.id = pl.post_id
-  group by p.id
-)
+DROP VIEW post_view;
 
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        p.*,
+        (
+            SELECT
+                u.banned
+            FROM
+                user_ u
+            WHERE
+                p.creator_id = u.id) AS banned,
+        (
+            SELECT
+                cb.id::bool
+            FROM
+                community_user_ban cb
+            WHERE
+                p.creator_id = cb.user_id
+                AND p.community_id = cb.community_id) AS banned_from_community,
+        (
+            SELECT
+                name
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                community
+            WHERE
+                p.community_id = community.id) AS community_name,
+        (
+            SELECT
+                removed
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_removed,
+        (
+            SELECT
+                deleted
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_deleted,
+        (
+            SELECT
+                nsfw
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_nsfw,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment
+            WHERE
+                comment.post_id = p.id) AS number_of_comments,
+        coalesce(sum(pl.score), 0) AS score,
+        count(
+            CASE WHEN pl.score = 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS upvotes,
+        count(
+            CASE WHEN pl.score = - 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS downvotes,
+        hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+    FROM
+        post p
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        p.id
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
 -- community
+DROP VIEW community_view;
 
-drop view community_view;
-create view community_view as 
-with all_community as
-(
-  select *,
-  (select name from user_ u where c.creator_id = u.id) as creator_name,
-  (select name from category ct where c.category_id = ct.id) as category_name,
-  (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-  (select count(*) from post p where p.community_id = c.id) as number_of_posts,
-  (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
-  hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
-  from community c
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        *,
+        (
+            SELECT
+                name
+            FROM
+                user_ u
+            WHERE
+                c.creator_id = u.id) AS creator_name,
+        (
+            SELECT
+                name
+            FROM
+                category ct
+            WHERE
+                c.category_id = ct.id) AS category_name,
+        (
+            SELECT
+                count(*)
+            FROM
+                community_follower cf
+            WHERE
+                cf.community_id = c.id) AS number_of_subscribers,
+        (
+            SELECT
+                count(*)
+            FROM
+                post p
+            WHERE
+                p.community_id = c.id) AS number_of_posts,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment co,
+                post p
+            WHERE
+                c.id = p.community_id
+                AND p.id = co.post_id) AS number_of_comments,
+        hot_rank ((
+            SELECT
+                count(*)
+            FROM community_follower cf
+            WHERE
+                cf.community_id = c.id), c.published) AS hot_rank
+FROM
+    community c
 )
-
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
-
-union all
-
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
 -- Reply and comment view
-create view comment_view as
-with all_comment as
-(
-  select        
-  c.*,
-  (select community_id from post p where p.id = c.post_id),
-  (select u.banned from user_ u where c.creator_id = u.id) as banned,
-  (select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
-  (select name from user_ where c.creator_id = user_.id) as creator_name,
-  coalesce(sum(cl.score), 0) as score,
-  count (case when cl.score = 1 then 1 else null end) as upvotes,
-  count (case when cl.score = -1 then 1 else null end) as downvotes
-  from comment c
-  left join comment_like cl on c.id = cl.comment_id
-  group by c.id
+CREATE VIEW comment_view AS
+with all_comment AS (
+    SELECT
+        c.*,
+        (
+            SELECT
+                community_id
+            FROM
+                post p
+            WHERE
+                p.id = c.post_id),
+            (
+                SELECT
+                    u.banned
+                FROM
+                    user_ u
+                WHERE
+                    c.creator_id = u.id) AS banned,
+                (
+                    SELECT
+                        cb.id::bool
+                    FROM
+                        community_user_ban cb,
+                        post p
+                    WHERE
+                        c.creator_id = cb.user_id
+                        AND p.id = c.post_id
+                        AND p.community_id = cb.community_id) AS banned_from_community,
+                    (
+                        SELECT
+                            name
+                        FROM
+                            user_
+                        WHERE
+                            c.creator_id = user_.id) AS creator_name,
+                        coalesce(sum(cl.score), 0) AS score,
+                    count(
+                        CASE WHEN cl.score = 1 THEN
+                            1
+                        ELSE
+                            NULL
+                        END) AS upvotes,
+                    count(
+                        CASE WHEN cl.score = - 1 THEN
+                            1
+                        ELSE
+                            NULL
+                        END) AS downvotes
+                FROM
+                    comment c
+                LEFT JOIN comment_like cl ON c.id = cl.comment_id
+            GROUP BY
+                c.id
 )
+    SELECT
+        ac.*,
+        u.id AS user_id,
+        coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS saved
+    FROM
+        all_comment ac;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-
-union all
-
-select 
-    ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as saved
-from all_comment ac
-;
-
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- user mention
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.post_id,
     c.parent_id,
@@ -184,41 +395,117 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id
-from user_mention um, comment_view c
-where um.comment_id = c.id;
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
 
 -- community tables
-drop view community_moderator_view;
-drop view community_follower_view;
-drop view community_user_ban_view;
-drop view site_view;
-
-create view community_moderator_view as 
-select *,
-(select name from user_ u where cm.user_id = u.id) as user_name,
-(select name from community c where cm.community_id = c.id) as community_name
-from community_moderator cm;
-
-create view community_follower_view as 
-select *,
-(select name from user_ u where cf.user_id = u.id) as user_name,
-(select name from community c where cf.community_id = c.id) as community_name
-from community_follower cf;
-
-create view community_user_ban_view as 
-select *,
-(select name from user_ u where cm.user_id = u.id) as user_name,
-(select name from community c where cm.community_id = c.id) as community_name
-from community_user_ban cm;
-
-create view site_view as 
-select *,
-(select name from user_ u where s.creator_id = u.id) as creator_name,
-(select count(*) from user_) as number_of_users,
-(select count(*) from post) as number_of_posts,
-(select count(*) from comment) as number_of_comments,
-(select count(*) from community) as number_of_communities
-from site s;
-
-alter table user_ rename column avatar to icon;
-alter table user_ alter column icon type bytea using icon::bytea;
+DROP VIEW community_moderator_view;
+
+DROP VIEW community_follower_view;
+
+DROP VIEW community_user_ban_view;
+
+DROP VIEW site_view;
+
+CREATE VIEW community_moderator_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_name,
+    (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_name
+FROM
+    community_moderator cm;
+
+CREATE VIEW community_follower_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cf.user_id = u.id) AS user_name,
+    (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cf.community_id = c.id) AS community_name
+FROM
+    community_follower cf;
+
+CREATE VIEW community_user_ban_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_name,
+    (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_name
+FROM
+    community_user_ban cm;
+
+CREATE VIEW site_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            s.creator_id = u.id) AS creator_name,
+    (
+        SELECT
+            count(*)
+        FROM
+            user_) AS number_of_users,
+    (
+        SELECT
+            count(*)
+        FROM
+            post) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment) AS number_of_comments,
+    (
+        SELECT
+            count(*)
+        FROM
+            community) AS number_of_communities
+FROM
+    site s;
+
+ALTER TABLE user_ RENAME COLUMN avatar TO icon;
+
+ALTER TABLE user_
+    ALTER COLUMN icon TYPE bytea
+    USING icon::bytea;
+
index f9265154b27bfe9b8b3e1503eb1c0815a5034f6d..79bf2cc557ff601341c505a23e511a127682e719 100644 (file)
 -- Rename to avatar
-alter table user_ rename column icon to avatar;
-alter table user_ alter column avatar type text;
+ALTER TABLE user_ RENAME COLUMN icon TO avatar;
 
--- Rebuild nearly all the views, to include the creator avatars
+ALTER TABLE user_
+    ALTER COLUMN avatar TYPE text;
 
+-- Rebuild nearly all the views, to include the creator avatars
 -- user
-drop view user_view;
-create view user_view as 
-select id,
-name,
-avatar,
-fedi_name,
-admin,
-banned,
-published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
+DROP VIEW user_view;
+
+CREATE VIEW user_view AS
+SELECT
+    id,
+    name,
+    avatar,
+    fedi_name,
+    admin,
+    banned,
+    published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
 
 -- post
 -- Recreate the view
-drop view post_view;
-create view post_view as
-with all_post as
-(
-  select        
-  p.*,
-  (select u.banned from user_ u where p.creator_id = u.id) as banned,
-  (select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-  (select name from user_ where p.creator_id = user_.id) as creator_name,
-  (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
-  (select name from community where p.community_id = community.id) as community_name,
-  (select removed from community c where p.community_id = c.id) as community_removed,
-  (select deleted from community c where p.community_id = c.id) as community_deleted,
-  (select nsfw from community c where p.community_id = c.id) as community_nsfw,
-  (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-  coalesce(sum(pl.score), 0) as score,
-  count (case when pl.score = 1 then 1 else null end) as upvotes,
-  count (case when pl.score = -1 then 1 else null end) as downvotes,
-  hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-  from post p
-  left join post_like pl on p.id = pl.post_id
-  group by p.id
-)
-
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+DROP VIEW post_view;
 
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        p.*,
+        (
+            SELECT
+                u.banned
+            FROM
+                user_ u
+            WHERE
+                p.creator_id = u.id) AS banned,
+        (
+            SELECT
+                cb.id::bool
+            FROM
+                community_user_ban cb
+            WHERE
+                p.creator_id = cb.user_id
+                AND p.community_id = cb.community_id) AS banned_from_community,
+        (
+            SELECT
+                name
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_name,
+        (
+            SELECT
+                avatar
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_avatar,
+        (
+            SELECT
+                name
+            FROM
+                community
+            WHERE
+                p.community_id = community.id) AS community_name,
+        (
+            SELECT
+                removed
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_removed,
+        (
+            SELECT
+                deleted
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_deleted,
+        (
+            SELECT
+                nsfw
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_nsfw,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment
+            WHERE
+                comment.post_id = p.id) AS number_of_comments,
+        coalesce(sum(pl.score), 0) AS score,
+        count(
+            CASE WHEN pl.score = 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS upvotes,
+        count(
+            CASE WHEN pl.score = - 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS downvotes,
+        hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+    FROM
+        post p
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        p.id
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
 -- community
-drop view community_view;
-create view community_view as 
-with all_community as
-(
-  select *,
-  (select name from user_ u where c.creator_id = u.id) as creator_name,
-  (select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
-  (select name from category ct where c.category_id = ct.id) as category_name,
-  (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-  (select count(*) from post p where p.community_id = c.id) as number_of_posts,
-  (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
-  hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
-  from community c
+DROP VIEW community_view;
+
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        *,
+        (
+            SELECT
+                name
+            FROM
+                user_ u
+            WHERE
+                c.creator_id = u.id) AS creator_name,
+        (
+            SELECT
+                avatar
+            FROM
+                user_ u
+            WHERE
+                c.creator_id = u.id) AS creator_avatar,
+        (
+            SELECT
+                name
+            FROM
+                category ct
+            WHERE
+                c.category_id = ct.id) AS category_name,
+        (
+            SELECT
+                count(*)
+            FROM
+                community_follower cf
+            WHERE
+                cf.community_id = c.id) AS number_of_subscribers,
+        (
+            SELECT
+                count(*)
+            FROM
+                post p
+            WHERE
+                p.community_id = c.id) AS number_of_posts,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment co,
+                post p
+            WHERE
+                c.id = p.community_id
+                AND p.id = co.post_id) AS number_of_comments,
+        hot_rank ((
+            SELECT
+                count(*)
+            FROM community_follower cf
+            WHERE
+                cf.community_id = c.id), c.published) AS hot_rank
+FROM
+    community c
 )
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
+-- reply and comment view
+DROP VIEW reply_view;
 
-union all
+DROP VIEW user_mention_view;
 
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
+DROP VIEW comment_view;
 
--- reply and comment view
-drop view reply_view;
-drop view user_mention_view;
-drop view comment_view;
-create view comment_view as
-with all_comment as
-(
-  select        
-  c.*,
-  (select community_id from post p where p.id = c.post_id),
-  (select u.banned from user_ u where c.creator_id = u.id) as banned,
-  (select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
-  (select name from user_ where c.creator_id = user_.id) as creator_name,
-  (select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
-  coalesce(sum(cl.score), 0) as score,
-  count (case when cl.score = 1 then 1 else null end) as upvotes,
-  count (case when cl.score = -1 then 1 else null end) as downvotes
-  from comment c
-  left join comment_like cl on c.id = cl.comment_id
-  group by c.id
+CREATE VIEW comment_view AS
+with all_comment AS (
+    SELECT
+        c.*,
+        (
+            SELECT
+                community_id
+            FROM
+                post p
+            WHERE
+                p.id = c.post_id),
+            (
+                SELECT
+                    u.banned
+                FROM
+                    user_ u
+                WHERE
+                    c.creator_id = u.id) AS banned,
+                (
+                    SELECT
+                        cb.id::bool
+                    FROM
+                        community_user_ban cb,
+                        post p
+                    WHERE
+                        c.creator_id = cb.user_id
+                        AND p.id = c.post_id
+                        AND p.community_id = cb.community_id) AS banned_from_community,
+                    (
+                        SELECT
+                            name
+                        FROM
+                            user_
+                        WHERE
+                            c.creator_id = user_.id) AS creator_name,
+                        (
+                            SELECT
+                                avatar
+                            FROM
+                                user_
+                            WHERE
+                                c.creator_id = user_.id) AS creator_avatar,
+                            coalesce(sum(cl.score), 0) AS score,
+                        count(
+                            CASE WHEN cl.score = 1 THEN
+                                1
+                            ELSE
+                                NULL
+                            END) AS upvotes,
+                        count(
+                            CASE WHEN cl.score = - 1 THEN
+                                1
+                            ELSE
+                                NULL
+                            END) AS downvotes
+                    FROM
+                        comment c
+                    LEFT JOIN comment_like cl ON c.id = cl.comment_id
+                GROUP BY
+                    c.id
 )
+        SELECT
+            ac.*,
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS saved
+    FROM
+        all_comment ac;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-
-union all
-
-select 
-    ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as saved
-from all_comment ac
-;
-
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- user mention
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.post_id,
     c.parent_id,
@@ -193,42 +424,136 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id
-from user_mention um, comment_view c
-where um.comment_id = c.id;
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
 
 -- community views
-drop view community_moderator_view;
-drop view community_follower_view;
-drop view community_user_ban_view;
-drop view site_view;
-
-create view community_moderator_view as 
-select *,
-(select name from user_ u where cm.user_id = u.id) as user_name,
-(select avatar from user_ u where cm.user_id = u.id),
-(select name from community c where cm.community_id = c.id) as community_name
-from community_moderator cm;
-
-create view community_follower_view as 
-select *,
-(select name from user_ u where cf.user_id = u.id) as user_name,
-(select avatar from user_ u where cf.user_id = u.id),
-(select name from community c where cf.community_id = c.id) as community_name
-from community_follower cf;
-
-create view community_user_ban_view as 
-select *,
-(select name from user_ u where cm.user_id = u.id) as user_name,
-(select avatar from user_ u where cm.user_id = u.id),
-(select name from community c where cm.community_id = c.id) as community_name
-from community_user_ban cm;
-
-create view site_view as 
-select *,
-(select name from user_ u where s.creator_id = u.id) as creator_name,
-(select avatar from user_ u where s.creator_id = u.id) as creator_avatar,
-(select count(*) from user_) as number_of_users,
-(select count(*) from post) as number_of_posts,
-(select count(*) from comment) as number_of_comments,
-(select count(*) from community) as number_of_communities
-from site s;
+DROP VIEW community_moderator_view;
+
+DROP VIEW community_follower_view;
+
+DROP VIEW community_user_ban_view;
+
+DROP VIEW site_view;
+
+CREATE VIEW community_moderator_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id), (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_name
+FROM
+    community_moderator cm;
+
+CREATE VIEW community_follower_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cf.user_id = u.id) AS user_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            cf.user_id = u.id), (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cf.community_id = c.id) AS community_name
+FROM
+    community_follower cf;
+
+CREATE VIEW community_user_ban_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id), (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_name
+FROM
+    community_user_ban cm;
+
+CREATE VIEW site_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            s.creator_id = u.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            s.creator_id = u.id) AS creator_avatar,
+    (
+        SELECT
+            count(*)
+        FROM
+            user_) AS number_of_users,
+    (
+        SELECT
+            count(*)
+        FROM
+            post) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment) AS number_of_comments,
+    (
+        SELECT
+            count(*)
+        FROM
+            community) AS number_of_communities
+FROM
+    site s;
+
index 92f771f843dfb2ac311fccaeb2d833d87eeffc78..759623c7231eab420348cfe7968a9c9a14dc3bf8 100644 (file)
@@ -1,15 +1,47 @@
 -- user
-drop view user_view;
-create view user_view as 
-select id,
-name,
-avatar,
-fedi_name,
-admin,
-banned,
-published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
+DROP VIEW user_view;
+
+CREATE VIEW user_view AS
+SELECT
+    id,
+    name,
+    avatar,
+    fedi_name,
+    admin,
+    banned,
+    published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
+
index 59972dfb89fb0adf83075112bfa97125c48f5a2a..d1a319e7a5c2575f8e19060f96b64bda2d0fbf53 100644 (file)
@@ -1,16 +1,48 @@
 -- user
-drop view user_view;
-create view user_view as 
-select id,
-name,
-avatar,
-email,
-fedi_name,
-admin,
-banned,
-published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
+DROP VIEW user_view;
+
+CREATE VIEW user_view AS
+SELECT
+    id,
+    name,
+    avatar,
+    email,
+    fedi_name,
+    admin,
+    banned,
+    published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
+
index ec061223d6a189146a25bfc878674194a3820e30..517e2b30bbcbaf3f97caf85b39e4ab41eac0ecba 100644 (file)
@@ -1,20 +1,55 @@
 -- Drop the columns
-drop view user_view;
-alter table user_ drop column show_avatars;
-alter table user_ drop column send_notifications_to_email;
+DROP VIEW user_view;
+
+ALTER TABLE user_
+    DROP COLUMN show_avatars;
+
+ALTER TABLE user_
+    DROP COLUMN send_notifications_to_email;
 
 -- Rebuild the view
-create view user_view as 
-select id,
-name,
-avatar,
-email,
-fedi_name,
-admin,
-banned,
-published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
+CREATE VIEW user_view AS
+SELECT
+    id,
+    name,
+    avatar,
+    email,
+    fedi_name,
+    admin,
+    banned,
+    published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
+
index 21f0aff49585ee983328e76fc576b023013d4b29..661907d263a7da8066d897472ef6284d3b7aeb5b 100644 (file)
@@ -1,22 +1,57 @@
 -- Add columns
-alter table user_ add column show_avatars boolean default true not null;
-alter table user_ add column send_notifications_to_email boolean default false not null;
+ALTER TABLE user_
+    ADD COLUMN show_avatars boolean DEFAULT TRUE NOT NULL;
+
+ALTER TABLE user_
+    ADD COLUMN send_notifications_to_email boolean DEFAULT FALSE NOT NULL;
 
 -- Rebuild the user_view
-drop view user_view;
-create view user_view as 
-select id,
-name,
-avatar,
-email,
-fedi_name,
-admin,
-banned,
-show_avatars,
-send_notifications_to_email,
-published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
+DROP VIEW user_view;
+
+CREATE VIEW user_view AS
+SELECT
+    id,
+    name,
+    avatar,
+    email,
+    fedi_name,
+    admin,
+    banned,
+    show_avatars,
+    send_notifications_to_email,
+    published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
+
index ad674c50b88e5de0f07c1155c09cf2bbc82a5840..b6e9b25b550932e567e982132ce8bd6a52cea39f 100644 (file)
@@ -1,16 +1,24 @@
-drop index idx_post_creator;
-drop index idx_post_community;
+DROP INDEX idx_post_creator;
 
-drop index idx_post_like_post;
-drop index idx_post_like_user;
+DROP INDEX idx_post_community;
 
-drop index idx_comment_creator;
-drop index idx_comment_parent;
-drop index idx_comment_post;
+DROP INDEX idx_post_like_post;
 
-drop index idx_comment_like_comment;
-drop index idx_comment_like_user;
-drop index idx_comment_like_post;
+DROP INDEX idx_post_like_user;
+
+DROP INDEX idx_comment_creator;
+
+DROP INDEX idx_comment_parent;
+
+DROP INDEX idx_comment_post;
+
+DROP INDEX idx_comment_like_comment;
+
+DROP INDEX idx_comment_like_user;
+
+DROP INDEX idx_comment_like_post;
+
+DROP INDEX idx_community_creator;
+
+DROP INDEX idx_community_category;
 
-drop index idx_community_creator;
-drop index idx_community_category;
index 911f0875bfa6f51d53ca93f4c9e04428df7666db..896e2adddcdb79a02e54531c89d00143572879cf 100644 (file)
@@ -1,17 +1,25 @@
 -- Go through all the tables joins, optimize every view, CTE, etc.
-create index idx_post_creator on post (creator_id);
-create index idx_post_community on post (community_id);
+CREATE INDEX idx_post_creator ON post (creator_id);
 
-create index idx_post_like_post on post_like (post_id);
-create index idx_post_like_user on post_like (user_id);
+CREATE INDEX idx_post_community ON post (community_id);
 
-create index idx_comment_creator on comment (creator_id);
-create index idx_comment_parent on comment (parent_id);
-create index idx_comment_post on comment (post_id);
+CREATE INDEX idx_post_like_post ON post_like (post_id);
 
-create index idx_comment_like_comment on comment_like (comment_id);
-create index idx_comment_like_user on comment_like (user_id);
-create index idx_comment_like_post on comment_like (post_id);
+CREATE INDEX idx_post_like_user ON post_like (user_id);
+
+CREATE INDEX idx_comment_creator ON comment (creator_id);
+
+CREATE INDEX idx_comment_parent ON comment (parent_id);
+
+CREATE INDEX idx_comment_post ON comment (post_id);
+
+CREATE INDEX idx_comment_like_comment ON comment_like (comment_id);
+
+CREATE INDEX idx_comment_like_user ON comment_like (user_id);
+
+CREATE INDEX idx_comment_like_post ON comment_like (post_id);
+
+CREATE INDEX idx_community_creator ON community (creator_id);
+
+CREATE INDEX idx_community_category ON community (category_id);
 
-create index idx_community_creator on community (creator_id);
-create index idx_community_category on community (category_id);
index 39985ab55f7cff5b15e95e6912f6b71e6ccab55e..778e483cbf9b01b01e2dfcdb8c053c5f660c58aa 100644 (file)
 -- functions and triggers
-drop trigger refresh_user on user_;
-drop function refresh_user();
-drop trigger refresh_post on post;
-drop function refresh_post();
-drop trigger refresh_post_like on post_like;
-drop function refresh_post_like();
-drop trigger refresh_community on community;
-drop function refresh_community();
-drop trigger refresh_community_follower on community_follower;
-drop function refresh_community_follower();
-drop trigger refresh_community_user_ban on community_user_ban;
-drop function refresh_community_user_ban();
-drop trigger refresh_comment on comment;
-drop function refresh_comment();
-drop trigger refresh_comment_like on comment_like;
-drop function refresh_comment_like();
+DROP TRIGGER refresh_user ON user_;
+
+DROP FUNCTION refresh_user ();
+
+DROP TRIGGER refresh_post ON post;
+
+DROP FUNCTION refresh_post ();
+
+DROP TRIGGER refresh_post_like ON post_like;
+
+DROP FUNCTION refresh_post_like ();
+
+DROP TRIGGER refresh_community ON community;
+
+DROP FUNCTION refresh_community ();
+
+DROP TRIGGER refresh_community_follower ON community_follower;
+
+DROP FUNCTION refresh_community_follower ();
+
+DROP TRIGGER refresh_community_user_ban ON community_user_ban;
+
+DROP FUNCTION refresh_community_user_ban ();
+
+DROP TRIGGER refresh_comment ON comment;
+
+DROP FUNCTION refresh_comment ();
+
+DROP TRIGGER refresh_comment_like ON comment_like;
+
+DROP FUNCTION refresh_comment_like ();
 
 -- post
 -- Recreate the view
-drop view post_view;
-create view post_view as
-with all_post as
-(
-  select        
-  p.*,
-  (select u.banned from user_ u where p.creator_id = u.id) as banned,
-  (select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-  (select name from user_ where p.creator_id = user_.id) as creator_name,
-  (select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
-  (select name from community where p.community_id = community.id) as community_name,
-  (select removed from community c where p.community_id = c.id) as community_removed,
-  (select deleted from community c where p.community_id = c.id) as community_deleted,
-  (select nsfw from community c where p.community_id = c.id) as community_nsfw,
-  (select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-  coalesce(sum(pl.score), 0) as score,
-  count (case when pl.score = 1 then 1 else null end) as upvotes,
-  count (case when pl.score = -1 then 1 else null end) as downvotes,
-  hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-  from post p
-  left join post_like pl on p.id = pl.post_id
-  group by p.id
+DROP VIEW post_view;
+
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        p.*,
+        (
+            SELECT
+                u.banned
+            FROM
+                user_ u
+            WHERE
+                p.creator_id = u.id) AS banned,
+        (
+            SELECT
+                cb.id::bool
+            FROM
+                community_user_ban cb
+            WHERE
+                p.creator_id = cb.user_id
+                AND p.community_id = cb.community_id) AS banned_from_community,
+        (
+            SELECT
+                name
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_name,
+        (
+            SELECT
+                avatar
+            FROM
+                user_
+            WHERE
+                p.creator_id = user_.id) AS creator_avatar,
+        (
+            SELECT
+                name
+            FROM
+                community
+            WHERE
+                p.community_id = community.id) AS community_name,
+        (
+            SELECT
+                removed
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_removed,
+        (
+            SELECT
+                deleted
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_deleted,
+        (
+            SELECT
+                nsfw
+            FROM
+                community c
+            WHERE
+                p.community_id = c.id) AS community_nsfw,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment
+            WHERE
+                comment.post_id = p.id) AS number_of_comments,
+        coalesce(sum(pl.score), 0) AS score,
+        count(
+            CASE WHEN pl.score = 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS upvotes,
+        count(
+            CASE WHEN pl.score = - 1 THEN
+                1
+            ELSE
+                NULL
+            END) AS downvotes,
+        hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+    FROM
+        post p
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        p.id
 )
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
+
+DROP VIEW post_mview;
 
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
-
-drop view post_mview;
-drop materialized view post_aggregates_mview;
-drop view post_aggregates_view;
+DROP MATERIALIZED VIEW post_aggregates_mview;
+
+DROP VIEW post_aggregates_view;
 
 -- user
-drop materialized view user_mview;
-drop view user_view;
-create view user_view as 
-select id,
-name,
-avatar,
-email,
-fedi_name,
-admin,
-banned,
-show_avatars,
-send_notifications_to_email,
-published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
+DROP MATERIALIZED VIEW user_mview;
+
+DROP VIEW user_view;
+
+CREATE VIEW user_view AS
+SELECT
+    id,
+    name,
+    avatar,
+    email,
+    fedi_name,
+    admin,
+    banned,
+    show_avatars,
+    send_notifications_to_email,
+    published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
 
 -- community
-drop view community_mview;
-drop materialized view community_aggregates_mview;
-drop view community_view;
-drop view community_aggregates_view;
-create view community_view as 
-with all_community as
-(
-  select *,
-  (select name from user_ u where c.creator_id = u.id) as creator_name,
-  (select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
-  (select name from category ct where c.category_id = ct.id) as category_name,
-  (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-  (select count(*) from post p where p.community_id = c.id) as number_of_posts,
-  (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
-  hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
-  from community c
-)
+DROP VIEW community_mview;
 
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
+DROP MATERIALIZED VIEW community_aggregates_mview;
 
-union all
+DROP VIEW community_view;
 
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
+DROP VIEW community_aggregates_view;
 
--- reply and comment view
-drop view reply_view;
-drop view user_mention_view;
-drop view comment_view;
-drop view comment_mview;
-drop materialized view comment_aggregates_mview;
-drop view comment_aggregates_view;
-create view comment_view as
-with all_comment as
-(
-  select        
-  c.*,
-  (select community_id from post p where p.id = c.post_id),
-  (select u.banned from user_ u where c.creator_id = u.id) as banned,
-  (select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
-  (select name from user_ where c.creator_id = user_.id) as creator_name,
-  (select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
-  coalesce(sum(cl.score), 0) as score,
-  count (case when cl.score = 1 then 1 else null end) as upvotes,
-  count (case when cl.score = -1 then 1 else null end) as downvotes
-  from comment c
-  left join comment_like cl on c.id = cl.comment_id
-  group by c.id
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        *,
+        (
+            SELECT
+                name
+            FROM
+                user_ u
+            WHERE
+                c.creator_id = u.id) AS creator_name,
+        (
+            SELECT
+                avatar
+            FROM
+                user_ u
+            WHERE
+                c.creator_id = u.id) AS creator_avatar,
+        (
+            SELECT
+                name
+            FROM
+                category ct
+            WHERE
+                c.category_id = ct.id) AS category_name,
+        (
+            SELECT
+                count(*)
+            FROM
+                community_follower cf
+            WHERE
+                cf.community_id = c.id) AS number_of_subscribers,
+        (
+            SELECT
+                count(*)
+            FROM
+                post p
+            WHERE
+                p.community_id = c.id) AS number_of_posts,
+        (
+            SELECT
+                count(*)
+            FROM
+                comment co,
+                post p
+            WHERE
+                c.id = p.community_id
+                AND p.id = co.post_id) AS number_of_comments,
+        hot_rank ((
+            SELECT
+                count(*)
+            FROM community_follower cf
+            WHERE
+                cf.community_id = c.id), c.published) AS hot_rank
+FROM
+    community c
 )
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
+
+-- reply and comment view
+DROP VIEW reply_view;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
+DROP VIEW user_mention_view;
 
-union all
+DROP VIEW comment_view;
 
-select 
-    ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as saved
-from all_comment ac
-;
-
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+DROP VIEW comment_mview;
+
+DROP MATERIALIZED VIEW comment_aggregates_mview;
+
+DROP VIEW comment_aggregates_view;
+
+CREATE VIEW comment_view AS
+with all_comment AS (
+    SELECT
+        c.*,
+        (
+            SELECT
+                community_id
+            FROM
+                post p
+            WHERE
+                p.id = c.post_id),
+            (
+                SELECT
+                    u.banned
+                FROM
+                    user_ u
+                WHERE
+                    c.creator_id = u.id) AS banned,
+                (
+                    SELECT
+                        cb.id::bool
+                    FROM
+                        community_user_ban cb,
+                        post p
+                    WHERE
+                        c.creator_id = cb.user_id
+                        AND p.id = c.post_id
+                        AND p.community_id = cb.community_id) AS banned_from_community,
+                    (
+                        SELECT
+                            name
+                        FROM
+                            user_
+                        WHERE
+                            c.creator_id = user_.id) AS creator_name,
+                        (
+                            SELECT
+                                avatar
+                            FROM
+                                user_
+                            WHERE
+                                c.creator_id = user_.id) AS creator_avatar,
+                            coalesce(sum(cl.score), 0) AS score,
+                        count(
+                            CASE WHEN cl.score = 1 THEN
+                                1
+                            ELSE
+                                NULL
+                            END) AS upvotes,
+                        count(
+                            CASE WHEN cl.score = - 1 THEN
+                                1
+                            ELSE
+                                NULL
+                            END) AS downvotes
+                    FROM
+                        comment c
+                    LEFT JOIN comment_like cl ON c.id = cl.comment_id
+                GROUP BY
+                    c.id
+)
+        SELECT
+            ac.*,
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS saved
+    FROM
+        all_comment ac;
+
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- user mention
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.post_id,
     c.parent_id,
@@ -218,6 +473,9 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id
-from user_mention um, comment_view c
-where um.comment_id = c.id;
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
 
index e0f206d3497049d3aecd2b16bbed34ec0d81ed0c..47fc5e0f81f9cff07e1c64259ab7eba0de9bf9d5 100644 (file)
 -- post
-create view post_aggregates_view as
-select        
-p.*,
-(select u.banned from user_ u where p.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-(select name from user_ where p.creator_id = user_.id) as creator_name,
-(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
-(select name from community where p.community_id = community.id) as community_name,
-(select removed from community c where p.community_id = c.id) as community_removed,
-(select deleted from community c where p.community_id = c.id) as community_deleted,
-(select nsfw from community c where p.community_id = c.id) as community_nsfw,
-(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-coalesce(sum(pl.score), 0) as score,
-count (case when pl.score = 1 then 1 else null end) as upvotes,
-count (case when pl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-from post p
-left join post_like pl on p.id = pl.post_id
-group by p.id;
-
-create materialized view post_aggregates_mview as select * from post_aggregates_view;
-
-create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
-
-drop view post_view;
-create view post_view as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_view pa
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            p.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb
+        WHERE
+            p.creator_id = cb.user_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_avatar,
+    (
+        SELECT
+            name
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_name,
+    (
+        SELECT
+            removed
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_removed,
+    (
+        SELECT
+            deleted
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_deleted,
+    (
+        SELECT
+            nsfw
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_nsfw,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment
+        WHERE
+            comment.post_id = p.id) AS number_of_comments,
+    coalesce(sum(pl.score), 0) AS score,
+    count(
+        CASE WHEN pl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN pl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+FROM
+    post p
+    LEFT JOIN post_like pl ON p.id = pl.post_id
+GROUP BY
+    p.id;
+
+CREATE MATERIALIZED VIEW post_aggregates_mview AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
+
+CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
+
+DROP VIEW post_view;
+
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_view pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
-
-create view post_mview as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_mview pa
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
+
+CREATE VIEW post_mview AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_mview pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
-
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
 -- user_view
-drop view user_view;
-create view user_view as 
-select 
-u.id,
-u.name,
-u.avatar,
-u.email,
-u.fedi_name,
-u.admin,
-u.banned,
-u.show_avatars,
-u.send_notifications_to_email,
-u.published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
-
-create materialized view user_mview as select * from user_view;
-
-create unique index idx_user_mview_id on user_mview (id);
+DROP VIEW user_view;
+
+CREATE VIEW user_view AS
+SELECT
+    u.id,
+    u.name,
+    u.avatar,
+    u.email,
+    u.fedi_name,
+    u.admin,
+    u.banned,
+    u.show_avatars,
+    u.send_notifications_to_email,
+    u.published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
+
+CREATE MATERIALIZED VIEW user_mview AS
+SELECT
+    *
+FROM
+    user_view;
+
+CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
 
 -- community
-create view community_aggregates_view as
-select c.*,
-(select name from user_ u where c.creator_id = u.id) as creator_name,
-(select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
-(select name from category ct where c.category_id = ct.id) as category_name,
-(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-(select count(*) from post p where p.community_id = c.id) as number_of_posts,
-(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
-hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
-from community c;
-
-create materialized view community_aggregates_mview as select * from community_aggregates_view;
-
-create unique index idx_community_aggregates_mview_id on community_aggregates_mview (id);
-
-drop view community_view;
-create view community_view as
-with all_community as
-(
-  select
-  ca.*
-  from community_aggregates_view ca
+CREATE VIEW community_aggregates_view AS
+SELECT
+    c.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS creator_avatar,
+    (
+        SELECT
+            name
+        FROM
+            category ct
+        WHERE
+            c.category_id = ct.id) AS category_name,
+    (
+        SELECT
+            count(*)
+        FROM
+            community_follower cf
+        WHERE
+            cf.community_id = c.id) AS number_of_subscribers,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.community_id = c.id) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment co,
+            post p
+        WHERE
+            c.id = p.community_id
+            AND p.id = co.post_id) AS number_of_comments,
+    hot_rank ((
+        SELECT
+            count(*)
+        FROM community_follower cf
+        WHERE
+            cf.community_id = c.id), c.published) AS hot_rank
+FROM
+    community c;
+
+CREATE MATERIALIZED VIEW community_aggregates_mview AS
+SELECT
+    *
+FROM
+    community_aggregates_view;
+
+CREATE UNIQUE INDEX idx_community_aggregates_mview_id ON community_aggregates_mview (id);
+
+DROP VIEW community_view;
+
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        ca.*
+    FROM
+        community_aggregates_view ca
 )
-
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
-
-union all
-
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
-
-create view community_mview as
-with all_community as
-(
-  select
-  ca.*
-  from community_aggregates_mview ca
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
+
+CREATE VIEW community_mview AS
+with all_community AS (
+    SELECT
+        ca.*
+    FROM
+        community_aggregates_mview ca
 )
-
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
-
-union all
-
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
-
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
 -- reply and comment view
-create view comment_aggregates_view as
-select        
-c.*,
-(select community_id from post p where p.id = c.post_id),
-(select u.banned from user_ u where c.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
-(select name from user_ where c.creator_id = user_.id) as creator_name,
-(select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
-coalesce(sum(cl.score), 0) as score,
-count (case when cl.score = 1 then 1 else null end) as upvotes,
-count (case when cl.score = -1 then 1 else null end) as downvotes
-from comment c
-left join comment_like cl on c.id = cl.comment_id
-group by c.id;
-
-create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
-
-create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
-
-drop view reply_view;
-drop view user_mention_view;
-drop view comment_view;
-
-create view comment_view as
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_view ca
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    c.*,
+    (
+        SELECT
+            community_id
+        FROM
+            post p
+        WHERE
+            p.id = c.post_id), (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb,
+            post p
+        WHERE
+            c.creator_id = cb.user_id
+            AND p.id = c.post_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_avatar,
+    coalesce(sum(cl.score), 0) AS score,
+    count(
+        CASE WHEN cl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN cl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes
+FROM
+    comment c
+    LEFT JOIN comment_like cl ON c.id = cl.comment_id
+GROUP BY
+    c.id;
+
+CREATE MATERIALIZED VIEW comment_aggregates_mview AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
+
+CREATE UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id);
+
+DROP VIEW reply_view;
+
+DROP VIEW user_mention_view;
+
+DROP VIEW comment_view;
+
+CREATE VIEW comment_view AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_view ca
 )
-
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-
-union all
-
-select 
+SELECT
     ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as saved
-from all_comment ac
-;
-
-create view comment_mview as
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_mview ca
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS saved
+    FROM
+        all_comment ac;
+
+CREATE VIEW comment_mview AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_mview ca
 )
-
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-
-union all
-
-select 
+SELECT
     ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as saved
-from all_comment ac
-;
-
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS saved
+    FROM
+        all_comment ac;
+
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- user mention
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.post_id,
     c.parent_id,
@@ -301,137 +581,150 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id
-from user_mention um, comment_view c
-where um.comment_id = c.id;
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
 
 -- user
-create or replace function refresh_user()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently user_mview;
-  refresh materialized view concurrently comment_aggregates_mview; -- cause of bans
-  refresh materialized view concurrently post_aggregates_mview;
-  return null;
-end $$;
-
-create trigger refresh_user
-after insert or update or delete or truncate
-on user_
-for each statement
-execute procedure refresh_user();
+CREATE OR REPLACE FUNCTION refresh_user ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY user_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY comment_aggregates_mview;
+    -- cause of bans
+    REFRESH MATERIALIZED VIEW CONCURRENTLY post_aggregates_mview;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER refresh_user
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON user_
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_user ();
 
 -- post
-create or replace function refresh_post()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently post_aggregates_mview;
-  refresh materialized view concurrently user_mview;
-  return null;
-end $$;
-
-create trigger refresh_post
-after insert or update or delete or truncate
-on post
-for each statement
-execute procedure refresh_post();
+CREATE OR REPLACE FUNCTION refresh_post ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY post_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY user_mview;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER refresh_post
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON post
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_post ();
 
 -- post_like
-create or replace function refresh_post_like()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently post_aggregates_mview;
-  refresh materialized view concurrently user_mview; 
-  return null;
-end $$;
-
-create trigger refresh_post_like
-after insert or update or delete or truncate
-on post_like
-for each statement
-execute procedure refresh_post_like();
+CREATE OR REPLACE FUNCTION refresh_post_like ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY post_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY user_mview;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER refresh_post_like
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON post_like
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_post_like ();
 
 -- community
-create or replace function refresh_community()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently post_aggregates_mview;
-  refresh materialized view concurrently community_aggregates_mview; 
-  refresh materialized view concurrently user_mview;
-  return null;
-end $$;
-
-create trigger refresh_community
-after insert or update or delete or truncate
-on community
-for each statement
-execute procedure refresh_community();
+CREATE OR REPLACE FUNCTION refresh_community ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY post_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY community_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY user_mview;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER refresh_community
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON community
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_community ();
 
 -- community_follower
-create or replace function refresh_community_follower()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently community_aggregates_mview;
-  refresh materialized view concurrently post_aggregates_mview;
-  return null;
-end $$;
-
-create trigger refresh_community_follower
-after insert or update or delete or truncate
-on community_follower
-for each statement
-execute procedure refresh_community_follower();
+CREATE OR REPLACE FUNCTION refresh_community_follower ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY community_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY post_aggregates_mview;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER refresh_community_follower
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON community_follower
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_community_follower ();
 
 -- community_user_ban
-create or replace function refresh_community_user_ban()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently comment_aggregates_mview;
-  refresh materialized view concurrently post_aggregates_mview;
-  return null;
-end $$;
-
-create trigger refresh_community_user_ban
-after insert or update or delete or truncate
-on community_user_ban
-for each statement
-execute procedure refresh_community_user_ban();
+CREATE OR REPLACE FUNCTION refresh_community_user_ban ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY comment_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY post_aggregates_mview;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER refresh_community_user_ban
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON community_user_ban
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_community_user_ban ();
 
 -- comment
-create or replace function refresh_comment()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently post_aggregates_mview;
-  refresh materialized view concurrently comment_aggregates_mview; 
-  refresh materialized view concurrently community_aggregates_mview; 
-  refresh materialized view concurrently user_mview; 
-  return null;
-end $$;
-
-create trigger refresh_comment
-after insert or update or delete or truncate
-on comment
-for each statement
-execute procedure refresh_comment();
+CREATE OR REPLACE FUNCTION refresh_comment ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY post_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY comment_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY community_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY user_mview;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER refresh_comment
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON comment
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_comment ();
 
 -- comment_like
-create or replace function refresh_comment_like()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently comment_aggregates_mview;
-  refresh materialized view concurrently user_mview;
-  return null;
-end $$;
-
-create trigger refresh_comment_like
-after insert or update or delete or truncate
-on comment_like
-for each statement
-execute procedure refresh_comment_like();
+CREATE OR REPLACE FUNCTION refresh_comment_like ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY comment_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY user_mview;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER refresh_comment_like
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON comment_like
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_comment_like ();
+
index 0d951e3eafad0828fb1198d950bb0c1c3b260077..9b55f39ecc62e460a84a709f69efa6eb5b9091f4 100644 (file)
@@ -1,34 +1,72 @@
 -- Drop the triggers
-drop trigger refresh_private_message on private_message;
-drop function refresh_private_message();
+DROP TRIGGER refresh_private_message ON private_message;
+
+DROP FUNCTION refresh_private_message ();
 
 -- Drop the view and table
-drop view private_message_view cascade;
-drop table private_message;
+DROP VIEW private_message_view CASCADE;
+
+DROP TABLE private_message;
 
 -- Rebuild the old views
-drop view user_view cascade;
-create view user_view as 
-select 
-u.id,
-u.name,
-u.avatar,
-u.email,
-u.fedi_name,
-u.admin,
-u.banned,
-u.show_avatars,
-u.send_notifications_to_email,
-u.published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
-
-create materialized view user_mview as select * from user_view;
-
-create unique index idx_user_mview_id on user_mview (id);
+DROP VIEW user_view CASCADE;
+
+CREATE VIEW user_view AS
+SELECT
+    u.id,
+    u.name,
+    u.avatar,
+    u.email,
+    u.fedi_name,
+    u.admin,
+    u.banned,
+    u.show_avatars,
+    u.send_notifications_to_email,
+    u.published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
+
+CREATE MATERIALIZED VIEW user_mview AS
+SELECT
+    *
+FROM
+    user_view;
+
+CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
 
 -- Drop the columns
-alter table user_ drop column matrix_user_id;
+ALTER TABLE user_
+    DROP COLUMN matrix_user_id;
+
index 48e16dd83d6fbe971a29c6781302da217c9bce83..cb19eb42bbf409ef17796d8779f0a729b00a49fc 100644 (file)
 -- Creating private message
-create table private_message (
-  id serial primary key,
-  creator_id int references user_ on update cascade on delete cascade not null,
-  recipient_id int references user_ on update cascade on delete cascade not null,
-  content text not null,
-  deleted boolean default false not null,
-  read boolean default false not null,
-  published timestamp not null default now(),
-  updated timestamp
+CREATE TABLE private_message (
+    id serial PRIMARY KEY,
+    creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    recipient_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    content text NOT NULL,
+    deleted boolean DEFAULT FALSE NOT NULL,
+    read boolean DEFAULT FALSE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp
 );
 
 -- Create the view and materialized view which has the avatar and creator name
-create view private_message_view as 
-select        
-pm.*,
-u.name as creator_name,
-u.avatar as creator_avatar,
-u2.name as recipient_name,
-u2.avatar as recipient_avatar
-from private_message pm
-inner join user_ u on u.id = pm.creator_id
-inner join user_ u2 on u2.id = pm.recipient_id;
+CREATE VIEW private_message_view AS
+SELECT
+    pm.*,
+    u.name AS creator_name,
+    u.avatar AS creator_avatar,
+    u2.name AS recipient_name,
+    u2.avatar AS recipient_avatar
+FROM
+    private_message pm
+    INNER JOIN user_ u ON u.id = pm.creator_id
+    INNER JOIN user_ u2 ON u2.id = pm.recipient_id;
 
-create materialized view private_message_mview as select * from private_message_view;
+CREATE MATERIALIZED VIEW private_message_mview AS
+SELECT
+    *
+FROM
+    private_message_view;
 
-create unique index idx_private_message_mview_id on private_message_mview (id);
+CREATE UNIQUE INDEX idx_private_message_mview_id ON private_message_mview (id);
 
 -- Create the triggers
-create or replace function refresh_private_message()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently private_message_mview;
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION refresh_private_message ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY private_message_mview;
+    RETURN NULL;
+END
+$$;
 
-create trigger refresh_private_message
-after insert or update or delete or truncate
-on private_message
-for each statement
-execute procedure refresh_private_message();
+CREATE TRIGGER refresh_private_message
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON private_message
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_private_message ();
 
 -- Update user to include matrix id
-alter table user_ add column matrix_user_id text unique;
+ALTER TABLE user_
+    ADD COLUMN matrix_user_id text UNIQUE;
 
-drop view user_view cascade;
-create view user_view as 
-select 
-u.id,
-u.name,
-u.avatar,
-u.email,
-u.matrix_user_id,
-u.fedi_name,
-u.admin,
-u.banned,
-u.show_avatars,
-u.send_notifications_to_email,
-u.published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
+DROP VIEW user_view CASCADE;
 
-create materialized view user_mview as select * from user_view;
+CREATE VIEW user_view AS
+SELECT
+    u.id,
+    u.name,
+    u.avatar,
+    u.email,
+    u.matrix_user_id,
+    u.fedi_name,
+    u.admin,
+    u.banned,
+    u.show_avatars,
+    u.send_notifications_to_email,
+    u.published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
 
-create unique index idx_user_mview_id on user_mview (id);
+CREATE MATERIALIZED VIEW user_mview AS
+SELECT
+    *
+FROM
+    user_view;
+
+CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
 
 -- This is what a group pm table would look like
 -- Not going to do it now because of the complications
--- 
+--
 -- create table private_message (
 --   id serial primary key,
 --   creator_id int references user_ on update cascade on delete cascade not null,
@@ -79,7 +120,7 @@ create unique index idx_user_mview_id on user_mview (id);
 --   published timestamp not null default now(),
 --   updated timestamp
 -- );
--- 
+--
 -- create table private_message_recipient (
 --   id serial primary key,
 --   private_message_id int references private_message on update cascade on delete cascade not null,
index 06ec5971ff6938c8468431123eabf9de4e1e98a8..ae32a42e5bad38ee890a7f2b25d7217546ee15f4 100644 (file)
@@ -1,25 +1,37 @@
 -- Drop the materialized / built views
-drop view reply_view;
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+DROP VIEW reply_view;
+
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
+
index ebbb1dff4bf6a28b75618bed9179209d06e987a4..f6a291d270b120678b16e8ce7f7db48d759d61a1 100644 (file)
@@ -1,27 +1,38 @@
 -- https://github.com/dessalines/lemmy/issues/197
-drop view reply_view;
+DROP VIEW reply_view;
 
 -- Do the reply_view referencing the comment_mview
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_mview cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_mview cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
+
index d93ebc2edcece19c220b72e6de4b3a72b28aadde..99d0856cf8e66deb376c4993c92e9b853cda72bd 100644 (file)
@@ -1 +1,2 @@
-drop view user_mention_mview;
+DROP VIEW user_mention_mview;
+
index b0ae4e9d914f45e5d3a192658fe14cabe95cc62b..0130f326824a696d674341eeaf6d8ace168cf796 100644 (file)
@@ -1,14 +1,13 @@
-create view user_mention_mview as 
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_mview ca
+CREATE VIEW user_mention_mview AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_mview ca
 )
-
-select
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.post_id,
     ac.parent_id,
@@ -26,20 +25,27 @@ select
     ac.score,
     ac.upvotes,
     ac.downvotes,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select 
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.post_id,
     ac.parent_id,
@@ -57,11 +63,11 @@ select
     ac.score,
     ac.upvotes,
     ac.downvotes,
-    null as user_id, 
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id
-from all_comment ac
-left join user_mention um on um.comment_id = ac.id
-;
+FROM
+    all_comment ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
 
index 0c6f112f0d0469c42dd11bf8e3effb8d695fdf79..d7452a00075555770ee9b86cd099929f0824d6b3 100644 (file)
@@ -1,2 +1,4 @@
-drop index idx_user_name_lower;
-drop index idx_user_email_lower;
+DROP INDEX idx_user_name_lower;
+
+DROP INDEX idx_user_email_lower;
+
index 07f854b1c99fdeab295d6a13c553c88b91ee472a..5cf7e0490e5f93d83f665d2f6967b2d424286e4b 100644 (file)
@@ -1,5 +1,4 @@
 -- Add case insensitive username and email uniqueness
-
 -- An example of showing the dupes:
 -- select
 --   max(id) as id,
@@ -8,22 +7,28 @@
 -- from user_
 -- group by lower(name)
 -- having count(*) > 1;
-
 -- Delete username dupes, keeping the first one
-delete
-from user_
-where id not in (
-  select min(id)
-  from user_
-  group by lower(name), lower(fedi_name)
-);
+DELETE FROM user_
+WHERE id NOT IN (
+        SELECT
+            min(id)
+        FROM
+            user_
+        GROUP BY
+            lower(name),
+            lower(fedi_name));
 
--- The user index 
-create unique index idx_user_name_lower on user_ (lower(name));
+-- The user index
+CREATE UNIQUE INDEX idx_user_name_lower ON user_ (lower(name));
 
 -- Email lower
-create unique index idx_user_email_lower on user_ (lower(email));
+CREATE UNIQUE INDEX idx_user_email_lower ON user_ (lower(email));
 
 -- Set empty emails properly to null
-update user_ set email = null where email = '';
+UPDATE
+    user_
+SET
+    email = NULL
+WHERE
+    email = '';
 
index 2bc765f857490d86e25384e5953119148b691736..19a5c37e8ec3981130f2c97a11debf27aac06c44 100644 (file)
 -- Drop the dependent views
-drop view post_view;
-drop view post_mview;
-drop materialized view post_aggregates_mview;
-drop view post_aggregates_view;
-drop view mod_remove_post_view;
-drop view mod_sticky_post_view;
-drop view mod_lock_post_view;
-drop view mod_remove_comment_view;
+DROP VIEW post_view;
 
-alter table post alter column name type varchar(100);
+DROP VIEW post_mview;
+
+DROP MATERIALIZED VIEW post_aggregates_mview;
+
+DROP VIEW post_aggregates_view;
+
+DROP VIEW mod_remove_post_view;
+
+DROP VIEW mod_sticky_post_view;
+
+DROP VIEW mod_lock_post_view;
+
+DROP VIEW mod_remove_comment_view;
+
+ALTER TABLE post
+    ALTER COLUMN name TYPE varchar(100);
 
 -- regen post view
-create view post_aggregates_view as
-select        
-p.*,
-(select u.banned from user_ u where p.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-(select name from user_ where p.creator_id = user_.id) as creator_name,
-(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
-(select name from community where p.community_id = community.id) as community_name,
-(select removed from community c where p.community_id = c.id) as community_removed,
-(select deleted from community c where p.community_id = c.id) as community_deleted,
-(select nsfw from community c where p.community_id = c.id) as community_nsfw,
-(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-coalesce(sum(pl.score), 0) as score,
-count (case when pl.score = 1 then 1 else null end) as upvotes,
-count (case when pl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-from post p
-left join post_like pl on p.id = pl.post_id
-group by p.id;
-
-create materialized view post_aggregates_mview as select * from post_aggregates_view;
-
-create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
-
-create view post_view as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_view pa
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            p.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb
+        WHERE
+            p.creator_id = cb.user_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_avatar,
+    (
+        SELECT
+            name
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_name,
+    (
+        SELECT
+            removed
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_removed,
+    (
+        SELECT
+            deleted
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_deleted,
+    (
+        SELECT
+            nsfw
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_nsfw,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment
+        WHERE
+            comment.post_id = p.id) AS number_of_comments,
+    coalesce(sum(pl.score), 0) AS score,
+    count(
+        CASE WHEN pl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN pl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+FROM
+    post p
+    LEFT JOIN post_like pl ON p.id = pl.post_id
+GROUP BY
+    p.id;
+
+CREATE MATERIALIZED VIEW post_aggregates_mview AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
+
+CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
+
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_view pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
-
-create view post_mview as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_mview pa
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
+
+CREATE VIEW post_mview AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_mview pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
 -- The mod views
+CREATE VIEW mod_remove_post_view AS
+SELECT
+    mrp.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mrp.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            post p
+        WHERE
+            mrp.post_id = p.id) AS post_name,
+    (
+        SELECT
+            c.id
+        FROM
+            post p,
+            community c
+        WHERE
+            mrp.post_id = p.id
+            AND p.community_id = c.id) AS community_id,
+    (
+        SELECT
+            c.name
+        FROM
+            post p,
+            community c
+        WHERE
+            mrp.post_id = p.id
+            AND p.community_id = c.id) AS community_name
+FROM
+    mod_remove_post mrp;
+
+CREATE VIEW mod_lock_post_view AS
+SELECT
+    mlp.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mlp.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            post p
+        WHERE
+            mlp.post_id = p.id) AS post_name,
+    (
+        SELECT
+            c.id
+        FROM
+            post p,
+            community c
+        WHERE
+            mlp.post_id = p.id
+            AND p.community_id = c.id) AS community_id,
+    (
+        SELECT
+            c.name
+        FROM
+            post p,
+            community c
+        WHERE
+            mlp.post_id = p.id
+            AND p.community_id = c.id) AS community_name
+FROM
+    mod_lock_post mlp;
+
+CREATE VIEW mod_remove_comment_view AS
+SELECT
+    mrc.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mrc.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            c.id
+        FROM
+            comment c
+        WHERE
+            mrc.comment_id = c.id) AS comment_user_id,
+    (
+        SELECT
+            name
+        FROM
+            user_ u,
+            comment c
+        WHERE
+            mrc.comment_id = c.id
+            AND u.id = c.creator_id) AS comment_user_name,
+    (
+        SELECT
+            content
+        FROM
+            comment c
+        WHERE
+            mrc.comment_id = c.id) AS comment_content,
+    (
+        SELECT
+            p.id
+        FROM
+            post p,
+            comment c
+        WHERE
+            mrc.comment_id = c.id
+            AND c.post_id = p.id) AS post_id,
+    (
+        SELECT
+            p.name
+        FROM
+            post p,
+            comment c
+        WHERE
+            mrc.comment_id = c.id
+            AND c.post_id = p.id) AS post_name,
+    (
+        SELECT
+            co.id
+        FROM
+            comment c,
+            post p,
+            community co
+        WHERE
+            mrc.comment_id = c.id
+            AND c.post_id = p.id
+            AND p.community_id = co.id) AS community_id,
+    (
+        SELECT
+            co.name
+        FROM
+            comment c,
+            post p,
+            community co
+        WHERE
+            mrc.comment_id = c.id
+            AND c.post_id = p.id
+            AND p.community_id = co.id) AS community_name
+FROM
+    mod_remove_comment mrc;
+
+CREATE VIEW mod_sticky_post_view AS
+SELECT
+    msp.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            msp.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            post p
+        WHERE
+            msp.post_id = p.id) AS post_name,
+    (
+        SELECT
+            c.id
+        FROM
+            post p,
+            community c
+        WHERE
+            msp.post_id = p.id
+            AND p.community_id = c.id) AS community_id,
+    (
+        SELECT
+            c.name
+        FROM
+            post p,
+            community c
+        WHERE
+            msp.post_id = p.id
+            AND p.community_id = c.id) AS community_name
+FROM
+    mod_sticky_post msp;
 
-create view mod_remove_post_view as 
-select mrp.*,
-(select name from user_ u where mrp.mod_user_id = u.id) as mod_user_name,
-(select name from post p where mrp.post_id = p.id) as post_name,
-(select c.id from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_id,
-(select c.name from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_name
-from mod_remove_post mrp;
-
-create view mod_lock_post_view as 
-select mlp.*,
-(select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
-(select name from post p where mlp.post_id = p.id) as post_name,
-(select c.id from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_id,
-(select c.name from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_name
-from mod_lock_post mlp;
-
-create view mod_remove_comment_view as 
-select mrc.*,
-(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
-(select c.id from comment c where mrc.comment_id = c.id) as comment_user_id,
-(select name from user_ u, comment c where mrc.comment_id = c.id and u.id = c.creator_id) as comment_user_name,
-(select content from comment c where mrc.comment_id = c.id) as comment_content,
-(select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
-(select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
-(select co.id from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_id, 
-(select co.name from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_name
-from mod_remove_comment mrc;
-
-create view mod_sticky_post_view as 
-select msp.*,
-(select name from user_ u where msp.mod_user_id = u.id) as mod_user_name,
-(select name from post p where msp.post_id = p.id) as post_name,
-(select c.id from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_id,
-(select c.name from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_name
-from mod_sticky_post msp;
index 006a7d049790f161fe5e4d0e25b87c4f91a80518..b0ed7254bfae7881bde16bd37a1529509345107b 100644 (file)
 -- Drop the dependent views
-drop view post_view;
-drop view post_mview;
-drop materialized view post_aggregates_mview;
-drop view post_aggregates_view;
-drop view mod_remove_post_view;
-drop view mod_sticky_post_view;
-drop view mod_lock_post_view;
-drop view mod_remove_comment_view;
+DROP VIEW post_view;
+
+DROP VIEW post_mview;
+
+DROP MATERIALIZED VIEW post_aggregates_mview;
+
+DROP VIEW post_aggregates_view;
+
+DROP VIEW mod_remove_post_view;
+
+DROP VIEW mod_sticky_post_view;
+
+DROP VIEW mod_lock_post_view;
+
+DROP VIEW mod_remove_comment_view;
 
 -- Add the extra post limit
-alter table post alter column name type varchar(200);
+ALTER TABLE post
+    ALTER COLUMN name TYPE varchar(200);
 
 -- regen post view
-create view post_aggregates_view as
-select        
-p.*,
-(select u.banned from user_ u where p.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-(select name from user_ where p.creator_id = user_.id) as creator_name,
-(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
-(select name from community where p.community_id = community.id) as community_name,
-(select removed from community c where p.community_id = c.id) as community_removed,
-(select deleted from community c where p.community_id = c.id) as community_deleted,
-(select nsfw from community c where p.community_id = c.id) as community_nsfw,
-(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-coalesce(sum(pl.score), 0) as score,
-count (case when pl.score = 1 then 1 else null end) as upvotes,
-count (case when pl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-from post p
-left join post_like pl on p.id = pl.post_id
-group by p.id;
-
-create materialized view post_aggregates_mview as select * from post_aggregates_view;
-
-create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
-
-create view post_view as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_view pa
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            p.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb
+        WHERE
+            p.creator_id = cb.user_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_avatar,
+    (
+        SELECT
+            name
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_name,
+    (
+        SELECT
+            removed
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_removed,
+    (
+        SELECT
+            deleted
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_deleted,
+    (
+        SELECT
+            nsfw
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_nsfw,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment
+        WHERE
+            comment.post_id = p.id) AS number_of_comments,
+    coalesce(sum(pl.score), 0) AS score,
+    count(
+        CASE WHEN pl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN pl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+FROM
+    post p
+    LEFT JOIN post_like pl ON p.id = pl.post_id
+GROUP BY
+    p.id;
+
+CREATE MATERIALIZED VIEW post_aggregates_mview AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
+
+CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
+
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_view pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
-
-create view post_mview as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_mview pa
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
+
+CREATE VIEW post_mview AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_mview pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
 -- The mod views
+CREATE VIEW mod_remove_post_view AS
+SELECT
+    mrp.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mrp.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            post p
+        WHERE
+            mrp.post_id = p.id) AS post_name,
+    (
+        SELECT
+            c.id
+        FROM
+            post p,
+            community c
+        WHERE
+            mrp.post_id = p.id
+            AND p.community_id = c.id) AS community_id,
+    (
+        SELECT
+            c.name
+        FROM
+            post p,
+            community c
+        WHERE
+            mrp.post_id = p.id
+            AND p.community_id = c.id) AS community_name
+FROM
+    mod_remove_post mrp;
+
+CREATE VIEW mod_lock_post_view AS
+SELECT
+    mlp.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mlp.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            post p
+        WHERE
+            mlp.post_id = p.id) AS post_name,
+    (
+        SELECT
+            c.id
+        FROM
+            post p,
+            community c
+        WHERE
+            mlp.post_id = p.id
+            AND p.community_id = c.id) AS community_id,
+    (
+        SELECT
+            c.name
+        FROM
+            post p,
+            community c
+        WHERE
+            mlp.post_id = p.id
+            AND p.community_id = c.id) AS community_name
+FROM
+    mod_lock_post mlp;
+
+CREATE VIEW mod_remove_comment_view AS
+SELECT
+    mrc.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            mrc.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            c.id
+        FROM
+            comment c
+        WHERE
+            mrc.comment_id = c.id) AS comment_user_id,
+    (
+        SELECT
+            name
+        FROM
+            user_ u,
+            comment c
+        WHERE
+            mrc.comment_id = c.id
+            AND u.id = c.creator_id) AS comment_user_name,
+    (
+        SELECT
+            content
+        FROM
+            comment c
+        WHERE
+            mrc.comment_id = c.id) AS comment_content,
+    (
+        SELECT
+            p.id
+        FROM
+            post p,
+            comment c
+        WHERE
+            mrc.comment_id = c.id
+            AND c.post_id = p.id) AS post_id,
+    (
+        SELECT
+            p.name
+        FROM
+            post p,
+            comment c
+        WHERE
+            mrc.comment_id = c.id
+            AND c.post_id = p.id) AS post_name,
+    (
+        SELECT
+            co.id
+        FROM
+            comment c,
+            post p,
+            community co
+        WHERE
+            mrc.comment_id = c.id
+            AND c.post_id = p.id
+            AND p.community_id = co.id) AS community_id,
+    (
+        SELECT
+            co.name
+        FROM
+            comment c,
+            post p,
+            community co
+        WHERE
+            mrc.comment_id = c.id
+            AND c.post_id = p.id
+            AND p.community_id = co.id) AS community_name
+FROM
+    mod_remove_comment mrc;
+
+CREATE VIEW mod_sticky_post_view AS
+SELECT
+    msp.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            msp.mod_user_id = u.id) AS mod_user_name,
+    (
+        SELECT
+            name
+        FROM
+            post p
+        WHERE
+            msp.post_id = p.id) AS post_name,
+    (
+        SELECT
+            c.id
+        FROM
+            post p,
+            community c
+        WHERE
+            msp.post_id = p.id
+            AND p.community_id = c.id) AS community_id,
+    (
+        SELECT
+            c.name
+        FROM
+            post p,
+            community c
+        WHERE
+            msp.post_id = p.id
+            AND p.community_id = c.id) AS community_name
+FROM
+    mod_sticky_post msp;
 
-create view mod_remove_post_view as 
-select mrp.*,
-(select name from user_ u where mrp.mod_user_id = u.id) as mod_user_name,
-(select name from post p where mrp.post_id = p.id) as post_name,
-(select c.id from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_id,
-(select c.name from post p, community c where mrp.post_id = p.id and p.community_id = c.id) as community_name
-from mod_remove_post mrp;
-
-create view mod_lock_post_view as 
-select mlp.*,
-(select name from user_ u where mlp.mod_user_id = u.id) as mod_user_name,
-(select name from post p where mlp.post_id = p.id) as post_name,
-(select c.id from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_id,
-(select c.name from post p, community c where mlp.post_id = p.id and p.community_id = c.id) as community_name
-from mod_lock_post mlp;
-
-create view mod_remove_comment_view as 
-select mrc.*,
-(select name from user_ u where mrc.mod_user_id = u.id) as mod_user_name,
-(select c.id from comment c where mrc.comment_id = c.id) as comment_user_id,
-(select name from user_ u, comment c where mrc.comment_id = c.id and u.id = c.creator_id) as comment_user_name,
-(select content from comment c where mrc.comment_id = c.id) as comment_content,
-(select p.id from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_id,
-(select p.name from post p, comment c where mrc.comment_id = c.id and c.post_id = p.id) as post_name,
-(select co.id from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_id, 
-(select co.name from comment c, post p, community co where mrc.comment_id = c.id and c.post_id = p.id and p.community_id = co.id) as community_name
-from mod_remove_comment mrc;
-
-create view mod_sticky_post_view as 
-select msp.*,
-(select name from user_ u where msp.mod_user_id = u.id) as mod_user_name,
-(select name from post p where msp.post_id = p.id) as post_name,
-(select c.id from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_id,
-(select c.name from post p, community c where msp.post_id = p.id and p.community_id = c.id) as community_name
-from mod_sticky_post msp;
index b6120d151fa6b73899fa4fceeace3f63542909b6..56edcaea321f673ab109c7c71ed116e2c47be74b 100644 (file)
+DROP VIEW reply_view;
 
-drop view reply_view;
-drop view user_mention_view;
-drop view user_mention_mview;
-drop view comment_view;
-drop view comment_mview;
-drop materialized view comment_aggregates_mview;
-drop view comment_aggregates_view;
+DROP VIEW user_mention_view;
 
--- reply and comment view
-create view comment_aggregates_view as
-select        
-c.*,
-(select community_id from post p where p.id = c.post_id),
-(select u.banned from user_ u where c.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
-(select name from user_ where c.creator_id = user_.id) as creator_name,
-(select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
-coalesce(sum(cl.score), 0) as score,
-count (case when cl.score = 1 then 1 else null end) as upvotes,
-count (case when cl.score = -1 then 1 else null end) as downvotes
-from comment c
-left join comment_like cl on c.id = cl.comment_id
-group by c.id;
-
-create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
+DROP VIEW user_mention_mview;
 
-create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
-
-create view comment_view as
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_view ca
-)
+DROP VIEW comment_view;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
+DROP VIEW comment_mview;
 
-union all
+DROP MATERIALIZED VIEW comment_aggregates_mview;
 
-select 
-    ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as saved
-from all_comment ac
-;
+DROP VIEW comment_aggregates_view;
 
-create view comment_mview as
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_mview ca
-)
+-- reply and comment view
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    c.*,
+    (
+        SELECT
+            community_id
+        FROM
+            post p
+        WHERE
+            p.id = c.post_id), (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb,
+            post p
+        WHERE
+            c.creator_id = cb.user_id
+            AND p.id = c.post_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_avatar,
+    coalesce(sum(cl.score), 0) AS score,
+    count(
+        CASE WHEN cl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN cl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes
+FROM
+    comment c
+    LEFT JOIN comment_like cl ON c.id = cl.comment_id
+GROUP BY
+    c.id;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
+CREATE MATERIALIZED VIEW comment_aggregates_mview AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
 
-union all
+CREATE UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id);
 
-select 
+CREATE VIEW comment_view AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_view ca
+)
+SELECT
     ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as saved
-from all_comment ac
-;
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS saved
+    FROM
+        all_comment ac;
 
+CREATE VIEW comment_mview AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_mview ca
+)
+SELECT
+    ac.*,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS saved
+    FROM
+        all_comment ac;
 
 -- Do the reply_view referencing the comment_mview
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_mview cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_mview cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- user mention
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.post_id,
     c.parent_id,
@@ -133,21 +207,22 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id
-from user_mention um, comment_view c
-where um.comment_id = c.id;
-
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
 
-create view user_mention_mview as 
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_mview ca
+CREATE VIEW user_mention_mview AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_mview ca
 )
-
-select
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.post_id,
     ac.parent_id,
@@ -165,20 +240,27 @@ select
     ac.score,
     ac.upvotes,
     ac.downvotes,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select 
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.post_id,
     ac.parent_id,
@@ -196,11 +278,11 @@ select
     ac.score,
     ac.upvotes,
     ac.downvotes,
-    null as user_id, 
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id
-from all_comment ac
-left join user_mention um on um.comment_id = ac.id
-;
+FROM
+    all_comment ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
 
index 8836a571a6f3de4b3587ff93eae501bb3fa070a5..bdff8c1daa4964ed1bed9e39497fbd0516c96423 100644 (file)
-
 -- Adding community name, hot_rank, to comment_view, user_mention_view, and subscribed to comment_view
-
 -- Rebuild the comment view
-drop view reply_view;
-drop view user_mention_view;
-drop view user_mention_mview;
-drop view comment_view;
-drop view comment_mview;
-drop materialized view comment_aggregates_mview;
-drop view comment_aggregates_view;
+DROP VIEW reply_view;
 
--- reply and comment view
-create view comment_aggregates_view as
-select        
-c.*,
-(select community_id from post p where p.id = c.post_id),
-(select co.name from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_name,
-(select u.banned from user_ u where c.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
-(select name from user_ where c.creator_id = user_.id) as creator_name,
-(select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
-coalesce(sum(cl.score), 0) as score,
-count (case when cl.score = 1 then 1 else null end) as upvotes,
-count (case when cl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(cl.score) , 0), c.published) as hot_rank
-from comment c
-left join comment_like cl on c.id = cl.comment_id
-group by c.id;
+DROP VIEW user_mention_view;
 
-create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
+DROP VIEW user_mention_mview;
 
-create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
+DROP VIEW comment_view;
 
-create view comment_view as
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_view ca
-)
+DROP VIEW comment_mview;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
+DROP MATERIALIZED VIEW comment_aggregates_mview;
 
-union all
+DROP VIEW comment_aggregates_view;
 
-select 
-    ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from all_comment ac
-;
+-- reply and comment view
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    c.*,
+    (
+        SELECT
+            community_id
+        FROM
+            post p
+        WHERE
+            p.id = c.post_id), (
+        SELECT
+            co.name
+        FROM
+            post p,
+            community co
+        WHERE
+            p.id = c.post_id
+            AND p.community_id = co.id) AS community_name,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb,
+            post p
+        WHERE
+            c.creator_id = cb.user_id
+            AND p.id = c.post_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_avatar,
+    coalesce(sum(cl.score), 0) AS score,
+    count(
+        CASE WHEN cl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN cl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(cl.score), 0), c.published) AS hot_rank
+FROM
+    comment c
+    LEFT JOIN comment_like cl ON c.id = cl.comment_id
+GROUP BY
+    c.id;
 
-create view comment_mview as
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_mview ca
-)
+CREATE MATERIALIZED VIEW comment_aggregates_mview AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
+CREATE UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id);
 
-union all
+CREATE VIEW comment_view AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_view ca
+)
+SELECT
+    ac.*,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.community_id = cf.community_id) AS subscribed,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS saved
+    FROM
+        all_comment ac;
 
-select 
+CREATE VIEW comment_mview AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_mview ca
+)
+SELECT
     ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from all_comment ac
-;
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.community_id = cf.community_id) AS subscribed,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS saved
+    FROM
+        all_comment ac;
 
 -- Do the reply_view referencing the comment_mview
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_mview cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_mview cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- user mention
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.post_id,
     c.parent_id,
@@ -143,21 +239,22 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id
-from user_mention um, comment_view c
-where um.comment_id = c.id;
-
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
 
-create view user_mention_mview as 
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_mview ca
+CREATE VIEW user_mention_mview AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_mview ca
 )
-
-select
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.post_id,
     ac.parent_id,
@@ -177,20 +274,27 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select 
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.post_id,
     ac.parent_id,
@@ -210,11 +314,11 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    null as user_id, 
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id
-from all_comment ac
-left join user_mention um on um.comment_id = ac.id
-;
+FROM
+    all_comment ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
 
index 8b912fa36c66a94ebb11df4096e940b6e490b16d..3bf7bfc55a391e418c7efd658f3e4101dbcdee73 100644 (file)
-drop view post_view;
-drop view post_mview;
-drop materialized view post_aggregates_mview;
-drop view post_aggregates_view;
+DROP VIEW post_view;
 
--- regen post view
-create view post_aggregates_view as
-select        
-p.*,
-(select u.banned from user_ u where p.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-(select name from user_ where p.creator_id = user_.id) as creator_name,
-(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
-(select name from community where p.community_id = community.id) as community_name,
-(select removed from community c where p.community_id = c.id) as community_removed,
-(select deleted from community c where p.community_id = c.id) as community_deleted,
-(select nsfw from community c where p.community_id = c.id) as community_nsfw,
-(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-coalesce(sum(pl.score), 0) as score,
-count (case when pl.score = 1 then 1 else null end) as upvotes,
-count (case when pl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank
-from post p
-left join post_like pl on p.id = pl.post_id
-group by p.id;
+DROP VIEW post_mview;
 
-create materialized view post_aggregates_mview as select * from post_aggregates_view;
+DROP MATERIALIZED VIEW post_aggregates_mview;
 
-create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
+DROP VIEW post_aggregates_view;
 
-create view post_view as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_view pa
-)
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+-- regen post view
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            p.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb
+        WHERE
+            p.creator_id = cb.user_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_avatar,
+    (
+        SELECT
+            name
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_name,
+    (
+        SELECT
+            removed
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_removed,
+    (
+        SELECT
+            deleted
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_deleted,
+    (
+        SELECT
+            nsfw
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_nsfw,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment
+        WHERE
+            comment.post_id = p.id) AS number_of_comments,
+    coalesce(sum(pl.score), 0) AS score,
+    count(
+        CASE WHEN pl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN pl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(pl.score), 0), p.published) AS hot_rank
+FROM
+    post p
+    LEFT JOIN post_like pl ON p.id = pl.post_id
+GROUP BY
+    p.id;
 
-union all
+CREATE MATERIALIZED VIEW post_aggregates_mview AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
 
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
 
-create view post_mview as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_mview pa
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_view pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+CREATE VIEW post_mview AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_mview pa
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
index e15412771bb9d1f07b29a02cb835fb4840fe68dc..a02406574ff6b38a80db027f4c81f59c371afac4 100644 (file)
 -- Adds a newest_activity_time for the post_views, in order to sort by newest comment
-drop view post_view;
-drop view post_mview;
-drop materialized view post_aggregates_mview;
-drop view post_aggregates_view;
+DROP VIEW post_view;
 
--- regen post view
-create view post_aggregates_view as
-select        
-p.*,
-(select u.banned from user_ u where p.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-(select name from user_ where p.creator_id = user_.id) as creator_name,
-(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
-(select name from community where p.community_id = community.id) as community_name,
-(select removed from community c where p.community_id = c.id) as community_removed,
-(select deleted from community c where p.community_id = c.id) as community_deleted,
-(select nsfw from community c where p.community_id = c.id) as community_nsfw,
-(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-coalesce(sum(pl.score), 0) as score,
-count (case when pl.score = 1 then 1 else null end) as upvotes,
-count (case when pl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(pl.score) , 0), 
-  (
-    case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
-    else greatest(c.recent_comment_time, p.published)
-    end
-  )
-) as hot_rank,
-(
-  case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
-  else greatest(c.recent_comment_time, p.published)
-  end
-) as newest_activity_time 
-from post p
-left join post_like pl on p.id = pl.post_id
-left join (
-  select post_id, 
-  max(published) as recent_comment_time
-  from comment
-  group by 1
-) c on p.id = c.post_id
-group by p.id, c.recent_comment_time;
+DROP VIEW post_mview;
 
-create materialized view post_aggregates_mview as select * from post_aggregates_view;
+DROP MATERIALIZED VIEW post_aggregates_mview;
 
-create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
+DROP VIEW post_aggregates_view;
 
-create view post_view as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_view pa
-)
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+-- regen post view
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            p.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb
+        WHERE
+            p.creator_id = cb.user_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_avatar,
+    (
+        SELECT
+            name
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_name,
+    (
+        SELECT
+            removed
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_removed,
+    (
+        SELECT
+            deleted
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_deleted,
+    (
+        SELECT
+            nsfw
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_nsfw,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment
+        WHERE
+            comment.post_id = p.id) AS number_of_comments,
+    coalesce(sum(pl.score), 0) AS score,
+    count(
+        CASE WHEN pl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN pl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(pl.score), 0), (
+            CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+                p.published -- Prevents necro-bumps
+            ELSE
+                greatest (c.recent_comment_time, p.published)
+            END)) AS hot_rank,
+    (
+        CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+            p.published -- Prevents necro-bumps
+        ELSE
+            greatest (c.recent_comment_time, p.published)
+        END) AS newest_activity_time
+FROM
+    post p
+    LEFT JOIN post_like pl ON p.id = pl.post_id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            max(published) AS recent_comment_time
+        FROM
+            comment
+        GROUP BY
+            1) c ON p.id = c.post_id
+GROUP BY
+    p.id,
+    c.recent_comment_time;
 
-union all
+CREATE MATERIALIZED VIEW post_aggregates_mview AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
 
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
 
-create view post_mview as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_mview pa
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_view pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+CREATE VIEW post_mview AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_mview pa
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
index 71dcc6bab10ea3ac617ba3ee6d8c3ea601ce6a20..08a9935399d71c24b3ec120b82fc432822ea5517 100644 (file)
 -- Adds a newest_activity_time for the post_views, in order to sort by newest comment
-drop view post_view;
-drop view post_mview;
-drop materialized view post_aggregates_mview;
-drop view post_aggregates_view;
+DROP VIEW post_view;
+
+DROP VIEW post_mview;
+
+DROP MATERIALIZED VIEW post_aggregates_mview;
+
+DROP VIEW post_aggregates_view;
 
 -- Drop the columns
-alter table post drop column embed_title;
-alter table post drop column embed_description;
-alter table post drop column embed_html;
-alter table post drop column thumbnail_url;
+ALTER TABLE post
+    DROP COLUMN embed_title;
 
--- regen post view
-create view post_aggregates_view as
-select        
-p.*,
-(select u.banned from user_ u where p.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-(select name from user_ where p.creator_id = user_.id) as creator_name,
-(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
-(select name from community where p.community_id = community.id) as community_name,
-(select removed from community c where p.community_id = c.id) as community_removed,
-(select deleted from community c where p.community_id = c.id) as community_deleted,
-(select nsfw from community c where p.community_id = c.id) as community_nsfw,
-(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-coalesce(sum(pl.score), 0) as score,
-count (case when pl.score = 1 then 1 else null end) as upvotes,
-count (case when pl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(pl.score) , 0), 
-  (
-    case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
-    else greatest(c.recent_comment_time, p.published)
-    end
-  )
-) as hot_rank,
-(
-  case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
-  else greatest(c.recent_comment_time, p.published)
-  end
-) as newest_activity_time 
-from post p
-left join post_like pl on p.id = pl.post_id
-left join (
-  select post_id, 
-  max(published) as recent_comment_time
-  from comment
-  group by 1
-) c on p.id = c.post_id
-group by p.id, c.recent_comment_time;
+ALTER TABLE post
+    DROP COLUMN embed_description;
 
-create materialized view post_aggregates_mview as select * from post_aggregates_view;
+ALTER TABLE post
+    DROP COLUMN embed_html;
 
-create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
+ALTER TABLE post
+    DROP COLUMN thumbnail_url;
 
-create view post_view as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_view pa
-)
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+-- regen post view
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            p.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb
+        WHERE
+            p.creator_id = cb.user_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_avatar,
+    (
+        SELECT
+            name
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_name,
+    (
+        SELECT
+            removed
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_removed,
+    (
+        SELECT
+            deleted
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_deleted,
+    (
+        SELECT
+            nsfw
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_nsfw,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment
+        WHERE
+            comment.post_id = p.id) AS number_of_comments,
+    coalesce(sum(pl.score), 0) AS score,
+    count(
+        CASE WHEN pl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN pl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(pl.score), 0), (
+            CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+                p.published -- Prevents necro-bumps
+            ELSE
+                greatest (c.recent_comment_time, p.published)
+            END)) AS hot_rank,
+    (
+        CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+            p.published -- Prevents necro-bumps
+        ELSE
+            greatest (c.recent_comment_time, p.published)
+        END) AS newest_activity_time
+FROM
+    post p
+    LEFT JOIN post_like pl ON p.id = pl.post_id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            max(published) AS recent_comment_time
+        FROM
+            comment
+        GROUP BY
+            1) c ON p.id = c.post_id
+GROUP BY
+    p.id,
+    c.recent_comment_time;
 
-union all
+CREATE MATERIALIZED VIEW post_aggregates_mview AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
 
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
 
-create view post_mview as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_mview pa
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_view pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+CREATE VIEW post_mview AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_mview pa
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
index d431bb7fa2ca7f87dacf9ba468d927a39d1afcf0..c0d372dbfcc00ec62b1a392cedfd17ed7384e017 100644 (file)
 -- Add the columns
-alter table post add column embed_title text;
-alter table post add column embed_description text;
-alter table post add column embed_html text;
-alter table post add column thumbnail_url text;
+ALTER TABLE post
+    ADD COLUMN embed_title text;
 
--- Regenerate the views
+ALTER TABLE post
+    ADD COLUMN embed_description text;
 
--- Adds a newest_activity_time for the post_views, in order to sort by newest comment
-drop view post_view;
-drop view post_mview;
-drop materialized view post_aggregates_mview;
-drop view post_aggregates_view;
+ALTER TABLE post
+    ADD COLUMN embed_html text;
 
--- regen post view
-create view post_aggregates_view as
-select        
-p.*,
-(select u.banned from user_ u where p.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-(select name from user_ where p.creator_id = user_.id) as creator_name,
-(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
-(select name from community where p.community_id = community.id) as community_name,
-(select removed from community c where p.community_id = c.id) as community_removed,
-(select deleted from community c where p.community_id = c.id) as community_deleted,
-(select nsfw from community c where p.community_id = c.id) as community_nsfw,
-(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-coalesce(sum(pl.score), 0) as score,
-count (case when pl.score = 1 then 1 else null end) as upvotes,
-count (case when pl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(pl.score) , 0), 
-  (
-    case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
-    else greatest(c.recent_comment_time, p.published)
-    end
-  )
-) as hot_rank,
-(
-  case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
-  else greatest(c.recent_comment_time, p.published)
-  end
-) as newest_activity_time
-from post p
-left join post_like pl on p.id = pl.post_id
-left join (
-  select post_id, 
-  max(published) as recent_comment_time
-  from comment
-  group by 1
-) c on p.id = c.post_id
-group by p.id, c.recent_comment_time;
+ALTER TABLE post
+    ADD COLUMN thumbnail_url text;
 
-create materialized view post_aggregates_mview as select * from post_aggregates_view;
+-- Regenerate the views
+-- Adds a newest_activity_time for the post_views, in order to sort by newest comment
+DROP VIEW post_view;
 
-create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
+DROP VIEW post_mview;
 
-create view post_view as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_view pa
-)
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+DROP MATERIALIZED VIEW post_aggregates_mview;
 
-union all
+DROP VIEW post_aggregates_view;
 
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+-- regen post view
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            p.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb
+        WHERE
+            p.creator_id = cb.user_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_avatar,
+    (
+        SELECT
+            name
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_name,
+    (
+        SELECT
+            removed
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_removed,
+    (
+        SELECT
+            deleted
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_deleted,
+    (
+        SELECT
+            nsfw
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_nsfw,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment
+        WHERE
+            comment.post_id = p.id) AS number_of_comments,
+    coalesce(sum(pl.score), 0) AS score,
+    count(
+        CASE WHEN pl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN pl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(pl.score), 0), (
+            CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+                p.published -- Prevents necro-bumps
+            ELSE
+                greatest (c.recent_comment_time, p.published)
+            END)) AS hot_rank,
+    (
+        CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+            p.published -- Prevents necro-bumps
+        ELSE
+            greatest (c.recent_comment_time, p.published)
+        END) AS newest_activity_time
+FROM
+    post p
+    LEFT JOIN post_like pl ON p.id = pl.post_id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            max(published) AS recent_comment_time
+        FROM
+            comment
+        GROUP BY
+            1) c ON p.id = c.post_id
+GROUP BY
+    p.id,
+    c.recent_comment_time;
 
-create view post_mview as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_mview pa
-)
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
+CREATE MATERIALIZED VIEW post_aggregates_mview AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
 
-union all
+CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
 
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_view pa
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
+CREATE VIEW post_mview AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_mview pa
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
index b1710623e45ed18740706d81e70e23dac0d97a27..27963d8571db6e1ddec9558e2bf61f1c7ead73f6 100644 (file)
@@ -1,16 +1,17 @@
-drop table activity;
+DROP TABLE activity;
 
-alter table user_ 
-drop column actor_id, 
-drop column private_key,
-drop column public_key,
-drop column bio,
-drop column local,
-drop column last_refreshed_at;
+ALTER TABLE user_
+    DROP COLUMN actor_id,
+    DROP COLUMN private_key,
+    DROP COLUMN public_key,
+    DROP COLUMN bio,
+    DROP COLUMN local,
+    DROP COLUMN last_refreshed_at;
+
+ALTER TABLE community
+    DROP COLUMN actor_id,
+    DROP COLUMN private_key,
+    DROP COLUMN public_key,
+    DROP COLUMN local,
+    DROP COLUMN last_refreshed_at;
 
-alter table community 
-drop column actor_id, 
-drop column private_key,
-drop column public_key,
-drop column local,
-drop column last_refreshed_at;
index 06db580e80dd6c94059f33a0e5475129531dac4d..1359fa4948878bb9e7b6dfe172286ebc1b8d0e2a 100644 (file)
@@ -1,36 +1,35 @@
 -- The Activitypub activity table
 -- All user actions must create a row here.
-create table activity (
-  id serial primary key,
-  user_id int references user_ on update cascade on delete cascade not null, -- Ensures that the user is set up here.
-  data jsonb not null,
-  local boolean not null default true,
-  published timestamp not null default now(),
-  updated timestamp
+CREATE TABLE activity (
+    id serial PRIMARY KEY,
+    user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, -- Ensures that the user is set up here.
+    data jsonb NOT NULL,
+    local boolean NOT NULL DEFAULT TRUE,
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp
 );
 
 -- Making sure that id is unique
-create unique index idx_activity_unique_apid on activity ((data ->> 'id'::text));
+CREATE UNIQUE INDEX idx_activity_unique_apid ON activity ((data ->> 'id'::text));
 
 -- Add federation columns to the two actor tables
-alter table user_ 
+ALTER TABLE user_
 -- TODO uniqueness constraints should be added on these 3 columns later
-add column actor_id character varying(255) not null default 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
-add column bio text, -- not on community, already has description
-add column local boolean not null default true,
-add column private_key text, -- These need to be generated from code
-add column public_key text,
-add column last_refreshed_at timestamp not null default now() -- Used to re-fetch federated actor periodically
+    ADD COLUMN actor_id character varying(255) NOT NULL DEFAULT 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
+    ADD COLUMN bio text, -- not on community, already has description
+    ADD COLUMN local boolean NOT NULL DEFAULT TRUE,
+    ADD COLUMN private_key text, -- These need to be generated from code
+    ADD COLUMN public_key text,
+    ADD COLUMN last_refreshed_at timestamp NOT NULL DEFAULT now() -- Used to re-fetch federated actor periodically
 ;
 
 -- Community
-alter table community 
-add column actor_id character varying(255) not null default 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
-add column local boolean not null default true,
-add column private_key text, -- These need to be generated from code
-add column public_key text,
-add column last_refreshed_at timestamp not null default now() -- Used to re-fetch federated actor periodically
+ALTER TABLE community
+    ADD COLUMN actor_id character varying(255) NOT NULL DEFAULT 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
+    ADD COLUMN local boolean NOT NULL DEFAULT TRUE,
+    ADD COLUMN private_key text, -- These need to be generated from code
+    ADD COLUMN public_key text,
+    ADD COLUMN last_refreshed_at timestamp NOT NULL DEFAULT now() -- Used to re-fetch federated actor periodically
 ;
 
 -- Don't worry about rebuilding the views right now.
-
index 50c95bb2a4f3fe4b2f4388d1b46f88f9faf9dcb0..6df4cb9d9cd9c51168aa8b75acebb4069b3591b4 100644 (file)
@@ -1,7 +1,8 @@
-alter table post 
-drop column ap_id, 
-drop column local;
+ALTER TABLE post
+    DROP COLUMN ap_id,
+    DROP COLUMN local;
+
+ALTER TABLE comment
+    DROP COLUMN ap_id,
+    DROP COLUMN local;
 
-alter table comment 
-drop column ap_id, 
-drop column local;
index 6c930cd8d86a50a1f7e1e0cd3ba546c25c4dedeb..ab72ac21d69e1a7bb1c20d85a3bfed1d30c2463b 100644 (file)
@@ -1,14 +1,11 @@
 -- Add federation columns to post, comment
-
-alter table post
+ALTER TABLE post
 -- TODO uniqueness constraints should be added on these 3 columns later
-add column ap_id character varying(255) not null default 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
-add column local boolean not null default true
-;
+    ADD COLUMN ap_id character varying(255) NOT NULL DEFAULT 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
+    ADD COLUMN local boolean NOT NULL DEFAULT TRUE;
 
-alter table comment
+ALTER TABLE comment
 -- TODO uniqueness constraints should be added on these 3 columns later
-add column ap_id character varying(255) not null default 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
-add column local boolean not null default true
-;
+    ADD COLUMN ap_id character varying(255) NOT NULL DEFAULT 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
+    ADD COLUMN local boolean NOT NULL DEFAULT TRUE;
 
index 46fc953a92eb75b1d8e0ceec9c59f9b3aed42a92..d2be7d3374785597f94a097a32ed1f6244e134cf 100644 (file)
@@ -1,36 +1,69 @@
 -- User table
-drop view user_view cascade;
+DROP VIEW user_view CASCADE;
 
-alter table user_ 
-add column fedi_name varchar(40) not null default 'http://fake.com';
+ALTER TABLE user_
+    ADD COLUMN fedi_name varchar(40) NOT NULL DEFAULT 'http://fake.com';
 
-alter table user_
-add constraint user__name_fedi_name_key unique (name, fedi_name);
+ALTER TABLE user_
+    ADD CONSTRAINT user__name_fedi_name_key UNIQUE (name, fedi_name);
 
 -- Community
-alter table community
-add constraint community_name_key unique (name);
+ALTER TABLE community
+    ADD CONSTRAINT community_name_key UNIQUE (name);
 
+CREATE VIEW user_view AS
+SELECT
+    u.id,
+    u.name,
+    u.avatar,
+    u.email,
+    u.matrix_user_id,
+    u.fedi_name,
+    u.admin,
+    u.banned,
+    u.show_avatars,
+    u.send_notifications_to_email,
+    u.published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
 
-create view user_view as 
-select 
-u.id,
-u.name,
-u.avatar,
-u.email,
-u.matrix_user_id,
-u.fedi_name,
-u.admin,
-u.banned,
-u.show_avatars,
-u.send_notifications_to_email,
-u.published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
+CREATE MATERIALIZED VIEW user_mview AS
+SELECT
+    *
+FROM
+    user_view;
 
-create materialized view user_mview as select * from user_view;
+CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
 
-create unique index idx_user_mview_id on user_mview (id);
index de65191d8ef5b1746ced7228a1286bc0e0e24314..1d3ba63fc6488dbeed2c3c8a8236b4dbb68cb94d 100644 (file)
@@ -1,38 +1,70 @@
 -- User table
-
 -- Need to regenerate user_view, user_mview
-drop view user_view cascade;
+DROP VIEW user_view CASCADE;
 
 -- Remove the fedi_name constraint, drop that useless column
-alter table user_ 
-drop constraint user__name_fedi_name_key;
+ALTER TABLE user_
+    DROP CONSTRAINT user__name_fedi_name_key;
 
-alter table user_
-drop column fedi_name;
+ALTER TABLE user_
+    DROP COLUMN fedi_name;
 
 -- Community
-alter table community
-drop constraint community_name_key;
+ALTER TABLE community
+    DROP CONSTRAINT community_name_key;
 
-create view user_view as 
-select 
-u.id,
-u.name,
-u.avatar,
-u.email,
-u.matrix_user_id,
-u.admin,
-u.banned,
-u.show_avatars,
-u.send_notifications_to_email,
-u.published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
+CREATE VIEW user_view AS
+SELECT
+    u.id,
+    u.name,
+    u.avatar,
+    u.email,
+    u.matrix_user_id,
+    u.admin,
+    u.banned,
+    u.show_avatars,
+    u.send_notifications_to_email,
+    u.published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
 
-create materialized view user_mview as select * from user_view;
+CREATE MATERIALIZED VIEW user_mview AS
+SELECT
+    *
+FROM
+    user_view;
 
-create unique index idx_user_mview_id on user_mview (id);
+CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
 
index ce2dde39e7b0600992f96268f15ef1d0c9c76def..2e88d51fee2ed468a02202af0584b55f229193ad 100644 (file)
 -- user_view
-drop view user_view cascade;
-
-create view user_view as 
-select 
-u.id,
-u.name,
-u.avatar,
-u.email,
-u.matrix_user_id,
-u.admin,
-u.banned,
-u.show_avatars,
-u.send_notifications_to_email,
-u.published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
-
-create materialized view user_mview as select * from user_view;
-
-create unique index idx_user_mview_id on user_mview (id);
+DROP VIEW user_view CASCADE;
+
+CREATE VIEW user_view AS
+SELECT
+    u.id,
+    u.name,
+    u.avatar,
+    u.email,
+    u.matrix_user_id,
+    u.admin,
+    u.banned,
+    u.show_avatars,
+    u.send_notifications_to_email,
+    u.published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
+
+CREATE MATERIALIZED VIEW user_mview AS
+SELECT
+    *
+FROM
+    user_view;
+
+CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
 
 -- community_view
-drop view community_aggregates_view cascade;
-create view community_aggregates_view as
-select c.*,
-(select name from user_ u where c.creator_id = u.id) as creator_name,
-(select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
-(select name from category ct where c.category_id = ct.id) as category_name,
-(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-(select count(*) from post p where p.community_id = c.id) as number_of_posts,
-(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
-hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
-from community c;
-
-create materialized view community_aggregates_mview as select * from community_aggregates_view;
-
-create unique index idx_community_aggregates_mview_id on community_aggregates_mview (id);
-
-create view community_view as
-with all_community as
-(
-  select
-  ca.*
-  from community_aggregates_view ca
+DROP VIEW community_aggregates_view CASCADE;
+
+CREATE VIEW community_aggregates_view AS
+SELECT
+    c.*,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS creator_avatar,
+    (
+        SELECT
+            name
+        FROM
+            category ct
+        WHERE
+            c.category_id = ct.id) AS category_name,
+    (
+        SELECT
+            count(*)
+        FROM
+            community_follower cf
+        WHERE
+            cf.community_id = c.id) AS number_of_subscribers,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.community_id = c.id) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment co,
+            post p
+        WHERE
+            c.id = p.community_id
+            AND p.id = co.post_id) AS number_of_comments,
+    hot_rank ((
+        SELECT
+            count(*)
+        FROM community_follower cf
+        WHERE
+            cf.community_id = c.id), c.published) AS hot_rank
+FROM
+    community c;
+
+CREATE MATERIALIZED VIEW community_aggregates_mview AS
+SELECT
+    *
+FROM
+    community_aggregates_view;
+
+CREATE UNIQUE INDEX idx_community_aggregates_mview_id ON community_aggregates_mview (id);
+
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        ca.*
+    FROM
+        community_aggregates_view ca
 )
-
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
-
-union all
-
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
-
-create view community_mview as
-with all_community as
-(
-  select
-  ca.*
-  from community_aggregates_mview ca
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
+
+CREATE VIEW community_mview AS
+with all_community AS (
+    SELECT
+        ca.*
+    FROM
+        community_aggregates_mview ca
 )
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
+-- community views
+DROP VIEW community_moderator_view;
+
+DROP VIEW community_follower_view;
+
+DROP VIEW community_user_ban_view;
+
+CREATE VIEW community_moderator_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id), (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_name
+FROM
+    community_moderator cm;
+
+CREATE VIEW community_follower_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cf.user_id = u.id) AS user_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            cf.user_id = u.id), (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cf.community_id = c.id) AS community_name
+FROM
+    community_follower cf;
+
+CREATE VIEW community_user_ban_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id), (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_name
+FROM
+    community_user_ban cm;
 
-union all
+-- post_view
+DROP VIEW post_view;
 
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
+DROP VIEW post_mview;
 
--- community views
-drop view community_moderator_view;
-drop view community_follower_view;
-drop view community_user_ban_view;
-
-create view community_moderator_view as 
-select *,
-(select name from user_ u where cm.user_id = u.id) as user_name,
-(select avatar from user_ u where cm.user_id = u.id),
-(select name from community c where cm.community_id = c.id) as community_name
-from community_moderator cm;
-
-create view community_follower_view as 
-select *,
-(select name from user_ u where cf.user_id = u.id) as user_name,
-(select avatar from user_ u where cf.user_id = u.id),
-(select name from community c where cf.community_id = c.id) as community_name
-from community_follower cf;
-
-create view community_user_ban_view as 
-select *,
-(select name from user_ u where cm.user_id = u.id) as user_name,
-(select avatar from user_ u where cm.user_id = u.id),
-(select name from community c where cm.community_id = c.id) as community_name
-from community_user_ban cm;
+DROP MATERIALIZED VIEW post_aggregates_mview;
 
--- post_view
-drop view post_view;
-drop view post_mview;
-drop materialized view post_aggregates_mview;
-drop view post_aggregates_view;
+DROP VIEW post_aggregates_view;
 
 -- regen post view
-create view post_aggregates_view as
-select        
-p.*,
-(select u.banned from user_ u where p.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-(select name from user_ where p.creator_id = user_.id) as creator_name,
-(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
-(select name from community where p.community_id = community.id) as community_name,
-(select removed from community c where p.community_id = c.id) as community_removed,
-(select deleted from community c where p.community_id = c.id) as community_deleted,
-(select nsfw from community c where p.community_id = c.id) as community_nsfw,
-(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-coalesce(sum(pl.score), 0) as score,
-count (case when pl.score = 1 then 1 else null end) as upvotes,
-count (case when pl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(pl.score) , 0), 
-  (
-    case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
-    else greatest(c.recent_comment_time, p.published)
-    end
-  )
-) as hot_rank,
-(
-  case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
-  else greatest(c.recent_comment_time, p.published)
-  end
-) as newest_activity_time
-from post p
-left join post_like pl on p.id = pl.post_id
-left join (
-  select post_id, 
-  max(published) as recent_comment_time
-  from comment
-  group by 1
-) c on p.id = c.post_id
-group by p.id, c.recent_comment_time;
-
-create materialized view post_aggregates_mview as select * from post_aggregates_view;
-
-create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
-
-create view post_view as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_view pa
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            p.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb
+        WHERE
+            p.creator_id = cb.user_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_avatar,
+    (
+        SELECT
+            name
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_name,
+    (
+        SELECT
+            removed
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_removed,
+    (
+        SELECT
+            deleted
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_deleted,
+    (
+        SELECT
+            nsfw
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_nsfw,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment
+        WHERE
+            comment.post_id = p.id) AS number_of_comments,
+    coalesce(sum(pl.score), 0) AS score,
+    count(
+        CASE WHEN pl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN pl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(pl.score), 0), (
+            CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+                p.published -- Prevents necro-bumps
+            ELSE
+                greatest (c.recent_comment_time, p.published)
+            END)) AS hot_rank,
+    (
+        CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+            p.published -- Prevents necro-bumps
+        ELSE
+            greatest (c.recent_comment_time, p.published)
+        END) AS newest_activity_time
+FROM
+    post p
+    LEFT JOIN post_like pl ON p.id = pl.post_id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            max(published) AS recent_comment_time
+        FROM
+            comment
+        GROUP BY
+            1) c ON p.id = c.post_id
+GROUP BY
+    p.id,
+    c.recent_comment_time;
+
+CREATE MATERIALIZED VIEW post_aggregates_mview AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
+
+CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
+
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_view pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
-
-create view post_mview as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_mview pa
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
+
+CREATE VIEW post_mview AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_mview pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
-
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
 -- reply_view, comment_view, user_mention
-drop view reply_view;
-drop view user_mention_view;
-drop view user_mention_mview;
-drop view comment_view;
-drop view comment_mview;
-drop materialized view comment_aggregates_mview;
-drop view comment_aggregates_view;
+DROP VIEW reply_view;
 
--- reply and comment view
-create view comment_aggregates_view as
-select        
-c.*,
-(select community_id from post p where p.id = c.post_id),
-(select co.name from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_name,
-(select u.banned from user_ u where c.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
-(select name from user_ where c.creator_id = user_.id) as creator_name,
-(select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
-coalesce(sum(cl.score), 0) as score,
-count (case when cl.score = 1 then 1 else null end) as upvotes,
-count (case when cl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(cl.score) , 0), c.published) as hot_rank
-from comment c
-left join comment_like cl on c.id = cl.comment_id
-group by c.id;
-
-create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
-
-create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
-
-create view comment_view as
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_view ca
-)
+DROP VIEW user_mention_view;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
+DROP VIEW user_mention_mview;
 
-union all
+DROP VIEW comment_view;
 
-select 
-    ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from all_comment ac
-;
-
-create view comment_mview as
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_mview ca
-)
+DROP VIEW comment_mview;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
+DROP MATERIALIZED VIEW comment_aggregates_mview;
 
-union all
+DROP VIEW comment_aggregates_view;
 
-select 
+-- reply and comment view
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    c.*,
+    (
+        SELECT
+            community_id
+        FROM
+            post p
+        WHERE
+            p.id = c.post_id), (
+        SELECT
+            co.name
+        FROM
+            post p,
+            community co
+        WHERE
+            p.id = c.post_id
+            AND p.community_id = co.id) AS community_name,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb,
+            post p
+        WHERE
+            c.creator_id = cb.user_id
+            AND p.id = c.post_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_avatar,
+    coalesce(sum(cl.score), 0) AS score,
+    count(
+        CASE WHEN cl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN cl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(cl.score), 0), c.published) AS hot_rank
+FROM
+    comment c
+    LEFT JOIN comment_like cl ON c.id = cl.comment_id
+GROUP BY
+    c.id;
+
+CREATE MATERIALIZED VIEW comment_aggregates_mview AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
+
+CREATE UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id);
+
+CREATE VIEW comment_view AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_view ca
+)
+SELECT
+    ac.*,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.community_id = cf.community_id) AS subscribed,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS saved
+    FROM
+        all_comment ac;
+
+CREATE VIEW comment_mview AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_mview ca
+)
+SELECT
     ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from all_comment ac
-;
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.community_id = cf.community_id) AS subscribed,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS saved
+    FROM
+        all_comment ac;
 
 -- Do the reply_view referencing the comment_mview
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_mview cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_mview cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- user mention
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.post_id,
     c.parent_id,
@@ -363,21 +733,22 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id
-from user_mention um, comment_view c
-where um.comment_id = c.id;
-
-
-create view user_mention_mview as 
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_mview ca
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
+
+CREATE VIEW user_mention_mview AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_mview ca
 )
-
-select
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.post_id,
     ac.parent_id,
@@ -397,20 +768,27 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select 
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.post_id,
     ac.parent_id,
@@ -430,11 +808,11 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    null as user_id, 
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id
-from all_comment ac
-left join user_mention um on um.comment_id = ac.id
-;
+FROM
+    all_comment ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
 
index 02499fc245cd7aa1e006d8fa520da9269d771f89..534ecdc2997e224a2b4489a01467071b0a4af018 100644 (file)
 -- user_view
-drop view user_view cascade;
-
-create view user_view as 
-select 
-u.id,
-u.actor_id,
-u.name,
-u.avatar,
-u.email,
-u.matrix_user_id,
-u.bio,
-u.local,
-u.admin,
-u.banned,
-u.show_avatars,
-u.send_notifications_to_email,
-u.published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
-
-create materialized view user_mview as select * from user_view;
-
-create unique index idx_user_mview_id on user_mview (id);
+DROP VIEW user_view CASCADE;
+
+CREATE VIEW user_view AS
+SELECT
+    u.id,
+    u.actor_id,
+    u.name,
+    u.avatar,
+    u.email,
+    u.matrix_user_id,
+    u.bio,
+    u.local,
+    u.admin,
+    u.banned,
+    u.show_avatars,
+    u.send_notifications_to_email,
+    u.published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
+
+CREATE MATERIALIZED VIEW user_mview AS
+SELECT
+    *
+FROM
+    user_view;
+
+CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
 
 -- community_view
-drop view community_aggregates_view cascade;
-create view community_aggregates_view as
+DROP VIEW community_aggregates_view CASCADE;
+
+CREATE VIEW community_aggregates_view AS
 -- Now that there's public and private keys, you have to be explicit here
-select c.id,
-c.name,
-c.title,
-c.description,
-c.category_id,
-c.creator_id,
-c.removed,
-c.published,
-c.updated,
-c.deleted,
-c.nsfw,
-c.actor_id,
-c.local,
-c.last_refreshed_at,
-(select actor_id from user_ u where c.creator_id = u.id) as creator_actor_id,
-(select local from user_ u where c.creator_id = u.id) as creator_local,
-(select name from user_ u where c.creator_id = u.id) as creator_name,
-(select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
-(select name from category ct where c.category_id = ct.id) as category_name,
-(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-(select count(*) from post p where p.community_id = c.id) as number_of_posts,
-(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
-hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
-from community c;
-
-create materialized view community_aggregates_mview as select * from community_aggregates_view;
-
-create unique index idx_community_aggregates_mview_id on community_aggregates_mview (id);
-
-create view community_view as
-with all_community as
-(
-  select
-  ca.*
-  from community_aggregates_view ca
+SELECT
+    c.id,
+    c.name,
+    c.title,
+    c.description,
+    c.category_id,
+    c.creator_id,
+    c.removed,
+    c.published,
+    c.updated,
+    c.deleted,
+    c.nsfw,
+    c.actor_id,
+    c.local,
+    c.last_refreshed_at,
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS creator_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS creator_local,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS creator_avatar,
+    (
+        SELECT
+            name
+        FROM
+            category ct
+        WHERE
+            c.category_id = ct.id) AS category_name,
+    (
+        SELECT
+            count(*)
+        FROM
+            community_follower cf
+        WHERE
+            cf.community_id = c.id) AS number_of_subscribers,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.community_id = c.id) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment co,
+            post p
+        WHERE
+            c.id = p.community_id
+            AND p.id = co.post_id) AS number_of_comments,
+    hot_rank ((
+        SELECT
+            count(*)
+        FROM community_follower cf
+        WHERE
+            cf.community_id = c.id), c.published) AS hot_rank
+FROM
+    community c;
+
+CREATE MATERIALIZED VIEW community_aggregates_mview AS
+SELECT
+    *
+FROM
+    community_aggregates_view;
+
+CREATE UNIQUE INDEX idx_community_aggregates_mview_id ON community_aggregates_mview (id);
+
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        ca.*
+    FROM
+        community_aggregates_view ca
 )
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
-
-union all
-
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
-
-create view community_mview as
-with all_community as
-(
-  select
-  ca.*
-  from community_aggregates_mview ca
+CREATE VIEW community_mview AS
+with all_community AS (
+    SELECT
+        ca.*
+    FROM
+        community_aggregates_mview ca
 )
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
+-- community views
+DROP VIEW community_moderator_view;
 
-union all
+DROP VIEW community_follower_view;
 
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
+DROP VIEW community_user_ban_view;
 
--- community views
-drop view community_moderator_view;
-drop view community_follower_view;
-drop view community_user_ban_view;
-
-create view community_moderator_view as 
-select *,
-(select actor_id from user_ u where cm.user_id = u.id) as user_actor_id,
-(select local from user_ u where cm.user_id = u.id) as user_local,
-(select name from user_ u where cm.user_id = u.id) as user_name,
-(select avatar from user_ u where cm.user_id = u.id),
-(select actor_id from community c where cm.community_id = c.id) as community_actor_id,
-(select local from community c where cm.community_id = c.id) as community_local,
-(select name from community c where cm.community_id = c.id) as community_name
-from community_moderator cm;
-
-create view community_follower_view as 
-select *,
-(select actor_id from user_ u where cf.user_id = u.id) as user_actor_id,
-(select local from user_ u where cf.user_id = u.id) as user_local,
-(select name from user_ u where cf.user_id = u.id) as user_name,
-(select avatar from user_ u where cf.user_id = u.id),
-(select actor_id from community c where cf.community_id = c.id) as community_actor_id,
-(select local from community c where cf.community_id = c.id) as community_local,
-(select name from community c where cf.community_id = c.id) as community_name
-from community_follower cf;
-
-create view community_user_ban_view as 
-select *,
-(select actor_id from user_ u where cm.user_id = u.id) as user_actor_id,
-(select local from user_ u where cm.user_id = u.id) as user_local,
-(select name from user_ u where cm.user_id = u.id) as user_name,
-(select avatar from user_ u where cm.user_id = u.id),
-(select actor_id from community c where cm.community_id = c.id) as community_actor_id,
-(select local from community c where cm.community_id = c.id) as community_local,
-(select name from community c where cm.community_id = c.id) as community_name
-from community_user_ban cm;
+CREATE VIEW community_moderator_view AS
+SELECT
+    *,
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_local,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id), (
+        SELECT
+            actor_id
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_local,
+    (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_name
+FROM
+    community_moderator cm;
+
+CREATE VIEW community_follower_view AS
+SELECT
+    *,
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            cf.user_id = u.id) AS user_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            cf.user_id = u.id) AS user_local,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cf.user_id = u.id) AS user_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            cf.user_id = u.id), (
+        SELECT
+            actor_id
+        FROM
+            community c
+        WHERE
+            cf.community_id = c.id) AS community_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            community c
+        WHERE
+            cf.community_id = c.id) AS community_local,
+    (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cf.community_id = c.id) AS community_name
+FROM
+    community_follower cf;
+
+CREATE VIEW community_user_ban_view AS
+SELECT
+    *,
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_local,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id) AS user_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            cm.user_id = u.id), (
+        SELECT
+            actor_id
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_local,
+    (
+        SELECT
+            name
+        FROM
+            community c
+        WHERE
+            cm.community_id = c.id) AS community_name
+FROM
+    community_user_ban cm;
 
 -- post_view
-drop view post_view;
-drop view post_mview;
-drop materialized view post_aggregates_mview;
-drop view post_aggregates_view;
+DROP VIEW post_view;
+
+DROP VIEW post_mview;
+
+DROP MATERIALIZED VIEW post_aggregates_mview;
+
+DROP VIEW post_aggregates_view;
 
 -- regen post view
-create view post_aggregates_view as
-select        
-p.*,
-(select u.banned from user_ u where p.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-(select actor_id from user_ where p.creator_id = user_.id) as creator_actor_id,
-(select local from user_ where p.creator_id = user_.id) as creator_local,
-(select name from user_ where p.creator_id = user_.id) as creator_name,
-(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
-(select actor_id from community where p.community_id = community.id) as community_actor_id,
-(select local from community where p.community_id = community.id) as community_local,
-(select name from community where p.community_id = community.id) as community_name,
-(select removed from community c where p.community_id = c.id) as community_removed,
-(select deleted from community c where p.community_id = c.id) as community_deleted,
-(select nsfw from community c where p.community_id = c.id) as community_nsfw,
-(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-coalesce(sum(pl.score), 0) as score,
-count (case when pl.score = 1 then 1 else null end) as upvotes,
-count (case when pl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(pl.score) , 0), 
-  (
-    case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
-    else greatest(c.recent_comment_time, p.published)
-    end
-  )
-) as hot_rank,
-(
-  case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
-  else greatest(c.recent_comment_time, p.published)
-  end
-) as newest_activity_time
-from post p
-left join post_like pl on p.id = pl.post_id
-left join (
-  select post_id, 
-  max(published) as recent_comment_time
-  from comment
-  group by 1
-) c on p.id = c.post_id
-group by p.id, c.recent_comment_time;
-
-create materialized view post_aggregates_mview as select * from post_aggregates_view;
-
-create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
-
-create view post_view as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_view pa
-)
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
-
-create view post_mview as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_mview pa
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            p.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb
+        WHERE
+            p.creator_id = cb.user_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            actor_id
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_local,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_avatar,
+    (
+        SELECT
+            actor_id
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_local,
+    (
+        SELECT
+            name
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_name,
+    (
+        SELECT
+            removed
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_removed,
+    (
+        SELECT
+            deleted
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_deleted,
+    (
+        SELECT
+            nsfw
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_nsfw,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment
+        WHERE
+            comment.post_id = p.id) AS number_of_comments,
+    coalesce(sum(pl.score), 0) AS score,
+    count(
+        CASE WHEN pl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN pl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(pl.score), 0), (
+            CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+                p.published -- Prevents necro-bumps
+            ELSE
+                greatest (c.recent_comment_time, p.published)
+            END)) AS hot_rank,
+    (
+        CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+            p.published -- Prevents necro-bumps
+        ELSE
+            greatest (c.recent_comment_time, p.published)
+        END) AS newest_activity_time
+FROM
+    post p
+    LEFT JOIN post_like pl ON p.id = pl.post_id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            max(published) AS recent_comment_time
+        FROM
+            comment
+        GROUP BY
+            1) c ON p.id = c.post_id
+GROUP BY
+    p.id,
+    c.recent_comment_time;
+
+CREATE MATERIALIZED VIEW post_aggregates_mview AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
+
+CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
+
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_view pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
+CREATE VIEW post_mview AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_mview pa
+)
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
 
 -- reply_view, comment_view, user_mention
-drop view reply_view;
-drop view user_mention_view;
-drop view user_mention_mview;
-drop view comment_view;
-drop view comment_mview;
-drop materialized view comment_aggregates_mview;
-drop view comment_aggregates_view;
+DROP VIEW reply_view;
 
--- reply and comment view
-create view comment_aggregates_view as
-select        
-c.*,
-(select community_id from post p where p.id = c.post_id),
-(select co.actor_id from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_actor_id,
-(select co.local from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_local,
-(select co.name from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_name,
-(select u.banned from user_ u where c.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
-(select actor_id from user_ where c.creator_id = user_.id) as creator_actor_id,
-(select local from user_ where c.creator_id = user_.id) as creator_local,
-(select name from user_ where c.creator_id = user_.id) as creator_name,
-(select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
-coalesce(sum(cl.score), 0) as score,
-count (case when cl.score = 1 then 1 else null end) as upvotes,
-count (case when cl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(cl.score) , 0), c.published) as hot_rank
-from comment c
-left join comment_like cl on c.id = cl.comment_id
-group by c.id;
-
-create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
-
-create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
-
-create view comment_view as
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_view ca
-)
+DROP VIEW user_mention_view;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
+DROP VIEW user_mention_mview;
 
-union all
+DROP VIEW comment_view;
 
-select 
-    ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from all_comment ac
-;
-
-create view comment_mview as
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_mview ca
-)
+DROP VIEW comment_mview;
+
+DROP MATERIALIZED VIEW comment_aggregates_mview;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
+DROP VIEW comment_aggregates_view;
 
-union all
+-- reply and comment view
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    c.*,
+    (
+        SELECT
+            community_id
+        FROM
+            post p
+        WHERE
+            p.id = c.post_id), (
+        SELECT
+            co.actor_id
+        FROM
+            post p,
+            community co
+        WHERE
+            p.id = c.post_id
+            AND p.community_id = co.id) AS community_actor_id,
+    (
+        SELECT
+            co.local
+        FROM
+            post p,
+            community co
+        WHERE
+            p.id = c.post_id
+            AND p.community_id = co.id) AS community_local,
+    (
+        SELECT
+            co.name
+        FROM
+            post p,
+            community co
+        WHERE
+            p.id = c.post_id
+            AND p.community_id = co.id) AS community_name,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb,
+            post p
+        WHERE
+            c.creator_id = cb.user_id
+            AND p.id = c.post_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            actor_id
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_local,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_avatar,
+    coalesce(sum(cl.score), 0) AS score,
+    count(
+        CASE WHEN cl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN cl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(cl.score), 0), c.published) AS hot_rank
+FROM
+    comment c
+    LEFT JOIN comment_like cl ON c.id = cl.comment_id
+GROUP BY
+    c.id;
 
-select 
+CREATE MATERIALIZED VIEW comment_aggregates_mview AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
+
+CREATE UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id);
+
+CREATE VIEW comment_view AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_view ca
+)
+SELECT
+    ac.*,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.community_id = cf.community_id) AS subscribed,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS saved
+    FROM
+        all_comment ac;
+
+CREATE VIEW comment_mview AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_mview ca
+)
+SELECT
     ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from all_comment ac
-;
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.community_id = cf.community_id) AS subscribed,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS saved
+    FROM
+        all_comment ac;
 
 -- Do the reply_view referencing the comment_mview
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_mview cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_mview cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- user mention
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.creator_actor_id,
     c.creator_local,
@@ -406,23 +912,36 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_mention um, comment_view c
-where um.comment_id = c.id;
-
-
-create view user_mention_mview as 
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_mview ca
-)
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
 
-select
+CREATE VIEW user_mention_mview AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_mview ca
+)
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -446,22 +965,41 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select 
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -485,13 +1023,25 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    null as user_id, 
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from all_comment ac
-left join user_mention um on um.comment_id = ac.id
-;
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    all_comment ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
 
index a172581a111f058623aef1a15efb95b80da91028..c812894188a178ce32cc2f29499a8cdb43b17564 100644 (file)
@@ -1,4 +1,5 @@
 -- The username index
-drop index idx_user_name_lower_actor_id;
-create unique index idx_user_name_lower on user_ (lower(name));
+DROP INDEX idx_user_name_lower_actor_id;
+
+CREATE UNIQUE INDEX idx_user_name_lower ON user_ (lower(name));
 
index 969eab0b6353af76c9169c99b4493c68f99b3504..5843fa28199fb3625be1db54dc6b15540157e9fe 100644 (file)
@@ -1,2 +1,4 @@
-drop index idx_user_name_lower;
-create unique index idx_user_name_lower_actor_id on user_ (lower(name), lower(actor_id));
+DROP INDEX idx_user_name_lower;
+
+CREATE UNIQUE INDEX idx_user_name_lower_actor_id ON user_ (lower(name), lower(actor_id));
+
index 15c92859240d2a5c7c8e9ca57c5ab419dd6d6a38..76c74f6ea0b6e660f7ddc1b5d08634b1b4f01e32 100644 (file)
@@ -1,21 +1,28 @@
-drop materialized view private_message_mview;
-drop view private_message_view;
+DROP MATERIALIZED VIEW private_message_mview;
 
-alter table private_message 
-drop column ap_id, 
-drop column local;
+DROP VIEW private_message_view;
 
-create view private_message_view as 
-select        
-pm.*,
-u.name as creator_name,
-u.avatar as creator_avatar,
-u2.name as recipient_name,
-u2.avatar as recipient_avatar
-from private_message pm
-inner join user_ u on u.id = pm.creator_id
-inner join user_ u2 on u2.id = pm.recipient_id;
+ALTER TABLE private_message
+    DROP COLUMN ap_id,
+    DROP COLUMN local;
 
-create materialized view private_message_mview as select * from private_message_view;
+CREATE VIEW private_message_view AS
+SELECT
+    pm.*,
+    u.name AS creator_name,
+    u.avatar AS creator_avatar,
+    u2.name AS recipient_name,
+    u2.avatar AS recipient_avatar
+FROM
+    private_message pm
+    INNER JOIN user_ u ON u.id = pm.creator_id
+    INNER JOIN user_ u2 ON u2.id = pm.recipient_id;
+
+CREATE MATERIALIZED VIEW private_message_mview AS
+SELECT
+    *
+FROM
+    private_message_view;
+
+CREATE UNIQUE INDEX idx_private_message_mview_id ON private_message_mview (id);
 
-create unique index idx_private_message_mview_id on private_message_mview (id);
index 9f152ada64e115acfe7f81cc27dd552714bfab33..6935d33cbec03b3d01006a5bc9dc35876bf9b769 100644 (file)
@@ -1,25 +1,32 @@
-alter table private_message
-add column ap_id character varying(255) not null default 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
-add column local boolean not null default true
-;
+ALTER TABLE private_message
+    ADD COLUMN ap_id character varying(255) NOT NULL DEFAULT 'http://fake.com', -- This needs to be checked and updated in code, building from the site url if local
+    ADD COLUMN local boolean NOT NULL DEFAULT TRUE;
 
-drop materialized view private_message_mview;
-drop view private_message_view;
-create view private_message_view as 
-select        
-pm.*,
-u.name as creator_name,
-u.avatar as creator_avatar,
-u.actor_id as creator_actor_id,
-u.local as creator_local,
-u2.name as recipient_name,
-u2.avatar as recipient_avatar,
-u2.actor_id as recipient_actor_id,
-u2.local as recipient_local
-from private_message pm
-inner join user_ u on u.id = pm.creator_id
-inner join user_ u2 on u2.id = pm.recipient_id;
+DROP MATERIALIZED VIEW private_message_mview;
 
-create materialized view private_message_mview as select * from private_message_view;
+DROP VIEW private_message_view;
+
+CREATE VIEW private_message_view AS
+SELECT
+    pm.*,
+    u.name AS creator_name,
+    u.avatar AS creator_avatar,
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u2.name AS recipient_name,
+    u2.avatar AS recipient_avatar,
+    u2.actor_id AS recipient_actor_id,
+    u2.local AS recipient_local
+FROM
+    private_message pm
+    INNER JOIN user_ u ON u.id = pm.creator_id
+    INNER JOIN user_ u2 ON u2.id = pm.recipient_id;
+
+CREATE MATERIALIZED VIEW private_message_mview AS
+SELECT
+    *
+FROM
+    private_message_view;
+
+CREATE UNIQUE INDEX idx_private_message_mview_id ON private_message_mview (id);
 
-create unique index idx_private_message_mview_id on private_message_mview (id);
index 5f72b76d18ff2f05526ed0cf501aec7d8478c154..ea82e57b4d939ede4579f4d1d670d1a87f225dfa 100644 (file)
 -- Dropping all the fast tables
-drop table user_fast;
-drop view post_fast_view;
-drop table post_aggregates_fast;
-drop view community_fast_view;
-drop table community_aggregates_fast;
-drop view reply_fast_view;
-drop view user_mention_fast_view;
-drop view comment_fast_view;
-drop table comment_aggregates_fast;
+DROP TABLE user_fast;
 
--- Re-adding all the triggers, functions, and mviews
+DROP VIEW post_fast_view;
 
--- private message
-create materialized view private_message_mview as select * from private_message_view;
+DROP TABLE post_aggregates_fast;
+
+DROP VIEW community_fast_view;
+
+DROP TABLE community_aggregates_fast;
+
+DROP VIEW reply_fast_view;
 
-create unique index idx_private_message_mview_id on private_message_mview (id);
+DROP VIEW user_mention_fast_view;
+
+DROP VIEW comment_fast_view;
+
+DROP TABLE comment_aggregates_fast;
+
+-- Re-adding all the triggers, functions, and mviews
+-- private message
+CREATE MATERIALIZED VIEW private_message_mview AS
+SELECT
+    *
+FROM
+    private_message_view;
 
+CREATE UNIQUE INDEX idx_private_message_mview_id ON private_message_mview (id);
 
 -- Create the triggers
-create or replace function refresh_private_message()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently private_message_mview;
-  return null;
-end $$;
-
-create trigger refresh_private_message
-after insert or update or delete or truncate
-on private_message
-for each statement
-execute procedure refresh_private_message();
-
--- user 
-create or replace function refresh_user()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently user_mview;
-  refresh materialized view concurrently comment_aggregates_mview; -- cause of bans
-  refresh materialized view concurrently post_aggregates_mview;
-  return null;
-end $$;
-
-drop trigger refresh_user on user_;
-create trigger refresh_user
-after insert or update or delete or truncate
-on user_
-for each statement
-execute procedure refresh_user();
-drop view user_view cascade;
-
-create view user_view as 
-select 
-u.id,
-u.actor_id,
-u.name,
-u.avatar,
-u.email,
-u.matrix_user_id,
-u.bio,
-u.local,
-u.admin,
-u.banned,
-u.show_avatars,
-u.send_notifications_to_email,
-u.published,
-(select count(*) from post p where p.creator_id = u.id) as number_of_posts,
-(select coalesce(sum(score), 0) from post p, post_like pl where u.id = p.creator_id and p.id = pl.post_id) as post_score,
-(select count(*) from comment c where c.creator_id = u.id) as number_of_comments,
-(select coalesce(sum(score), 0) from comment c, comment_like cl where u.id = c.creator_id and c.id = cl.comment_id) as comment_score
-from user_ u;
-
-create materialized view user_mview as select * from user_view;
-
-create unique index idx_user_mview_id on user_mview (id);
+CREATE OR REPLACE FUNCTION refresh_private_message ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY private_message_mview;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER refresh_private_message
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON private_message
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_private_message ();
+
+-- user
+CREATE OR REPLACE FUNCTION refresh_user ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY user_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY comment_aggregates_mview;
+    -- cause of bans
+    REFRESH MATERIALIZED VIEW CONCURRENTLY post_aggregates_mview;
+    RETURN NULL;
+END
+$$;
+
+DROP TRIGGER refresh_user ON user_;
+
+CREATE TRIGGER refresh_user
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON user_
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_user ();
+
+DROP VIEW user_view CASCADE;
+
+CREATE VIEW user_view AS
+SELECT
+    u.id,
+    u.actor_id,
+    u.name,
+    u.avatar,
+    u.email,
+    u.matrix_user_id,
+    u.bio,
+    u.local,
+    u.admin,
+    u.banned,
+    u.show_avatars,
+    u.send_notifications_to_email,
+    u.published,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.creator_id = u.id) AS number_of_posts,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            post p,
+            post_like pl
+        WHERE
+            u.id = p.creator_id
+            AND p.id = pl.post_id) AS post_score,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment c
+        WHERE
+            c.creator_id = u.id) AS number_of_comments,
+    (
+        SELECT
+            coalesce(sum(score), 0)
+        FROM
+            comment c,
+            comment_like cl
+        WHERE
+            u.id = c.creator_id
+            AND c.id = cl.comment_id) AS comment_score
+FROM
+    user_ u;
+
+CREATE MATERIALIZED VIEW user_mview AS
+SELECT
+    *
+FROM
+    user_view;
+
+CREATE UNIQUE INDEX idx_user_mview_id ON user_mview (id);
 
 -- community
-drop trigger refresh_community on community;
-create trigger refresh_community
-after insert or update or delete or truncate
-on community
-for each statement
-execute procedure refresh_community();
-
-create or replace function refresh_community()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently post_aggregates_mview;
-  refresh materialized view concurrently community_aggregates_mview; 
-  refresh materialized view concurrently user_mview;
-  return null;
-end $$;
-
-drop view community_aggregates_view cascade;
-create view community_aggregates_view as
+DROP TRIGGER refresh_community ON community;
+
+CREATE TRIGGER refresh_community
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON community
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_community ();
+
+CREATE OR REPLACE FUNCTION refresh_community ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY post_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY community_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY user_mview;
+    RETURN NULL;
+END
+$$;
+
+DROP VIEW community_aggregates_view CASCADE;
+
+CREATE VIEW community_aggregates_view AS
 -- Now that there's public and private keys, you have to be explicit here
-select c.id,
-c.name,
-c.title,
-c.description,
-c.category_id,
-c.creator_id,
-c.removed,
-c.published,
-c.updated,
-c.deleted,
-c.nsfw,
-c.actor_id,
-c.local,
-c.last_refreshed_at,
-(select actor_id from user_ u where c.creator_id = u.id) as creator_actor_id,
-(select local from user_ u where c.creator_id = u.id) as creator_local,
-(select name from user_ u where c.creator_id = u.id) as creator_name,
-(select avatar from user_ u where c.creator_id = u.id) as creator_avatar,
-(select name from category ct where c.category_id = ct.id) as category_name,
-(select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers,
-(select count(*) from post p where p.community_id = c.id) as number_of_posts,
-(select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments,
-hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank
-from community c;
-
-create materialized view community_aggregates_mview as select * from community_aggregates_view;
-
-create unique index idx_community_aggregates_mview_id on community_aggregates_mview (id);
-
-create view community_view as
-with all_community as
-(
-  select
-  ca.*
-  from community_aggregates_view ca
+SELECT
+    c.id,
+    c.name,
+    c.title,
+    c.description,
+    c.category_id,
+    c.creator_id,
+    c.removed,
+    c.published,
+    c.updated,
+    c.deleted,
+    c.nsfw,
+    c.actor_id,
+    c.local,
+    c.last_refreshed_at,
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS creator_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS creator_local,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS creator_avatar,
+    (
+        SELECT
+            name
+        FROM
+            category ct
+        WHERE
+            c.category_id = ct.id) AS category_name,
+    (
+        SELECT
+            count(*)
+        FROM
+            community_follower cf
+        WHERE
+            cf.community_id = c.id) AS number_of_subscribers,
+    (
+        SELECT
+            count(*)
+        FROM
+            post p
+        WHERE
+            p.community_id = c.id) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment co,
+            post p
+        WHERE
+            c.id = p.community_id
+            AND p.id = co.post_id) AS number_of_comments,
+    hot_rank ((
+        SELECT
+            count(*)
+        FROM community_follower cf
+        WHERE
+            cf.community_id = c.id), c.published) AS hot_rank
+FROM
+    community c;
+
+CREATE MATERIALIZED VIEW community_aggregates_mview AS
+SELECT
+    *
+FROM
+    community_aggregates_view;
+
+CREATE UNIQUE INDEX idx_community_aggregates_mview_id ON community_aggregates_mview (id);
+
+CREATE VIEW community_view AS
+with all_community AS (
+    SELECT
+        ca.*
+    FROM
+        community_aggregates_view ca
 )
-
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
-
-union all
-
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
-
-create view community_mview as
-with all_community as
-(
-  select
-  ca.*
-  from community_aggregates_mview ca
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
+
+CREATE VIEW community_mview AS
+with all_community AS (
+    SELECT
+        ca.*
+    FROM
+        community_aggregates_mview ca
 )
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN all_community ac
+UNION ALL
+SELECT
+    ac.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    all_community ac;
 
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join all_community ac
-
-union all
-
-select 
-ac.*,
-null as user_id,
-null as subscribed
-from all_community ac
-;
 -- Post
-drop view post_view;
-drop view post_aggregates_view;
+DROP VIEW post_view;
+
+DROP VIEW post_aggregates_view;
 
 -- regen post view
-create view post_aggregates_view as
-select        
-p.*,
-(select u.banned from user_ u where p.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb where p.creator_id = cb.user_id and p.community_id = cb.community_id) as banned_from_community,
-(select actor_id from user_ where p.creator_id = user_.id) as creator_actor_id,
-(select local from user_ where p.creator_id = user_.id) as creator_local,
-(select name from user_ where p.creator_id = user_.id) as creator_name,
-(select avatar from user_ where p.creator_id = user_.id) as creator_avatar,
-(select actor_id from community where p.community_id = community.id) as community_actor_id,
-(select local from community where p.community_id = community.id) as community_local,
-(select name from community where p.community_id = community.id) as community_name,
-(select removed from community c where p.community_id = c.id) as community_removed,
-(select deleted from community c where p.community_id = c.id) as community_deleted,
-(select nsfw from community c where p.community_id = c.id) as community_nsfw,
-(select count(*) from comment where comment.post_id = p.id) as number_of_comments,
-coalesce(sum(pl.score), 0) as score,
-count (case when pl.score = 1 then 1 else null end) as upvotes,
-count (case when pl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(pl.score) , 0), 
-  (
-    case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
-    else greatest(c.recent_comment_time, p.published)
-    end
-  )
-) as hot_rank,
-(
-  case when (p.published < ('now'::timestamp - '1 month'::interval)) then p.published -- Prevents necro-bumps
-  else greatest(c.recent_comment_time, p.published)
-  end
-) as newest_activity_time
-from post p
-left join post_like pl on p.id = pl.post_id
-left join (
-  select post_id, 
-  max(published) as recent_comment_time
-  from comment
-  group by 1
-) c on p.id = c.post_id
-group by p.id, c.recent_comment_time;
-
-create materialized view post_aggregates_mview as select * from post_aggregates_view;
-
-create unique index idx_post_aggregates_mview_id on post_aggregates_mview (id);
-
-create view post_view as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_view pa
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            p.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb
+        WHERE
+            p.creator_id = cb.user_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            actor_id
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_local,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            p.creator_id = user_.id) AS creator_avatar,
+    (
+        SELECT
+            actor_id
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_local,
+    (
+        SELECT
+            name
+        FROM
+            community
+        WHERE
+            p.community_id = community.id) AS community_name,
+    (
+        SELECT
+            removed
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_removed,
+    (
+        SELECT
+            deleted
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_deleted,
+    (
+        SELECT
+            nsfw
+        FROM
+            community c
+        WHERE
+            p.community_id = c.id) AS community_nsfw,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment
+        WHERE
+            comment.post_id = p.id) AS number_of_comments,
+    coalesce(sum(pl.score), 0) AS score,
+    count(
+        CASE WHEN pl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN pl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(pl.score), 0), (
+            CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+                p.published -- Prevents necro-bumps
+            ELSE
+                greatest (c.recent_comment_time, p.published)
+            END)) AS hot_rank,
+    (
+        CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+            p.published -- Prevents necro-bumps
+        ELSE
+            greatest (c.recent_comment_time, p.published)
+        END) AS newest_activity_time
+FROM
+    post p
+    LEFT JOIN post_like pl ON p.id = pl.post_id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            max(published) AS recent_comment_time
+        FROM
+            comment
+        GROUP BY
+            1) c ON p.id = c.post_id
+GROUP BY
+    p.id,
+    c.recent_comment_time;
+
+CREATE MATERIALIZED VIEW post_aggregates_mview AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
+
+CREATE UNIQUE INDEX idx_post_aggregates_mview_id ON post_aggregates_mview (id);
+
+CREATE VIEW post_view AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_view pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
-
-create view post_mview as 
-with all_post as (
-  select
-  pa.*
-  from post_aggregates_mview pa
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
+
+CREATE VIEW post_mview AS
+with all_post AS (
+    SELECT
+        pa.*
+    FROM
+        post_aggregates_mview pa
 )
-select
-ap.*,
-u.id as user_id,
-coalesce(pl.score, 0) as my_vote,
-(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed,
-(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read,
-(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved
-from user_ u
-cross join all_post ap
-left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id
-
-union all
-
-select 
-ap.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from all_post ap
-;
-
-drop trigger refresh_post on post;
-create trigger refresh_post
-after insert or update or delete or truncate
-on post
-for each statement
-execute procedure refresh_post();
-
-create or replace function refresh_post()
-returns trigger language plpgsql
-as $$
-begin
-  refresh materialized view concurrently post_aggregates_mview;
-  refresh materialized view concurrently user_mview;
-  return null;
-end $$;
-
+SELECT
+    ap.*,
+    u.id AS user_id,
+    coalesce(pl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::bool
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND cf.community_id = ap.community_id) AS subscribed,
+    (
+        SELECT
+            pr.id::bool
+        FROM
+            post_read pr
+        WHERE
+            u.id = pr.user_id
+            AND pr.post_id = ap.id) AS read,
+    (
+        SELECT
+            ps.id::bool
+        FROM
+            post_saved ps
+        WHERE
+            u.id = ps.user_id
+            AND ps.post_id = ap.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_post ap
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND ap.id = pl.post_id
+    UNION ALL
+    SELECT
+        ap.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS read,
+        NULL AS saved
+    FROM
+        all_post ap;
+
+DROP TRIGGER refresh_post ON post;
+
+CREATE TRIGGER refresh_post
+    AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON post
+    FOR EACH statement
+    EXECUTE PROCEDURE refresh_post ();
+
+CREATE OR REPLACE FUNCTION refresh_post ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    REFRESH MATERIALIZED VIEW CONCURRENTLY post_aggregates_mview;
+    REFRESH MATERIALIZED VIEW CONCURRENTLY user_mview;
+    RETURN NULL;
+END
+$$;
 
 -- User mention, comment, reply
-drop view user_mention_view;
-drop view comment_view;
-drop view comment_aggregates_view;
+DROP VIEW user_mention_view;
 
--- reply and comment view
-create view comment_aggregates_view as
-select        
-c.*,
-(select community_id from post p where p.id = c.post_id),
-(select co.actor_id from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_actor_id,
-(select co.local from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_local,
-(select co.name from post p, community co where p.id = c.post_id and p.community_id = co.id) as community_name,
-(select u.banned from user_ u where c.creator_id = u.id) as banned,
-(select cb.id::bool from community_user_ban cb, post p where c.creator_id = cb.user_id and p.id = c.post_id and p.community_id = cb.community_id) as banned_from_community,
-(select actor_id from user_ where c.creator_id = user_.id) as creator_actor_id,
-(select local from user_ where c.creator_id = user_.id) as creator_local,
-(select name from user_ where c.creator_id = user_.id) as creator_name,
-(select avatar from user_ where c.creator_id = user_.id) as creator_avatar,
-coalesce(sum(cl.score), 0) as score,
-count (case when cl.score = 1 then 1 else null end) as upvotes,
-count (case when cl.score = -1 then 1 else null end) as downvotes,
-hot_rank(coalesce(sum(cl.score) , 0), c.published) as hot_rank
-from comment c
-left join comment_like cl on c.id = cl.comment_id
-group by c.id;
-
-create materialized view comment_aggregates_mview as select * from comment_aggregates_view;
-
-create unique index idx_comment_aggregates_mview_id on comment_aggregates_mview (id);
-
-create view comment_view as
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_view ca
-)
+DROP VIEW comment_view;
 
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
+DROP VIEW comment_aggregates_view;
 
-union all
-
-select 
+-- reply and comment view
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    c.*,
+    (
+        SELECT
+            community_id
+        FROM
+            post p
+        WHERE
+            p.id = c.post_id), (
+        SELECT
+            co.actor_id
+        FROM
+            post p,
+            community co
+        WHERE
+            p.id = c.post_id
+            AND p.community_id = co.id) AS community_actor_id,
+    (
+        SELECT
+            co.local
+        FROM
+            post p,
+            community co
+        WHERE
+            p.id = c.post_id
+            AND p.community_id = co.id) AS community_local,
+    (
+        SELECT
+            co.name
+        FROM
+            post p,
+            community co
+        WHERE
+            p.id = c.post_id
+            AND p.community_id = co.id) AS community_name,
+    (
+        SELECT
+            u.banned
+        FROM
+            user_ u
+        WHERE
+            c.creator_id = u.id) AS banned,
+    (
+        SELECT
+            cb.id::bool
+        FROM
+            community_user_ban cb,
+            post p
+        WHERE
+            c.creator_id = cb.user_id
+            AND p.id = c.post_id
+            AND p.community_id = cb.community_id) AS banned_from_community,
+    (
+        SELECT
+            actor_id
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_local,
+    (
+        SELECT
+            name
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_
+        WHERE
+            c.creator_id = user_.id) AS creator_avatar,
+    coalesce(sum(cl.score), 0) AS score,
+    count(
+        CASE WHEN cl.score = 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS upvotes,
+    count(
+        CASE WHEN cl.score = - 1 THEN
+            1
+        ELSE
+            NULL
+        END) AS downvotes,
+    hot_rank (coalesce(sum(cl.score), 0), c.published) AS hot_rank
+FROM
+    comment c
+    LEFT JOIN comment_like cl ON c.id = cl.comment_id
+GROUP BY
+    c.id;
+
+CREATE MATERIALIZED VIEW comment_aggregates_mview AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
+
+CREATE UNIQUE INDEX idx_comment_aggregates_mview_id ON comment_aggregates_mview (id);
+
+CREATE VIEW comment_view AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_view ca
+)
+SELECT
     ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from all_comment ac
-;
-
-create view comment_mview as
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_mview ca
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.community_id = cf.community_id) AS subscribed,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS saved
+    FROM
+        all_comment ac;
+
+CREATE VIEW comment_mview AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_mview ca
 )
-
-select
-ac.*,
-u.id as user_id,
-coalesce(cl.score, 0) as my_vote,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.community_id = cf.community_id) as subscribed,
-(select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-
-union all
-
-select 
+SELECT
     ac.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from all_comment ac
-;
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.community_id = cf.community_id) AS subscribed,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    UNION ALL
+    SELECT
+        ac.*,
+        NULL AS user_id,
+        NULL AS my_vote,
+        NULL AS subscribed,
+        NULL AS saved
+    FROM
+        all_comment ac;
 
 -- Do the reply_view referencing the comment_mview
-create view reply_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_mview cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_mview cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- user mention
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.creator_actor_id,
     c.creator_local,
@@ -444,23 +833,36 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_mention um, comment_view c
-where um.comment_id = c.id;
-
-
-create view user_mention_mview as 
-with all_comment as
-(
-  select
-  ca.*
-  from comment_aggregates_mview ca
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
+
+CREATE VIEW user_mention_mview AS
+with all_comment AS (
+    SELECT
+        ca.*
+    FROM
+        comment_aggregates_mview ca
 )
-
-select
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -484,22 +886,41 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_ u
-cross join all_comment ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select 
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_ u
+    CROSS JOIN all_comment ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -523,13 +944,25 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    null as user_id, 
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from all_comment ac
-left join user_mention um on um.comment_id = ac.id
-;
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    all_comment ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
 
index bd792a8b9d99c06c8c72a07f9454c235bad9415e..0c633597ed0fbe81328222afab14eda129b00f09 100644 (file)
 -- Drop the mviews
-drop view post_mview;
-drop materialized view user_mview;
-drop view community_mview;
-drop materialized view private_message_mview;
-drop view user_mention_mview;
-drop view reply_view;
-drop view comment_mview;
-drop materialized view post_aggregates_mview;
-drop materialized view community_aggregates_mview;
-drop materialized view comment_aggregates_mview;
-drop trigger refresh_private_message on private_message;
+DROP VIEW post_mview;
+
+DROP MATERIALIZED VIEW user_mview;
+
+DROP VIEW community_mview;
+
+DROP MATERIALIZED VIEW private_message_mview;
+
+DROP VIEW user_mention_mview;
+
+DROP VIEW reply_view;
+
+DROP VIEW comment_mview;
+
+DROP MATERIALIZED VIEW post_aggregates_mview;
+
+DROP MATERIALIZED VIEW community_aggregates_mview;
+
+DROP MATERIALIZED VIEW comment_aggregates_mview;
+
+DROP TRIGGER refresh_private_message ON private_message;
 
 -- User
-drop view user_view;
-create view user_view as
-select 
-       u.id,
-  u.actor_id,
-       u.name,
-       u.avatar,
-       u.email,
-       u.matrix_user_id,
-  u.bio,
-  u.local,
-       u.admin,
-       u.banned,
-       u.show_avatars,
-       u.send_notifications_to_email,
-       u.published,
-       coalesce(pd.posts, 0) as number_of_posts,
-       coalesce(pd.score, 0) as post_score,
-       coalesce(cd.comments, 0) as number_of_comments,
-       coalesce(cd.score, 0) as comment_score
-from user_ u
-left join (
-    select
-        p.creator_id as creator_id,
-        count(distinct p.id) as posts,
-        sum(pl.score) as score
-    from post p
-    join post_like pl on p.id = pl.post_id
-    group by p.creator_id
-) pd on u.id = pd.creator_id
-left join (
-    select
-        c.creator_id,
-        count(distinct c.id) as comments,
-        sum(cl.score) as score
-    from comment c
-    join comment_like cl on c.id = cl.comment_id
-    group by c.creator_id
-) cd on u.id = cd.creator_id;
-
-
-create table user_fast as select * from user_view;
-alter table user_fast add primary key (id);
-
-drop trigger refresh_user on user_;
-
-create trigger refresh_user
-after insert or update or delete
-on user_
-for each row
-execute procedure refresh_user();
-
--- Sample insert 
+DROP VIEW user_view;
+
+CREATE VIEW user_view AS
+SELECT
+    u.id,
+    u.actor_id,
+    u.name,
+    u.avatar,
+    u.email,
+    u.matrix_user_id,
+    u.bio,
+    u.local,
+    u.admin,
+    u.banned,
+    u.show_avatars,
+    u.send_notifications_to_email,
+    u.published,
+    coalesce(pd.posts, 0) AS number_of_posts,
+    coalesce(pd.score, 0) AS post_score,
+    coalesce(cd.comments, 0) AS number_of_comments,
+    coalesce(cd.score, 0) AS comment_score
+FROM
+    user_ u
+    LEFT JOIN (
+        SELECT
+            p.creator_id AS creator_id,
+            count(DISTINCT p.id) AS posts,
+            sum(pl.score) AS score
+        FROM
+            post p
+            JOIN post_like pl ON p.id = pl.post_id
+        GROUP BY
+            p.creator_id) pd ON u.id = pd.creator_id
+    LEFT JOIN (
+        SELECT
+            c.creator_id,
+            count(DISTINCT c.id) AS comments,
+            sum(cl.score) AS score
+        FROM
+            comment c
+            JOIN comment_like cl ON c.id = cl.comment_id
+        GROUP BY
+            c.creator_id) cd ON u.id = cd.creator_id;
+
+CREATE TABLE user_fast AS
+SELECT
+    *
+FROM
+    user_view;
+
+ALTER TABLE user_fast
+    ADD PRIMARY KEY (id);
+
+DROP TRIGGER refresh_user ON user_;
+
+CREATE TRIGGER refresh_user
+    AFTER INSERT OR UPDATE OR DELETE ON user_
+    FOR EACH ROW
+    EXECUTE PROCEDURE refresh_user ();
+
+-- Sample insert
 -- insert into user_(name, password_encrypted) values ('test_name', 'bleh');
 -- Sample delete
 -- delete from user_ where name like 'test_name';
 -- Sample update
 -- update user_ set avatar = 'hai'  where name like 'test_name';
-create or replace function refresh_user()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from user_fast where id = OLD.id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from user_fast where id = OLD.id;
-    insert into user_fast select * from user_view where id = NEW.id;
-    
-    -- Refresh post_fast, cause of user info changes
-    delete from post_aggregates_fast where creator_id = NEW.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where creator_id = NEW.id;
-
-    delete from comment_aggregates_fast where creator_id = NEW.id;
-    insert into comment_aggregates_fast select * from comment_aggregates_view where creator_id = NEW.id;
-
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into user_fast select * from user_view where id = NEW.id;
-  END IF;
-
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION refresh_user ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM user_fast
+        WHERE id = OLD.id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM user_fast
+        WHERE id = OLD.id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.id;
+        -- Refresh post_fast, cause of user info changes
+        DELETE FROM post_aggregates_fast
+        WHERE creator_id = NEW.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            creator_id = NEW.id;
+        DELETE FROM comment_aggregates_fast
+        WHERE creator_id = NEW.id;
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            creator_id = NEW.id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
 -- Post
 -- Redoing the views : Credit eiknat
-drop view post_view;
-drop view post_aggregates_view;
-
-create view post_aggregates_view as
-select
-       p.*,
-       -- creator details
-       u.actor_id as creator_actor_id,
-       u."local" as creator_local,
-       u."name" as creator_name,
-       u.avatar as creator_avatar,
-  u.banned as banned,
-  cb.id::bool as banned_from_community,
-       -- community details
-       c.actor_id as community_actor_id,
-       c."local" as community_local,
-       c."name" as community_name,
-       c.removed as community_removed,
-       c.deleted as community_deleted,
-       c.nsfw as community_nsfw,
-       -- post score data/comment count
-       coalesce(ct.comments, 0) as number_of_comments,
-       coalesce(pl.score, 0) as score,
-       coalesce(pl.upvotes, 0) as upvotes,
-       coalesce(pl.downvotes, 0) as downvotes,
-       hot_rank(
-               coalesce(pl.score , 0), (
-                       case
-                               when (p.published < ('now'::timestamp - '1 month'::interval))
-                               then p.published
-                               else greatest(ct.recent_comment_time, p.published)
-                       end
-               )
-       ) as hot_rank,
-       (
-               case
-                       when (p.published < ('now'::timestamp - '1 month'::interval))
-                       then p.published
-                       else greatest(ct.recent_comment_time, p.published)
-               end
-       ) as newest_activity_time
-from post p
-left join user_ u on p.creator_id = u.id
-left join community_user_ban cb on p.creator_id = cb.user_id and p.community_id = cb.community_id
-left join community c on p.community_id = c.id
-left join (
-       select
-               post_id,
-               count(*) as comments,
-               max(published) as recent_comment_time
-       from comment
-       group by post_id
-) ct on ct.post_id = p.id
-left join (
-       select
-               post_id,
-               sum(score) as score,
-               sum(score) filter (where score = 1) as upvotes,
-               -sum(score) filter (where score = -1) as downvotes
-       from post_like
-       group by post_id
-) pl on pl.post_id = p.id
-order by p.id;
-
-create view post_view as
-select
-       pav.*,
-       us.id as user_id,
-       us.user_vote as my_vote,
-       us.is_subbed::bool as subscribed,
-       us.is_read::bool as read,
-       us.is_saved::bool as saved
-from post_aggregates_view pav
-cross join lateral (
-       select
-               u.id,
-               coalesce(cf.community_id, 0) as is_subbed,
-               coalesce(pr.post_id, 0) as is_read,
-               coalesce(ps.post_id, 0) as is_saved,
-               coalesce(pl.score, 0) as user_vote
-       from user_ u
-       left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
-       left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
-       left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
-       left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
-) as us
-
-union all
-
-select 
-pav.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from post_aggregates_view pav;
+DROP VIEW post_view;
+
+DROP VIEW post_aggregates_view;
+
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    -- creator details
+    u.actor_id AS creator_actor_id,
+    u."local" AS creator_local,
+    u."name" AS creator_name,
+    u.avatar AS creator_avatar,
+    u.banned AS banned,
+    cb.id::bool AS banned_from_community,
+    -- community details
+    c.actor_id AS community_actor_id,
+    c."local" AS community_local,
+    c."name" AS community_name,
+    c.removed AS community_removed,
+    c.deleted AS community_deleted,
+    c.nsfw AS community_nsfw,
+    -- post score data/comment count
+    coalesce(ct.comments, 0) AS number_of_comments,
+    coalesce(pl.score, 0) AS score,
+    coalesce(pl.upvotes, 0) AS upvotes,
+    coalesce(pl.downvotes, 0) AS downvotes,
+    hot_rank (coalesce(pl.score, 0), (
+            CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+                p.published
+            ELSE
+                greatest (ct.recent_comment_time, p.published)
+            END)) AS hot_rank,
+    (
+        CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+            p.published
+        ELSE
+            greatest (ct.recent_comment_time, p.published)
+        END) AS newest_activity_time
+FROM
+    post p
+    LEFT JOIN user_ u ON p.creator_id = u.id
+    LEFT JOIN community_user_ban cb ON p.creator_id = cb.user_id
+        AND p.community_id = cb.community_id
+    LEFT JOIN community c ON p.community_id = c.id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            count(*) AS comments,
+            max(published) AS recent_comment_time
+        FROM
+            comment
+        GROUP BY
+            post_id) ct ON ct.post_id = p.id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            sum(score) AS score,
+            sum(score) FILTER (WHERE score = 1) AS upvotes,
+            - sum(score) FILTER (WHERE score = - 1) AS downvotes
+        FROM
+            post_like
+        GROUP BY
+            post_id) pl ON pl.post_id = p.id
+ORDER BY
+    p.id;
+
+CREATE VIEW post_view AS
+SELECT
+    pav.*,
+    us.id AS user_id,
+    us.user_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_read::bool AS read,
+    us.is_saved::bool AS saved
+FROM
+    post_aggregates_view pav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id,
+            coalesce(cf.community_id, 0) AS is_subbed,
+            coalesce(pr.post_id, 0) AS is_read,
+            coalesce(ps.post_id, 0) AS is_saved,
+            coalesce(pl.score, 0) AS user_vote
+        FROM
+            user_ u
+            LEFT JOIN community_user_ban cb ON u.id = cb.user_id
+                AND cb.community_id = pav.community_id
+        LEFT JOIN community_follower cf ON u.id = cf.user_id
+            AND cf.community_id = pav.community_id
+    LEFT JOIN post_read pr ON u.id = pr.user_id
+        AND pr.post_id = pav.id
+    LEFT JOIN post_saved ps ON u.id = ps.user_id
+        AND ps.post_id = pav.id
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND pav.id = pl.post_id) AS us
+UNION ALL
+SELECT
+    pav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS read,
+    NULL AS saved
+FROM
+    post_aggregates_view pav;
 
 -- The post fast table
-create table post_aggregates_fast as select * from post_aggregates_view;
-alter table post_aggregates_fast add primary key (id);
+CREATE TABLE post_aggregates_fast AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
+
+ALTER TABLE post_aggregates_fast
+    ADD PRIMARY KEY (id);
 
 -- For the hot rank resorting
-create index idx_post_aggregates_fast_hot_rank_published on post_aggregates_fast (hot_rank desc, published desc);
-
-create view post_fast_view as 
-select
-       pav.*,
-       us.id as user_id,
-       us.user_vote as my_vote,
-       us.is_subbed::bool as subscribed,
-       us.is_read::bool as read,
-       us.is_saved::bool as saved
-from post_aggregates_fast pav
-cross join lateral (
-       select
-               u.id,
-               coalesce(cf.community_id, 0) as is_subbed,
-               coalesce(pr.post_id, 0) as is_read,
-               coalesce(ps.post_id, 0) as is_saved,
-               coalesce(pl.score, 0) as user_vote
-       from user_ u
-       left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
-       left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
-       left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
-       left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
-) as us
-
-union all
-
-select 
-pav.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from post_aggregates_fast pav;
-
-drop trigger refresh_post on post;
-
-create trigger refresh_post
-after insert or update or delete
-on post
-for each row
-execute procedure refresh_post();
+CREATE INDEX idx_post_aggregates_fast_hot_rank_published ON post_aggregates_fast (hot_rank DESC, published DESC);
+
+CREATE VIEW post_fast_view AS
+SELECT
+    pav.*,
+    us.id AS user_id,
+    us.user_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_read::bool AS read,
+    us.is_saved::bool AS saved
+FROM
+    post_aggregates_fast pav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id,
+            coalesce(cf.community_id, 0) AS is_subbed,
+            coalesce(pr.post_id, 0) AS is_read,
+            coalesce(ps.post_id, 0) AS is_saved,
+            coalesce(pl.score, 0) AS user_vote
+        FROM
+            user_ u
+            LEFT JOIN community_user_ban cb ON u.id = cb.user_id
+                AND cb.community_id = pav.community_id
+        LEFT JOIN community_follower cf ON u.id = cf.user_id
+            AND cf.community_id = pav.community_id
+    LEFT JOIN post_read pr ON u.id = pr.user_id
+        AND pr.post_id = pav.id
+    LEFT JOIN post_saved ps ON u.id = ps.user_id
+        AND ps.post_id = pav.id
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND pav.id = pl.post_id) AS us
+UNION ALL
+SELECT
+    pav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS read,
+    NULL AS saved
+FROM
+    post_aggregates_fast pav;
+
+DROP TRIGGER refresh_post ON post;
+
+CREATE TRIGGER refresh_post
+    AFTER INSERT OR UPDATE OR DELETE ON post
+    FOR EACH ROW
+    EXECUTE PROCEDURE refresh_post ();
 
 -- Sample select
 -- select id, name from post_fast_view where name like 'test_post' and user_id is null;
--- Sample insert 
+-- Sample insert
 -- insert into post(name, creator_id, community_id) values ('test_post', 2, 2);
 -- Sample delete
 -- delete from post where name like 'test_post';
 -- Sample update
 -- update post set community_id = 4  where name like 'test_post';
-create or replace function refresh_post()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
-
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts - 1 where id = OLD.community_id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
-
-    -- Update that users number of posts, post score
-    delete from user_fast where id = NEW.creator_id;
-    insert into user_fast select * from user_view where id = NEW.creator_id;
-  
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts + 1 where id = NEW.community_id;
-
-    -- Update the hot rank on the post table
-    -- TODO this might not correctly update it, using a 1 week interval
-    update post_aggregates_fast as paf
-    set hot_rank = pav.hot_rank 
-    from post_aggregates_view as pav
-    where paf.id = pav.id  and (pav.published > ('now'::timestamp - '1 week'::interval));
-  END IF;
-
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION refresh_post ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts - 1
+        WHERE
+            id = OLD.community_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update that users number of posts, post score
+        DELETE FROM user_fast
+        WHERE id = NEW.creator_id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.creator_id;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts + 1
+        WHERE
+            id = NEW.community_id;
+        -- Update the hot rank on the post table
+        -- TODO this might not correctly update it, using a 1 week interval
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = pav.hot_rank
+        FROM
+            post_aggregates_view AS pav
+        WHERE
+            paf.id = pav.id
+            AND (pav.published > ('now'::timestamp - '1 week'::interval));
+    END IF;
+    RETURN NULL;
+END
+$$;
 
 -- Community
 -- Redoing the views : Credit eiknat
-drop view community_moderator_view;
-drop view community_follower_view;
-drop view community_user_ban_view;
-drop view community_view;
-drop view community_aggregates_view;
-
-create view community_aggregates_view as
-select 
+DROP VIEW community_moderator_view;
+
+DROP VIEW community_follower_view;
+
+DROP VIEW community_user_ban_view;
+
+DROP VIEW community_view;
+
+DROP VIEW community_aggregates_view;
+
+CREATE VIEW community_aggregates_view AS
+SELECT
     c.id,
     c.name,
     c.title,
@@ -310,302 +408,382 @@ select
     c.actor_id,
     c.local,
     c.last_refreshed_at,
-    u.actor_id as creator_actor_id,
-    u.local as creator_local,
-    u.name as creator_name,
-    u.avatar as creator_avatar,
-    cat.name as category_name,
-    coalesce(cf.subs, 0) as number_of_subscribers,
-    coalesce(cd.posts, 0) as number_of_posts,
-    coalesce(cd.comments, 0) as number_of_comments,
-    hot_rank(cf.subs, c.published) as hot_rank
-from community c
-left join user_ u on c.creator_id = u.id
-left join category cat on c.category_id = cat.id
-left join (
-    select
-        p.community_id,
-        count(distinct p.id) as posts,
-        count(distinct ct.id) as comments
-    from post p
-    join comment ct on p.id = ct.post_id
-    group by p.community_id
-) cd on cd.community_id = c.id
-left join (
-    select
-        community_id,
-        count(*) as subs 
-    from community_follower
-    group by community_id 
-) cf on cf.community_id = c.id;
-
-create view community_view as
-select
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u.name AS creator_name,
+    u.avatar AS creator_avatar,
+    cat.name AS category_name,
+    coalesce(cf.subs, 0) AS number_of_subscribers,
+    coalesce(cd.posts, 0) AS number_of_posts,
+    coalesce(cd.comments, 0) AS number_of_comments,
+    hot_rank (cf.subs, c.published) AS hot_rank
+FROM
+    community c
+    LEFT JOIN user_ u ON c.creator_id = u.id
+    LEFT JOIN category cat ON c.category_id = cat.id
+    LEFT JOIN (
+        SELECT
+            p.community_id,
+            count(DISTINCT p.id) AS posts,
+            count(DISTINCT ct.id) AS comments
+        FROM
+            post p
+            JOIN comment ct ON p.id = ct.post_id
+        GROUP BY
+            p.community_id) cd ON cd.community_id = c.id
+    LEFT JOIN (
+        SELECT
+            community_id,
+            count(*) AS subs
+        FROM
+            community_follower
+        GROUP BY
+            community_id) cf ON cf.community_id = c.id;
+
+CREATE VIEW community_view AS
+SELECT
     cv.*,
-    us.user as user_id,
-    us.is_subbed::bool as subscribed
-from community_aggregates_view cv
-cross join lateral (
-       select
-               u.id as user,
-               coalesce(cf.community_id, 0) as is_subbed
-       from user_ u
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = cv.id
-) as us
-
-union all
-
-select 
+    us.user AS user_id,
+    us.is_subbed::bool AS subscribed
+FROM
+    community_aggregates_view cv
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user,
+            coalesce(cf.community_id, 0) AS is_subbed
+        FROM
+            user_ u
+            LEFT JOIN community_follower cf ON u.id = cf.user_id
+                AND cf.community_id = cv.id) AS us
+UNION ALL
+SELECT
     cv.*,
-    null as user_id,
-    null as subscribed
-from community_aggregates_view cv;
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    community_aggregates_view cv;
 
-create view community_moderator_view as
-select
+CREATE VIEW community_moderator_view AS
+SELECT
     cm.*,
-    u.actor_id as user_actor_id,
-    u.local as user_local,
-    u.name as user_name,
-    u.avatar as avatar,
-    c.actor_id as community_actor_id,
-    c.local as community_local,
-    c.name as community_name
-from community_moderator cm
-left join user_ u on cm.user_id = u.id
-left join community c on cm.community_id = c.id;
-
-create view community_follower_view as
-select
+    u.actor_id AS user_actor_id,
+    u.local AS user_local,
+    u.name AS user_name,
+    u.avatar AS avatar,
+    c.actor_id AS community_actor_id,
+    c.local AS community_local,
+    c.name AS community_name
+FROM
+    community_moderator cm
+    LEFT JOIN user_ u ON cm.user_id = u.id
+    LEFT JOIN community c ON cm.community_id = c.id;
+
+CREATE VIEW community_follower_view AS
+SELECT
     cf.*,
-    u.actor_id as user_actor_id,
-    u.local as user_local,
-    u.name as user_name,
-    u.avatar as avatar,
-    c.actor_id as community_actor_id,
-    c.local as community_local,
-    c.name as community_name
-from community_follower cf
-left join user_ u on cf.user_id = u.id
-left join community c on cf.community_id = c.id;
-
-create view community_user_ban_view as
-select
+    u.actor_id AS user_actor_id,
+    u.local AS user_local,
+    u.name AS user_name,
+    u.avatar AS avatar,
+    c.actor_id AS community_actor_id,
+    c.local AS community_local,
+    c.name AS community_name
+FROM
+    community_follower cf
+    LEFT JOIN user_ u ON cf.user_id = u.id
+    LEFT JOIN community c ON cf.community_id = c.id;
+
+CREATE VIEW community_user_ban_view AS
+SELECT
     cb.*,
-    u.actor_id as user_actor_id,
-    u.local as user_local,
-    u.name as user_name,
-    u.avatar as avatar,
-    c.actor_id as community_actor_id,
-    c.local as community_local,
-    c.name as community_name
-from community_user_ban cb
-left join user_ u on cb.user_id = u.id
-left join community c on cb.community_id = c.id;
+    u.actor_id AS user_actor_id,
+    u.local AS user_local,
+    u.name AS user_name,
+    u.avatar AS avatar,
+    c.actor_id AS community_actor_id,
+    c.local AS community_local,
+    c.name AS community_name
+FROM
+    community_user_ban cb
+    LEFT JOIN user_ u ON cb.user_id = u.id
+    LEFT JOIN community c ON cb.community_id = c.id;
 
 -- The community fast table
-
-create table community_aggregates_fast as select * from community_aggregates_view;
-alter table community_aggregates_fast add primary key (id);
-
-create view community_fast_view as
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join (
-  select
-  ca.*
-  from community_aggregates_fast ca
-) ac
-
-union all
-
-select 
-caf.*,
-null as user_id,
-null as subscribed
-from community_aggregates_fast caf;
-
-drop trigger refresh_community on community;
-
-create trigger refresh_community
-after insert or update or delete
-on community
-for each row
-execute procedure refresh_community();
+CREATE TABLE community_aggregates_fast AS
+SELECT
+    *
+FROM
+    community_aggregates_view;
+
+ALTER TABLE community_aggregates_fast
+    ADD PRIMARY KEY (id);
+
+CREATE VIEW community_fast_view AS
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN (
+        SELECT
+            ca.*
+        FROM
+            community_aggregates_fast ca) ac
+UNION ALL
+SELECT
+    caf.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    community_aggregates_fast caf;
+
+DROP TRIGGER refresh_community ON community;
+
+CREATE TRIGGER refresh_community
+    AFTER INSERT OR UPDATE OR DELETE ON community
+    FOR EACH ROW
+    EXECUTE PROCEDURE refresh_community ();
 
 -- Sample select
 -- select * from community_fast_view where name like 'test_community_name' and user_id is null;
--- Sample insert 
+-- Sample insert
 -- insert into community(name, title, category_id, creator_id) values ('test_community_name', 'test_community_title', 1, 2);
 -- Sample delete
 -- delete from community where name like 'test_community_name';
 -- Sample update
 -- update community set title = 'test_community_title_2'  where name like 'test_community_name';
-create or replace function refresh_community()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from community_aggregates_fast where id = OLD.id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from community_aggregates_fast where id = OLD.id;
-    insert into community_aggregates_fast select * from community_aggregates_view where id = NEW.id;
-
-    -- Update user view due to owner changes
-    delete from user_fast where id = NEW.creator_id;
-    insert into user_fast select * from user_view where id = NEW.creator_id;
-    
-    -- Update post view due to community changes
-    delete from post_aggregates_fast where community_id = NEW.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where community_id = NEW.id;
-
-  -- TODO make sure this shows up in the users page ?
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into community_aggregates_fast select * from community_aggregates_view where id = NEW.id;
-  END IF;
-
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION refresh_community ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM community_aggregates_fast
+        WHERE id = OLD.id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM community_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO community_aggregates_fast
+        SELECT
+            *
+        FROM
+            community_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update user view due to owner changes
+        DELETE FROM user_fast
+        WHERE id = NEW.creator_id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.creator_id;
+        -- Update post view due to community changes
+        DELETE FROM post_aggregates_fast
+        WHERE community_id = NEW.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            community_id = NEW.id;
+        -- TODO make sure this shows up in the users page ?
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO community_aggregates_fast
+        SELECT
+            *
+        FROM
+            community_aggregates_view
+        WHERE
+            id = NEW.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
 -- Comment
-
-drop view user_mention_view;
-drop view comment_view;
-drop view comment_aggregates_view;
-
-create view comment_aggregates_view as 
-select
-       ct.*,
-       -- community details
-       p.community_id,
-       c.actor_id as community_actor_id,
-       c."local" as community_local,
-       c."name" as community_name,
-       -- creator details
-       u.banned as banned,
-  coalesce(cb.id, 0)::bool as banned_from_community,
-       u.actor_id as creator_actor_id,
-       u.local as creator_local,
-       u.name as creator_name,
-       u.avatar as creator_avatar,
-       -- score details
-       coalesce(cl.total, 0) as score,
-       coalesce(cl.up, 0) as upvotes,
-       coalesce(cl.down, 0) as downvotes,
-       hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
-from comment ct
-left join post p on ct.post_id = p.id
-left join community c on p.community_id = c.id
-left join user_ u on ct.creator_id = u.id 
-left join community_user_ban cb on ct.creator_id = cb.user_id and p.id = ct.post_id and p.community_id = cb.community_id
-left join (
-       select
-               l.comment_id as id,
-               sum(l.score) as total,
-               count(case when l.score = 1 then 1 else null end) as up,
-               count(case when l.score = -1 then 1 else null end) as down
-       from comment_like l
-       group by comment_id
-) as cl on cl.id = ct.id;
-
-create or replace view comment_view as (
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_view cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
-
-union all 
-
-select 
+DROP VIEW user_mention_view;
+
+DROP VIEW comment_view;
+
+DROP VIEW comment_aggregates_view;
+
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    ct.*,
+    -- community details
+    p.community_id,
+    c.actor_id AS community_actor_id,
+    c."local" AS community_local,
+    c."name" AS community_name,
+    -- creator details
+    u.banned AS banned,
+    coalesce(cb.id, 0)::bool AS banned_from_community,
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u.name AS creator_name,
+    u.avatar AS creator_avatar,
+    -- score details
+    coalesce(cl.total, 0) AS score,
+    coalesce(cl.up, 0) AS upvotes,
+    coalesce(cl.down, 0) AS downvotes,
+    hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
+FROM
+    comment ct
+    LEFT JOIN post p ON ct.post_id = p.id
+    LEFT JOIN community c ON p.community_id = c.id
+    LEFT JOIN user_ u ON ct.creator_id = u.id
+    LEFT JOIN community_user_ban cb ON ct.creator_id = cb.user_id
+        AND p.id = ct.post_id
+        AND p.community_id = cb.community_id
+    LEFT JOIN (
+        SELECT
+            l.comment_id AS id,
+            sum(l.score) AS total,
+            count(
+                CASE WHEN l.score = 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS up,
+            count(
+                CASE WHEN l.score = - 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS down
+        FROM
+            comment_like l
+        GROUP BY
+            comment_id) AS cl ON cl.id = ct.id;
+
+CREATE OR REPLACE VIEW comment_view AS (
+    SELECT
+        cav.*,
+        us.user_id AS user_id,
+        us.my_vote AS my_vote,
+        us.is_subbed::bool AS subscribed,
+        us.is_saved::bool AS saved
+    FROM
+        comment_aggregates_view cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
     cav.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_view cav
-);
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_view cav);
 
 -- The fast view
-create table comment_aggregates_fast as select * from comment_aggregates_view;
-alter table comment_aggregates_fast add primary key (id);
-
-create view comment_fast_view as
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_fast cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
-
-union all 
-
-select 
+CREATE TABLE comment_aggregates_fast AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
+
+ALTER TABLE comment_aggregates_fast
+    ADD PRIMARY KEY (id);
+
+CREATE VIEW comment_fast_view AS
+SELECT
+    cav.*,
+    us.user_id AS user_id,
+    us.my_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_saved::bool AS saved
+FROM
+    comment_aggregates_fast cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
     cav.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_fast cav;
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_fast cav;
 
 -- Do the reply_view referencing the comment_fast_view
-create view reply_fast_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_fast_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_fast_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_fast_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- user mention
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.creator_actor_id,
     c.creator_local,
@@ -633,15 +811,30 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_mention um, comment_view c
-where um.comment_id = c.id;
-
-create view user_mention_fast_view as 
-select
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
+
+CREATE VIEW user_mention_fast_view AS
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -665,26 +858,45 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_ u
-cross join (
-  select
-  ca.*
-  from comment_aggregates_fast ca
-) ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select 
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_ u
+    CROSS JOIN (
+        SELECT
+            ca.*
+        FROM
+            comment_aggregates_fast ca) ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -708,232 +920,345 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    null as user_id, 
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from comment_aggregates_fast ac
-left join user_mention um on um.comment_id = ac.id
-;
-
-
-drop trigger refresh_comment on comment;
-
-create trigger refresh_comment
-after insert or update or delete
-on comment
-for each row
-execute procedure refresh_comment();
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    comment_aggregates_fast ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
+
+DROP TRIGGER refresh_comment ON comment;
+
+CREATE TRIGGER refresh_comment
+    AFTER INSERT OR UPDATE OR DELETE ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE refresh_comment ();
 
 -- Sample select
 -- select * from comment_fast_view where content = 'test_comment' and user_id is null;
--- Sample insert 
+-- Sample insert
 -- insert into comment(creator_id, post_id, content) values (2, 2, 'test_comment');
 -- Sample delete
 -- delete from comment where content like 'test_comment';
 -- Sample update
 -- update comment set removed = true where content like 'test_comment';
-create or replace function refresh_comment()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments - 1
-    from post as p
-    where caf.id = p.community_id and p.id = OLD.post_id;
-
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
-
-    -- Update user view due to comment count
-    update user_fast 
-    set number_of_comments = number_of_comments + 1
-    where id = NEW.creator_id;
-    
-    -- Update post view due to comment count, new comment activity time, but only on new posts
-    -- TODO this could be done more efficiently
-    delete from post_aggregates_fast where id = NEW.post_id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.post_id;
-
-    -- Force the hot rank as zero on week-older posts
-    update post_aggregates_fast as paf
-    set hot_rank = 0
-    where paf.id = NEW.post_id and (paf.published < ('now'::timestamp - '1 week'::interval));
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments + 1 
-    from post as p
-    where caf.id = p.community_id and p.id = NEW.post_id;
-
-  END IF;
-
-  return null;
-end $$;
-
+CREATE OR REPLACE FUNCTION refresh_comment ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments - 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = OLD.post_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update user view due to comment count
+        UPDATE
+            user_fast
+        SET
+            number_of_comments = number_of_comments + 1
+        WHERE
+            id = NEW.creator_id;
+        -- Update post view due to comment count, new comment activity time, but only on new posts
+        -- TODO this could be done more efficiently
+        DELETE FROM post_aggregates_fast
+        WHERE id = NEW.post_id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.post_id;
+        -- Force the hot rank as zero on week-older posts
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = 0
+        WHERE
+            paf.id = NEW.post_id
+            AND (paf.published < ('now'::timestamp - '1 week'::interval));
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments + 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = NEW.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
 -- post_like
 -- select id, score, my_vote from post_fast_view where id = 29 and user_id = 4;
--- Sample insert 
+-- Sample insert
 -- insert into post_like(user_id, post_id, score) values (4, 29, 1);
 -- Sample delete
 -- delete from post_like where user_id = 4 and post_id = 29;
 -- Sample update
 -- update post_like set score = -1 where user_id = 4 and post_id = 29;
-
 -- TODO test this a LOT
-create or replace function refresh_post_like()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    update post_aggregates_fast 
-    set score = case 
-      when (OLD.score = 1) then score - 1 
-      else score + 1 end,
-    upvotes = case 
-      when (OLD.score = 1) then upvotes - 1 
-      else upvotes end,
-    downvotes = case 
-      when (OLD.score = -1) then downvotes - 1 
-      else downvotes end
-    where id = OLD.post_id;
-
-  ELSIF (TG_OP = 'INSERT') THEN
-    update post_aggregates_fast 
-    set score = case 
-      when (NEW.score = 1) then score + 1 
-      else score - 1 end,
-    upvotes = case 
-      when (NEW.score = 1) then upvotes + 1 
-      else upvotes end,
-    downvotes = case 
-      when (NEW.score = -1) then downvotes + 1 
-      else downvotes end
-    where id = NEW.post_id;
-  END IF;
-
-  return null;
-end $$;
-
-drop trigger refresh_post_like on post_like;
-create trigger refresh_post_like
-after insert or delete
-on post_like
-for each row
-execute procedure refresh_post_like();
+CREATE OR REPLACE FUNCTION refresh_post_like ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        UPDATE
+            post_aggregates_fast
+        SET
+            score = CASE WHEN (OLD.score = 1) THEN
+                score - 1
+            ELSE
+                score + 1
+            END,
+            upvotes = CASE WHEN (OLD.score = 1) THEN
+                upvotes - 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN (OLD.score = - 1) THEN
+                downvotes - 1
+            ELSE
+                downvotes
+            END
+        WHERE
+            id = OLD.post_id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        UPDATE
+            post_aggregates_fast
+        SET
+            score = CASE WHEN (NEW.score = 1) THEN
+                score + 1
+            ELSE
+                score - 1
+            END,
+            upvotes = CASE WHEN (NEW.score = 1) THEN
+                upvotes + 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN (NEW.score = - 1) THEN
+                downvotes + 1
+            ELSE
+                downvotes
+            END
+        WHERE
+            id = NEW.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+DROP TRIGGER refresh_post_like ON post_like;
+
+CREATE TRIGGER refresh_post_like
+    AFTER INSERT OR DELETE ON post_like
+    FOR EACH ROW
+    EXECUTE PROCEDURE refresh_post_like ();
 
 -- comment_like
 -- select id, score, my_vote from comment_fast_view where id = 29 and user_id = 4;
--- Sample insert 
+-- Sample insert
 -- insert into comment_like(user_id, comment_id, post_id, score) values (4, 29, 51, 1);
 -- Sample delete
 -- delete from comment_like where user_id = 4 and comment_id = 29;
 -- Sample update
 -- update comment_like set score = -1 where user_id = 4 and comment_id = 29;
-create or replace function refresh_comment_like()
-returns trigger language plpgsql
-as $$
-begin
-  -- TODO possibly select from comment_fast to get previous scores, instead of re-fetching the views?
-  IF (TG_OP = 'DELETE') THEN
-    update comment_aggregates_fast 
-    set score = case 
-      when (OLD.score = 1) then score - 1 
-      else score + 1 end,
-    upvotes = case 
-      when (OLD.score = 1) then upvotes - 1 
-      else upvotes end,
-    downvotes = case 
-      when (OLD.score = -1) then downvotes - 1 
-      else downvotes end
-    where id = OLD.comment_id;
-
-  ELSIF (TG_OP = 'INSERT') THEN
-    update comment_aggregates_fast 
-    set score = case 
-      when (NEW.score = 1) then score + 1 
-      else score - 1 end,
-    upvotes = case 
-      when (NEW.score = 1) then upvotes + 1 
-      else upvotes end,
-    downvotes = case 
-      when (NEW.score = -1) then downvotes + 1 
-      else downvotes end
-    where id = NEW.comment_id;
-  END IF;
-
-  return null;
-end $$;
-
-drop trigger refresh_comment_like on comment_like;
-create trigger refresh_comment_like
-after insert or delete
-on comment_like
-for each row
-execute procedure refresh_comment_like();
+CREATE OR REPLACE FUNCTION refresh_comment_like ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    -- TODO possibly select from comment_fast to get previous scores, instead of re-fetching the views?
+    IF (TG_OP = 'DELETE') THEN
+        UPDATE
+            comment_aggregates_fast
+        SET
+            score = CASE WHEN (OLD.score = 1) THEN
+                score - 1
+            ELSE
+                score + 1
+            END,
+            upvotes = CASE WHEN (OLD.score = 1) THEN
+                upvotes - 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN (OLD.score = - 1) THEN
+                downvotes - 1
+            ELSE
+                downvotes
+            END
+        WHERE
+            id = OLD.comment_id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        UPDATE
+            comment_aggregates_fast
+        SET
+            score = CASE WHEN (NEW.score = 1) THEN
+                score + 1
+            ELSE
+                score - 1
+            END,
+            upvotes = CASE WHEN (NEW.score = 1) THEN
+                upvotes + 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN (NEW.score = - 1) THEN
+                downvotes + 1
+            ELSE
+                downvotes
+            END
+        WHERE
+            id = NEW.comment_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+DROP TRIGGER refresh_comment_like ON comment_like;
+
+CREATE TRIGGER refresh_comment_like
+    AFTER INSERT OR DELETE ON comment_like
+    FOR EACH ROW
+    EXECUTE PROCEDURE refresh_comment_like ();
 
 -- Community user ban
+DROP TRIGGER refresh_community_user_ban ON community_user_ban;
 
-drop trigger refresh_community_user_ban on community_user_ban;
-create trigger refresh_community_user_ban
-after insert or delete -- Note this is missing after update
-on community_user_ban
-for each row
-execute procedure refresh_community_user_ban();
+CREATE TRIGGER refresh_community_user_ban
+    AFTER INSERT OR DELETE -- Note this is missing after update
+    ON community_user_ban
+    FOR EACH ROW
+    EXECUTE PROCEDURE refresh_community_user_ban ();
 
 -- select creator_name, banned_from_community from comment_fast_view where user_id = 4 and content = 'test_before_ban';
 -- select creator_name, banned_from_community, community_id from comment_aggregates_fast where content = 'test_before_ban';
--- Sample insert 
+-- Sample insert
 -- insert into comment(creator_id, post_id, content) values (1198, 341, 'test_before_ban');
 -- insert into community_user_ban(community_id, user_id) values (2, 1198);
 -- Sample delete
 -- delete from community_user_ban where user_id = 1198 and community_id = 2;
 -- delete from comment where content = 'test_before_ban';
 -- update comment_aggregates_fast set banned_from_community = false where creator_id = 1198 and community_id = 2;
-create or replace function refresh_community_user_ban()
-returns trigger language plpgsql
-as $$
-begin
-  -- TODO possibly select from comment_fast to get previous scores, instead of re-fetching the views?
-  IF (TG_OP = 'DELETE') THEN
-    update comment_aggregates_fast set banned_from_community = false where creator_id = OLD.user_id and community_id = OLD.community_id;
-    update post_aggregates_fast set banned_from_community = false where creator_id = OLD.user_id and community_id = OLD.community_id;
-  ELSIF (TG_OP = 'INSERT') THEN
-    update comment_aggregates_fast set banned_from_community = true where creator_id = NEW.user_id and community_id = NEW.community_id;
-    update post_aggregates_fast set banned_from_community = true where creator_id = NEW.user_id and community_id = NEW.community_id;
-  END IF;
-
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION refresh_community_user_ban ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    -- TODO possibly select from comment_fast to get previous scores, instead of re-fetching the views?
+    IF (TG_OP = 'DELETE') THEN
+        UPDATE
+            comment_aggregates_fast
+        SET
+            banned_from_community = FALSE
+        WHERE
+            creator_id = OLD.user_id
+            AND community_id = OLD.community_id;
+        UPDATE
+            post_aggregates_fast
+        SET
+            banned_from_community = FALSE
+        WHERE
+            creator_id = OLD.user_id
+            AND community_id = OLD.community_id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        UPDATE
+            comment_aggregates_fast
+        SET
+            banned_from_community = TRUE
+        WHERE
+            creator_id = NEW.user_id
+            AND community_id = NEW.community_id;
+        UPDATE
+            post_aggregates_fast
+        SET
+            banned_from_community = TRUE
+        WHERE
+            creator_id = NEW.user_id
+            AND community_id = NEW.community_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
 -- Community follower
+DROP TRIGGER refresh_community_follower ON community_follower;
+
+CREATE TRIGGER refresh_community_follower
+    AFTER INSERT OR DELETE -- Note this is missing after update
+    ON community_follower
+    FOR EACH ROW
+    EXECUTE PROCEDURE refresh_community_follower ();
+
+CREATE OR REPLACE FUNCTION refresh_community_follower ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_subscribers = number_of_subscribers - 1
+        WHERE
+            id = OLD.community_id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_subscribers = number_of_subscribers + 1
+        WHERE
+            id = NEW.community_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-drop trigger refresh_community_follower on community_follower;
-create trigger refresh_community_follower
-after insert or delete -- Note this is missing after update
-on community_follower
-for each row
-execute procedure refresh_community_follower();
-
-create or replace function refresh_community_follower()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    update community_aggregates_fast set number_of_subscribers = number_of_subscribers - 1 where id = OLD.community_id;
-  ELSIF (TG_OP = 'INSERT') THEN
-    update community_aggregates_fast set number_of_subscribers = number_of_subscribers + 1 where id = NEW.community_id;
-  END IF;
-
-  return null;
-end $$;
index b8e4452e3da8568053c55dd7b56a8b04fe6995e7..fd1ca1cb441210c3886b77c74b216094c2ec19a8 100644 (file)
-drop view user_mention_view;
-drop view reply_fast_view;
-drop view comment_fast_view;
-drop view comment_view;
+DROP VIEW user_mention_view;
 
-drop view user_mention_fast_view;
-drop table comment_aggregates_fast;
-drop view comment_aggregates_view;
+DROP VIEW reply_fast_view;
 
-create view comment_aggregates_view as 
-select
-       ct.*,
-       -- community details
-       p.community_id,
-       c.actor_id as community_actor_id,
-       c."local" as community_local,
-       c."name" as community_name,
-       -- creator details
-       u.banned as banned,
-  coalesce(cb.id, 0)::bool as banned_from_community,
-       u.actor_id as creator_actor_id,
-       u.local as creator_local,
-       u.name as creator_name,
-       u.avatar as creator_avatar,
-       -- score details
-       coalesce(cl.total, 0) as score,
-       coalesce(cl.up, 0) as upvotes,
-       coalesce(cl.down, 0) as downvotes,
-       hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
-from comment ct
-left join post p on ct.post_id = p.id
-left join community c on p.community_id = c.id
-left join user_ u on ct.creator_id = u.id 
-left join community_user_ban cb on ct.creator_id = cb.user_id and p.id = ct.post_id and p.community_id = cb.community_id
-left join (
-       select
-               l.comment_id as id,
-               sum(l.score) as total,
-               count(case when l.score = 1 then 1 else null end) as up,
-               count(case when l.score = -1 then 1 else null end) as down
-       from comment_like l
-       group by comment_id
-) as cl on cl.id = ct.id;
+DROP VIEW comment_fast_view;
 
-create or replace view comment_view as (
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_view cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
+DROP VIEW comment_view;
 
-union all 
+DROP VIEW user_mention_fast_view;
 
-select 
-    cav.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_view cav
-);
+DROP TABLE comment_aggregates_fast;
+
+DROP VIEW comment_aggregates_view;
 
-create table comment_aggregates_fast as select * from comment_aggregates_view;
-alter table comment_aggregates_fast add primary key (id);
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    ct.*,
+    -- community details
+    p.community_id,
+    c.actor_id AS community_actor_id,
+    c."local" AS community_local,
+    c."name" AS community_name,
+    -- creator details
+    u.banned AS banned,
+    coalesce(cb.id, 0)::bool AS banned_from_community,
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u.name AS creator_name,
+    u.avatar AS creator_avatar,
+    -- score details
+    coalesce(cl.total, 0) AS score,
+    coalesce(cl.up, 0) AS upvotes,
+    coalesce(cl.down, 0) AS downvotes,
+    hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
+FROM
+    comment ct
+    LEFT JOIN post p ON ct.post_id = p.id
+    LEFT JOIN community c ON p.community_id = c.id
+    LEFT JOIN user_ u ON ct.creator_id = u.id
+    LEFT JOIN community_user_ban cb ON ct.creator_id = cb.user_id
+        AND p.id = ct.post_id
+        AND p.community_id = cb.community_id
+    LEFT JOIN (
+        SELECT
+            l.comment_id AS id,
+            sum(l.score) AS total,
+            count(
+                CASE WHEN l.score = 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS up,
+            count(
+                CASE WHEN l.score = - 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS down
+        FROM
+            comment_like l
+        GROUP BY
+            comment_id) AS cl ON cl.id = ct.id;
 
-create view comment_fast_view as
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_fast cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
+CREATE OR REPLACE VIEW comment_view AS (
+    SELECT
+        cav.*,
+        us.user_id AS user_id,
+        us.my_vote AS my_vote,
+        us.is_subbed::bool AS subscribed,
+        us.is_saved::bool AS saved
+    FROM
+        comment_aggregates_view cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
+    cav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_view cav);
 
-union all 
+CREATE TABLE comment_aggregates_fast AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
 
-select 
+ALTER TABLE comment_aggregates_fast
+    ADD PRIMARY KEY (id);
+
+CREATE VIEW comment_fast_view AS
+SELECT
+    cav.*,
+    us.user_id AS user_id,
+    us.my_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_saved::bool AS saved
+FROM
+    comment_aggregates_fast cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
     cav.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_fast cav;
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_fast cav;
 
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.creator_actor_id,
     c.creator_local,
@@ -137,15 +167,30 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_mention um, comment_view c
-where um.comment_id = c.id;
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
 
-create view user_mention_fast_view as 
-select
+CREATE VIEW user_mention_fast_view AS
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -169,26 +214,45 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_ u
-cross join (
-  select
-  ca.*
-  from comment_aggregates_fast ca
-) ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select 
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_ u
+    CROSS JOIN (
+        SELECT
+            ca.*
+        FROM
+            comment_aggregates_fast ca) ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -212,177 +276,220 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    null as user_id, 
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from comment_aggregates_fast ac
-left join user_mention um on um.comment_id = ac.id
-;
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    comment_aggregates_fast ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
 
 -- Do the reply_view referencing the comment_fast_view
-create view reply_fast_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_fast_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_fast_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_fast_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- add creator_published to the post view
-drop view post_fast_view;
-drop table post_aggregates_fast;
-drop view post_view;
-drop view post_aggregates_view;
+DROP VIEW post_fast_view;
+
+DROP TABLE post_aggregates_fast;
 
-create view post_aggregates_view as
-select
-       p.*,
-       -- creator details
-       u.actor_id as creator_actor_id,
-       u."local" as creator_local,
-       u."name" as creator_name,
-       u.avatar as creator_avatar,
-  u.banned as banned,
-  cb.id::bool as banned_from_community,
-       -- community details
-       c.actor_id as community_actor_id,
-       c."local" as community_local,
-       c."name" as community_name,
-       c.removed as community_removed,
-       c.deleted as community_deleted,
-       c.nsfw as community_nsfw,
-       -- post score data/comment count
-       coalesce(ct.comments, 0) as number_of_comments,
-       coalesce(pl.score, 0) as score,
-       coalesce(pl.upvotes, 0) as upvotes,
-       coalesce(pl.downvotes, 0) as downvotes,
-       hot_rank(
-               coalesce(pl.score , 0), (
-                       case
-                               when (p.published < ('now'::timestamp - '1 month'::interval))
-                               then p.published
-                               else greatest(ct.recent_comment_time, p.published)
-                       end
-               )
-       ) as hot_rank,
-       (
-               case
-                       when (p.published < ('now'::timestamp - '1 month'::interval))
-                       then p.published
-                       else greatest(ct.recent_comment_time, p.published)
-               end
-       ) as newest_activity_time
-from post p
-left join user_ u on p.creator_id = u.id
-left join community_user_ban cb on p.creator_id = cb.user_id and p.community_id = cb.community_id
-left join community c on p.community_id = c.id
-left join (
-       select
-               post_id,
-               count(*) as comments,
-               max(published) as recent_comment_time
-       from comment
-       group by post_id
-) ct on ct.post_id = p.id
-left join (
-       select
-               post_id,
-               sum(score) as score,
-               sum(score) filter (where score = 1) as upvotes,
-               -sum(score) filter (where score = -1) as downvotes
-       from post_like
-       group by post_id
-) pl on pl.post_id = p.id
-order by p.id;
+DROP VIEW post_view;
 
-create view post_view as
-select
-       pav.*,
-       us.id as user_id,
-       us.user_vote as my_vote,
-       us.is_subbed::bool as subscribed,
-       us.is_read::bool as read,
-       us.is_saved::bool as saved
-from post_aggregates_view pav
-cross join lateral (
-       select
-               u.id,
-               coalesce(cf.community_id, 0) as is_subbed,
-               coalesce(pr.post_id, 0) as is_read,
-               coalesce(ps.post_id, 0) as is_saved,
-               coalesce(pl.score, 0) as user_vote
-       from user_ u
-       left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
-       left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
-       left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
-       left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
-) as us
+DROP VIEW post_aggregates_view;
 
-union all
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    -- creator details
+    u.actor_id AS creator_actor_id,
+    u."local" AS creator_local,
+    u."name" AS creator_name,
+    u.avatar AS creator_avatar,
+    u.banned AS banned,
+    cb.id::bool AS banned_from_community,
+    -- community details
+    c.actor_id AS community_actor_id,
+    c."local" AS community_local,
+    c."name" AS community_name,
+    c.removed AS community_removed,
+    c.deleted AS community_deleted,
+    c.nsfw AS community_nsfw,
+    -- post score data/comment count
+    coalesce(ct.comments, 0) AS number_of_comments,
+    coalesce(pl.score, 0) AS score,
+    coalesce(pl.upvotes, 0) AS upvotes,
+    coalesce(pl.downvotes, 0) AS downvotes,
+    hot_rank (coalesce(pl.score, 0), (
+            CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+                p.published
+            ELSE
+                greatest (ct.recent_comment_time, p.published)
+            END)) AS hot_rank,
+    (
+        CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+            p.published
+        ELSE
+            greatest (ct.recent_comment_time, p.published)
+        END) AS newest_activity_time
+FROM
+    post p
+    LEFT JOIN user_ u ON p.creator_id = u.id
+    LEFT JOIN community_user_ban cb ON p.creator_id = cb.user_id
+        AND p.community_id = cb.community_id
+    LEFT JOIN community c ON p.community_id = c.id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            count(*) AS comments,
+            max(published) AS recent_comment_time
+        FROM
+            comment
+        GROUP BY
+            post_id) ct ON ct.post_id = p.id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            sum(score) AS score,
+            sum(score) FILTER (WHERE score = 1) AS upvotes,
+            - sum(score) FILTER (WHERE score = - 1) AS downvotes
+        FROM
+            post_like
+        GROUP BY
+            post_id) pl ON pl.post_id = p.id
+ORDER BY
+    p.id;
 
-select 
-pav.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from post_aggregates_view pav;
+CREATE VIEW post_view AS
+SELECT
+    pav.*,
+    us.id AS user_id,
+    us.user_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_read::bool AS read,
+    us.is_saved::bool AS saved
+FROM
+    post_aggregates_view pav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id,
+            coalesce(cf.community_id, 0) AS is_subbed,
+            coalesce(pr.post_id, 0) AS is_read,
+            coalesce(ps.post_id, 0) AS is_saved,
+            coalesce(pl.score, 0) AS user_vote
+        FROM
+            user_ u
+            LEFT JOIN community_user_ban cb ON u.id = cb.user_id
+                AND cb.community_id = pav.community_id
+        LEFT JOIN community_follower cf ON u.id = cf.user_id
+            AND cf.community_id = pav.community_id
+    LEFT JOIN post_read pr ON u.id = pr.user_id
+        AND pr.post_id = pav.id
+    LEFT JOIN post_saved ps ON u.id = ps.user_id
+        AND ps.post_id = pav.id
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND pav.id = pl.post_id) AS us
+UNION ALL
+SELECT
+    pav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS read,
+    NULL AS saved
+FROM
+    post_aggregates_view pav;
 
-create table post_aggregates_fast as select * from post_aggregates_view;
-alter table post_aggregates_fast add primary key (id);
+CREATE TABLE post_aggregates_fast AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
 
-create view post_fast_view as 
-select
-       pav.*,
-       us.id as user_id,
-       us.user_vote as my_vote,
-       us.is_subbed::bool as subscribed,
-       us.is_read::bool as read,
-       us.is_saved::bool as saved
-from post_aggregates_fast pav
-cross join lateral (
-       select
-               u.id,
-               coalesce(cf.community_id, 0) as is_subbed,
-               coalesce(pr.post_id, 0) as is_read,
-               coalesce(ps.post_id, 0) as is_saved,
-               coalesce(pl.score, 0) as user_vote
-       from user_ u
-       left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
-       left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
-       left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
-       left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
-) as us
+ALTER TABLE post_aggregates_fast
+    ADD PRIMARY KEY (id);
 
-union all
+CREATE VIEW post_fast_view AS
+SELECT
+    pav.*,
+    us.id AS user_id,
+    us.user_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_read::bool AS read,
+    us.is_saved::bool AS saved
+FROM
+    post_aggregates_fast pav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id,
+            coalesce(cf.community_id, 0) AS is_subbed,
+            coalesce(pr.post_id, 0) AS is_read,
+            coalesce(ps.post_id, 0) AS is_saved,
+            coalesce(pl.score, 0) AS user_vote
+        FROM
+            user_ u
+            LEFT JOIN community_user_ban cb ON u.id = cb.user_id
+                AND cb.community_id = pav.community_id
+        LEFT JOIN community_follower cf ON u.id = cf.user_id
+            AND cf.community_id = pav.community_id
+    LEFT JOIN post_read pr ON u.id = pr.user_id
+        AND pr.post_id = pav.id
+    LEFT JOIN post_saved ps ON u.id = ps.user_id
+        AND ps.post_id = pav.id
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND pav.id = pl.post_id) AS us
+UNION ALL
+SELECT
+    pav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS read,
+    NULL AS saved
+FROM
+    post_aggregates_fast pav;
 
-select 
-pav.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from post_aggregates_fast pav;
\ No newline at end of file
index 1f2b59ea942cf2a209fb46c33cbaf54ea9c73605..6d3ff57d02ad94b1a2cd949df9cefd7aec78a0d2 100644 (file)
-drop view user_mention_view;
-drop view reply_fast_view;
-drop view comment_fast_view;
-drop view comment_view;
+DROP VIEW user_mention_view;
 
-drop view user_mention_fast_view;
-drop table comment_aggregates_fast;
-drop view comment_aggregates_view;
+DROP VIEW reply_fast_view;
 
-create view comment_aggregates_view as 
-select
-       ct.*,
-       -- community details
-       p.community_id,
-       c.actor_id as community_actor_id,
-       c."local" as community_local,
-       c."name" as community_name,
-       -- creator details
-       u.banned as banned,
-  coalesce(cb.id, 0)::bool as banned_from_community,
-       u.actor_id as creator_actor_id,
-       u.local as creator_local,
-       u.name as creator_name,
-  u.published as creator_published,
-       u.avatar as creator_avatar,
-       -- score details
-       coalesce(cl.total, 0) as score,
-       coalesce(cl.up, 0) as upvotes,
-       coalesce(cl.down, 0) as downvotes,
-       hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
-from comment ct
-left join post p on ct.post_id = p.id
-left join community c on p.community_id = c.id
-left join user_ u on ct.creator_id = u.id 
-left join community_user_ban cb on ct.creator_id = cb.user_id and p.id = ct.post_id and p.community_id = cb.community_id
-left join (
-       select
-               l.comment_id as id,
-               sum(l.score) as total,
-               count(case when l.score = 1 then 1 else null end) as up,
-               count(case when l.score = -1 then 1 else null end) as down
-       from comment_like l
-       group by comment_id
-) as cl on cl.id = ct.id;
+DROP VIEW comment_fast_view;
 
-create or replace view comment_view as (
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_view cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
+DROP VIEW comment_view;
 
-union all 
+DROP VIEW user_mention_fast_view;
 
-select 
-    cav.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_view cav
-);
+DROP TABLE comment_aggregates_fast;
+
+DROP VIEW comment_aggregates_view;
 
-create table comment_aggregates_fast as select * from comment_aggregates_view;
-alter table comment_aggregates_fast add primary key (id);
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    ct.*,
+    -- community details
+    p.community_id,
+    c.actor_id AS community_actor_id,
+    c."local" AS community_local,
+    c."name" AS community_name,
+    -- creator details
+    u.banned AS banned,
+    coalesce(cb.id, 0)::bool AS banned_from_community,
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u.name AS creator_name,
+    u.published AS creator_published,
+    u.avatar AS creator_avatar,
+    -- score details
+    coalesce(cl.total, 0) AS score,
+    coalesce(cl.up, 0) AS upvotes,
+    coalesce(cl.down, 0) AS downvotes,
+    hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
+FROM
+    comment ct
+    LEFT JOIN post p ON ct.post_id = p.id
+    LEFT JOIN community c ON p.community_id = c.id
+    LEFT JOIN user_ u ON ct.creator_id = u.id
+    LEFT JOIN community_user_ban cb ON ct.creator_id = cb.user_id
+        AND p.id = ct.post_id
+        AND p.community_id = cb.community_id
+    LEFT JOIN (
+        SELECT
+            l.comment_id AS id,
+            sum(l.score) AS total,
+            count(
+                CASE WHEN l.score = 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS up,
+            count(
+                CASE WHEN l.score = - 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS down
+        FROM
+            comment_like l
+        GROUP BY
+            comment_id) AS cl ON cl.id = ct.id;
 
-create view comment_fast_view as
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_fast cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
+CREATE OR REPLACE VIEW comment_view AS (
+    SELECT
+        cav.*,
+        us.user_id AS user_id,
+        us.my_vote AS my_vote,
+        us.is_subbed::bool AS subscribed,
+        us.is_saved::bool AS saved
+    FROM
+        comment_aggregates_view cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
+    cav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_view cav);
 
-union all 
+CREATE TABLE comment_aggregates_fast AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
 
-select 
+ALTER TABLE comment_aggregates_fast
+    ADD PRIMARY KEY (id);
+
+CREATE VIEW comment_fast_view AS
+SELECT
+    cav.*,
+    us.user_id AS user_id,
+    us.my_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_saved::bool AS saved
+FROM
+    comment_aggregates_fast cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
     cav.*,
-    null as user_id, 
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_fast cav;
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_fast cav;
 
-create view user_mention_view as
-select 
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.creator_actor_id,
     c.creator_local,
@@ -138,15 +168,30 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_mention um, comment_view c
-where um.comment_id = c.id;
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
 
-create view user_mention_fast_view as 
-select
+CREATE VIEW user_mention_fast_view AS
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -170,26 +215,45 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_ u
-cross join (
-  select
-  ca.*
-  from comment_aggregates_fast ca
-) ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select 
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_ u
+    CROSS JOIN (
+        SELECT
+            ca.*
+        FROM
+            comment_aggregates_fast ca) ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -213,178 +277,221 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    null as user_id, 
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from comment_aggregates_fast ac
-left join user_mention um on um.comment_id = ac.id
-;
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    comment_aggregates_fast ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
 
 -- Do the reply_view referencing the comment_fast_view
-create view reply_fast_view as 
-with closereply as (
-    select 
-    c2.id, 
-    c2.creator_id as sender_id, 
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_fast_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_fast_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_fast_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- add creator_published to the post view
-drop view post_fast_view;
-drop table post_aggregates_fast;
-drop view post_view;
-drop view post_aggregates_view;
+DROP VIEW post_fast_view;
+
+DROP TABLE post_aggregates_fast;
 
-create view post_aggregates_view as
-select
-       p.*,
-       -- creator details
-       u.actor_id as creator_actor_id,
-       u."local" as creator_local,
-       u."name" as creator_name,
-  u.published as creator_published,
-       u.avatar as creator_avatar,
-  u.banned as banned,
-  cb.id::bool as banned_from_community,
-       -- community details
-       c.actor_id as community_actor_id,
-       c."local" as community_local,
-       c."name" as community_name,
-       c.removed as community_removed,
-       c.deleted as community_deleted,
-       c.nsfw as community_nsfw,
-       -- post score data/comment count
-       coalesce(ct.comments, 0) as number_of_comments,
-       coalesce(pl.score, 0) as score,
-       coalesce(pl.upvotes, 0) as upvotes,
-       coalesce(pl.downvotes, 0) as downvotes,
-       hot_rank(
-               coalesce(pl.score , 0), (
-                       case
-                               when (p.published < ('now'::timestamp - '1 month'::interval))
-                               then p.published
-                               else greatest(ct.recent_comment_time, p.published)
-                       end
-               )
-       ) as hot_rank,
-       (
-               case
-                       when (p.published < ('now'::timestamp - '1 month'::interval))
-                       then p.published
-                       else greatest(ct.recent_comment_time, p.published)
-               end
-       ) as newest_activity_time
-from post p
-left join user_ u on p.creator_id = u.id
-left join community_user_ban cb on p.creator_id = cb.user_id and p.community_id = cb.community_id
-left join community c on p.community_id = c.id
-left join (
-       select
-               post_id,
-               count(*) as comments,
-               max(published) as recent_comment_time
-       from comment
-       group by post_id
-) ct on ct.post_id = p.id
-left join (
-       select
-               post_id,
-               sum(score) as score,
-               sum(score) filter (where score = 1) as upvotes,
-               -sum(score) filter (where score = -1) as downvotes
-       from post_like
-       group by post_id
-) pl on pl.post_id = p.id
-order by p.id;
+DROP VIEW post_view;
 
-create view post_view as
-select
-       pav.*,
-       us.id as user_id,
-       us.user_vote as my_vote,
-       us.is_subbed::bool as subscribed,
-       us.is_read::bool as read,
-       us.is_saved::bool as saved
-from post_aggregates_view pav
-cross join lateral (
-       select
-               u.id,
-               coalesce(cf.community_id, 0) as is_subbed,
-               coalesce(pr.post_id, 0) as is_read,
-               coalesce(ps.post_id, 0) as is_saved,
-               coalesce(pl.score, 0) as user_vote
-       from user_ u
-       left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
-       left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
-       left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
-       left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
-) as us
+DROP VIEW post_aggregates_view;
 
-union all
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    -- creator details
+    u.actor_id AS creator_actor_id,
+    u."local" AS creator_local,
+    u."name" AS creator_name,
+    u.published AS creator_published,
+    u.avatar AS creator_avatar,
+    u.banned AS banned,
+    cb.id::bool AS banned_from_community,
+    -- community details
+    c.actor_id AS community_actor_id,
+    c."local" AS community_local,
+    c."name" AS community_name,
+    c.removed AS community_removed,
+    c.deleted AS community_deleted,
+    c.nsfw AS community_nsfw,
+    -- post score data/comment count
+    coalesce(ct.comments, 0) AS number_of_comments,
+    coalesce(pl.score, 0) AS score,
+    coalesce(pl.upvotes, 0) AS upvotes,
+    coalesce(pl.downvotes, 0) AS downvotes,
+    hot_rank (coalesce(pl.score, 0), (
+            CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+                p.published
+            ELSE
+                greatest (ct.recent_comment_time, p.published)
+            END)) AS hot_rank,
+    (
+        CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+            p.published
+        ELSE
+            greatest (ct.recent_comment_time, p.published)
+        END) AS newest_activity_time
+FROM
+    post p
+    LEFT JOIN user_ u ON p.creator_id = u.id
+    LEFT JOIN community_user_ban cb ON p.creator_id = cb.user_id
+        AND p.community_id = cb.community_id
+    LEFT JOIN community c ON p.community_id = c.id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            count(*) AS comments,
+            max(published) AS recent_comment_time
+        FROM
+            comment
+        GROUP BY
+            post_id) ct ON ct.post_id = p.id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            sum(score) AS score,
+            sum(score) FILTER (WHERE score = 1) AS upvotes,
+            - sum(score) FILTER (WHERE score = - 1) AS downvotes
+        FROM
+            post_like
+        GROUP BY
+            post_id) pl ON pl.post_id = p.id
+ORDER BY
+    p.id;
 
-select 
-pav.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from post_aggregates_view pav;
+CREATE VIEW post_view AS
+SELECT
+    pav.*,
+    us.id AS user_id,
+    us.user_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_read::bool AS read,
+    us.is_saved::bool AS saved
+FROM
+    post_aggregates_view pav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id,
+            coalesce(cf.community_id, 0) AS is_subbed,
+            coalesce(pr.post_id, 0) AS is_read,
+            coalesce(ps.post_id, 0) AS is_saved,
+            coalesce(pl.score, 0) AS user_vote
+        FROM
+            user_ u
+            LEFT JOIN community_user_ban cb ON u.id = cb.user_id
+                AND cb.community_id = pav.community_id
+        LEFT JOIN community_follower cf ON u.id = cf.user_id
+            AND cf.community_id = pav.community_id
+    LEFT JOIN post_read pr ON u.id = pr.user_id
+        AND pr.post_id = pav.id
+    LEFT JOIN post_saved ps ON u.id = ps.user_id
+        AND ps.post_id = pav.id
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND pav.id = pl.post_id) AS us
+UNION ALL
+SELECT
+    pav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS read,
+    NULL AS saved
+FROM
+    post_aggregates_view pav;
 
-create table post_aggregates_fast as select * from post_aggregates_view;
-alter table post_aggregates_fast add primary key (id);
+CREATE TABLE post_aggregates_fast AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
 
-create view post_fast_view as 
-select
-       pav.*,
-       us.id as user_id,
-       us.user_vote as my_vote,
-       us.is_subbed::bool as subscribed,
-       us.is_read::bool as read,
-       us.is_saved::bool as saved
-from post_aggregates_fast pav
-cross join lateral (
-       select
-               u.id,
-               coalesce(cf.community_id, 0) as is_subbed,
-               coalesce(pr.post_id, 0) as is_read,
-               coalesce(ps.post_id, 0) as is_saved,
-               coalesce(pl.score, 0) as user_vote
-       from user_ u
-       left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
-       left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
-       left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
-       left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
-) as us
+ALTER TABLE post_aggregates_fast
+    ADD PRIMARY KEY (id);
 
-union all
+CREATE VIEW post_fast_view AS
+SELECT
+    pav.*,
+    us.id AS user_id,
+    us.user_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_read::bool AS read,
+    us.is_saved::bool AS saved
+FROM
+    post_aggregates_fast pav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id,
+            coalesce(cf.community_id, 0) AS is_subbed,
+            coalesce(pr.post_id, 0) AS is_read,
+            coalesce(ps.post_id, 0) AS is_saved,
+            coalesce(pl.score, 0) AS user_vote
+        FROM
+            user_ u
+            LEFT JOIN community_user_ban cb ON u.id = cb.user_id
+                AND cb.community_id = pav.community_id
+        LEFT JOIN community_follower cf ON u.id = cf.user_id
+            AND cf.community_id = pav.community_id
+    LEFT JOIN post_read pr ON u.id = pr.user_id
+        AND pr.post_id = pav.id
+    LEFT JOIN post_saved ps ON u.id = ps.user_id
+        AND ps.post_id = pav.id
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND pav.id = pl.post_id) AS us
+UNION ALL
+SELECT
+    pav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS read,
+    NULL AS saved
+FROM
+    post_aggregates_fast pav;
 
-select 
-pav.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from post_aggregates_fast pav;
\ No newline at end of file
index b7c9d51ef30f723d527dad8ff1c08f925e6ac5da..b29521b4d7bc7933acd572743b4ffd49ddd54a05 100644 (file)
-drop view user_mention_view;
-drop view reply_fast_view;
-drop view comment_fast_view;
-drop view comment_view;
+DROP VIEW user_mention_view;
 
-drop view user_mention_fast_view;
-drop table comment_aggregates_fast;
-drop view comment_aggregates_view;
+DROP VIEW reply_fast_view;
 
-create view comment_aggregates_view as
-select
-       ct.*,
-       -- community details
-       p.community_id,
-       c.actor_id as community_actor_id,
-       c."local" as community_local,
-       c."name" as community_name,
-       -- creator details
-       u.banned as banned,
-  coalesce(cb.id, 0)::bool as banned_from_community,
-       u.actor_id as creator_actor_id,
-       u.local as creator_local,
-       u.name as creator_name,
-  u.published as creator_published,
-       u.avatar as creator_avatar,
-       -- score details
-       coalesce(cl.total, 0) as score,
-       coalesce(cl.up, 0) as upvotes,
-       coalesce(cl.down, 0) as downvotes,
-       hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
-from comment ct
-left join post p on ct.post_id = p.id
-left join community c on p.community_id = c.id
-left join user_ u on ct.creator_id = u.id
-left join community_user_ban cb on ct.creator_id = cb.user_id and p.id = ct.post_id and p.community_id = cb.community_id
-left join (
-       select
-               l.comment_id as id,
-               sum(l.score) as total,
-               count(case when l.score = 1 then 1 else null end) as up,
-               count(case when l.score = -1 then 1 else null end) as down
-       from comment_like l
-       group by comment_id
-) as cl on cl.id = ct.id;
+DROP VIEW comment_fast_view;
 
-create or replace view comment_view as (
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_view cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
+DROP VIEW comment_view;
 
-union all
+DROP VIEW user_mention_fast_view;
 
-select
-    cav.*,
-    null as user_id,
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_view cav
-);
+DROP TABLE comment_aggregates_fast;
+
+DROP VIEW comment_aggregates_view;
 
-create table comment_aggregates_fast as select * from comment_aggregates_view;
-alter table comment_aggregates_fast add primary key (id);
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    ct.*,
+    -- community details
+    p.community_id,
+    c.actor_id AS community_actor_id,
+    c."local" AS community_local,
+    c."name" AS community_name,
+    -- creator details
+    u.banned AS banned,
+    coalesce(cb.id, 0)::bool AS banned_from_community,
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u.name AS creator_name,
+    u.published AS creator_published,
+    u.avatar AS creator_avatar,
+    -- score details
+    coalesce(cl.total, 0) AS score,
+    coalesce(cl.up, 0) AS upvotes,
+    coalesce(cl.down, 0) AS downvotes,
+    hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
+FROM
+    comment ct
+    LEFT JOIN post p ON ct.post_id = p.id
+    LEFT JOIN community c ON p.community_id = c.id
+    LEFT JOIN user_ u ON ct.creator_id = u.id
+    LEFT JOIN community_user_ban cb ON ct.creator_id = cb.user_id
+        AND p.id = ct.post_id
+        AND p.community_id = cb.community_id
+    LEFT JOIN (
+        SELECT
+            l.comment_id AS id,
+            sum(l.score) AS total,
+            count(
+                CASE WHEN l.score = 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS up,
+            count(
+                CASE WHEN l.score = - 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS down
+        FROM
+            comment_like l
+        GROUP BY
+            comment_id) AS cl ON cl.id = ct.id;
 
-create view comment_fast_view as
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_fast cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
+CREATE OR REPLACE VIEW comment_view AS (
+    SELECT
+        cav.*,
+        us.user_id AS user_id,
+        us.my_vote AS my_vote,
+        us.is_subbed::bool AS subscribed,
+        us.is_saved::bool AS saved
+    FROM
+        comment_aggregates_view cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
+    cav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_view cav);
 
-union all
+CREATE TABLE comment_aggregates_fast AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
 
-select
+ALTER TABLE comment_aggregates_fast
+    ADD PRIMARY KEY (id);
+
+CREATE VIEW comment_fast_view AS
+SELECT
+    cav.*,
+    us.user_id AS user_id,
+    us.my_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_saved::bool AS saved
+FROM
+    comment_aggregates_fast cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
     cav.*,
-    null as user_id,
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_fast cav;
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_fast cav;
 
-create view user_mention_view as
-select
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.creator_actor_id,
     c.creator_local,
@@ -138,15 +168,30 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_mention um, comment_view c
-where um.comment_id = c.id;
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
 
-create view user_mention_fast_view as
-select
+CREATE VIEW user_mention_fast_view AS
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -170,26 +215,45 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_ u
-cross join (
-  select
-  ca.*
-  from comment_aggregates_fast ca
-) ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_ u
+    CROSS JOIN (
+        SELECT
+            ca.*
+        FROM
+            comment_aggregates_fast ca) ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -213,37 +277,60 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    null as user_id,
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from comment_aggregates_fast ac
-left join user_mention um on um.comment_id = ac.id
-;
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    comment_aggregates_fast ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
 
 -- Do the reply_view referencing the comment_fast_view
-create view reply_fast_view as
-with closereply as (
-    select
-    c2.id,
-    c2.creator_id as sender_id,
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_fast_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_fast_view cv, closereply
-where closereply.id = cv.id
-;
\ No newline at end of file
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_fast_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
+
index 4cfa7edbc4c151af88956ff07a923073b3942a44..aef427335b0181d422fecf806a9f3ba495b04db7 100644 (file)
-drop view user_mention_view;
-drop view reply_fast_view;
-drop view comment_fast_view;
-drop view comment_view;
+DROP VIEW user_mention_view;
 
-drop view user_mention_fast_view;
-drop table comment_aggregates_fast;
-drop view comment_aggregates_view;
+DROP VIEW reply_fast_view;
 
-create view comment_aggregates_view as
-select
-       ct.*,
-       -- post details
-       p."name" as post_name,
-       p.community_id,
-       -- community details
-       c.actor_id as community_actor_id,
-       c."local" as community_local,
-       c."name" as community_name,
-       -- creator details
-       u.banned as banned,
-  coalesce(cb.id, 0)::bool as banned_from_community,
-       u.actor_id as creator_actor_id,
-       u.local as creator_local,
-       u.name as creator_name,
-  u.published as creator_published,
-       u.avatar as creator_avatar,
-       -- score details
-       coalesce(cl.total, 0) as score,
-       coalesce(cl.up, 0) as upvotes,
-       coalesce(cl.down, 0) as downvotes,
-       hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
-from comment ct
-left join post p on ct.post_id = p.id
-left join community c on p.community_id = c.id
-left join user_ u on ct.creator_id = u.id
-left join community_user_ban cb on ct.creator_id = cb.user_id and p.id = ct.post_id and p.community_id = cb.community_id
-left join (
-       select
-               l.comment_id as id,
-               sum(l.score) as total,
-               count(case when l.score = 1 then 1 else null end) as up,
-               count(case when l.score = -1 then 1 else null end) as down
-       from comment_like l
-       group by comment_id
-) as cl on cl.id = ct.id;
+DROP VIEW comment_fast_view;
 
-create or replace view comment_view as (
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_view cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
+DROP VIEW comment_view;
 
-union all
+DROP VIEW user_mention_fast_view;
 
-select
-    cav.*,
-    null as user_id,
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_view cav
-);
+DROP TABLE comment_aggregates_fast;
+
+DROP VIEW comment_aggregates_view;
 
-create table comment_aggregates_fast as select * from comment_aggregates_view;
-alter table comment_aggregates_fast add primary key (id);
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    ct.*,
+    -- post details
+    p."name" AS post_name,
+    p.community_id,
+    -- community details
+    c.actor_id AS community_actor_id,
+    c."local" AS community_local,
+    c."name" AS community_name,
+    -- creator details
+    u.banned AS banned,
+    coalesce(cb.id, 0)::bool AS banned_from_community,
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u.name AS creator_name,
+    u.published AS creator_published,
+    u.avatar AS creator_avatar,
+    -- score details
+    coalesce(cl.total, 0) AS score,
+    coalesce(cl.up, 0) AS upvotes,
+    coalesce(cl.down, 0) AS downvotes,
+    hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
+FROM
+    comment ct
+    LEFT JOIN post p ON ct.post_id = p.id
+    LEFT JOIN community c ON p.community_id = c.id
+    LEFT JOIN user_ u ON ct.creator_id = u.id
+    LEFT JOIN community_user_ban cb ON ct.creator_id = cb.user_id
+        AND p.id = ct.post_id
+        AND p.community_id = cb.community_id
+    LEFT JOIN (
+        SELECT
+            l.comment_id AS id,
+            sum(l.score) AS total,
+            count(
+                CASE WHEN l.score = 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS up,
+            count(
+                CASE WHEN l.score = - 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS down
+        FROM
+            comment_like l
+        GROUP BY
+            comment_id) AS cl ON cl.id = ct.id;
 
-create view comment_fast_view as
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_fast cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
+CREATE OR REPLACE VIEW comment_view AS (
+    SELECT
+        cav.*,
+        us.user_id AS user_id,
+        us.my_vote AS my_vote,
+        us.is_subbed::bool AS subscribed,
+        us.is_saved::bool AS saved
+    FROM
+        comment_aggregates_view cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
+    cav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_view cav);
 
-union all
+CREATE TABLE comment_aggregates_fast AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
 
-select
+ALTER TABLE comment_aggregates_fast
+    ADD PRIMARY KEY (id);
+
+CREATE VIEW comment_fast_view AS
+SELECT
+    cav.*,
+    us.user_id AS user_id,
+    us.my_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_saved::bool AS saved
+FROM
+    comment_aggregates_fast cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
     cav.*,
-    null as user_id,
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_fast cav;
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_fast cav;
 
-create view user_mention_view as
-select
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.creator_actor_id,
     c.creator_local,
@@ -141,15 +171,30 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_mention um, comment_view c
-where um.comment_id = c.id;
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
 
-create view user_mention_fast_view as
-select
+CREATE VIEW user_mention_fast_view AS
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -174,26 +219,45 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_ u
-cross join (
-  select
-  ca.*
-  from comment_aggregates_fast ca
-) ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_ u
+    CROSS JOIN (
+        SELECT
+            ca.*
+        FROM
+            comment_aggregates_fast ca) ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -218,37 +282,60 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    null as user_id,
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from comment_aggregates_fast ac
-left join user_mention um on um.comment_id = ac.id
-;
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    comment_aggregates_fast ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
 
 -- Do the reply_view referencing the comment_fast_view
-create view reply_fast_view as
-with closereply as (
-    select
-    c2.id,
-    c2.creator_id as sender_id,
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_fast_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_fast_view cv, closereply
-where closereply.id = cv.id
-;
\ No newline at end of file
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_fast_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
+
index 7a4f2e2f1e95c7c48015c5706405da5a06bae366..e74da33d1147b445f6daf8f8abc69410774b8ee1 100644 (file)
@@ -1,20 +1,34 @@
+ALTER TABLE community
+    ALTER COLUMN actor_id SET NOT NULL;
 
-alter table community alter column actor_id set not null;
-alter table community alter column actor_id set default 'http://fake.com';
-alter table user_ alter column actor_id set not null;
-alter table user_ alter column actor_id set default 'http://fake.com';
+ALTER TABLE community
+    ALTER COLUMN actor_id SET DEFAULT 'http://fake.com';
 
-drop function generate_unique_changeme;
+ALTER TABLE user_
+    ALTER COLUMN actor_id SET NOT NULL;
 
-update community
-set actor_id = 'http://fake.com'
-where actor_id like 'changeme_%';
+ALTER TABLE user_
+    ALTER COLUMN actor_id SET DEFAULT 'http://fake.com';
 
-update user_
-set actor_id = 'http://fake.com'
-where actor_id like 'changeme_%';
+DROP FUNCTION generate_unique_changeme;
 
-drop index idx_user_lower_actor_id;
-create unique index idx_user_name_lower_actor_id on user_ (lower(name), lower(actor_id));
+UPDATE
+    community
+SET
+    actor_id = 'http://fake.com'
+WHERE
+    actor_id LIKE 'changeme_%';
+
+UPDATE
+    user_
+SET
+    actor_id = 'http://fake.com'
+WHERE
+    actor_id LIKE 'changeme_%';
+
+DROP INDEX idx_user_lower_actor_id;
+
+CREATE UNIQUE INDEX idx_user_name_lower_actor_id ON user_ (lower(name), lower(actor_id));
+
+DROP INDEX idx_community_lower_actor_id;
 
-drop index idx_community_lower_actor_id;
index e32ed5e089e012aab5a81655fb72e176caf74adb..527c73b1ea8d90f5704dfc97efd1cc526cee685c 100644 (file)
@@ -1,50 +1,78 @@
 -- Following this issue : https://github.com/LemmyNet/lemmy/issues/957
-
 -- Creating a unique changeme actor_id
-create or replace function generate_unique_changeme() 
-returns text language sql 
-as $$
-  select 'changeme_' || string_agg (substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil (random() * 62)::integer, 1), '')
-  from generate_series(1, 20)
+CREATE OR REPLACE FUNCTION generate_unique_changeme ()
+    RETURNS text
+    LANGUAGE sql
+    AS $$
+    SELECT
+        'changeme_' || string_agg(substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil(random() * 62)::integer, 1), '')
+    FROM
+        generate_series(1, 20)
 $$;
 
 -- Need to delete the possible community and user dupes for ones that don't start with the fake one
 -- A few test inserts, to make sure this removes later dupes
 -- insert into community (name, title, category_id, creator_id) values ('testcom', 'another testcom', 1, 2);
-delete from community a using (
-  select min(id) as id, actor_id
-    from community 
-    group by actor_id having count(*) > 1
-) b
-where a.actor_id = b.actor_id 
-and a.id <> b.id;
-
-delete from user_ a using (
-  select min(id) as id, actor_id
-    from user_ 
-    group by actor_id having count(*) > 1
-) b
-where a.actor_id = b.actor_id 
-and a.id <> b.id;
+DELETE FROM community a USING (
+    SELECT
+        min(id) AS id,
+        actor_id
+    FROM
+        community
+    GROUP BY
+        actor_id
+    HAVING
+        count(*) > 1) b
+WHERE
+    a.actor_id = b.actor_id
+    AND a.id <> b.id;
+
+DELETE FROM user_ a USING (
+    SELECT
+        min(id) AS id,
+        actor_id
+    FROM
+        user_
+    GROUP BY
+        actor_id
+    HAVING
+        count(*) > 1) b
+WHERE
+    a.actor_id = b.actor_id
+    AND a.id <> b.id;
 
 -- Replacing the current default on the columns, to the unique one
-update community 
-set actor_id = generate_unique_changeme()
-where actor_id = 'http://fake.com';
+UPDATE
+    community
+SET
+    actor_id = generate_unique_changeme ()
+WHERE
+    actor_id = 'http://fake.com';
 
-update user_ 
-set actor_id = generate_unique_changeme()
-where actor_id = 'http://fake.com';
+UPDATE
+    user_
+SET
+    actor_id = generate_unique_changeme ()
+WHERE
+    actor_id = 'http://fake.com';
 
 -- Add the unique indexes
-alter table community alter column actor_id set not null;
-alter table community alter column actor_id set default generate_unique_changeme();
+ALTER TABLE community
+    ALTER COLUMN actor_id SET NOT NULL;
+
+ALTER TABLE community
+    ALTER COLUMN actor_id SET DEFAULT generate_unique_changeme ();
 
-alter table user_ alter column actor_id set not null;
-alter table user_ alter column actor_id set default generate_unique_changeme();
+ALTER TABLE user_
+    ALTER COLUMN actor_id SET NOT NULL;
+
+ALTER TABLE user_
+    ALTER COLUMN actor_id SET DEFAULT generate_unique_changeme ();
 
 -- Add lowercase uniqueness too
-drop index idx_user_name_lower_actor_id;
-create unique index idx_user_lower_actor_id on user_ (lower(actor_id));
+DROP INDEX idx_user_name_lower_actor_id;
+
+CREATE UNIQUE INDEX idx_user_lower_actor_id ON user_ (lower(actor_id));
+
+CREATE UNIQUE INDEX idx_community_lower_actor_id ON community (lower(actor_id));
 
-create unique index idx_community_lower_actor_id on community (lower(actor_id));
index 8ac1a99c93e3c66241f6e4afd6168db9182aecbe..cb04cdd6a363bcb80328e14e5f51f32f47005efa 100644 (file)
 -- Drops first
-drop view site_view;
-drop table user_fast;
-drop view user_view;
-drop view post_fast_view;
-drop table post_aggregates_fast;
-drop view post_view;
-drop view post_aggregates_view;
-drop view community_moderator_view;
-drop view community_follower_view;
-drop view community_user_ban_view;
-drop view community_view;
-drop view community_aggregates_view;
-drop view community_fast_view;
-drop table community_aggregates_fast;
-drop view private_message_view;
-drop view user_mention_view;
-drop view reply_fast_view;
-drop view comment_fast_view;
-drop view comment_view;
-drop view user_mention_fast_view;
-drop table comment_aggregates_fast;
-drop view comment_aggregates_view;
-
-alter table site 
-  drop column icon,
-  drop column banner;
-
-alter table community 
-  drop column icon,
-  drop column banner;
-
-alter table user_ drop column banner;
+DROP VIEW site_view;
+
+DROP TABLE user_fast;
+
+DROP VIEW user_view;
+
+DROP VIEW post_fast_view;
+
+DROP TABLE post_aggregates_fast;
+
+DROP VIEW post_view;
+
+DROP VIEW post_aggregates_view;
+
+DROP VIEW community_moderator_view;
+
+DROP VIEW community_follower_view;
+
+DROP VIEW community_user_ban_view;
+
+DROP VIEW community_view;
+
+DROP VIEW community_aggregates_view;
+
+DROP VIEW community_fast_view;
+
+DROP TABLE community_aggregates_fast;
+
+DROP VIEW private_message_view;
+
+DROP VIEW user_mention_view;
+
+DROP VIEW reply_fast_view;
+
+DROP VIEW comment_fast_view;
+
+DROP VIEW comment_view;
+
+DROP VIEW user_mention_fast_view;
+
+DROP TABLE comment_aggregates_fast;
+
+DROP VIEW comment_aggregates_view;
+
+ALTER TABLE site
+    DROP COLUMN icon,
+    DROP COLUMN banner;
+
+ALTER TABLE community
+    DROP COLUMN icon,
+    DROP COLUMN banner;
+
+ALTER TABLE user_
+    DROP COLUMN banner;
 
 -- Site
-create view site_view as 
-select *,
-(select name from user_ u where s.creator_id = u.id) as creator_name,
-(select avatar from user_ u where s.creator_id = u.id) as creator_avatar,
-(select count(*) from user_) as number_of_users,
-(select count(*) from post) as number_of_posts,
-(select count(*) from comment) as number_of_comments,
-(select count(*) from community) as number_of_communities
-from site s;
+CREATE VIEW site_view AS
+SELECT
+    *,
+    (
+        SELECT
+            name
+        FROM
+            user_ u
+        WHERE
+            s.creator_id = u.id) AS creator_name,
+    (
+        SELECT
+            avatar
+        FROM
+            user_ u
+        WHERE
+            s.creator_id = u.id) AS creator_avatar,
+    (
+        SELECT
+            count(*)
+        FROM
+            user_) AS number_of_users,
+    (
+        SELECT
+            count(*)
+        FROM
+            post) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment) AS number_of_comments,
+    (
+        SELECT
+            count(*)
+        FROM
+            community) AS number_of_communities
+FROM
+    site s;
 
 -- User
-create view user_view as
-select 
-       u.id,
-  u.actor_id,
-       u.name,
-       u.avatar,
-       u.email,
-       u.matrix_user_id,
-  u.bio,
-  u.local,
-       u.admin,
-       u.banned,
-       u.show_avatars,
-       u.send_notifications_to_email,
-       u.published,
-       coalesce(pd.posts, 0) as number_of_posts,
-       coalesce(pd.score, 0) as post_score,
-       coalesce(cd.comments, 0) as number_of_comments,
-       coalesce(cd.score, 0) as comment_score
-from user_ u
-left join (
-    select
-        p.creator_id as creator_id,
-        count(distinct p.id) as posts,
-        sum(pl.score) as score
-    from post p
-    join post_like pl on p.id = pl.post_id
-    group by p.creator_id
-) pd on u.id = pd.creator_id
-left join (
-    select
-        c.creator_id,
-        count(distinct c.id) as comments,
-        sum(cl.score) as score
-    from comment c
-    join comment_like cl on c.id = cl.comment_id
-    group by c.creator_id
-) cd on u.id = cd.creator_id;
-
-create table user_fast as select * from user_view;
-alter table user_fast add primary key (id);
+CREATE VIEW user_view AS
+SELECT
+    u.id,
+    u.actor_id,
+    u.name,
+    u.avatar,
+    u.email,
+    u.matrix_user_id,
+    u.bio,
+    u.local,
+    u.admin,
+    u.banned,
+    u.show_avatars,
+    u.send_notifications_to_email,
+    u.published,
+    coalesce(pd.posts, 0) AS number_of_posts,
+    coalesce(pd.score, 0) AS post_score,
+    coalesce(cd.comments, 0) AS number_of_comments,
+    coalesce(cd.score, 0) AS comment_score
+FROM
+    user_ u
+    LEFT JOIN (
+        SELECT
+            p.creator_id AS creator_id,
+            count(DISTINCT p.id) AS posts,
+            sum(pl.score) AS score
+        FROM
+            post p
+            JOIN post_like pl ON p.id = pl.post_id
+        GROUP BY
+            p.creator_id) pd ON u.id = pd.creator_id
+    LEFT JOIN (
+        SELECT
+            c.creator_id,
+            count(DISTINCT c.id) AS comments,
+            sum(cl.score) AS score
+        FROM
+            comment c
+            JOIN comment_like cl ON c.id = cl.comment_id
+        GROUP BY
+            c.creator_id) cd ON u.id = cd.creator_id;
+
+CREATE TABLE user_fast AS
+SELECT
+    *
+FROM
+    user_view;
+
+ALTER TABLE user_fast
+    ADD PRIMARY KEY (id);
 
 -- Post fast
-
-create view post_aggregates_view as
-select
-       p.*,
-       -- creator details
-       u.actor_id as creator_actor_id,
-       u."local" as creator_local,
-       u."name" as creator_name,
-  u.published as creator_published,
-       u.avatar as creator_avatar,
-  u.banned as banned,
-  cb.id::bool as banned_from_community,
-       -- community details
-       c.actor_id as community_actor_id,
-       c."local" as community_local,
-       c."name" as community_name,
-       c.removed as community_removed,
-       c.deleted as community_deleted,
-       c.nsfw as community_nsfw,
-       -- post score data/comment count
-       coalesce(ct.comments, 0) as number_of_comments,
-       coalesce(pl.score, 0) as score,
-       coalesce(pl.upvotes, 0) as upvotes,
-       coalesce(pl.downvotes, 0) as downvotes,
-       hot_rank(
-               coalesce(pl.score , 0), (
-                       case
-                               when (p.published < ('now'::timestamp - '1 month'::interval))
-                               then p.published
-                               else greatest(ct.recent_comment_time, p.published)
-                       end
-               )
-       ) as hot_rank,
-       (
-               case
-                       when (p.published < ('now'::timestamp - '1 month'::interval))
-                       then p.published
-                       else greatest(ct.recent_comment_time, p.published)
-               end
-       ) as newest_activity_time
-from post p
-left join user_ u on p.creator_id = u.id
-left join community_user_ban cb on p.creator_id = cb.user_id and p.community_id = cb.community_id
-left join community c on p.community_id = c.id
-left join (
-       select
-               post_id,
-               count(*) as comments,
-               max(published) as recent_comment_time
-       from comment
-       group by post_id
-) ct on ct.post_id = p.id
-left join (
-       select
-               post_id,
-               sum(score) as score,
-               sum(score) filter (where score = 1) as upvotes,
-               -sum(score) filter (where score = -1) as downvotes
-       from post_like
-       group by post_id
-) pl on pl.post_id = p.id
-order by p.id;
-
-create view post_view as
-select
-       pav.*,
-       us.id as user_id,
-       us.user_vote as my_vote,
-       us.is_subbed::bool as subscribed,
-       us.is_read::bool as read,
-       us.is_saved::bool as saved
-from post_aggregates_view pav
-cross join lateral (
-       select
-               u.id,
-               coalesce(cf.community_id, 0) as is_subbed,
-               coalesce(pr.post_id, 0) as is_read,
-               coalesce(ps.post_id, 0) as is_saved,
-               coalesce(pl.score, 0) as user_vote
-       from user_ u
-       left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
-       left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
-       left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
-       left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
-) as us
-
-union all
-
-select 
-pav.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from post_aggregates_view pav;
-
-create table post_aggregates_fast as select * from post_aggregates_view;
-alter table post_aggregates_fast add primary key (id);
-
-create view post_fast_view as 
-select
-       pav.*,
-       us.id as user_id,
-       us.user_vote as my_vote,
-       us.is_subbed::bool as subscribed,
-       us.is_read::bool as read,
-       us.is_saved::bool as saved
-from post_aggregates_fast pav
-cross join lateral (
-       select
-               u.id,
-               coalesce(cf.community_id, 0) as is_subbed,
-               coalesce(pr.post_id, 0) as is_read,
-               coalesce(ps.post_id, 0) as is_saved,
-               coalesce(pl.score, 0) as user_vote
-       from user_ u
-       left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
-       left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
-       left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
-       left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
-) as us
-
-union all
-
-select 
-pav.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from post_aggregates_fast pav;
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    -- creator details
+    u.actor_id AS creator_actor_id,
+    u."local" AS creator_local,
+    u."name" AS creator_name,
+    u.published AS creator_published,
+    u.avatar AS creator_avatar,
+    u.banned AS banned,
+    cb.id::bool AS banned_from_community,
+    -- community details
+    c.actor_id AS community_actor_id,
+    c."local" AS community_local,
+    c."name" AS community_name,
+    c.removed AS community_removed,
+    c.deleted AS community_deleted,
+    c.nsfw AS community_nsfw,
+    -- post score data/comment count
+    coalesce(ct.comments, 0) AS number_of_comments,
+    coalesce(pl.score, 0) AS score,
+    coalesce(pl.upvotes, 0) AS upvotes,
+    coalesce(pl.downvotes, 0) AS downvotes,
+    hot_rank (coalesce(pl.score, 0), (
+            CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+                p.published
+            ELSE
+                greatest (ct.recent_comment_time, p.published)
+            END)) AS hot_rank,
+    (
+        CASE WHEN (p.published < ('now'::timestamp - '1 month'::interval)) THEN
+            p.published
+        ELSE
+            greatest (ct.recent_comment_time, p.published)
+        END) AS newest_activity_time
+FROM
+    post p
+    LEFT JOIN user_ u ON p.creator_id = u.id
+    LEFT JOIN community_user_ban cb ON p.creator_id = cb.user_id
+        AND p.community_id = cb.community_id
+    LEFT JOIN community c ON p.community_id = c.id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            count(*) AS comments,
+            max(published) AS recent_comment_time
+        FROM
+            comment
+        GROUP BY
+            post_id) ct ON ct.post_id = p.id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            sum(score) AS score,
+            sum(score) FILTER (WHERE score = 1) AS upvotes,
+            - sum(score) FILTER (WHERE score = - 1) AS downvotes
+        FROM
+            post_like
+        GROUP BY
+            post_id) pl ON pl.post_id = p.id
+ORDER BY
+    p.id;
+
+CREATE VIEW post_view AS
+SELECT
+    pav.*,
+    us.id AS user_id,
+    us.user_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_read::bool AS read,
+    us.is_saved::bool AS saved
+FROM
+    post_aggregates_view pav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id,
+            coalesce(cf.community_id, 0) AS is_subbed,
+            coalesce(pr.post_id, 0) AS is_read,
+            coalesce(ps.post_id, 0) AS is_saved,
+            coalesce(pl.score, 0) AS user_vote
+        FROM
+            user_ u
+            LEFT JOIN community_user_ban cb ON u.id = cb.user_id
+                AND cb.community_id = pav.community_id
+        LEFT JOIN community_follower cf ON u.id = cf.user_id
+            AND cf.community_id = pav.community_id
+    LEFT JOIN post_read pr ON u.id = pr.user_id
+        AND pr.post_id = pav.id
+    LEFT JOIN post_saved ps ON u.id = ps.user_id
+        AND ps.post_id = pav.id
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND pav.id = pl.post_id) AS us
+UNION ALL
+SELECT
+    pav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS read,
+    NULL AS saved
+FROM
+    post_aggregates_view pav;
+
+CREATE TABLE post_aggregates_fast AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
+
+ALTER TABLE post_aggregates_fast
+    ADD PRIMARY KEY (id);
+
+CREATE VIEW post_fast_view AS
+SELECT
+    pav.*,
+    us.id AS user_id,
+    us.user_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_read::bool AS read,
+    us.is_saved::bool AS saved
+FROM
+    post_aggregates_fast pav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id,
+            coalesce(cf.community_id, 0) AS is_subbed,
+            coalesce(pr.post_id, 0) AS is_read,
+            coalesce(ps.post_id, 0) AS is_saved,
+            coalesce(pl.score, 0) AS user_vote
+        FROM
+            user_ u
+            LEFT JOIN community_user_ban cb ON u.id = cb.user_id
+                AND cb.community_id = pav.community_id
+        LEFT JOIN community_follower cf ON u.id = cf.user_id
+            AND cf.community_id = pav.community_id
+    LEFT JOIN post_read pr ON u.id = pr.user_id
+        AND pr.post_id = pav.id
+    LEFT JOIN post_saved ps ON u.id = ps.user_id
+        AND ps.post_id = pav.id
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND pav.id = pl.post_id) AS us
+UNION ALL
+SELECT
+    pav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS read,
+    NULL AS saved
+FROM
+    post_aggregates_fast pav;
 
 -- Community
-create view community_aggregates_view as
-select 
+CREATE VIEW community_aggregates_view AS
+SELECT
     c.id,
     c.name,
     c.title,
@@ -240,250 +317,292 @@ select
     c.actor_id,
     c.local,
     c.last_refreshed_at,
-    u.actor_id as creator_actor_id,
-    u.local as creator_local,
-    u.name as creator_name,
-    u.avatar as creator_avatar,
-    cat.name as category_name,
-    coalesce(cf.subs, 0) as number_of_subscribers,
-    coalesce(cd.posts, 0) as number_of_posts,
-    coalesce(cd.comments, 0) as number_of_comments,
-    hot_rank(cf.subs, c.published) as hot_rank
-from community c
-left join user_ u on c.creator_id = u.id
-left join category cat on c.category_id = cat.id
-left join (
-    select
-        p.community_id,
-        count(distinct p.id) as posts,
-        count(distinct ct.id) as comments
-    from post p
-    join comment ct on p.id = ct.post_id
-    group by p.community_id
-) cd on cd.community_id = c.id
-left join (
-    select
-        community_id,
-        count(*) as subs 
-    from community_follower
-    group by community_id 
-) cf on cf.community_id = c.id;
-
-create view community_view as
-select
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u.name AS creator_name,
+    u.avatar AS creator_avatar,
+    cat.name AS category_name,
+    coalesce(cf.subs, 0) AS number_of_subscribers,
+    coalesce(cd.posts, 0) AS number_of_posts,
+    coalesce(cd.comments, 0) AS number_of_comments,
+    hot_rank (cf.subs, c.published) AS hot_rank
+FROM
+    community c
+    LEFT JOIN user_ u ON c.creator_id = u.id
+    LEFT JOIN category cat ON c.category_id = cat.id
+    LEFT JOIN (
+        SELECT
+            p.community_id,
+            count(DISTINCT p.id) AS posts,
+            count(DISTINCT ct.id) AS comments
+        FROM
+            post p
+            JOIN comment ct ON p.id = ct.post_id
+        GROUP BY
+            p.community_id) cd ON cd.community_id = c.id
+    LEFT JOIN (
+        SELECT
+            community_id,
+            count(*) AS subs
+        FROM
+            community_follower
+        GROUP BY
+            community_id) cf ON cf.community_id = c.id;
+
+CREATE VIEW community_view AS
+SELECT
     cv.*,
-    us.user as user_id,
-    us.is_subbed::bool as subscribed
-from community_aggregates_view cv
-cross join lateral (
-       select
-               u.id as user,
-               coalesce(cf.community_id, 0) as is_subbed
-       from user_ u
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = cv.id
-) as us
-
-union all
-
-select 
+    us.user AS user_id,
+    us.is_subbed::bool AS subscribed
+FROM
+    community_aggregates_view cv
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user,
+            coalesce(cf.community_id, 0) AS is_subbed
+        FROM
+            user_ u
+            LEFT JOIN community_follower cf ON u.id = cf.user_id
+                AND cf.community_id = cv.id) AS us
+UNION ALL
+SELECT
     cv.*,
-    null as user_id,
-    null as subscribed
-from community_aggregates_view cv;
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    community_aggregates_view cv;
 
-create view community_moderator_view as
-select
+CREATE VIEW community_moderator_view AS
+SELECT
     cm.*,
-    u.actor_id as user_actor_id,
-    u.local as user_local,
-    u.name as user_name,
-    u.avatar as avatar,
-    c.actor_id as community_actor_id,
-    c.local as community_local,
-    c.name as community_name
-from community_moderator cm
-left join user_ u on cm.user_id = u.id
-left join community c on cm.community_id = c.id;
-
-create view community_follower_view as
-select
+    u.actor_id AS user_actor_id,
+    u.local AS user_local,
+    u.name AS user_name,
+    u.avatar AS avatar,
+    c.actor_id AS community_actor_id,
+    c.local AS community_local,
+    c.name AS community_name
+FROM
+    community_moderator cm
+    LEFT JOIN user_ u ON cm.user_id = u.id
+    LEFT JOIN community c ON cm.community_id = c.id;
+
+CREATE VIEW community_follower_view AS
+SELECT
     cf.*,
-    u.actor_id as user_actor_id,
-    u.local as user_local,
-    u.name as user_name,
-    u.avatar as avatar,
-    c.actor_id as community_actor_id,
-    c.local as community_local,
-    c.name as community_name
-from community_follower cf
-left join user_ u on cf.user_id = u.id
-left join community c on cf.community_id = c.id;
-
-create view community_user_ban_view as
-select
+    u.actor_id AS user_actor_id,
+    u.local AS user_local,
+    u.name AS user_name,
+    u.avatar AS avatar,
+    c.actor_id AS community_actor_id,
+    c.local AS community_local,
+    c.name AS community_name
+FROM
+    community_follower cf
+    LEFT JOIN user_ u ON cf.user_id = u.id
+    LEFT JOIN community c ON cf.community_id = c.id;
+
+CREATE VIEW community_user_ban_view AS
+SELECT
     cb.*,
-    u.actor_id as user_actor_id,
-    u.local as user_local,
-    u.name as user_name,
-    u.avatar as avatar,
-    c.actor_id as community_actor_id,
-    c.local as community_local,
-    c.name as community_name
-from community_user_ban cb
-left join user_ u on cb.user_id = u.id
-left join community c on cb.community_id = c.id;
+    u.actor_id AS user_actor_id,
+    u.local AS user_local,
+    u.name AS user_name,
+    u.avatar AS avatar,
+    c.actor_id AS community_actor_id,
+    c.local AS community_local,
+    c.name AS community_name
+FROM
+    community_user_ban cb
+    LEFT JOIN user_ u ON cb.user_id = u.id
+    LEFT JOIN community c ON cb.community_id = c.id;
 
 -- The community fast table
-
-create table community_aggregates_fast as select * from community_aggregates_view;
-alter table community_aggregates_fast add primary key (id);
-
-create view community_fast_view as
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join (
-  select
-  ca.*
-  from community_aggregates_fast ca
-) ac
-
-union all
-
-select 
-caf.*,
-null as user_id,
-null as subscribed
-from community_aggregates_fast caf;
-
+CREATE TABLE community_aggregates_fast AS
+SELECT
+    *
+FROM
+    community_aggregates_view;
+
+ALTER TABLE community_aggregates_fast
+    ADD PRIMARY KEY (id);
+
+CREATE VIEW community_fast_view AS
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN (
+        SELECT
+            ca.*
+        FROM
+            community_aggregates_fast ca) ac
+UNION ALL
+SELECT
+    caf.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    community_aggregates_fast caf;
 
 -- Private message
-create view private_message_view as 
-select        
-pm.*,
-u.name as creator_name,
-u.avatar as creator_avatar,
-u.actor_id as creator_actor_id,
-u.local as creator_local,
-u2.name as recipient_name,
-u2.avatar as recipient_avatar,
-u2.actor_id as recipient_actor_id,
-u2.local as recipient_local
-from private_message pm
-inner join user_ u on u.id = pm.creator_id
-inner join user_ u2 on u2.id = pm.recipient_id;
-
+CREATE VIEW private_message_view AS
+SELECT
+    pm.*,
+    u.name AS creator_name,
+    u.avatar AS creator_avatar,
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u2.name AS recipient_name,
+    u2.avatar AS recipient_avatar,
+    u2.actor_id AS recipient_actor_id,
+    u2.local AS recipient_local
+FROM
+    private_message pm
+    INNER JOIN user_ u ON u.id = pm.creator_id
+    INNER JOIN user_ u2 ON u2.id = pm.recipient_id;
 
 -- Comments, mentions, replies
-
-create view comment_aggregates_view as
-select
-       ct.*,
-       -- post details
-       p."name" as post_name,
-       p.community_id,
-       -- community details
-       c.actor_id as community_actor_id,
-       c."local" as community_local,
-       c."name" as community_name,
-       -- creator details
-       u.banned as banned,
-  coalesce(cb.id, 0)::bool as banned_from_community,
-       u.actor_id as creator_actor_id,
-       u.local as creator_local,
-       u.name as creator_name,
-  u.published as creator_published,
-       u.avatar as creator_avatar,
-       -- score details
-       coalesce(cl.total, 0) as score,
-       coalesce(cl.up, 0) as upvotes,
-       coalesce(cl.down, 0) as downvotes,
-       hot_rank(coalesce(cl.total, 0), ct.published) as hot_rank
-from comment ct
-left join post p on ct.post_id = p.id
-left join community c on p.community_id = c.id
-left join user_ u on ct.creator_id = u.id
-left join community_user_ban cb on ct.creator_id = cb.user_id and p.id = ct.post_id and p.community_id = cb.community_id
-left join (
-       select
-               l.comment_id as id,
-               sum(l.score) as total,
-               count(case when l.score = 1 then 1 else null end) as up,
-               count(case when l.score = -1 then 1 else null end) as down
-       from comment_like l
-       group by comment_id
-) as cl on cl.id = ct.id;
-
-create or replace view comment_view as (
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_view cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
-
-union all
-
-select
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    ct.*,
+    -- post details
+    p."name" AS post_name,
+    p.community_id,
+    -- community details
+    c.actor_id AS community_actor_id,
+    c."local" AS community_local,
+    c."name" AS community_name,
+    -- creator details
+    u.banned AS banned,
+    coalesce(cb.id, 0)::bool AS banned_from_community,
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u.name AS creator_name,
+    u.published AS creator_published,
+    u.avatar AS creator_avatar,
+    -- score details
+    coalesce(cl.total, 0) AS score,
+    coalesce(cl.up, 0) AS upvotes,
+    coalesce(cl.down, 0) AS downvotes,
+    hot_rank (coalesce(cl.total, 0), ct.published) AS hot_rank
+FROM
+    comment ct
+    LEFT JOIN post p ON ct.post_id = p.id
+    LEFT JOIN community c ON p.community_id = c.id
+    LEFT JOIN user_ u ON ct.creator_id = u.id
+    LEFT JOIN community_user_ban cb ON ct.creator_id = cb.user_id
+        AND p.id = ct.post_id
+        AND p.community_id = cb.community_id
+    LEFT JOIN (
+        SELECT
+            l.comment_id AS id,
+            sum(l.score) AS total,
+            count(
+                CASE WHEN l.score = 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS up,
+            count(
+                CASE WHEN l.score = - 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS down
+        FROM
+            comment_like l
+        GROUP BY
+            comment_id) AS cl ON cl.id = ct.id;
+
+CREATE OR REPLACE VIEW comment_view AS (
+    SELECT
+        cav.*,
+        us.user_id AS user_id,
+        us.my_vote AS my_vote,
+        us.is_subbed::bool AS subscribed,
+        us.is_saved::bool AS saved
+    FROM
+        comment_aggregates_view cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
+    cav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_view cav);
+
+CREATE TABLE comment_aggregates_fast AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
+
+ALTER TABLE comment_aggregates_fast
+    ADD PRIMARY KEY (id);
+
+CREATE VIEW comment_fast_view AS
+SELECT
     cav.*,
-    null as user_id,
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_view cav
-);
-
-create table comment_aggregates_fast as select * from comment_aggregates_view;
-alter table comment_aggregates_fast add primary key (id);
-
-create view comment_fast_view as
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_fast cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
-
-union all
-
-select
+    us.user_id AS user_id,
+    us.my_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_saved::bool AS saved
+FROM
+    comment_aggregates_fast cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
     cav.*,
-    null as user_id,
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_fast cav;
-
-create view user_mention_view as
-select
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_fast cav;
+
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.creator_actor_id,
     c.creator_local,
@@ -512,15 +631,30 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_mention um, comment_view c
-where um.comment_id = c.id;
-
-create view user_mention_fast_view as
-select
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
+
+CREATE VIEW user_mention_fast_view AS
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -545,26 +679,45 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_ u
-cross join (
-  select
-  ca.*
-  from comment_aggregates_fast ca
-) ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_ u
+    CROSS JOIN (
+        SELECT
+            ca.*
+        FROM
+            comment_aggregates_fast ca) ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -589,116 +742,204 @@ select
     ac.upvotes,
     ac.downvotes,
     ac.hot_rank,
-    null as user_id,
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from comment_aggregates_fast ac
-left join user_mention um on um.comment_id = ac.id
-;
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    comment_aggregates_fast ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
 
 -- Do the reply_view referencing the comment_fast_view
-create view reply_fast_view as
-with closereply as (
-    select
-    c2.id,
-    c2.creator_id as sender_id,
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_fast_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_fast_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_fast_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- redoing the triggers
-create or replace function refresh_post()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
-
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts - 1 where id = OLD.community_id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
-
-    -- Update that users number of posts, post score
-    delete from user_fast where id = NEW.creator_id;
-    insert into user_fast select * from user_view where id = NEW.creator_id;
-  
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts + 1 where id = NEW.community_id;
-
-    -- Update the hot rank on the post table
-    -- TODO this might not correctly update it, using a 1 week interval
-    update post_aggregates_fast as paf
-    set hot_rank = pav.hot_rank 
-    from post_aggregates_view as pav
-    where paf.id = pav.id  and (pav.published > ('now'::timestamp - '1 week'::interval));
-  END IF;
-
-  return null;
-end $$;
-
-create or replace function refresh_comment()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments - 1
-    from post as p
-    where caf.id = p.community_id and p.id = OLD.post_id;
-
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
-
-    -- Update user view due to comment count
-    update user_fast 
-    set number_of_comments = number_of_comments + 1
-    where id = NEW.creator_id;
-    
-    -- Update post view due to comment count, new comment activity time, but only on new posts
-    -- TODO this could be done more efficiently
-    delete from post_aggregates_fast where id = NEW.post_id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.post_id;
-
-    -- Force the hot rank as zero on week-older posts
-    update post_aggregates_fast as paf
-    set hot_rank = 0
-    where paf.id = NEW.post_id and (paf.published < ('now'::timestamp - '1 week'::interval));
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments + 1 
-    from post as p
-    where caf.id = p.community_id and p.id = NEW.post_id;
-
-  END IF;
-
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION refresh_post ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts - 1
+        WHERE
+            id = OLD.community_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update that users number of posts, post score
+        DELETE FROM user_fast
+        WHERE id = NEW.creator_id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.creator_id;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts + 1
+        WHERE
+            id = NEW.community_id;
+        -- Update the hot rank on the post table
+        -- TODO this might not correctly update it, using a 1 week interval
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = pav.hot_rank
+        FROM
+            post_aggregates_view AS pav
+        WHERE
+            paf.id = pav.id
+            AND (pav.published > ('now'::timestamp - '1 week'::interval));
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION refresh_comment ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments - 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = OLD.post_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update user view due to comment count
+        UPDATE
+            user_fast
+        SET
+            number_of_comments = number_of_comments + 1
+        WHERE
+            id = NEW.creator_id;
+        -- Update post view due to comment count, new comment activity time, but only on new posts
+        -- TODO this could be done more efficiently
+        DELETE FROM post_aggregates_fast
+        WHERE id = NEW.post_id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.post_id;
+        -- Force the hot rank as zero on week-older posts
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = 0
+        WHERE
+            paf.id = NEW.post_id
+            AND (paf.published < ('now'::timestamp - '1 week'::interval));
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments + 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = NEW.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
index 97f35fb0ed7193836ce1fbb5199d13a12a8b7cf0..7293de2058d254930b6b1f406d214c2676539560 100644 (file)
 --  Community Banner
 --  User Banner (User avatar is already there)
 --  User preferred name (already in table, needs to be added to view)
-
 -- It also adds hot_rank_active to post_view
-
-alter table site 
-  add column icon text,
-  add column banner text;
-
-alter table community 
-  add column icon text,
-  add column banner text;
-
-alter table user_ add column banner text;
-
-drop view site_view;
-create view site_view as 
-select s.*,
-u.name as creator_name,
-u.preferred_username as creator_preferred_username, 
-u.avatar as creator_avatar,
-(select count(*) from user_) as number_of_users,
-(select count(*) from post) as number_of_posts,
-(select count(*) from comment) as number_of_comments,
-(select count(*) from community) as number_of_communities
-from site s
-left join user_ u on s.creator_id = u.id;
+ALTER TABLE site
+    ADD COLUMN icon text,
+    ADD COLUMN banner text;
+
+ALTER TABLE community
+    ADD COLUMN icon text,
+    ADD COLUMN banner text;
+
+ALTER TABLE user_
+    ADD COLUMN banner text;
+
+DROP VIEW site_view;
+
+CREATE VIEW site_view AS
+SELECT
+    s.*,
+    u.name AS creator_name,
+    u.preferred_username AS creator_preferred_username,
+    u.avatar AS creator_avatar,
+    (
+        SELECT
+            count(*)
+        FROM
+            user_) AS number_of_users,
+    (
+        SELECT
+            count(*)
+        FROM
+            post) AS number_of_posts,
+    (
+        SELECT
+            count(*)
+        FROM
+            comment) AS number_of_comments,
+    (
+        SELECT
+            count(*)
+        FROM
+            community) AS number_of_communities
+FROM
+    site s
+    LEFT JOIN user_ u ON s.creator_id = u.id;
 
 -- User
-drop table user_fast;
-drop view user_view;
-create view user_view as
-select 
-       u.id,
-  u.actor_id,
-       u.name,
-  u.preferred_username,
-       u.avatar,
-  u.banner,
-       u.email,
-       u.matrix_user_id,
-  u.bio,
-  u.local,
-       u.admin,
-       u.banned,
-       u.show_avatars,
-       u.send_notifications_to_email,
-       u.published,
-       coalesce(pd.posts, 0) as number_of_posts,
-       coalesce(pd.score, 0) as post_score,
-       coalesce(cd.comments, 0) as number_of_comments,
-       coalesce(cd.score, 0) as comment_score
-from user_ u
-left join (
-    select
-        p.creator_id as creator_id,
-        count(distinct p.id) as posts,
-        sum(pl.score) as score
-    from post p
-    join post_like pl on p.id = pl.post_id
-    group by p.creator_id
-) pd on u.id = pd.creator_id
-left join (
-    select
-        c.creator_id,
-        count(distinct c.id) as comments,
-        sum(cl.score) as score
-    from comment c
-    join comment_like cl on c.id = cl.comment_id
-    group by c.creator_id
-) cd on u.id = cd.creator_id;
-
-create table user_fast as select * from user_view;
-alter table user_fast add primary key (id);
+DROP TABLE user_fast;
+
+DROP VIEW user_view;
+
+CREATE VIEW user_view AS
+SELECT
+    u.id,
+    u.actor_id,
+    u.name,
+    u.preferred_username,
+    u.avatar,
+    u.banner,
+    u.email,
+    u.matrix_user_id,
+    u.bio,
+    u.local,
+    u.admin,
+    u.banned,
+    u.show_avatars,
+    u.send_notifications_to_email,
+    u.published,
+    coalesce(pd.posts, 0) AS number_of_posts,
+    coalesce(pd.score, 0) AS post_score,
+    coalesce(cd.comments, 0) AS number_of_comments,
+    coalesce(cd.score, 0) AS comment_score
+FROM
+    user_ u
+    LEFT JOIN (
+        SELECT
+            p.creator_id AS creator_id,
+            count(DISTINCT p.id) AS posts,
+            sum(pl.score) AS score
+        FROM
+            post p
+            JOIN post_like pl ON p.id = pl.post_id
+        GROUP BY
+            p.creator_id) pd ON u.id = pd.creator_id
+    LEFT JOIN (
+        SELECT
+            c.creator_id,
+            count(DISTINCT c.id) AS comments,
+            sum(cl.score) AS score
+        FROM
+            comment c
+            JOIN comment_like cl ON c.id = cl.comment_id
+        GROUP BY
+            c.creator_id) cd ON u.id = cd.creator_id;
+
+CREATE TABLE user_fast AS
+SELECT
+    *
+FROM
+    user_view;
+
+ALTER TABLE user_fast
+    ADD PRIMARY KEY (id);
 
 -- private message
-drop view private_message_view;
-create view private_message_view as 
-select        
-pm.*,
-u.name as creator_name,
-u.preferred_username as creator_preferred_username,
-u.avatar as creator_avatar,
-u.actor_id as creator_actor_id,
-u.local as creator_local,
-u2.name as recipient_name,
-u2.preferred_username as recipient_preferred_username,
-u2.avatar as recipient_avatar,
-u2.actor_id as recipient_actor_id,
-u2.local as recipient_local
-from private_message pm
-inner join user_ u on u.id = pm.creator_id
-inner join user_ u2 on u2.id = pm.recipient_id;
+DROP VIEW private_message_view;
+
+CREATE VIEW private_message_view AS
+SELECT
+    pm.*,
+    u.name AS creator_name,
+    u.preferred_username AS creator_preferred_username,
+    u.avatar AS creator_avatar,
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u2.name AS recipient_name,
+    u2.preferred_username AS recipient_preferred_username,
+    u2.avatar AS recipient_avatar,
+    u2.actor_id AS recipient_actor_id,
+    u2.local AS recipient_local
+FROM
+    private_message pm
+    INNER JOIN user_ u ON u.id = pm.creator_id
+    INNER JOIN user_ u2 ON u2.id = pm.recipient_id;
 
 -- Post fast
-drop view post_fast_view;
-drop table post_aggregates_fast;
-drop view post_view;
-drop view post_aggregates_view;
-
-create view post_aggregates_view as
-select
-       p.*,
-       -- creator details
-       u.actor_id as creator_actor_id,
-       u."local" as creator_local,
-       u."name" as creator_name,
-  u."preferred_username" as creator_preferred_username,
-  u.published as creator_published,
-       u.avatar as creator_avatar,
-  u.banned as banned,
-  cb.id::bool as banned_from_community,
-       -- community details
-       c.actor_id as community_actor_id,
-       c."local" as community_local,
-       c."name" as community_name,
-  c.icon as community_icon,
-       c.removed as community_removed,
-       c.deleted as community_deleted,
-       c.nsfw as community_nsfw,
-       -- post score data/comment count
-       coalesce(ct.comments, 0) as number_of_comments,
-       coalesce(pl.score, 0) as score,
-       coalesce(pl.upvotes, 0) as upvotes,
-       coalesce(pl.downvotes, 0) as downvotes,
-       hot_rank(coalesce(pl.score, 1), p.published) as hot_rank,
-  hot_rank(coalesce(pl.score, 1), greatest(ct.recent_comment_time, p.published)) as hot_rank_active,
-       greatest(ct.recent_comment_time, p.published) as newest_activity_time
-from post p
-left join user_ u on p.creator_id = u.id
-left join community_user_ban cb on p.creator_id = cb.user_id and p.community_id = cb.community_id
-left join community c on p.community_id = c.id
-left join (
-       select
-               post_id,
-               count(*) as comments,
-               max(published) as recent_comment_time
-       from comment
-       group by post_id
-) ct on ct.post_id = p.id
-left join (
-       select
-               post_id,
-               sum(score) as score,
-               sum(score) filter (where score = 1) as upvotes,
-               -sum(score) filter (where score = -1) as downvotes
-       from post_like
-       group by post_id
-) pl on pl.post_id = p.id
-order by p.id;
-
-create view post_view as
-select
-       pav.*,
-       us.id as user_id,
-       us.user_vote as my_vote,
-       us.is_subbed::bool as subscribed,
-       us.is_read::bool as read,
-       us.is_saved::bool as saved
-from post_aggregates_view pav
-cross join lateral (
-       select
-               u.id,
-               coalesce(cf.community_id, 0) as is_subbed,
-               coalesce(pr.post_id, 0) as is_read,
-               coalesce(ps.post_id, 0) as is_saved,
-               coalesce(pl.score, 0) as user_vote
-       from user_ u
-       left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
-       left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
-       left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
-       left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
-) as us
-
-union all
-
-select 
-pav.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from post_aggregates_view pav;
-
-create table post_aggregates_fast as select * from post_aggregates_view;
-alter table post_aggregates_fast add primary key (id);
+DROP VIEW post_fast_view;
+
+DROP TABLE post_aggregates_fast;
+
+DROP VIEW post_view;
+
+DROP VIEW post_aggregates_view;
+
+CREATE VIEW post_aggregates_view AS
+SELECT
+    p.*,
+    -- creator details
+    u.actor_id AS creator_actor_id,
+    u."local" AS creator_local,
+    u."name" AS creator_name,
+    u."preferred_username" AS creator_preferred_username,
+    u.published AS creator_published,
+    u.avatar AS creator_avatar,
+    u.banned AS banned,
+    cb.id::bool AS banned_from_community,
+    -- community details
+    c.actor_id AS community_actor_id,
+    c."local" AS community_local,
+    c."name" AS community_name,
+    c.icon AS community_icon,
+    c.removed AS community_removed,
+    c.deleted AS community_deleted,
+    c.nsfw AS community_nsfw,
+    -- post score data/comment count
+    coalesce(ct.comments, 0) AS number_of_comments,
+    coalesce(pl.score, 0) AS score,
+    coalesce(pl.upvotes, 0) AS upvotes,
+    coalesce(pl.downvotes, 0) AS downvotes,
+    hot_rank (coalesce(pl.score, 1), p.published) AS hot_rank,
+    hot_rank (coalesce(pl.score, 1), greatest (ct.recent_comment_time, p.published)) AS hot_rank_active,
+    greatest (ct.recent_comment_time, p.published) AS newest_activity_time
+FROM
+    post p
+    LEFT JOIN user_ u ON p.creator_id = u.id
+    LEFT JOIN community_user_ban cb ON p.creator_id = cb.user_id
+        AND p.community_id = cb.community_id
+    LEFT JOIN community c ON p.community_id = c.id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            count(*) AS comments,
+            max(published) AS recent_comment_time
+        FROM
+            comment
+        GROUP BY
+            post_id) ct ON ct.post_id = p.id
+    LEFT JOIN (
+        SELECT
+            post_id,
+            sum(score) AS score,
+            sum(score) FILTER (WHERE score = 1) AS upvotes,
+            - sum(score) FILTER (WHERE score = - 1) AS downvotes
+        FROM
+            post_like
+        GROUP BY
+            post_id) pl ON pl.post_id = p.id
+ORDER BY
+    p.id;
+
+CREATE VIEW post_view AS
+SELECT
+    pav.*,
+    us.id AS user_id,
+    us.user_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_read::bool AS read,
+    us.is_saved::bool AS saved
+FROM
+    post_aggregates_view pav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id,
+            coalesce(cf.community_id, 0) AS is_subbed,
+            coalesce(pr.post_id, 0) AS is_read,
+            coalesce(ps.post_id, 0) AS is_saved,
+            coalesce(pl.score, 0) AS user_vote
+        FROM
+            user_ u
+            LEFT JOIN community_user_ban cb ON u.id = cb.user_id
+                AND cb.community_id = pav.community_id
+        LEFT JOIN community_follower cf ON u.id = cf.user_id
+            AND cf.community_id = pav.community_id
+    LEFT JOIN post_read pr ON u.id = pr.user_id
+        AND pr.post_id = pav.id
+    LEFT JOIN post_saved ps ON u.id = ps.user_id
+        AND ps.post_id = pav.id
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND pav.id = pl.post_id) AS us
+UNION ALL
+SELECT
+    pav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS read,
+    NULL AS saved
+FROM
+    post_aggregates_view pav;
+
+CREATE TABLE post_aggregates_fast AS
+SELECT
+    *
+FROM
+    post_aggregates_view;
+
+ALTER TABLE post_aggregates_fast
+    ADD PRIMARY KEY (id);
 
 -- For the hot rank resorting
-create index idx_post_aggregates_fast_hot_rank_published on post_aggregates_fast (hot_rank desc, published desc);
-create index idx_post_aggregates_fast_hot_rank_active_published on post_aggregates_fast (hot_rank_active desc, published desc);
-
-create view post_fast_view as 
-select
-       pav.*,
-       us.id as user_id,
-       us.user_vote as my_vote,
-       us.is_subbed::bool as subscribed,
-       us.is_read::bool as read,
-       us.is_saved::bool as saved
-from post_aggregates_fast pav
-cross join lateral (
-       select
-               u.id,
-               coalesce(cf.community_id, 0) as is_subbed,
-               coalesce(pr.post_id, 0) as is_read,
-               coalesce(ps.post_id, 0) as is_saved,
-               coalesce(pl.score, 0) as user_vote
-       from user_ u
-       left join community_user_ban cb on u.id = cb.user_id and cb.community_id = pav.community_id
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = pav.community_id
-       left join post_read pr on u.id = pr.user_id and pr.post_id = pav.id
-       left join post_saved ps on u.id = ps.user_id and ps.post_id = pav.id
-       left join post_like pl on u.id = pl.user_id and pav.id = pl.post_id
-) as us
-
-union all
-
-select 
-pav.*,
-null as user_id,
-null as my_vote,
-null as subscribed,
-null as read,
-null as saved
-from post_aggregates_fast pav;
+CREATE INDEX idx_post_aggregates_fast_hot_rank_published ON post_aggregates_fast (hot_rank DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_fast_hot_rank_active_published ON post_aggregates_fast (hot_rank_active DESC, published DESC);
+
+CREATE VIEW post_fast_view AS
+SELECT
+    pav.*,
+    us.id AS user_id,
+    us.user_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_read::bool AS read,
+    us.is_saved::bool AS saved
+FROM
+    post_aggregates_fast pav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id,
+            coalesce(cf.community_id, 0) AS is_subbed,
+            coalesce(pr.post_id, 0) AS is_read,
+            coalesce(ps.post_id, 0) AS is_saved,
+            coalesce(pl.score, 0) AS user_vote
+        FROM
+            user_ u
+            LEFT JOIN community_user_ban cb ON u.id = cb.user_id
+                AND cb.community_id = pav.community_id
+        LEFT JOIN community_follower cf ON u.id = cf.user_id
+            AND cf.community_id = pav.community_id
+    LEFT JOIN post_read pr ON u.id = pr.user_id
+        AND pr.post_id = pav.id
+    LEFT JOIN post_saved ps ON u.id = ps.user_id
+        AND ps.post_id = pav.id
+    LEFT JOIN post_like pl ON u.id = pl.user_id
+        AND pav.id = pl.post_id) AS us
+UNION ALL
+SELECT
+    pav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS read,
+    NULL AS saved
+FROM
+    post_aggregates_fast pav;
 
 -- Community
-drop view community_moderator_view;
-drop view community_follower_view;
-drop view community_user_ban_view;
-drop view community_view;
-drop view community_aggregates_view;
-drop view community_fast_view;
-drop table community_aggregates_fast;
-
-create view community_aggregates_view as
-select 
+DROP VIEW community_moderator_view;
+
+DROP VIEW community_follower_view;
+
+DROP VIEW community_user_ban_view;
+
+DROP VIEW community_view;
+
+DROP VIEW community_aggregates_view;
+
+DROP VIEW community_fast_view;
+
+DROP TABLE community_aggregates_fast;
+
+CREATE VIEW community_aggregates_view AS
+SELECT
     c.id,
     c.name,
     c.title,
@@ -258,249 +320,299 @@ select
     c.actor_id,
     c.local,
     c.last_refreshed_at,
-    u.actor_id as creator_actor_id,
-    u.local as creator_local,
-    u.name as creator_name,
-    u.preferred_username as creator_preferred_username,
-    u.avatar as creator_avatar,
-    cat.name as category_name,
-    coalesce(cf.subs, 0) as number_of_subscribers,
-    coalesce(cd.posts, 0) as number_of_posts,
-    coalesce(cd.comments, 0) as number_of_comments,
-    hot_rank(cf.subs, c.published) as hot_rank
-from community c
-left join user_ u on c.creator_id = u.id
-left join category cat on c.category_id = cat.id
-left join (
-    select
-        p.community_id,
-        count(distinct p.id) as posts,
-        count(distinct ct.id) as comments
-    from post p
-    join comment ct on p.id = ct.post_id
-    group by p.community_id
-) cd on cd.community_id = c.id
-left join (
-    select
-        community_id,
-        count(*) as subs 
-    from community_follower
-    group by community_id 
-) cf on cf.community_id = c.id;
-
-create view community_view as
-select
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u.name AS creator_name,
+    u.preferred_username AS creator_preferred_username,
+    u.avatar AS creator_avatar,
+    cat.name AS category_name,
+    coalesce(cf.subs, 0) AS number_of_subscribers,
+    coalesce(cd.posts, 0) AS number_of_posts,
+    coalesce(cd.comments, 0) AS number_of_comments,
+    hot_rank (cf.subs, c.published) AS hot_rank
+FROM
+    community c
+    LEFT JOIN user_ u ON c.creator_id = u.id
+    LEFT JOIN category cat ON c.category_id = cat.id
+    LEFT JOIN (
+        SELECT
+            p.community_id,
+            count(DISTINCT p.id) AS posts,
+            count(DISTINCT ct.id) AS comments
+        FROM
+            post p
+            JOIN comment ct ON p.id = ct.post_id
+        GROUP BY
+            p.community_id) cd ON cd.community_id = c.id
+    LEFT JOIN (
+        SELECT
+            community_id,
+            count(*) AS subs
+        FROM
+            community_follower
+        GROUP BY
+            community_id) cf ON cf.community_id = c.id;
+
+CREATE VIEW community_view AS
+SELECT
     cv.*,
-    us.user as user_id,
-    us.is_subbed::bool as subscribed
-from community_aggregates_view cv
-cross join lateral (
-       select
-               u.id as user,
-               coalesce(cf.community_id, 0) as is_subbed
-       from user_ u
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = cv.id
-) as us
-
-union all
-
-select 
+    us.user AS user_id,
+    us.is_subbed::bool AS subscribed
+FROM
+    community_aggregates_view cv
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user,
+            coalesce(cf.community_id, 0) AS is_subbed
+        FROM
+            user_ u
+            LEFT JOIN community_follower cf ON u.id = cf.user_id
+                AND cf.community_id = cv.id) AS us
+UNION ALL
+SELECT
     cv.*,
-    null as user_id,
-    null as subscribed
-from community_aggregates_view cv;
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    community_aggregates_view cv;
 
-create view community_moderator_view as
-select
+CREATE VIEW community_moderator_view AS
+SELECT
     cm.*,
-    u.actor_id as user_actor_id,
-    u.local as user_local,
-    u.name as user_name,
-    u.preferred_username as user_preferred_username,
-    u.avatar as avatar,
-    c.actor_id as community_actor_id,
-    c.local as community_local,
-    c.name as community_name,
-    c.icon as community_icon
-from community_moderator cm
-left join user_ u on cm.user_id = u.id
-left join community c on cm.community_id = c.id;
-
-create view community_follower_view as
-select
+    u.actor_id AS user_actor_id,
+    u.local AS user_local,
+    u.name AS user_name,
+    u.preferred_username AS user_preferred_username,
+    u.avatar AS avatar,
+    c.actor_id AS community_actor_id,
+    c.local AS community_local,
+    c.name AS community_name,
+    c.icon AS community_icon
+FROM
+    community_moderator cm
+    LEFT JOIN user_ u ON cm.user_id = u.id
+    LEFT JOIN community c ON cm.community_id = c.id;
+
+CREATE VIEW community_follower_view AS
+SELECT
     cf.*,
-    u.actor_id as user_actor_id,
-    u.local as user_local,
-    u.name as user_name,
-    u.preferred_username as user_preferred_username,
-    u.avatar as avatar,
-    c.actor_id as community_actor_id,
-    c.local as community_local,
-    c.name as community_name,
-    c.icon as community_icon
-from community_follower cf
-left join user_ u on cf.user_id = u.id
-left join community c on cf.community_id = c.id;
-
-create view community_user_ban_view as
-select
+    u.actor_id AS user_actor_id,
+    u.local AS user_local,
+    u.name AS user_name,
+    u.preferred_username AS user_preferred_username,
+    u.avatar AS avatar,
+    c.actor_id AS community_actor_id,
+    c.local AS community_local,
+    c.name AS community_name,
+    c.icon AS community_icon
+FROM
+    community_follower cf
+    LEFT JOIN user_ u ON cf.user_id = u.id
+    LEFT JOIN community c ON cf.community_id = c.id;
+
+CREATE VIEW community_user_ban_view AS
+SELECT
     cb.*,
-    u.actor_id as user_actor_id,
-    u.local as user_local,
-    u.name as user_name,
-    u.preferred_username as user_preferred_username,
-    u.avatar as avatar,
-    c.actor_id as community_actor_id,
-    c.local as community_local,
-    c.name as community_name,
-    c.icon as community_icon
-from community_user_ban cb
-left join user_ u on cb.user_id = u.id
-left join community c on cb.community_id = c.id;
+    u.actor_id AS user_actor_id,
+    u.local AS user_local,
+    u.name AS user_name,
+    u.preferred_username AS user_preferred_username,
+    u.avatar AS avatar,
+    c.actor_id AS community_actor_id,
+    c.local AS community_local,
+    c.name AS community_name,
+    c.icon AS community_icon
+FROM
+    community_user_ban cb
+    LEFT JOIN user_ u ON cb.user_id = u.id
+    LEFT JOIN community c ON cb.community_id = c.id;
 
 -- The community fast table
-
-create table community_aggregates_fast as select * from community_aggregates_view;
-alter table community_aggregates_fast add primary key (id);
-
-create view community_fast_view as
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join (
-  select
-  ca.*
-  from community_aggregates_fast ca
-) ac
-
-union all
-
-select 
-caf.*,
-null as user_id,
-null as subscribed
-from community_aggregates_fast caf;
+CREATE TABLE community_aggregates_fast AS
+SELECT
+    *
+FROM
+    community_aggregates_view;
+
+ALTER TABLE community_aggregates_fast
+    ADD PRIMARY KEY (id);
+
+CREATE VIEW community_fast_view AS
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN (
+        SELECT
+            ca.*
+        FROM
+            community_aggregates_fast ca) ac
+UNION ALL
+SELECT
+    caf.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    community_aggregates_fast caf;
 
 -- Comments, mentions, replies
-drop view user_mention_view;
-drop view reply_fast_view;
-drop view comment_fast_view;
-drop view comment_view;
-drop view user_mention_fast_view;
-drop table comment_aggregates_fast;
-drop view comment_aggregates_view;
-
-create view comment_aggregates_view as
-select
-       ct.*,
-       -- post details
-       p."name" as post_name,
-       p.community_id,
-       -- community details
-       c.actor_id as community_actor_id,
-       c."local" as community_local,
-       c."name" as community_name,
-  c.icon as community_icon,
-       -- creator details
-       u.banned as banned,
-  coalesce(cb.id, 0)::bool as banned_from_community,
-       u.actor_id as creator_actor_id,
-       u.local as creator_local,
-       u.name as creator_name,
-  u.preferred_username as creator_preferred_username,
-  u.published as creator_published,
-       u.avatar as creator_avatar,
-       -- score details
-       coalesce(cl.total, 0) as score,
-       coalesce(cl.up, 0) as upvotes,
-       coalesce(cl.down, 0) as downvotes,
-       hot_rank(coalesce(cl.total, 1), p.published) as hot_rank,
-       hot_rank(coalesce(cl.total, 1), ct.published) as hot_rank_active
-from comment ct
-left join post p on ct.post_id = p.id
-left join community c on p.community_id = c.id
-left join user_ u on ct.creator_id = u.id
-left join community_user_ban cb on ct.creator_id = cb.user_id and p.id = ct.post_id and p.community_id = cb.community_id
-left join (
-       select
-               l.comment_id as id,
-               sum(l.score) as total,
-               count(case when l.score = 1 then 1 else null end) as up,
-               count(case when l.score = -1 then 1 else null end) as down
-       from comment_like l
-       group by comment_id
-) as cl on cl.id = ct.id;
-
-create or replace view comment_view as (
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_view cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
-
-union all
-
-select
+DROP VIEW user_mention_view;
+
+DROP VIEW reply_fast_view;
+
+DROP VIEW comment_fast_view;
+
+DROP VIEW comment_view;
+
+DROP VIEW user_mention_fast_view;
+
+DROP TABLE comment_aggregates_fast;
+
+DROP VIEW comment_aggregates_view;
+
+CREATE VIEW comment_aggregates_view AS
+SELECT
+    ct.*,
+    -- post details
+    p."name" AS post_name,
+    p.community_id,
+    -- community details
+    c.actor_id AS community_actor_id,
+    c."local" AS community_local,
+    c."name" AS community_name,
+    c.icon AS community_icon,
+    -- creator details
+    u.banned AS banned,
+    coalesce(cb.id, 0)::bool AS banned_from_community,
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u.name AS creator_name,
+    u.preferred_username AS creator_preferred_username,
+    u.published AS creator_published,
+    u.avatar AS creator_avatar,
+    -- score details
+    coalesce(cl.total, 0) AS score,
+    coalesce(cl.up, 0) AS upvotes,
+    coalesce(cl.down, 0) AS downvotes,
+    hot_rank (coalesce(cl.total, 1), p.published) AS hot_rank,
+    hot_rank (coalesce(cl.total, 1), ct.published) AS hot_rank_active
+FROM
+    comment ct
+    LEFT JOIN post p ON ct.post_id = p.id
+    LEFT JOIN community c ON p.community_id = c.id
+    LEFT JOIN user_ u ON ct.creator_id = u.id
+    LEFT JOIN community_user_ban cb ON ct.creator_id = cb.user_id
+        AND p.id = ct.post_id
+        AND p.community_id = cb.community_id
+    LEFT JOIN (
+        SELECT
+            l.comment_id AS id,
+            sum(l.score) AS total,
+            count(
+                CASE WHEN l.score = 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS up,
+            count(
+                CASE WHEN l.score = - 1 THEN
+                    1
+                ELSE
+                    NULL
+                END) AS down
+        FROM
+            comment_like l
+        GROUP BY
+            comment_id) AS cl ON cl.id = ct.id;
+
+CREATE OR REPLACE VIEW comment_view AS (
+    SELECT
+        cav.*,
+        us.user_id AS user_id,
+        us.my_vote AS my_vote,
+        us.is_subbed::bool AS subscribed,
+        us.is_saved::bool AS saved
+    FROM
+        comment_aggregates_view cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
     cav.*,
-    null as user_id,
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_view cav
-);
-
-create table comment_aggregates_fast as select * from comment_aggregates_view;
-alter table comment_aggregates_fast add primary key (id);
-
-create view comment_fast_view as
-select
-       cav.*,
-  us.user_id as user_id,
-  us.my_vote as my_vote,
-  us.is_subbed::bool as subscribed,
-  us.is_saved::bool as saved
-from comment_aggregates_fast cav
-cross join lateral (
-       select
-               u.id as user_id,
-               coalesce(cl.score, 0) as my_vote,
-    coalesce(cf.id, 0) as is_subbed,
-    coalesce(cs.id, 0) as is_saved
-       from user_ u
-       left join comment_like cl on u.id = cl.user_id and cav.id = cl.comment_id
-       left join comment_saved cs on u.id = cs.user_id and cs.comment_id = cav.id
-       left join community_follower cf on u.id = cf.user_id and cav.community_id = cf.community_id
-) as us
-
-union all
-
-select
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_view cav);
+
+CREATE TABLE comment_aggregates_fast AS
+SELECT
+    *
+FROM
+    comment_aggregates_view;
+
+ALTER TABLE comment_aggregates_fast
+    ADD PRIMARY KEY (id);
+
+CREATE VIEW comment_fast_view AS
+SELECT
     cav.*,
-    null as user_id,
-    null as my_vote,
-    null as subscribed,
-    null as saved
-from comment_aggregates_fast cav;
-
-create view user_mention_view as
-select
+    us.user_id AS user_id,
+    us.my_vote AS my_vote,
+    us.is_subbed::bool AS subscribed,
+    us.is_saved::bool AS saved
+FROM
+    comment_aggregates_fast cav
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user_id,
+            coalesce(cl.score, 0) AS my_vote,
+            coalesce(cf.id, 0) AS is_subbed,
+            coalesce(cs.id, 0) AS is_saved
+        FROM
+            user_ u
+            LEFT JOIN comment_like cl ON u.id = cl.user_id
+                AND cav.id = cl.comment_id
+        LEFT JOIN comment_saved cs ON u.id = cs.user_id
+            AND cs.comment_id = cav.id
+    LEFT JOIN community_follower cf ON u.id = cf.user_id
+        AND cav.community_id = cf.community_id) AS us
+UNION ALL
+SELECT
+    cav.*,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS subscribed,
+    NULL AS saved
+FROM
+    comment_aggregates_fast cav;
+
+CREATE VIEW user_mention_view AS
+SELECT
     c.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     c.creator_id,
     c.creator_actor_id,
     c.creator_local,
@@ -532,15 +644,30 @@ select
     c.my_vote,
     c.saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_mention um, comment_view c
-where um.comment_id = c.id;
-
-create view user_mention_fast_view as
-select
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_mention um,
+    comment_view c
+WHERE
+    um.comment_id = c.id;
+
+CREATE VIEW user_mention_fast_view AS
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -568,26 +695,45 @@ select
     ac.downvotes,
     ac.hot_rank,
     ac.hot_rank_active,
-    u.id as user_id,
-    coalesce(cl.score, 0) as my_vote,
-    (select cs.id::bool from comment_saved cs where u.id = cs.user_id and cs.comment_id = ac.id) as saved,
+    u.id AS user_id,
+    coalesce(cl.score, 0) AS my_vote,
+    (
+        SELECT
+            cs.id::bool
+        FROM
+            comment_saved cs
+        WHERE
+            u.id = cs.user_id
+            AND cs.comment_id = ac.id) AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from user_ u
-cross join (
-  select
-  ca.*
-  from comment_aggregates_fast ca
-) ac
-left join comment_like cl on u.id = cl.user_id and ac.id = cl.comment_id
-left join user_mention um on um.comment_id = ac.id
-
-union all
-
-select
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    user_ u
+    CROSS JOIN (
+        SELECT
+            ca.*
+        FROM
+            comment_aggregates_fast ca) ac
+    LEFT JOIN comment_like cl ON u.id = cl.user_id
+        AND ac.id = cl.comment_id
+    LEFT JOIN user_mention um ON um.comment_id = ac.id
+UNION ALL
+SELECT
     ac.id,
-    um.id as user_mention_id,
+    um.id AS user_mention_id,
     ac.creator_id,
     ac.creator_actor_id,
     ac.creator_local,
@@ -615,134 +761,227 @@ select
     ac.downvotes,
     ac.hot_rank,
     ac.hot_rank_active,
-    null as user_id,
-    null as my_vote,
-    null as saved,
+    NULL AS user_id,
+    NULL AS my_vote,
+    NULL AS saved,
     um.recipient_id,
-    (select actor_id from user_ u where u.id = um.recipient_id) as recipient_actor_id,
-    (select local from user_ u where u.id = um.recipient_id) as recipient_local
-from comment_aggregates_fast ac
-left join user_mention um on um.comment_id = ac.id
-;
+    (
+        SELECT
+            actor_id
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_actor_id,
+    (
+        SELECT
+            local
+        FROM
+            user_ u
+        WHERE
+            u.id = um.recipient_id) AS recipient_local
+FROM
+    comment_aggregates_fast ac
+    LEFT JOIN user_mention um ON um.comment_id = ac.id;
 
 -- Do the reply_view referencing the comment_fast_view
-create view reply_fast_view as
-with closereply as (
-    select
-    c2.id,
-    c2.creator_id as sender_id,
-    c.creator_id as recipient_id
-    from comment c
-    inner join comment c2 on c.id = c2.parent_id
-    where c2.creator_id != c.creator_id
-    -- Do union where post is null
-    union
-    select
-    c.id,
-    c.creator_id as sender_id,
-    p.creator_id as recipient_id
-    from comment c, post p
-    where c.post_id = p.id and c.parent_id is null and c.creator_id != p.creator_id
+CREATE VIEW reply_fast_view AS
+with closereply AS (
+    SELECT
+        c2.id,
+        c2.creator_id AS sender_id,
+        c.creator_id AS recipient_id
+    FROM
+        comment c
+        INNER JOIN comment c2 ON c.id = c2.parent_id
+    WHERE
+        c2.creator_id != c.creator_id
+        -- Do union where post is null
+    UNION
+    SELECT
+        c.id,
+        c.creator_id AS sender_id,
+        p.creator_id AS recipient_id
+    FROM
+        comment c,
+        post p
+    WHERE
+        c.post_id = p.id
+        AND c.parent_id IS NULL
+        AND c.creator_id != p.creator_id
 )
-select cv.*,
-closereply.recipient_id
-from comment_fast_view cv, closereply
-where closereply.id = cv.id
-;
+SELECT
+    cv.*,
+    closereply.recipient_id
+FROM
+    comment_fast_view cv,
+    closereply
+WHERE
+    closereply.id = cv.id;
 
 -- Adding hot rank active to the triggers
-create or replace function refresh_post()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
-
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts - 1 where id = OLD.community_id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
-
-    -- Update that users number of posts, post score
-    delete from user_fast where id = NEW.creator_id;
-    insert into user_fast select * from user_view where id = NEW.creator_id;
-  
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts + 1 where id = NEW.community_id;
-
-    -- Update the hot rank on the post table
-    -- TODO this might not correctly update it, using a 1 week interval
-    update post_aggregates_fast as paf
-    set 
-      hot_rank = pav.hot_rank,
-      hot_rank_active = pav.hot_rank_active
-    from post_aggregates_view as pav
-    where paf.id = pav.id  and (pav.published > ('now'::timestamp - '1 week'::interval));
-  END IF;
-
-  return null;
-end $$;
-
-create or replace function refresh_comment()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments - 1
-    from post as p
-    where caf.id = p.community_id and p.id = OLD.post_id;
-
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
-
-    -- Update user view due to comment count
-    update user_fast 
-    set number_of_comments = number_of_comments + 1
-    where id = NEW.creator_id;
-    
-    -- Update post view due to comment count, new comment activity time, but only on new posts
-    -- TODO this could be done more efficiently
-    delete from post_aggregates_fast where id = NEW.post_id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.post_id;
-
-    -- Update the comment hot_ranks as of last week
-    update comment_aggregates_fast as caf
-    set 
-      hot_rank = cav.hot_rank,
-      hot_rank_active = cav.hot_rank_active
-    from comment_aggregates_view as cav
-    where caf.id = cav.id and (cav.published > ('now'::timestamp - '1 week'::interval));
-
-    -- Update the post ranks
-    update post_aggregates_fast as paf
-    set 
-      hot_rank = pav.hot_rank,
-      hot_rank_active = pav.hot_rank_active
-    from post_aggregates_view as pav
-    where paf.id = pav.id  and (pav.published > ('now'::timestamp - '1 week'::interval));
-
-    -- Force the hot rank active as zero on 2 day-older posts (necro-bump)
-    update post_aggregates_fast as paf
-    set hot_rank_active = 0
-    where paf.id = NEW.post_id and (paf.published < ('now'::timestamp - '2 days'::interval));
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments + 1 
-    from post as p
-    where caf.id = p.community_id and p.id = NEW.post_id;
-
-  END IF;
-
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION refresh_post ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts - 1
+        WHERE
+            id = OLD.community_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update that users number of posts, post score
+        DELETE FROM user_fast
+        WHERE id = NEW.creator_id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.creator_id;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts + 1
+        WHERE
+            id = NEW.community_id;
+        -- Update the hot rank on the post table
+        -- TODO this might not correctly update it, using a 1 week interval
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = pav.hot_rank,
+            hot_rank_active = pav.hot_rank_active
+        FROM
+            post_aggregates_view AS pav
+        WHERE
+            paf.id = pav.id
+            AND (pav.published > ('now'::timestamp - '1 week'::interval));
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION refresh_comment ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments - 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = OLD.post_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update user view due to comment count
+        UPDATE
+            user_fast
+        SET
+            number_of_comments = number_of_comments + 1
+        WHERE
+            id = NEW.creator_id;
+        -- Update post view due to comment count, new comment activity time, but only on new posts
+        -- TODO this could be done more efficiently
+        DELETE FROM post_aggregates_fast
+        WHERE id = NEW.post_id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.post_id;
+        -- Update the comment hot_ranks as of last week
+        UPDATE
+            comment_aggregates_fast AS caf
+        SET
+            hot_rank = cav.hot_rank,
+            hot_rank_active = cav.hot_rank_active
+        FROM
+            comment_aggregates_view AS cav
+        WHERE
+            caf.id = cav.id
+            AND (cav.published > ('now'::timestamp - '1 week'::interval));
+        -- Update the post ranks
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = pav.hot_rank,
+            hot_rank_active = pav.hot_rank_active
+        FROM
+            post_aggregates_view AS pav
+        WHERE
+            paf.id = pav.id
+            AND (pav.published > ('now'::timestamp - '1 week'::interval));
+        -- Force the hot rank active as zero on 2 day-older posts (necro-bump)
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank_active = 0
+        WHERE
+            paf.id = NEW.post_id
+            AND (paf.published < ('now'::timestamp - '2 days'::interval));
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments + 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = NEW.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
index 53b016c88e5e23574986612b713cf7bf652ca05d..984f407bc2fac29093d7e7d736f444ea91ac1591 100644 (file)
@@ -1,11 +1,14 @@
 -- Drop first
-drop view community_view;
-drop view community_aggregates_view;
-drop view community_fast_view;
-drop table community_aggregates_fast;
+DROP VIEW community_view;
 
-create view community_aggregates_view as
-select
+DROP VIEW community_aggregates_view;
+
+DROP VIEW community_fast_view;
+
+DROP TABLE community_aggregates_fast;
+
+CREATE VIEW community_aggregates_view AS
+SELECT
     c.id,
     c.name,
     c.title,
@@ -22,79 +25,96 @@ select
     c.actor_id,
     c.local,
     c.last_refreshed_at,
-    u.actor_id as creator_actor_id,
-    u.local as creator_local,
-    u.name as creator_name,
-    u.preferred_username as creator_preferred_username,
-    u.avatar as creator_avatar,
-    cat.name as category_name,
-    coalesce(cf.subs, 0) as number_of_subscribers,
-    coalesce(cd.posts, 0) as number_of_posts,
-    coalesce(cd.comments, 0) as number_of_comments,
-    hot_rank(cf.subs, c.published) as hot_rank
-from community c
-left join user_ u on c.creator_id = u.id
-left join category cat on c.category_id = cat.id
-left join (
-    select
-        p.community_id,
-        count(distinct p.id) as posts,
-        count(distinct ct.id) as comments
-    from post p
-    join comment ct on p.id = ct.post_id
-    group by p.community_id
-) cd on cd.community_id = c.id
-left join (
-    select
-        community_id,
-        count(*) as subs
-    from community_follower
-    group by community_id
-) cf on cf.community_id = c.id;
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u.name AS creator_name,
+    u.preferred_username AS creator_preferred_username,
+    u.avatar AS creator_avatar,
+    cat.name AS category_name,
+    coalesce(cf.subs, 0) AS number_of_subscribers,
+    coalesce(cd.posts, 0) AS number_of_posts,
+    coalesce(cd.comments, 0) AS number_of_comments,
+    hot_rank (cf.subs, c.published) AS hot_rank
+FROM
+    community c
+    LEFT JOIN user_ u ON c.creator_id = u.id
+    LEFT JOIN category cat ON c.category_id = cat.id
+    LEFT JOIN (
+        SELECT
+            p.community_id,
+            count(DISTINCT p.id) AS posts,
+            count(DISTINCT ct.id) AS comments
+        FROM
+            post p
+            JOIN comment ct ON p.id = ct.post_id
+        GROUP BY
+            p.community_id) cd ON cd.community_id = c.id
+    LEFT JOIN (
+        SELECT
+            community_id,
+            count(*) AS subs
+        FROM
+            community_follower
+        GROUP BY
+            community_id) cf ON cf.community_id = c.id;
 
-create view community_view as
-select
+CREATE VIEW community_view AS
+SELECT
     cv.*,
-    us.user as user_id,
-    us.is_subbed::bool as subscribed
-from community_aggregates_view cv
-cross join lateral (
-       select
-               u.id as user,
-               coalesce(cf.community_id, 0) as is_subbed
-       from user_ u
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = cv.id
-) as us
-
-union all
-
-select
+    us.user AS user_id,
+    us.is_subbed::bool AS subscribed
+FROM
+    community_aggregates_view cv
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user,
+            coalesce(cf.community_id, 0) AS is_subbed
+        FROM
+            user_ u
+            LEFT JOIN community_follower cf ON u.id = cf.user_id
+                AND cf.community_id = cv.id) AS us
+UNION ALL
+SELECT
     cv.*,
-    null as user_id,
-    null as subscribed
-from community_aggregates_view cv;
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    community_aggregates_view cv;
 
 -- The community fast table
+CREATE TABLE community_aggregates_fast AS
+SELECT
+    *
+FROM
+    community_aggregates_view;
 
-create table community_aggregates_fast as select * from community_aggregates_view;
-alter table community_aggregates_fast add primary key (id);
-
-create view community_fast_view as
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join (
-  select
-  ca.*
-  from community_aggregates_fast ca
-) ac
+ALTER TABLE community_aggregates_fast
+    ADD PRIMARY KEY (id);
 
-union all
+CREATE VIEW community_fast_view AS
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN (
+        SELECT
+            ca.*
+        FROM
+            community_aggregates_fast ca) ac
+UNION ALL
+SELECT
+    caf.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    community_aggregates_fast caf;
 
-select
-caf.*,
-null as user_id,
-null as subscribed
-from community_aggregates_fast caf;
\ No newline at end of file
index de5d447d1691145fbb3766f74e68e248baf89008..8258f5d4cfe4f6d55f2f7a89b5b7d25c9b036c6e 100644 (file)
@@ -1,11 +1,14 @@
 -- Drop first
-drop view community_view;
-drop view community_aggregates_view;
-drop view community_fast_view;
-drop table community_aggregates_fast;
+DROP VIEW community_view;
 
-create view community_aggregates_view as
-select
+DROP VIEW community_aggregates_view;
+
+DROP VIEW community_fast_view;
+
+DROP TABLE community_aggregates_fast;
+
+CREATE VIEW community_aggregates_view AS
+SELECT
     c.id,
     c.name,
     c.title,
@@ -22,79 +25,96 @@ select
     c.actor_id,
     c.local,
     c.last_refreshed_at,
-    u.actor_id as creator_actor_id,
-    u.local as creator_local,
-    u.name as creator_name,
-    u.preferred_username as creator_preferred_username,
-    u.avatar as creator_avatar,
-    cat.name as category_name,
-    coalesce(cf.subs, 0) as number_of_subscribers,
-    coalesce(cd.posts, 0) as number_of_posts,
-    coalesce(cd.comments, 0) as number_of_comments,
-    hot_rank(cf.subs, c.published) as hot_rank
-from community c
-left join user_ u on c.creator_id = u.id
-left join category cat on c.category_id = cat.id
-left join (
-    select
-        p.community_id,
-        count(distinct p.id) as posts,
-        count(distinct ct.id) as comments
-    from post p
-    left join comment ct on p.id = ct.post_id
-    group by p.community_id
-) cd on cd.community_id = c.id
-left join (
-    select
-        community_id,
-        count(*) as subs
-    from community_follower
-    group by community_id
-) cf on cf.community_id = c.id;
+    u.actor_id AS creator_actor_id,
+    u.local AS creator_local,
+    u.name AS creator_name,
+    u.preferred_username AS creator_preferred_username,
+    u.avatar AS creator_avatar,
+    cat.name AS category_name,
+    coalesce(cf.subs, 0) AS number_of_subscribers,
+    coalesce(cd.posts, 0) AS number_of_posts,
+    coalesce(cd.comments, 0) AS number_of_comments,
+    hot_rank (cf.subs, c.published) AS hot_rank
+FROM
+    community c
+    LEFT JOIN user_ u ON c.creator_id = u.id
+    LEFT JOIN category cat ON c.category_id = cat.id
+    LEFT JOIN (
+        SELECT
+            p.community_id,
+            count(DISTINCT p.id) AS posts,
+            count(DISTINCT ct.id) AS comments
+        FROM
+            post p
+            LEFT JOIN comment ct ON p.id = ct.post_id
+        GROUP BY
+            p.community_id) cd ON cd.community_id = c.id
+    LEFT JOIN (
+        SELECT
+            community_id,
+            count(*) AS subs
+        FROM
+            community_follower
+        GROUP BY
+            community_id) cf ON cf.community_id = c.id;
 
-create view community_view as
-select
+CREATE VIEW community_view AS
+SELECT
     cv.*,
-    us.user as user_id,
-    us.is_subbed::bool as subscribed
-from community_aggregates_view cv
-cross join lateral (
-       select
-               u.id as user,
-               coalesce(cf.community_id, 0) as is_subbed
-       from user_ u
-       left join community_follower cf on u.id = cf.user_id and cf.community_id = cv.id
-) as us
-
-union all
-
-select
+    us.user AS user_id,
+    us.is_subbed::bool AS subscribed
+FROM
+    community_aggregates_view cv
+    CROSS JOIN LATERAL (
+        SELECT
+            u.id AS user,
+            coalesce(cf.community_id, 0) AS is_subbed
+        FROM
+            user_ u
+            LEFT JOIN community_follower cf ON u.id = cf.user_id
+                AND cf.community_id = cv.id) AS us
+UNION ALL
+SELECT
     cv.*,
-    null as user_id,
-    null as subscribed
-from community_aggregates_view cv;
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    community_aggregates_view cv;
 
 -- The community fast table
+CREATE TABLE community_aggregates_fast AS
+SELECT
+    *
+FROM
+    community_aggregates_view;
 
-create table community_aggregates_fast as select * from community_aggregates_view;
-alter table community_aggregates_fast add primary key (id);
-
-create view community_fast_view as
-select
-ac.*,
-u.id as user_id,
-(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed
-from user_ u
-cross join (
-  select
-  ca.*
-  from community_aggregates_fast ca
-) ac
+ALTER TABLE community_aggregates_fast
+    ADD PRIMARY KEY (id);
 
-union all
+CREATE VIEW community_fast_view AS
+SELECT
+    ac.*,
+    u.id AS user_id,
+    (
+        SELECT
+            cf.id::boolean
+        FROM
+            community_follower cf
+        WHERE
+            u.id = cf.user_id
+            AND ac.id = cf.community_id) AS subscribed
+FROM
+    user_ u
+    CROSS JOIN (
+        SELECT
+            ca.*
+        FROM
+            community_aggregates_fast ca) ac
+UNION ALL
+SELECT
+    caf.*,
+    NULL AS user_id,
+    NULL AS subscribed
+FROM
+    community_aggregates_fast caf;
 
-select
-caf.*,
-null as user_id,
-null as subscribed
-from community_aggregates_fast caf;
\ No newline at end of file
index 2b43b59c358eb6e922a1f8788b08a3c07b2f7a22..1fce04bafa31f822363da8be5a678c9fe4e44dfd 100644 (file)
@@ -1,27 +1,55 @@
 -- Drop the uniques
-alter table private_message drop constraint idx_private_message_ap_id;
-alter table post drop constraint idx_post_ap_id;
-alter table comment drop constraint idx_comment_ap_id;
-alter table user_ drop constraint idx_user_actor_id;
-alter table community drop constraint idx_community_actor_id;
+ALTER TABLE private_message
+    DROP CONSTRAINT idx_private_message_ap_id;
 
-alter table private_message alter column ap_id set not null;
-alter table private_message alter column ap_id set default 'http://fake.com';
+ALTER TABLE post
+    DROP CONSTRAINT idx_post_ap_id;
 
-alter table post alter column ap_id set not null;
-alter table post alter column ap_id set default 'http://fake.com';
+ALTER TABLE comment
+    DROP CONSTRAINT idx_comment_ap_id;
 
-alter table comment alter column ap_id set not null;
-alter table comment alter column ap_id set default 'http://fake.com';
+ALTER TABLE user_
+    DROP CONSTRAINT idx_user_actor_id;
 
-update private_message
-set ap_id = 'http://fake.com'
-where ap_id like 'changeme_%';
+ALTER TABLE community
+    DROP CONSTRAINT idx_community_actor_id;
 
-update post
-set ap_id = 'http://fake.com'
-where ap_id like 'changeme_%';
+ALTER TABLE private_message
+    ALTER COLUMN ap_id SET NOT NULL;
+
+ALTER TABLE private_message
+    ALTER COLUMN ap_id SET DEFAULT 'http://fake.com';
+
+ALTER TABLE post
+    ALTER COLUMN ap_id SET NOT NULL;
+
+ALTER TABLE post
+    ALTER COLUMN ap_id SET DEFAULT 'http://fake.com';
+
+ALTER TABLE comment
+    ALTER COLUMN ap_id SET NOT NULL;
+
+ALTER TABLE comment
+    ALTER COLUMN ap_id SET DEFAULT 'http://fake.com';
+
+UPDATE
+    private_message
+SET
+    ap_id = 'http://fake.com'
+WHERE
+    ap_id LIKE 'changeme_%';
+
+UPDATE
+    post
+SET
+    ap_id = 'http://fake.com'
+WHERE
+    ap_id LIKE 'changeme_%';
+
+UPDATE
+    comment
+SET
+    ap_id = 'http://fake.com'
+WHERE
+    ap_id LIKE 'changeme_%';
 
-update comment
-set ap_id = 'http://fake.com'
-where ap_id like 'changeme_%';
index 75e81eef22c6abc6098cf628c9ed674de6f9dba1..22c275becd8d417dfcd1d7b361a53734df58fa71 100644 (file)
 -- Add unique ap_id for private_message, comment, and post
-
 -- Need to delete the possible dupes for ones that don't start with the fake one
-delete from private_message a using (
-  select min(id) as id, ap_id
-    from private_message 
-    group by ap_id having count(*) > 1
-) b
-where a.ap_id = b.ap_id 
-and a.id <> b.id;
-
-delete from post a using (
-  select min(id) as id, ap_id
-    from post 
-    group by ap_id having count(*) > 1
-) b
-where a.ap_id = b.ap_id 
-and a.id <> b.id;
-
-delete from comment a using (
-  select min(id) as id, ap_id
-    from comment 
-    group by ap_id having count(*) > 1
-) b
-where a.ap_id = b.ap_id 
-and a.id <> b.id;
+DELETE FROM private_message a USING (
+    SELECT
+        min(id) AS id,
+        ap_id
+    FROM
+        private_message
+    GROUP BY
+        ap_id
+    HAVING
+        count(*) > 1) b
+WHERE
+    a.ap_id = b.ap_id
+    AND a.id <> b.id;
+
+DELETE FROM post a USING (
+    SELECT
+        min(id) AS id,
+        ap_id
+    FROM
+        post
+    GROUP BY
+        ap_id
+    HAVING
+        count(*) > 1) b
+WHERE
+    a.ap_id = b.ap_id
+    AND a.id <> b.id;
+
+DELETE FROM comment a USING (
+    SELECT
+        min(id) AS id,
+        ap_id
+    FROM
+        comment
+    GROUP BY
+        ap_id
+    HAVING
+        count(*) > 1) b
+WHERE
+    a.ap_id = b.ap_id
+    AND a.id <> b.id;
 
 -- Replacing the current default on the columns, to the unique one
-update private_message 
-set ap_id = generate_unique_changeme()
-where ap_id = 'http://fake.com';
+UPDATE
+    private_message
+SET
+    ap_id = generate_unique_changeme ()
+WHERE
+    ap_id = 'http://fake.com';
 
-update post 
-set ap_id = generate_unique_changeme()
-where ap_id = 'http://fake.com';
+UPDATE
+    post
+SET
+    ap_id = generate_unique_changeme ()
+WHERE
+    ap_id = 'http://fake.com';
 
-update comment 
-set ap_id = generate_unique_changeme()
-where ap_id = 'http://fake.com';
+UPDATE
+    comment
+SET
+    ap_id = generate_unique_changeme ()
+WHERE
+    ap_id = 'http://fake.com';
 
 -- Add the unique indexes
-alter table private_message alter column ap_id set not null;
-alter table private_message alter column ap_id set default generate_unique_changeme();
+ALTER TABLE private_message
+    ALTER COLUMN ap_id SET NOT NULL;
+
+ALTER TABLE private_message
+    ALTER COLUMN ap_id SET DEFAULT generate_unique_changeme ();
+
+ALTER TABLE post
+    ALTER COLUMN ap_id SET NOT NULL;
+
+ALTER TABLE post
+    ALTER COLUMN ap_id SET DEFAULT generate_unique_changeme ();
 
-alter table post alter column ap_id set not null;
-alter table post alter column ap_id set default generate_unique_changeme();
+ALTER TABLE comment
+    ALTER COLUMN ap_id SET NOT NULL;
 
-alter table comment alter column ap_id set not null;
-alter table comment alter column ap_id set default generate_unique_changeme();
+ALTER TABLE comment
+    ALTER COLUMN ap_id SET DEFAULT generate_unique_changeme ();
 
 -- Add the uniques, for user_ and community too
-alter table private_message add constraint idx_private_message_ap_id unique (ap_id);
-alter table post add constraint idx_post_ap_id unique (ap_id);
-alter table comment add constraint idx_comment_ap_id unique (ap_id);
-alter table user_ add constraint idx_user_actor_id unique (actor_id);
-alter table community add constraint idx_community_actor_id unique (actor_id);
+ALTER TABLE private_message
+    ADD CONSTRAINT idx_private_message_ap_id UNIQUE (ap_id);
+
+ALTER TABLE post
+    ADD CONSTRAINT idx_post_ap_id UNIQUE (ap_id);
+
+ALTER TABLE comment
+    ADD CONSTRAINT idx_comment_ap_id UNIQUE (ap_id);
+
+ALTER TABLE user_
+    ADD CONSTRAINT idx_user_actor_id UNIQUE (actor_id);
+
+ALTER TABLE community
+    ADD CONSTRAINT idx_community_actor_id UNIQUE (actor_id);
+
index 80dc308ac54de388f2daa110ba23b45534bf5c39..bebe2856e2ea61a59669641c359064c902030e04 100644 (file)
@@ -1 +1,2 @@
-drop schema utils cascade;
\ No newline at end of file
+DROP SCHEMA utils CASCADE;
+
index 8f9552ea97052a11714ce400ab37ed80450a9c17..7e2f794faf75c28fbb01f14f9eac3b28aadd98d8 100644 (file)
-create schema utils;
+CREATE SCHEMA utils;
 
-create table utils.deps_saved_ddl
-(
-  id serial NOT NULL,
-  view_schema character varying(255),
-  view_name character varying(255),
-  ddl_to_run text,
-  CONSTRAINT deps_saved_ddl_pkey PRIMARY KEY (id)
+CREATE TABLE utils.deps_saved_ddl (
+    id serial NOT NULL,
+    view_schema character varying(255),
+    view_name character varying(255),
+    ddl_to_run text,
+    CONSTRAINT deps_saved_ddl_pkey PRIMARY KEY (id)
 );
 
-create or replace function utils.save_and_drop_views(p_view_schema name, p_view_name name)
+CREATE OR REPLACE FUNCTION utils.save_and_drop_views (p_view_schema name, p_view_name name)
     RETURNS void
     LANGUAGE plpgsql
     COST 100
-AS $BODY$
+    AS $BODY$
+DECLARE
+    v_curr record;
+BEGIN
+    FOR v_curr IN (
+        SELECT
+            obj_schema,
+            obj_name,
+            obj_type
+        FROM ( WITH RECURSIVE recursive_deps (
+                obj_schema,
+                obj_name,
+                obj_type,
+                depth
+) AS (
+                SELECT
+                    p_view_schema::name,
+                    p_view_name,
+                    NULL::varchar,
+                    0
+                UNION
+                SELECT
+                    dep_schema::varchar,
+                    dep_name::varchar,
+                    dep_type::varchar,
+                    recursive_deps.depth + 1
+                FROM (
+                    SELECT
+                        ref_nsp.nspname ref_schema,
+                        ref_cl.relname ref_name,
+                        rwr_cl.relkind dep_type,
+                        rwr_nsp.nspname dep_schema,
+                        rwr_cl.relname dep_name
+                    FROM
+                        pg_depend dep
+                        JOIN pg_class ref_cl ON dep.refobjid = ref_cl.oid
+                        JOIN pg_namespace ref_nsp ON ref_cl.relnamespace = ref_nsp.oid
+                        JOIN pg_rewrite rwr ON dep.objid = rwr.oid
+                        JOIN pg_class rwr_cl ON rwr.ev_class = rwr_cl.oid
+                        JOIN pg_namespace rwr_nsp ON rwr_cl.relnamespace = rwr_nsp.oid
+                    WHERE
+                        dep.deptype = 'n'
+                        AND dep.classid = 'pg_rewrite'::regclass) deps
+                    JOIN recursive_deps ON deps.ref_schema = recursive_deps.obj_schema
+                        AND deps.ref_name = recursive_deps.obj_name
+                WHERE (deps.ref_schema != deps.dep_schema
+                    OR deps.ref_name != deps.dep_name))
+            SELECT
+                obj_schema,
+                obj_name,
+                obj_type,
+                depth
+            FROM
+                recursive_deps
+            WHERE
+                depth > 0) t
+        GROUP BY
+            obj_schema,
+            obj_name,
+            obj_type
+        ORDER BY
+            max(depth) DESC)
+            LOOP
+                IF v_curr.obj_type = 'v' THEN
+                    INSERT INTO utils.deps_saved_ddl (view_schema, view_name, ddl_to_run)
+                    SELECT
+                        p_view_schema,
+                        p_view_name,
+                        'CREATE VIEW ' || v_curr.obj_schema || '.' || v_curr.obj_name || ' AS ' || view_definition
+                    FROM
+                        information_schema.views
+                    WHERE
+                        table_schema = v_curr.obj_schema
+                        AND table_name = v_curr.obj_name;
+                    EXECUTE 'DROP VIEW' || ' ' || v_curr.obj_schema || '.' || v_curr.obj_name;
+                END IF;
+            END LOOP;
+END;
+$BODY$;
 
-declare
-  v_curr record;
-begin
-for v_curr in 
-(
-  select obj_schema, obj_name, obj_type from
-  (
-  with recursive recursive_deps(obj_schema, obj_name, obj_type, depth) as 
-  (
-    select p_view_schema::name, p_view_name, null::varchar, 0
-    union
-    select dep_schema::varchar, dep_name::varchar, dep_type::varchar, recursive_deps.depth + 1 from 
-    (
-      select ref_nsp.nspname ref_schema, ref_cl.relname ref_name, 
-      rwr_cl.relkind dep_type,
-      rwr_nsp.nspname dep_schema,
-      rwr_cl.relname dep_name
-      from pg_depend dep
-      join pg_class ref_cl on dep.refobjid = ref_cl.oid
-      join pg_namespace ref_nsp on ref_cl.relnamespace = ref_nsp.oid
-      join pg_rewrite rwr on dep.objid = rwr.oid
-      join pg_class rwr_cl on rwr.ev_class = rwr_cl.oid
-      join pg_namespace rwr_nsp on rwr_cl.relnamespace = rwr_nsp.oid
-      where dep.deptype = 'n'
-      and dep.classid = 'pg_rewrite'::regclass
-    ) deps
-    join recursive_deps on deps.ref_schema = recursive_deps.obj_schema and deps.ref_name = recursive_deps.obj_name
-    where (deps.ref_schema != deps.dep_schema or deps.ref_name != deps.dep_name)
-  )
-  select obj_schema, obj_name, obj_type, depth
-  from recursive_deps 
-  where depth > 0
-  ) t
-  group by obj_schema, obj_name, obj_type
-  order by max(depth) desc
-) loop
-  if v_curr.obj_type = 'v' then
-    insert into utils.deps_saved_ddl(view_schema, view_name, ddl_to_run)
-    select p_view_schema, p_view_name, 'CREATE VIEW ' || v_curr.obj_schema || '.' || v_curr.obj_name || ' AS ' || view_definition
-    from information_schema.views
-    where table_schema = v_curr.obj_schema and table_name = v_curr.obj_name;
+CREATE OR REPLACE FUNCTION utils.restore_views (p_view_schema character varying, p_view_name character varying)
+    RETURNS void
+    LANGUAGE plpgsql
+    COST 100
+    AS $BODY$
+DECLARE
+    v_curr record;
+BEGIN
+    FOR v_curr IN (
+        SELECT
+            ddl_to_run,
+            id
+        FROM
+            utils.deps_saved_ddl
+        WHERE
+            view_schema = p_view_schema
+            AND view_name = p_view_name
+        ORDER BY
+            id DESC)
+            LOOP
+                BEGIN
+                    EXECUTE v_curr.ddl_to_run;
+                    DELETE FROM utils.deps_saved_ddl
+                    WHERE id = v_curr.id;
+                EXCEPTION
+                    WHEN OTHERS THEN
+                        -- keep looping, but please check for errors or remove left overs to handle manually
+                END;
+    END LOOP;
+END;
 
-    execute 'DROP VIEW' || ' ' || v_curr.obj_schema || '.' || v_curr.obj_name;
-  end if;
-end loop;
-end;
 $BODY$;
 
-create or replace function utils.restore_views(p_view_schema character varying, p_view_name character varying)
-  RETURNS void 
-  LANGUAGE plpgsql
-  COST 100
-AS $BODY$
-declare
-  v_curr record;
-begin
-for v_curr in 
-(
-  select ddl_to_run, id 
-  from utils.deps_saved_ddl
-  where view_schema = p_view_schema and view_name = p_view_name
-  order by id desc
-) loop
-begin
-  execute v_curr.ddl_to_run;
-  delete from utils.deps_saved_ddl where id = v_curr.id;
-  EXCEPTION WHEN OTHERS THEN
-      -- keep looping, but please check for errors or remove left overs to handle manually
-         end;
-end loop;
-end;
-$BODY$;
\ No newline at end of file
index 6b44e8a40d9a7183735b60700b6f531d0968df15..5a905564aa9464eb05a0b337b179f12e23199fe9 100644 (file)
-create or replace function refresh_community()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from community_aggregates_fast where id = OLD.id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from community_aggregates_fast where id = OLD.id;
-    insert into community_aggregates_fast select * from community_aggregates_view where id = NEW.id;
-
-    -- Update user view due to owner changes
-    delete from user_fast where id = NEW.creator_id;
-    insert into user_fast select * from user_view where id = NEW.creator_id;
-    
-    -- Update post view due to community changes
-    delete from post_aggregates_fast where community_id = NEW.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where community_id = NEW.id;
-
-  -- TODO make sure this shows up in the users page ?
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into community_aggregates_fast select * from community_aggregates_view where id = NEW.id;
-  END IF;
-
-  return null;
-end $$;
-
-create or replace function refresh_user()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from user_fast where id = OLD.id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from user_fast where id = OLD.id;
-    insert into user_fast select * from user_view where id = NEW.id;
-    
-    -- Refresh post_fast, cause of user info changes
-    delete from post_aggregates_fast where creator_id = NEW.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where creator_id = NEW.id;
-
-    delete from comment_aggregates_fast where creator_id = NEW.id;
-    insert into comment_aggregates_fast select * from comment_aggregates_view where creator_id = NEW.id;
-
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into user_fast select * from user_view where id = NEW.id;
-  END IF;
-
-  return null;
-end $$;
-
-create or replace function refresh_post()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
-
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts - 1 where id = OLD.community_id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
-
-    -- Update that users number of posts, post score
-    delete from user_fast where id = NEW.creator_id;
-    insert into user_fast select * from user_view where id = NEW.creator_id;
-  
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts + 1 where id = NEW.community_id;
-
-    -- Update the hot rank on the post table
-    -- TODO this might not correctly update it, using a 1 week interval
-    update post_aggregates_fast as paf
-    set hot_rank = pav.hot_rank 
-    from post_aggregates_view as pav
-    where paf.id = pav.id  and (pav.published > ('now'::timestamp - '1 week'::interval));
-  END IF;
-
-  return null;
-end $$;
-
-create or replace function refresh_comment()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments - 1
-    from post as p
-    where caf.id = p.community_id and p.id = OLD.post_id;
-
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
-
-    -- Update user view due to comment count
-    update user_fast 
-    set number_of_comments = number_of_comments + 1
-    where id = NEW.creator_id;
-    
-    -- Update post view due to comment count, new comment activity time, but only on new posts
-    -- TODO this could be done more efficiently
-    delete from post_aggregates_fast where id = NEW.post_id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.post_id;
-
-    -- Force the hot rank as zero on week-older posts
-    update post_aggregates_fast as paf
-    set hot_rank = 0
-    where paf.id = NEW.post_id and (paf.published < ('now'::timestamp - '1 week'::interval));
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments + 1 
-    from post as p
-    where caf.id = p.community_id and p.id = NEW.post_id;
-
-  END IF;
-
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION refresh_community ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM community_aggregates_fast
+        WHERE id = OLD.id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM community_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO community_aggregates_fast
+        SELECT
+            *
+        FROM
+            community_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update user view due to owner changes
+        DELETE FROM user_fast
+        WHERE id = NEW.creator_id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.creator_id;
+        -- Update post view due to community changes
+        DELETE FROM post_aggregates_fast
+        WHERE community_id = NEW.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            community_id = NEW.id;
+        -- TODO make sure this shows up in the users page ?
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO community_aggregates_fast
+        SELECT
+            *
+        FROM
+            community_aggregates_view
+        WHERE
+            id = NEW.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION refresh_user ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM user_fast
+        WHERE id = OLD.id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM user_fast
+        WHERE id = OLD.id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.id;
+        -- Refresh post_fast, cause of user info changes
+        DELETE FROM post_aggregates_fast
+        WHERE creator_id = NEW.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            creator_id = NEW.id;
+        DELETE FROM comment_aggregates_fast
+        WHERE creator_id = NEW.id;
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            creator_id = NEW.id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION refresh_post ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts - 1
+        WHERE
+            id = OLD.community_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update that users number of posts, post score
+        DELETE FROM user_fast
+        WHERE id = NEW.creator_id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.creator_id;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts + 1
+        WHERE
+            id = NEW.community_id;
+        -- Update the hot rank on the post table
+        -- TODO this might not correctly update it, using a 1 week interval
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = pav.hot_rank
+        FROM
+            post_aggregates_view AS pav
+        WHERE
+            paf.id = pav.id
+            AND (pav.published > ('now'::timestamp - '1 week'::interval));
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION refresh_comment ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments - 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = OLD.post_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update user view due to comment count
+        UPDATE
+            user_fast
+        SET
+            number_of_comments = number_of_comments + 1
+        WHERE
+            id = NEW.creator_id;
+        -- Update post view due to comment count, new comment activity time, but only on new posts
+        -- TODO this could be done more efficiently
+        DELETE FROM post_aggregates_fast
+        WHERE id = NEW.post_id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.post_id;
+        -- Force the hot rank as zero on week-older posts
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = 0
+        WHERE
+            paf.id = NEW.post_id
+            AND (paf.published < ('now'::timestamp - '1 week'::interval));
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments + 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = NEW.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
index 39f7744709534989eafbc84b368a1bb5f5d4c1a4..f94cbf1be8ee19b1f36caf2ac2123290726632fe 100644 (file)
 -- This adds on conflict do nothing triggers to all the insert_intos
 -- Github issue: https://github.com/LemmyNet/lemmy/issues/1179
+CREATE OR REPLACE FUNCTION refresh_community ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM community_aggregates_fast
+        WHERE id = OLD.id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM community_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO community_aggregates_fast
+        SELECT
+            *
+        FROM
+            community_aggregates_view
+        WHERE
+            id = NEW.id
+        ON CONFLICT (id)
+            DO NOTHING;
+        -- Update user view due to owner changes
+        DELETE FROM user_fast
+        WHERE id = NEW.creator_id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.creator_id
+        ON CONFLICT (id)
+            DO NOTHING;
+        -- Update post view due to community changes
+        DELETE FROM post_aggregates_fast
+        WHERE community_id = NEW.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            community_id = NEW.id
+        ON CONFLICT (id)
+            DO NOTHING;
+        -- TODO make sure this shows up in the users page ?
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO community_aggregates_fast
+        SELECT
+            *
+        FROM
+            community_aggregates_view
+        WHERE
+            id = NEW.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create or replace function refresh_community()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from community_aggregates_fast where id = OLD.id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from community_aggregates_fast where id = OLD.id;
-    insert into community_aggregates_fast select * from community_aggregates_view where id = NEW.id on conflict (id) do nothing;
+CREATE OR REPLACE FUNCTION refresh_user ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM user_fast
+        WHERE id = OLD.id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM user_fast
+        WHERE id = OLD.id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.id
+        ON CONFLICT (id)
+            DO NOTHING;
+        -- Refresh post_fast, cause of user info changes
+        DELETE FROM post_aggregates_fast
+        WHERE creator_id = NEW.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            creator_id = NEW.id
+        ON CONFLICT (id)
+            DO NOTHING;
+        DELETE FROM comment_aggregates_fast
+        WHERE creator_id = NEW.id;
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            creator_id = NEW.id
+        ON CONFLICT (id)
+            DO NOTHING;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-    -- Update user view due to owner changes
-    delete from user_fast where id = NEW.creator_id;
-    insert into user_fast select * from user_view where id = NEW.creator_id on conflict (id) do nothing;
-    
-    -- Update post view due to community changes
-    delete from post_aggregates_fast where community_id = NEW.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where community_id = NEW.id on conflict (id) do nothing;
+CREATE OR REPLACE FUNCTION refresh_post ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts - 1
+        WHERE
+            id = OLD.community_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id
+        ON CONFLICT (id)
+            DO NOTHING;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update that users number of posts, post score
+        DELETE FROM user_fast
+        WHERE id = NEW.creator_id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.creator_id
+        ON CONFLICT (id)
+            DO NOTHING;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts + 1
+        WHERE
+            id = NEW.community_id;
+        -- Update the hot rank on the post table
+        -- TODO this might not correctly update it, using a 1 week interval
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = pav.hot_rank
+        FROM
+            post_aggregates_view AS pav
+        WHERE
+            paf.id = pav.id
+            AND (pav.published > ('now'::timestamp - '1 week'::interval));
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-  -- TODO make sure this shows up in the users page ?
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into community_aggregates_fast select * from community_aggregates_view where id = NEW.id;
-  END IF;
+CREATE OR REPLACE FUNCTION refresh_comment ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments - 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = OLD.post_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id
+        ON CONFLICT (id)
+            DO NOTHING;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update user view due to comment count
+        UPDATE
+            user_fast
+        SET
+            number_of_comments = number_of_comments + 1
+        WHERE
+            id = NEW.creator_id;
+        -- Update post view due to comment count, new comment activity time, but only on new posts
+        -- TODO this could be done more efficiently
+        DELETE FROM post_aggregates_fast
+        WHERE id = NEW.post_id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.post_id
+        ON CONFLICT (id)
+            DO NOTHING;
+        -- Force the hot rank as zero on week-older posts
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = 0
+        WHERE
+            paf.id = NEW.post_id
+            AND (paf.published < ('now'::timestamp - '1 week'::interval));
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments + 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = NEW.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-  return null;
-end $$;
-
-create or replace function refresh_user()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from user_fast where id = OLD.id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from user_fast where id = OLD.id;
-    insert into user_fast select * from user_view where id = NEW.id on conflict(id) do nothing;
-    
-    -- Refresh post_fast, cause of user info changes
-    delete from post_aggregates_fast where creator_id = NEW.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where creator_id = NEW.id on conflict (id) do nothing;
-
-    delete from comment_aggregates_fast where creator_id = NEW.id;
-    insert into comment_aggregates_fast select * from comment_aggregates_view where creator_id = NEW.id on conflict (id) do nothing;
-
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into user_fast select * from user_view where id = NEW.id;
-  END IF;
-
-  return null;
-end $$;
-
-create or replace function refresh_post()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
-
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts - 1 where id = OLD.community_id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id on conflict (id) do nothing;
-
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
-
-    -- Update that users number of posts, post score
-    delete from user_fast where id = NEW.creator_id;
-    insert into user_fast select * from user_view where id = NEW.creator_id on conflict (id) do nothing;
-  
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts + 1 where id = NEW.community_id;
-
-    -- Update the hot rank on the post table
-    -- TODO this might not correctly update it, using a 1 week interval
-    update post_aggregates_fast as paf
-    set hot_rank = pav.hot_rank 
-    from post_aggregates_view as pav
-    where paf.id = pav.id  and (pav.published > ('now'::timestamp - '1 week'::interval));
-  END IF;
-
-  return null;
-end $$;
-
-
-create or replace function refresh_comment()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments - 1
-    from post as p
-    where caf.id = p.community_id and p.id = OLD.post_id;
-
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id on conflict (id) do nothing;
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
-
-    -- Update user view due to comment count
-    update user_fast 
-    set number_of_comments = number_of_comments + 1
-    where id = NEW.creator_id;
-    
-    -- Update post view due to comment count, new comment activity time, but only on new posts
-    -- TODO this could be done more efficiently
-    delete from post_aggregates_fast where id = NEW.post_id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.post_id on conflict (id) do nothing;
-
-    -- Force the hot rank as zero on week-older posts
-    update post_aggregates_fast as paf
-    set hot_rank = 0
-    where paf.id = NEW.post_id and (paf.published < ('now'::timestamp - '1 week'::interval));
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments + 1 
-    from post as p
-    where caf.id = p.community_id and p.id = NEW.post_id;
-
-  END IF;
-
-  return null;
-end $$;
index d05418f5c9f46472e557054f66db1a2fd6e9d915..291913cb0a94056b9b5b9482a65dbdc05946fab4 100644 (file)
-create or replace function refresh_post()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
+CREATE OR REPLACE FUNCTION refresh_post ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts - 1
+        WHERE
+            id = OLD.community_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id
+        ON CONFLICT (id)
+            DO NOTHING;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update that users number of posts, post score
+        DELETE FROM user_fast
+        WHERE id = NEW.creator_id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.creator_id
+        ON CONFLICT (id)
+            DO NOTHING;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts + 1
+        WHERE
+            id = NEW.community_id;
+        -- Update the hot rank on the post table
+        -- TODO this might not correctly update it, using a 1 week interval
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = pav.hot_rank
+        FROM
+            post_aggregates_view AS pav
+        WHERE
+            paf.id = pav.id
+            AND (pav.published > ('now'::timestamp - '1 week'::interval));
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts - 1 where id = OLD.community_id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id on conflict (id) do nothing;
+CREATE OR REPLACE FUNCTION refresh_comment ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments - 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = OLD.post_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id
+        ON CONFLICT (id)
+            DO NOTHING;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update user view due to comment count
+        UPDATE
+            user_fast
+        SET
+            number_of_comments = number_of_comments + 1
+        WHERE
+            id = NEW.creator_id;
+        -- Update post view due to comment count, new comment activity time, but only on new posts
+        -- TODO this could be done more efficiently
+        DELETE FROM post_aggregates_fast
+        WHERE id = NEW.post_id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.post_id
+        ON CONFLICT (id)
+            DO NOTHING;
+        -- Force the hot rank as zero on week-older posts
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = 0
+        WHERE
+            paf.id = NEW.post_id
+            AND (paf.published < ('now'::timestamp - '1 week'::interval));
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments + 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = NEW.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
-
-    -- Update that users number of posts, post score
-    delete from user_fast where id = NEW.creator_id;
-    insert into user_fast select * from user_view where id = NEW.creator_id on conflict (id) do nothing;
-  
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts + 1 where id = NEW.community_id;
-
-    -- Update the hot rank on the post table
-    -- TODO this might not correctly update it, using a 1 week interval
-    update post_aggregates_fast as paf
-    set hot_rank = pav.hot_rank 
-    from post_aggregates_view as pav
-    where paf.id = pav.id  and (pav.published > ('now'::timestamp - '1 week'::interval));
-  END IF;
-
-  return null;
-end $$;
-
-
-create or replace function refresh_comment()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments - 1
-    from post as p
-    where caf.id = p.community_id and p.id = OLD.post_id;
-
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id on conflict (id) do nothing;
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
-
-    -- Update user view due to comment count
-    update user_fast 
-    set number_of_comments = number_of_comments + 1
-    where id = NEW.creator_id;
-    
-    -- Update post view due to comment count, new comment activity time, but only on new posts
-    -- TODO this could be done more efficiently
-    delete from post_aggregates_fast where id = NEW.post_id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.post_id on conflict (id) do nothing;
-
-    -- Force the hot rank as zero on week-older posts
-    update post_aggregates_fast as paf
-    set hot_rank = 0
-    where paf.id = NEW.post_id and (paf.published < ('now'::timestamp - '1 week'::interval));
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments + 1 
-    from post as p
-    where caf.id = p.community_id and p.id = NEW.post_id;
-
-  END IF;
-
-  return null;
-end $$;
index 64a5dccb4d1ccab40b437a9e067b575db49186cc..44934412f1c1352c70f84cda3b12349ea551f502 100644 (file)
 -- Forgot to add hot rank active to these two triggers
+CREATE OR REPLACE FUNCTION refresh_post ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts - 1
+        WHERE
+            id = OLD.community_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM post_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id
+        ON CONFLICT (id)
+            DO NOTHING;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update that users number of posts, post score
+        DELETE FROM user_fast
+        WHERE id = NEW.creator_id;
+        INSERT INTO user_fast
+        SELECT
+            *
+        FROM
+            user_view
+        WHERE
+            id = NEW.creator_id
+        ON CONFLICT (id)
+            DO NOTHING;
+        -- Update community number of posts
+        UPDATE
+            community_aggregates_fast
+        SET
+            number_of_posts = number_of_posts + 1
+        WHERE
+            id = NEW.community_id;
+        -- Update the hot rank on the post table
+        -- TODO this might not correctly update it, using a 1 week interval
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = pav.hot_rank,
+            hot_rank_active = pav.hot_rank_active
+        FROM
+            post_aggregates_view AS pav
+        WHERE
+            paf.id = pav.id
+            AND (pav.published > ('now'::timestamp - '1 week'::interval));
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create or replace function refresh_post()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
+CREATE OR REPLACE FUNCTION refresh_comment ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments - 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = OLD.post_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        DELETE FROM comment_aggregates_fast
+        WHERE id = OLD.id;
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id
+        ON CONFLICT (id)
+            DO NOTHING;
+    ELSIF (TG_OP = 'INSERT') THEN
+        INSERT INTO comment_aggregates_fast
+        SELECT
+            *
+        FROM
+            comment_aggregates_view
+        WHERE
+            id = NEW.id;
+        -- Update user view due to comment count
+        UPDATE
+            user_fast
+        SET
+            number_of_comments = number_of_comments + 1
+        WHERE
+            id = NEW.creator_id;
+        -- Update post view due to comment count, new comment activity time, but only on new posts
+        -- TODO this could be done more efficiently
+        DELETE FROM post_aggregates_fast
+        WHERE id = NEW.post_id;
+        INSERT INTO post_aggregates_fast
+        SELECT
+            *
+        FROM
+            post_aggregates_view
+        WHERE
+            id = NEW.post_id
+        ON CONFLICT (id)
+            DO NOTHING;
+        -- Update the comment hot_ranks as of last week
+        UPDATE
+            comment_aggregates_fast AS caf
+        SET
+            hot_rank = cav.hot_rank,
+            hot_rank_active = cav.hot_rank_active
+        FROM
+            comment_aggregates_view AS cav
+        WHERE
+            caf.id = cav.id
+            AND (cav.published > ('now'::timestamp - '1 week'::interval));
+        -- Update the post ranks
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank = pav.hot_rank,
+            hot_rank_active = pav.hot_rank_active
+        FROM
+            post_aggregates_view AS pav
+        WHERE
+            paf.id = pav.id
+            AND (pav.published > ('now'::timestamp - '1 week'::interval));
+        -- Force the hot rank active as zero on 2 day-older posts (necro-bump)
+        UPDATE
+            post_aggregates_fast AS paf
+        SET
+            hot_rank_active = 0
+        WHERE
+            paf.id = NEW.post_id
+            AND (paf.published < ('now'::timestamp - '2 days'::interval));
+        -- Update community number of comments
+        UPDATE
+            community_aggregates_fast AS caf
+        SET
+            number_of_comments = number_of_comments + 1
+        FROM
+            post AS p
+        WHERE
+            caf.id = p.community_id
+            AND p.id = NEW.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts - 1 where id = OLD.community_id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from post_aggregates_fast where id = OLD.id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id on conflict (id) do nothing;
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.id;
-
-    -- Update that users number of posts, post score
-    delete from user_fast where id = NEW.creator_id;
-    insert into user_fast select * from user_view where id = NEW.creator_id on conflict (id) do nothing;
-  
-    -- Update community number of posts
-    update community_aggregates_fast set number_of_posts = number_of_posts + 1 where id = NEW.community_id;
-
-    -- Update the hot rank on the post table
-    -- TODO this might not correctly update it, using a 1 week interval
-    update post_aggregates_fast as paf
-    set 
-      hot_rank = pav.hot_rank,
-      hot_rank_active = pav.hot_rank_active
-    from post_aggregates_view as pav
-    where paf.id = pav.id  and (pav.published > ('now'::timestamp - '1 week'::interval));
-  END IF;
-
-  return null;
-end $$;
-
-create or replace function refresh_comment()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'DELETE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments - 1
-    from post as p
-    where caf.id = p.community_id and p.id = OLD.post_id;
-
-  ELSIF (TG_OP = 'UPDATE') THEN
-    delete from comment_aggregates_fast where id = OLD.id;
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id on conflict (id) do nothing;
-  ELSIF (TG_OP = 'INSERT') THEN
-    insert into comment_aggregates_fast select * from comment_aggregates_view where id = NEW.id;
-
-    -- Update user view due to comment count
-    update user_fast 
-    set number_of_comments = number_of_comments + 1
-    where id = NEW.creator_id;
-    
-    -- Update post view due to comment count, new comment activity time, but only on new posts
-    -- TODO this could be done more efficiently
-    delete from post_aggregates_fast where id = NEW.post_id;
-    insert into post_aggregates_fast select * from post_aggregates_view where id = NEW.post_id on conflict (id) do nothing;
-
-    -- Update the comment hot_ranks as of last week
-    update comment_aggregates_fast as caf
-    set 
-      hot_rank = cav.hot_rank,
-      hot_rank_active = cav.hot_rank_active
-    from comment_aggregates_view as cav
-    where caf.id = cav.id and (cav.published > ('now'::timestamp - '1 week'::interval));
-
-    -- Update the post ranks
-    update post_aggregates_fast as paf
-    set 
-      hot_rank = pav.hot_rank,
-      hot_rank_active = pav.hot_rank_active
-    from post_aggregates_view as pav
-    where paf.id = pav.id  and (pav.published > ('now'::timestamp - '1 week'::interval));
-
-    -- Force the hot rank active as zero on 2 day-older posts (necro-bump)
-    update post_aggregates_fast as paf
-    set hot_rank_active = 0
-    where paf.id = NEW.post_id and (paf.published < ('now'::timestamp - '2 days'::interval));
-
-    -- Update community number of comments
-    update community_aggregates_fast as caf
-    set number_of_comments = number_of_comments + 1 
-    from post as p
-    where caf.id = p.community_id and p.id = NEW.post_id;
-
-  END IF;
-
-  return null;
-end $$;
index e1c39faaf2b4eeb1f23cacac3cd6d3f47adb5fa0..9dfdbd48eece047cfb79f8f007500d019dd11524 100644 (file)
@@ -1,4 +1,8 @@
-drop view comment_report_view;
-drop view post_report_view;
-drop table comment_report;
-drop table post_report;
+DROP VIEW comment_report_view;
+
+DROP VIEW post_report_view;
+
+DROP TABLE comment_report;
+
+DROP TABLE post_report;
+
index e9dce1adb327996dab53b3fad71f2d2d7461c193..9aafa3a7037ad98a91fdd4d321628de36949e3c2 100644 (file)
@@ -1,89 +1,94 @@
-create table comment_report (
-  id            serial    primary key,
-  creator_id    int       references user_ on update cascade on delete cascade not null,   -- user reporting comment
-  comment_id    int       references comment on update cascade on delete cascade not null, -- comment being reported
-  original_comment_text  text      not null,
-  reason        text      not null,
-  resolved      bool      not null default false,
-  resolver_id   int       references user_ on update cascade on delete cascade,   -- user resolving report
-  published     timestamp not null default now(),
-  updated       timestamp null,
-  unique(comment_id, creator_id) -- users should only be able to report a comment once
+CREATE TABLE comment_report (
+    id serial PRIMARY KEY,
+    creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, -- user reporting comment
+    comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, -- comment being reported
+    original_comment_text text NOT NULL,
+    reason text NOT NULL,
+    resolved bool NOT NULL DEFAULT FALSE,
+    resolver_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE, -- user resolving report
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp NULL,
+    UNIQUE (comment_id, creator_id) -- users should only be able to report a comment once
 );
 
-create table post_report (
-  id            serial    primary key,
-  creator_id    int       references user_ on update cascade on delete cascade not null, -- user reporting post
-  post_id       int       references post on update cascade on delete cascade not null,  -- post being reported
-  original_post_name     varchar(100) not null,
-  original_post_url       text,
-  original_post_body      text,
-  reason        text      not null,
-  resolved      bool      not null default false,
-  resolver_id   int       references user_ on update cascade on delete cascade,   -- user resolving report
-  published     timestamp not null default now(),
-  updated       timestamp null,
-  unique(post_id, creator_id) -- users should only be able to report a post once
+CREATE TABLE post_report (
+    id serial PRIMARY KEY,
+    creator_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, -- user reporting post
+    post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, -- post being reported
+    original_post_name varchar(100) NOT NULL,
+    original_post_url text,
+    original_post_body text,
+    reason text NOT NULL,
+    resolved bool NOT NULL DEFAULT FALSE,
+    resolver_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE, -- user resolving report
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp NULL,
+    UNIQUE (post_id, creator_id) -- users should only be able to report a post once
 );
 
-create or replace view comment_report_view as
-select cr.*,
-c.post_id,
-c.content as current_comment_text,
-p.community_id,
--- report creator details
-f.actor_id as creator_actor_id,
-f.name as creator_name,
-f.preferred_username as creator_preferred_username,
-f.avatar as creator_avatar,
-f.local as creator_local,
--- comment creator details
-u.id as comment_creator_id,
-u.actor_id as comment_creator_actor_id,
-u.name as comment_creator_name,
-u.preferred_username as comment_creator_preferred_username,
-u.avatar as comment_creator_avatar,
-u.local as comment_creator_local,
--- resolver details
-r.actor_id as resolver_actor_id,
-r.name as resolver_name,
-r.preferred_username as resolver_preferred_username,
-r.avatar as resolver_avatar,
-r.local as resolver_local
-from comment_report cr
-left join comment c on c.id = cr.comment_id
-left join post p on p.id = c.post_id
-left join user_ u on u.id = c.creator_id
-left join user_ f on f.id = cr.creator_id
-left join user_ r on r.id = cr.resolver_id;
+CREATE OR REPLACE VIEW comment_report_view AS
+SELECT
+    cr.*,
+    c.post_id,
+    c.content AS current_comment_text,
+    p.community_id,
+    -- report creator details
+    f.actor_id AS creator_actor_id,
+    f.name AS creator_name,
+    f.preferred_username AS creator_preferred_username,
+    f.avatar AS creator_avatar,
+    f.local AS creator_local,
+    -- comment creator details
+    u.id AS comment_creator_id,
+    u.actor_id AS comment_creator_actor_id,
+    u.name AS comment_creator_name,
+    u.preferred_username AS comment_creator_preferred_username,
+    u.avatar AS comment_creator_avatar,
+    u.local AS comment_creator_local,
+    -- resolver details
+    r.actor_id AS resolver_actor_id,
+    r.name AS resolver_name,
+    r.preferred_username AS resolver_preferred_username,
+    r.avatar AS resolver_avatar,
+    r.local AS resolver_local
+FROM
+    comment_report cr
+    LEFT JOIN comment c ON c.id = cr.comment_id
+    LEFT JOIN post p ON p.id = c.post_id
+    LEFT JOIN user_ u ON u.id = c.creator_id
+    LEFT JOIN user_ f ON f.id = cr.creator_id
+    LEFT JOIN user_ r ON r.id = cr.resolver_id;
+
+CREATE OR REPLACE VIEW post_report_view AS
+SELECT
+    pr.*,
+    p.name AS current_post_name,
+    p.url AS current_post_url,
+    p.body AS current_post_body,
+    p.community_id,
+    -- report creator details
+    f.actor_id AS creator_actor_id,
+    f.name AS creator_name,
+    f.preferred_username AS creator_preferred_username,
+    f.avatar AS creator_avatar,
+    f.local AS creator_local,
+    -- post creator details
+    u.id AS post_creator_id,
+    u.actor_id AS post_creator_actor_id,
+    u.name AS post_creator_name,
+    u.preferred_username AS post_creator_preferred_username,
+    u.avatar AS post_creator_avatar,
+    u.local AS post_creator_local,
+    -- resolver details
+    r.actor_id AS resolver_actor_id,
+    r.name AS resolver_name,
+    r.preferred_username AS resolver_preferred_username,
+    r.avatar AS resolver_avatar,
+    r.local AS resolver_local
+FROM
+    post_report pr
+    LEFT JOIN post p ON p.id = pr.post_id
+    LEFT JOIN user_ u ON u.id = p.creator_id
+    LEFT JOIN user_ f ON f.id = pr.creator_id
+    LEFT JOIN user_ r ON r.id = pr.resolver_id;
 
-create or replace view post_report_view as
-select pr.*,
-p.name as current_post_name,
-p.url as current_post_url,
-p.body as current_post_body,
-p.community_id,
--- report creator details
-f.actor_id as creator_actor_id,
-f.name as creator_name,
-f.preferred_username as creator_preferred_username,
-f.avatar as creator_avatar,
-f.local as creator_local,
--- post creator details
-u.id as post_creator_id,
-u.actor_id as post_creator_actor_id,
-u.name as post_creator_name,
-u.preferred_username as post_creator_preferred_username,
-u.avatar as post_creator_avatar,
-u.local as post_creator_local,
--- resolver details
-r.actor_id as resolver_actor_id,
-r.name as resolver_name,
-r.preferred_username as resolver_preferred_username,
-r.avatar as resolver_avatar,
-r.local as resolver_local
-from post_report pr
-left join post p on p.id = pr.post_id
-left join user_ u on u.id = p.creator_id
-left join user_ f on f.id = pr.creator_id
-left join user_ r on r.id = pr.resolver_id;
index 7978df72ef39bfacd245a76e70dc2df27310e3f6..5abef5a7f6d0ea48cf35b95e524b9b61bd5c384e 100644 (file)
@@ -1 +1,3 @@
-ALTER TABLE activity DROP COLUMN ap_id;
\ No newline at end of file
+ALTER TABLE activity
+    DROP COLUMN ap_id;
+
index ab22a4c441337b95700910f7dc14052f4b1ced2c..d0df8814a873f8b66bb00b63cde42bebd0799b0b 100644 (file)
@@ -1 +1,3 @@
-ALTER TABLE activity ADD COLUMN ap_id TEXT;
\ No newline at end of file
+ALTER TABLE activity
+    ADD COLUMN ap_id TEXT;
+
index bb093f1bbd9f85ebf7f5fcc344588f941f628ce0..580c93e0dad8ffc3b31466ff52a1c05443eb751f 100644 (file)
@@ -1,2 +1,6 @@
-ALTER TABLE activity ADD COLUMN user_id INTEGER;
-ALTER TABLE activity DROP COLUMN sensitive;
\ No newline at end of file
+ALTER TABLE activity
+    ADD COLUMN user_id INTEGER;
+
+ALTER TABLE activity
+    DROP COLUMN sensitive;
+
index 22a1a2fae7adf7a5039ec8f0a5e5cb2f47982801..66b33dd8b9a9da56b2de2ec6bb68b7620258b3cc 100644 (file)
@@ -1,2 +1,6 @@
-ALTER TABLE activity DROP COLUMN user_id;
-ALTER TABLE activity ADD COLUMN sensitive BOOLEAN DEFAULT TRUE;
\ No newline at end of file
+ALTER TABLE activity
+    DROP COLUMN user_id;
+
+ALTER TABLE activity
+    ADD COLUMN sensitive BOOLEAN DEFAULT TRUE;
+
index 72cbeaa377fe1963cc19ee3c0f409cee5bf55a26..f61482f9d72265bd4b064bde771d9293e630f28a 100644 (file)
@@ -1 +1,3 @@
-ALTER TABLE community_follower DROP COLUMN pending;
\ No newline at end of file
+ALTER TABLE community_follower
+    DROP COLUMN pending;
+
index 599bed624891075354b9f54c73a1ea1de6233110..163dc05232ebace13b8278c31903e1be34cfb04e 100644 (file)
@@ -1 +1,3 @@
-ALTER TABLE community_follower ADD COLUMN pending BOOLEAN DEFAULT FALSE;
\ No newline at end of file
+ALTER TABLE community_follower
+    ADD COLUMN pending BOOLEAN DEFAULT FALSE;
+
index 09b05878cf3e98eb09455f39af28a671e65ab4af..51a9ef6bcdb7235649e894f26379f6f78af4806c 100644 (file)
@@ -1 +1,3 @@
-ALTER TABLE user_ DROP COLUMN deleted;
+ALTER TABLE user_
+    DROP COLUMN deleted;
+
index 1d752c573cbb8a5b7cc980424f0c245f7a180466..7fc226f6ae3efbac22b4da7bbc520652497f29e3 100644 (file)
@@ -1 +1,3 @@
-ALTER TABLE user_ ADD COLUMN deleted BOOLEAN DEFAULT FALSE NOT NULL;
+ALTER TABLE user_
+    ADD COLUMN deleted BOOLEAN DEFAULT FALSE NOT NULL;
+
index 914bdb8d87a8f4076bdb7d4173d09b9d9fc5831f..6535f0da9e3a53a13b8fb599595d748dbe5415f7 100644 (file)
@@ -1,21 +1,23 @@
 -- Site aggregates
-drop table site_aggregates;
-drop trigger site_aggregates_site on site;
-drop trigger site_aggregates_user_insert on user_;
-drop trigger site_aggregates_user_delete on user_;
-drop trigger site_aggregates_post_insert on post;
-drop trigger site_aggregates_post_delete on post;
-drop trigger site_aggregates_comment_insert on comment;
-drop trigger site_aggregates_comment_delete on comment;
-drop trigger site_aggregates_community_insert on community;
-drop trigger site_aggregates_community_delete on community;
-drop function 
-  site_aggregates_site,
-  site_aggregates_user_insert,
-  site_aggregates_user_delete,
-  site_aggregates_post_insert,
-  site_aggregates_post_delete,
-  site_aggregates_comment_insert,
-  site_aggregates_comment_delete,
-  site_aggregates_community_insert,
-  site_aggregates_community_delete;
+DROP TABLE site_aggregates;
+
+DROP TRIGGER site_aggregates_site ON site;
+
+DROP TRIGGER site_aggregates_user_insert ON user_;
+
+DROP TRIGGER site_aggregates_user_delete ON user_;
+
+DROP TRIGGER site_aggregates_post_insert ON post;
+
+DROP TRIGGER site_aggregates_post_delete ON post;
+
+DROP TRIGGER site_aggregates_comment_insert ON comment;
+
+DROP TRIGGER site_aggregates_comment_delete ON comment;
+
+DROP TRIGGER site_aggregates_community_insert ON community;
+
+DROP TRIGGER site_aggregates_community_delete ON community;
+
+DROP FUNCTION site_aggregates_site, site_aggregates_user_insert, site_aggregates_user_delete, site_aggregates_post_insert, site_aggregates_post_delete, site_aggregates_comment_insert, site_aggregates_comment_delete, site_aggregates_community_insert, site_aggregates_community_delete;
+
index 679543d19cae5e959b2de2df6fe8a53944f56c24..79b5a9f87120e84ddd14a8079637e2bd5f5ccbb5 100644 (file)
 -- Add site aggregates
-create table site_aggregates (
-  id serial primary key,
-  site_id int references site on update cascade on delete cascade not null,
-  users bigint not null default 1,
-  posts bigint not null default 0,
-  comments bigint not null default 0,
-  communities bigint not null default 0
+CREATE TABLE site_aggregates (
+    id serial PRIMARY KEY,
+    site_id int REFERENCES site ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    users bigint NOT NULL DEFAULT 1,
+    posts bigint NOT NULL DEFAULT 0,
+    comments bigint NOT NULL DEFAULT 0,
+    communities bigint NOT NULL DEFAULT 0
 );
 
-insert into site_aggregates (site_id, users, posts, comments, communities)
-  select id as site_id,
-  ( select coalesce(count(*), 0) from user_ where local = true) as users, 
-  ( select coalesce(count(*), 0) from post where local = true) as posts,
-  ( select coalesce(count(*), 0) from comment where local = true) as comments,
-  ( select coalesce(count(*), 0) from community where local = true) as communities
-  from site;
+INSERT INTO site_aggregates (site_id, users, posts, comments, communities)
+SELECT
+    id AS site_id,
+    (
+        SELECT
+            coalesce(count(*), 0)
+        FROM
+            user_
+        WHERE
+            local = TRUE) AS users,
+    (
+        SELECT
+            coalesce(count(*), 0)
+        FROM
+            post
+        WHERE
+            local = TRUE) AS posts,
+    (
+        SELECT
+            coalesce(count(*), 0)
+        FROM
+            comment
+        WHERE
+            local = TRUE) AS comments,
+    (
+        SELECT
+            coalesce(count(*), 0)
+        FROM
+            community
+        WHERE
+            local = TRUE) AS communities
+FROM
+    site;
 
 -- initial site add
-create function site_aggregates_site()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into site_aggregates (site_id) values (NEW.id);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from site_aggregates where site_id = OLD.id;
-  END IF;
-  return null;
-end $$;
-
-create trigger site_aggregates_site
-after insert or delete on site
-for each row
-execute procedure site_aggregates_site();
+CREATE FUNCTION site_aggregates_site ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO site_aggregates (site_id)
+            VALUES (NEW.id);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM site_aggregates
+        WHERE site_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER site_aggregates_site
+    AFTER INSERT OR DELETE ON site
+    FOR EACH ROW
+    EXECUTE PROCEDURE site_aggregates_site ();
 
 -- Add site aggregate triggers
 -- user
-create function site_aggregates_user_insert()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates 
-  set users = users + 1;
-  return null;
-end $$;
-
-create function site_aggregates_user_delete()
-returns trigger language plpgsql
-as $$
-begin
-  -- Join to site since the creator might not be there anymore
-  update site_aggregates sa
-  set users = users - 1
-  from site s
-  where sa.site_id = s.id;
-  return null;
-end $$;
-
-create trigger site_aggregates_user_insert
-after insert on user_
-for each row
-when (NEW.local = true)
-execute procedure site_aggregates_user_insert();
-
-create trigger site_aggregates_user_delete
-after delete on user_
-for each row
-when (OLD.local = true)
-execute procedure site_aggregates_user_delete();
+CREATE FUNCTION site_aggregates_user_insert ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates
+    SET
+        users = users + 1;
+    RETURN NULL;
+END
+$$;
+
+CREATE FUNCTION site_aggregates_user_delete ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    -- Join to site since the creator might not be there anymore
+    UPDATE
+        site_aggregates sa
+    SET
+        users = users - 1
+    FROM
+        site s
+    WHERE
+        sa.site_id = s.id;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER site_aggregates_user_insert
+    AFTER INSERT ON user_
+    FOR EACH ROW
+    WHEN (NEW.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_user_insert ();
+
+CREATE TRIGGER site_aggregates_user_delete
+    AFTER DELETE ON user_
+    FOR EACH ROW
+    WHEN (OLD.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_user_delete ();
 
 -- post
-create function site_aggregates_post_insert()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates 
-  set posts = posts + 1;
-  return null;
-end $$;
-
-create function site_aggregates_post_delete()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates sa
-  set posts = posts - 1
-  from site s
-  where sa.site_id = s.id;
-  return null;
-end $$;
-
-create trigger site_aggregates_post_insert
-after insert on post
-for each row
-when (NEW.local = true)
-execute procedure site_aggregates_post_insert();
-
-create trigger site_aggregates_post_delete
-after delete on post
-for each row
-when (OLD.local = true)
-execute procedure site_aggregates_post_delete();
+CREATE FUNCTION site_aggregates_post_insert ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates
+    SET
+        posts = posts + 1;
+    RETURN NULL;
+END
+$$;
+
+CREATE FUNCTION site_aggregates_post_delete ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates sa
+    SET
+        posts = posts - 1
+    FROM
+        site s
+    WHERE
+        sa.site_id = s.id;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER site_aggregates_post_insert
+    AFTER INSERT ON post
+    FOR EACH ROW
+    WHEN (NEW.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_post_insert ();
+
+CREATE TRIGGER site_aggregates_post_delete
+    AFTER DELETE ON post
+    FOR EACH ROW
+    WHEN (OLD.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_post_delete ();
 
 -- comment
-create function site_aggregates_comment_insert()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates 
-  set comments = comments + 1;
-  return null;
-end $$;
-
-create function site_aggregates_comment_delete()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates sa
-  set comments = comments - 1
-  from site s
-  where sa.site_id = s.id;
-  return null;
-end $$;
-
-create trigger site_aggregates_comment_insert
-after insert on comment
-for each row
-when (NEW.local = true)
-execute procedure site_aggregates_comment_insert();
-
-create trigger site_aggregates_comment_delete
-after delete on comment
-for each row
-when (OLD.local = true)
-execute procedure site_aggregates_comment_delete();
+CREATE FUNCTION site_aggregates_comment_insert ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates
+    SET
+        comments = comments + 1;
+    RETURN NULL;
+END
+$$;
+
+CREATE FUNCTION site_aggregates_comment_delete ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates sa
+    SET
+        comments = comments - 1
+    FROM
+        site s
+    WHERE
+        sa.site_id = s.id;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER site_aggregates_comment_insert
+    AFTER INSERT ON comment
+    FOR EACH ROW
+    WHEN (NEW.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_comment_insert ();
+
+CREATE TRIGGER site_aggregates_comment_delete
+    AFTER DELETE ON comment
+    FOR EACH ROW
+    WHEN (OLD.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_comment_delete ();
 
 -- community
-create function site_aggregates_community_insert()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates 
-  set communities = communities + 1;
-  return null;
-end $$;
-
-create function site_aggregates_community_delete()
-returns trigger language plpgsql
-as $$
-begin
-  update site_aggregates sa
-  set communities = communities - 1
-  from site s
-  where sa.site_id = s.id;
-  return null;
-end $$;
-
-create trigger site_aggregates_community_insert
-after insert on community
-for each row
-when (NEW.local = true)
-execute procedure site_aggregates_community_insert();
-
-create trigger site_aggregates_community_delete
-after delete on community
-for each row
-when (OLD.local = true)
-execute procedure site_aggregates_community_delete();
+CREATE FUNCTION site_aggregates_community_insert ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates
+    SET
+        communities = communities + 1;
+    RETURN NULL;
+END
+$$;
+
+CREATE FUNCTION site_aggregates_community_delete ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates sa
+    SET
+        communities = communities - 1
+    FROM
+        site s
+    WHERE
+        sa.site_id = s.id;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER site_aggregates_community_insert
+    AFTER INSERT ON community
+    FOR EACH ROW
+    WHEN (NEW.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_community_insert ();
+
+CREATE TRIGGER site_aggregates_community_delete
+    AFTER DELETE ON community
+    FOR EACH ROW
+    WHEN (OLD.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_community_delete ();
+
index a7b5e4737968161ec839e3c3afc6bc85797b6775..756919d931631b3be1c564fc1edc8b4ff505568a 100644 (file)
@@ -1,13 +1,15 @@
 -- User aggregates
-drop table user_aggregates;
-drop trigger user_aggregates_user on user_;
-drop trigger user_aggregates_post_count on post;
-drop trigger user_aggregates_post_score on post_like;
-drop trigger user_aggregates_comment_count on comment;
-drop trigger user_aggregates_comment_score on comment_like;
-drop function 
-  user_aggregates_user, 
-  user_aggregates_post_count,
-  user_aggregates_post_score,
-  user_aggregates_comment_count,
-  user_aggregates_comment_score;
+DROP TABLE user_aggregates;
+
+DROP TRIGGER user_aggregates_user ON user_;
+
+DROP TRIGGER user_aggregates_post_count ON post;
+
+DROP TRIGGER user_aggregates_post_score ON post_like;
+
+DROP TRIGGER user_aggregates_comment_count ON comment;
+
+DROP TRIGGER user_aggregates_comment_score ON comment_like;
+
+DROP FUNCTION user_aggregates_user, user_aggregates_post_count, user_aggregates_post_score, user_aggregates_comment_count, user_aggregates_comment_score;
+
index 7b4c83af26541634993e085426f3b11bce072843..79218b90eb5e6acb8dae758384da9f1753782de7 100644 (file)
 -- Add user aggregates
-create table user_aggregates (
-  id serial primary key,
-  user_id int references user_ on update cascade on delete cascade not null,
-  post_count bigint not null default 0,
-  post_score bigint not null default 0,
-  comment_count bigint not null default 0,
-  comment_score bigint not null default 0,
-  unique (user_id)
+CREATE TABLE user_aggregates (
+    id serial PRIMARY KEY,
+    user_id int REFERENCES user_ ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    post_count bigint NOT NULL DEFAULT 0,
+    post_score bigint NOT NULL DEFAULT 0,
+    comment_count bigint NOT NULL DEFAULT 0,
+    comment_score bigint NOT NULL DEFAULT 0,
+    UNIQUE (user_id)
 );
 
-insert into user_aggregates (user_id, post_count, post_score, comment_count, comment_score)
-  select u.id,
-  coalesce(pd.posts, 0),
-  coalesce(pd.score, 0),
-  coalesce(cd.comments, 0),
-  coalesce(cd.score, 0)
-  from user_ u
-  left join (
-    select p.creator_id,
-      count(distinct p.id) as posts,
-      sum(pl.score) as score
-      from post p
-      left join post_like pl on p.id = pl.post_id
-      group by p.creator_id
-    ) pd on u.id = pd.creator_id
-  left join ( 
-    select c.creator_id,
-    count(distinct c.id) as comments,
-    sum(cl.score) as score
-    from comment c
-    left join comment_like cl on c.id = cl.comment_id
-    group by c.creator_id
-  ) cd on u.id = cd.creator_id;
-
+INSERT INTO user_aggregates (user_id, post_count, post_score, comment_count, comment_score)
+SELECT
+    u.id,
+    coalesce(pd.posts, 0),
+    coalesce(pd.score, 0),
+    coalesce(cd.comments, 0),
+    coalesce(cd.score, 0)
+FROM
+    user_ u
+    LEFT JOIN (
+        SELECT
+            p.creator_id,
+            count(DISTINCT p.id) AS posts,
+            sum(pl.score) AS score
+        FROM
+            post p
+            LEFT JOIN post_like pl ON p.id = pl.post_id
+        GROUP BY
+            p.creator_id) pd ON u.id = pd.creator_id
+    LEFT JOIN (
+        SELECT
+            c.creator_id,
+            count(DISTINCT c.id) AS comments,
+            sum(cl.score) AS score
+        FROM
+            comment c
+            LEFT JOIN comment_like cl ON c.id = cl.comment_id
+        GROUP BY
+            c.creator_id) cd ON u.id = cd.creator_id;
 
 -- Add user aggregate triggers
-
 -- initial user add
-create function user_aggregates_user()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into user_aggregates (user_id) values (NEW.id);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from user_aggregates where user_id = OLD.id;
-  END IF;
-  return null;
-end $$;
-
-create trigger user_aggregates_user
-after insert or delete on user_
-for each row
-execute procedure user_aggregates_user();
+CREATE FUNCTION user_aggregates_user ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO user_aggregates (user_id)
+            VALUES (NEW.id);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM user_aggregates
+        WHERE user_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER user_aggregates_user
+    AFTER INSERT OR DELETE ON user_
+    FOR EACH ROW
+    EXECUTE PROCEDURE user_aggregates_user ();
 
 -- post count
-create function user_aggregates_post_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update user_aggregates 
-    set post_count = post_count + 1 where user_id = NEW.creator_id;
-
-  ELSIF (TG_OP = 'DELETE') THEN
-    update user_aggregates 
-    set post_count = post_count - 1 where user_id = OLD.creator_id;
-
-    -- If the post gets deleted, the score calculation trigger won't fire, 
-    -- so you need to re-calculate
-    update user_aggregates ua
-    set post_score = pd.score
-    from (
-      select u.id,
-      coalesce(0, sum(pl.score)) as score
-      -- User join because posts could be empty
-      from user_ u 
-      left join post p on u.id = p.creator_id
-      left join post_like pl on p.id = pl.post_id
-      group by u.id
-    ) pd 
-    where ua.user_id = OLD.creator_id;
-
-  END IF;
-  return null;
-end $$;
-
-create trigger user_aggregates_post_count
-after insert or delete on post
-for each row
-execute procedure user_aggregates_post_count();
+CREATE FUNCTION user_aggregates_post_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            user_aggregates
+        SET
+            post_count = post_count + 1
+        WHERE
+            user_id = NEW.creator_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            user_aggregates
+        SET
+            post_count = post_count - 1
+        WHERE
+            user_id = OLD.creator_id;
+        -- If the post gets deleted, the score calculation trigger won't fire,
+        -- so you need to re-calculate
+        UPDATE
+            user_aggregates ua
+        SET
+            post_score = pd.score
+        FROM (
+            SELECT
+                u.id,
+                coalesce(0, sum(pl.score)) AS score
+                -- User join because posts could be empty
+            FROM
+                user_ u
+            LEFT JOIN post p ON u.id = p.creator_id
+            LEFT JOIN post_like pl ON p.id = pl.post_id
+        GROUP BY
+            u.id) pd
+    WHERE
+        ua.user_id = OLD.creator_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER user_aggregates_post_count
+    AFTER INSERT OR DELETE ON post
+    FOR EACH ROW
+    EXECUTE PROCEDURE user_aggregates_post_count ();
 
 -- post score
-create function user_aggregates_post_score()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    -- Need to get the post creator, not the voter
-    update user_aggregates ua
-    set post_score = post_score + NEW.score
-    from post p
-    where ua.user_id = p.creator_id and p.id = NEW.post_id;
-    
-  ELSIF (TG_OP = 'DELETE') THEN
-    update user_aggregates ua
-    set post_score = post_score - OLD.score
-    from post p
-    where ua.user_id = p.creator_id and p.id = OLD.post_id;
-  END IF;
-  return null;
-end $$;
-
-create trigger user_aggregates_post_score
-after insert or delete on post_like
-for each row
-execute procedure user_aggregates_post_score();
+CREATE FUNCTION user_aggregates_post_score ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        -- Need to get the post creator, not the voter
+        UPDATE
+            user_aggregates ua
+        SET
+            post_score = post_score + NEW.score
+        FROM
+            post p
+        WHERE
+            ua.user_id = p.creator_id
+            AND p.id = NEW.post_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            user_aggregates ua
+        SET
+            post_score = post_score - OLD.score
+        FROM
+            post p
+        WHERE
+            ua.user_id = p.creator_id
+            AND p.id = OLD.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER user_aggregates_post_score
+    AFTER INSERT OR DELETE ON post_like
+    FOR EACH ROW
+    EXECUTE PROCEDURE user_aggregates_post_score ();
 
 -- comment count
-create function user_aggregates_comment_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update user_aggregates 
-    set comment_count = comment_count + 1 where user_id = NEW.creator_id;
-  ELSIF (TG_OP = 'DELETE') THEN
-    update user_aggregates 
-    set comment_count = comment_count - 1 where user_id = OLD.creator_id;
-
-    -- If the comment gets deleted, the score calculation trigger won't fire, 
-    -- so you need to re-calculate
-    update user_aggregates ua
-    set comment_score = cd.score
-    from (
-      select u.id,
-      coalesce(0, sum(cl.score)) as score
-      -- User join because comments could be empty
-      from user_ u 
-      left join comment c on u.id = c.creator_id
-      left join comment_like cl on c.id = cl.comment_id
-      group by u.id
-    ) cd 
-    where ua.user_id = OLD.creator_id;
-  END IF;
-  return null;
-end $$;
-
-create trigger user_aggregates_comment_count
-after insert or delete on comment
-for each row
-execute procedure user_aggregates_comment_count();
+CREATE FUNCTION user_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            user_aggregates
+        SET
+            comment_count = comment_count + 1
+        WHERE
+            user_id = NEW.creator_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            user_aggregates
+        SET
+            comment_count = comment_count - 1
+        WHERE
+            user_id = OLD.creator_id;
+        -- If the comment gets deleted, the score calculation trigger won't fire,
+        -- so you need to re-calculate
+        UPDATE
+            user_aggregates ua
+        SET
+            comment_score = cd.score
+        FROM (
+            SELECT
+                u.id,
+                coalesce(0, sum(cl.score)) AS score
+                -- User join because comments could be empty
+            FROM
+                user_ u
+            LEFT JOIN comment c ON u.id = c.creator_id
+            LEFT JOIN comment_like cl ON c.id = cl.comment_id
+        GROUP BY
+            u.id) cd
+    WHERE
+        ua.user_id = OLD.creator_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER user_aggregates_comment_count
+    AFTER INSERT OR DELETE ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE user_aggregates_comment_count ();
 
 -- comment score
-create function user_aggregates_comment_score()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    -- Need to get the post creator, not the voter
-    update user_aggregates ua
-    set comment_score = comment_score + NEW.score
-    from comment c
-    where ua.user_id = c.creator_id and c.id = NEW.comment_id;
-  ELSIF (TG_OP = 'DELETE') THEN
-    update user_aggregates ua
-    set comment_score = comment_score - OLD.score
-    from comment c
-    where ua.user_id = c.creator_id and c.id = OLD.comment_id;
-  END IF;
-  return null;
-end $$;
+CREATE FUNCTION user_aggregates_comment_score ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        -- Need to get the post creator, not the voter
+        UPDATE
+            user_aggregates ua
+        SET
+            comment_score = comment_score + NEW.score
+        FROM
+            comment c
+        WHERE
+            ua.user_id = c.creator_id
+            AND c.id = NEW.comment_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            user_aggregates ua
+        SET
+            comment_score = comment_score - OLD.score
+        FROM
+            comment c
+        WHERE
+            ua.user_id = c.creator_id
+            AND c.id = OLD.comment_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER user_aggregates_comment_score
+    AFTER INSERT OR DELETE ON comment_like
+    FOR EACH ROW
+    EXECUTE PROCEDURE user_aggregates_comment_score ();
 
-create trigger user_aggregates_comment_score
-after insert or delete on comment_like
-for each row
-execute procedure user_aggregates_comment_score();
index fc0ffd21aa996be5ebd4af173f6c8a98cb05f7ba..6a694e505ad3f86490e6b0ddfd18bfe777ed6c35 100644 (file)
@@ -1,11 +1,13 @@
 -- community aggregates
-drop table community_aggregates;
-drop trigger community_aggregates_community on community;
-drop trigger community_aggregates_post_count on post;
-drop trigger community_aggregates_comment_count on comment;
-drop trigger community_aggregates_subscriber_count on community_follower;
-drop function 
-  community_aggregates_community,
-  community_aggregates_post_count,
-  community_aggregates_comment_count,
-  community_aggregates_subscriber_count;
+DROP TABLE community_aggregates;
+
+DROP TRIGGER community_aggregates_community ON community;
+
+DROP TRIGGER community_aggregates_post_count ON post;
+
+DROP TRIGGER community_aggregates_comment_count ON comment;
+
+DROP TRIGGER community_aggregates_subscriber_count ON community_follower;
+
+DROP FUNCTION community_aggregates_community, community_aggregates_post_count, community_aggregates_comment_count, community_aggregates_subscriber_count;
+
index 129b58c001debe246298a86891fbb96570765bd2..2770d3d8d9e46277b0b4009a9e9694e3886803bb 100644 (file)
 -- Add community aggregates
-create table community_aggregates (
-  id serial primary key,
-  community_id int references community on update cascade on delete cascade not null,
-  subscribers bigint not null default 0,
-  posts bigint not null default 0,
-  comments bigint not null default 0,
-  published timestamp not null default now(),
-  unique (community_id)
+CREATE TABLE community_aggregates (
+    id serial PRIMARY KEY,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    subscribers bigint NOT NULL DEFAULT 0,
+    posts bigint NOT NULL DEFAULT 0,
+    comments bigint NOT NULL DEFAULT 0,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (community_id)
 );
 
-insert into community_aggregates (community_id, subscribers, posts, comments, published)
-  select 
+INSERT INTO community_aggregates (community_id, subscribers, posts, comments, published)
+SELECT
     c.id,
-    coalesce(cf.subs, 0) as subscribers,
-    coalesce(cd.posts, 0) as posts,
-    coalesce(cd.comments, 0) as comments,
+    coalesce(cf.subs, 0) AS subscribers,
+    coalesce(cd.posts, 0) AS posts,
+    coalesce(cd.comments, 0) AS comments,
     c.published
-  from community c
-  left join ( 
-    select 
-      p.community_id,
-      count(distinct p.id) as posts,
-      count(distinct ct.id) as comments
-    from post p
-    left join comment ct on p.id = ct.post_id
-    group by p.community_id
-  ) cd on cd.community_id = c.id
-  left join ( 
-    select 
-      community_follower.community_id,
-      count(*) as subs
-    from community_follower
-    group by community_follower.community_id
-  ) cf on cf.community_id = c.id;
+FROM
+    community c
+    LEFT JOIN (
+        SELECT
+            p.community_id,
+            count(DISTINCT p.id) AS posts,
+            count(DISTINCT ct.id) AS comments
+        FROM
+            post p
+            LEFT JOIN comment ct ON p.id = ct.post_id
+        GROUP BY
+            p.community_id) cd ON cd.community_id = c.id
+    LEFT JOIN (
+        SELECT
+            community_follower.community_id,
+            count(*) AS subs
+        FROM
+            community_follower
+        GROUP BY
+            community_follower.community_id) cf ON cf.community_id = c.id;
 
 -- Add community aggregate triggers
-
 -- initial community add
-create function community_aggregates_community()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into community_aggregates (community_id) values (NEW.id);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from community_aggregates where community_id = OLD.id;
-  END IF;
-  return null;
-end $$;
+CREATE FUNCTION community_aggregates_community ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO community_aggregates (community_id)
+            VALUES (NEW.id);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM community_aggregates
+        WHERE community_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create trigger community_aggregates_community
-after insert or delete on community
-for each row
-execute procedure community_aggregates_community();
--- post count
-create function community_aggregates_post_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update community_aggregates 
-    set posts = posts + 1 where community_id = NEW.community_id;
-  ELSIF (TG_OP = 'DELETE') THEN
-    update community_aggregates 
-    set posts = posts - 1 where community_id = OLD.community_id;
+CREATE TRIGGER community_aggregates_community
+    AFTER INSERT OR DELETE ON community
+    FOR EACH ROW
+    EXECUTE PROCEDURE community_aggregates_community ();
 
-    -- Update the counts if the post got deleted
-    update community_aggregates ca
-    set posts = coalesce(cd.posts, 0),
-    comments = coalesce(cd.comments, 0)
-    from ( 
-      select 
-      c.id,
-      count(distinct p.id) as posts,
-      count(distinct ct.id) as comments
-      from community c
-      left join post p on c.id = p.community_id
-      left join comment ct on p.id = ct.post_id
-      group by c.id
-    ) cd 
-    where ca.community_id = OLD.community_id;
-  END IF;
-  return null;
-end $$;
+-- post count
+CREATE FUNCTION community_aggregates_post_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            community_aggregates
+        SET
+            posts = posts + 1
+        WHERE
+            community_id = NEW.community_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            community_aggregates
+        SET
+            posts = posts - 1
+        WHERE
+            community_id = OLD.community_id;
+        -- Update the counts if the post got deleted
+        UPDATE
+            community_aggregates ca
+        SET
+            posts = coalesce(cd.posts, 0),
+            comments = coalesce(cd.comments, 0)
+        FROM (
+            SELECT
+                c.id,
+                count(DISTINCT p.id) AS posts,
+                count(DISTINCT ct.id) AS comments
+            FROM
+                community c
+            LEFT JOIN post p ON c.id = p.community_id
+            LEFT JOIN comment ct ON p.id = ct.post_id
+        GROUP BY
+            c.id) cd
+    WHERE
+        ca.community_id = OLD.community_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create trigger community_aggregates_post_count
-after insert or delete on post
-for each row
-execute procedure community_aggregates_post_count();
+CREATE TRIGGER community_aggregates_post_count
+    AFTER INSERT OR DELETE ON post
+    FOR EACH ROW
+    EXECUTE PROCEDURE community_aggregates_post_count ();
 
 -- comment count
-create function community_aggregates_comment_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update community_aggregates ca
-    set comments = comments + 1 from comment c, post p
-    where p.id = c.post_id 
-    and p.id = NEW.post_id 
-    and ca.community_id = p.community_id;
-  ELSIF (TG_OP = 'DELETE') THEN
-    update community_aggregates ca
-    set comments = comments - 1 from comment c, post p
-    where p.id = c.post_id 
-    and p.id = OLD.post_id 
-    and ca.community_id = p.community_id;
-
-  END IF;
-  return null;
-end $$;
+CREATE FUNCTION community_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            community_aggregates ca
+        SET
+            comments = comments + 1
+        FROM
+            comment c,
+            post p
+        WHERE
+            p.id = c.post_id
+            AND p.id = NEW.post_id
+            AND ca.community_id = p.community_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            community_aggregates ca
+        SET
+            comments = comments - 1
+        FROM
+            comment c,
+            post p
+        WHERE
+            p.id = c.post_id
+            AND p.id = OLD.post_id
+            AND ca.community_id = p.community_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create trigger community_aggregates_comment_count
-after insert or delete on comment
-for each row
-execute procedure community_aggregates_comment_count();
+CREATE TRIGGER community_aggregates_comment_count
+    AFTER INSERT OR DELETE ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE community_aggregates_comment_count ();
 
 -- subscriber count
-create function community_aggregates_subscriber_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update community_aggregates 
-    set subscribers = subscribers + 1 where community_id = NEW.community_id;
-  ELSIF (TG_OP = 'DELETE') THEN
-    update community_aggregates 
-    set subscribers = subscribers - 1 where community_id = OLD.community_id;
-  END IF;
-  return null;
-end $$;
+CREATE FUNCTION community_aggregates_subscriber_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            community_aggregates
+        SET
+            subscribers = subscribers + 1
+        WHERE
+            community_id = NEW.community_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            community_aggregates
+        SET
+            subscribers = subscribers - 1
+        WHERE
+            community_id = OLD.community_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create trigger community_aggregates_subscriber_count
-after insert or delete on community_follower
-for each row
-execute procedure community_aggregates_subscriber_count();
+CREATE TRIGGER community_aggregates_subscriber_count
+    AFTER INSERT OR DELETE ON community_follower
+    FOR EACH ROW
+    EXECUTE PROCEDURE community_aggregates_subscriber_count ();
 
index 7b4024cdac757ba002051dcb3286e5cdb0c716e2..38bfb998bd1436abd971ed8e165de4db316f9749 100644 (file)
@@ -1,11 +1,13 @@
 -- post aggregates
-drop table post_aggregates;
-drop trigger post_aggregates_post on post;
-drop trigger post_aggregates_comment_count on comment;
-drop trigger post_aggregates_score on post_like;
-drop trigger post_aggregates_stickied on post;
-drop function 
-  post_aggregates_post,
-  post_aggregates_comment_count,
-  post_aggregates_score,
-  post_aggregates_stickied;
+DROP TABLE post_aggregates;
+
+DROP TRIGGER post_aggregates_post ON post;
+
+DROP TRIGGER post_aggregates_comment_count ON comment;
+
+DROP TRIGGER post_aggregates_score ON post_like;
+
+DROP TRIGGER post_aggregates_stickied ON post;
+
+DROP FUNCTION post_aggregates_post, post_aggregates_comment_count, post_aggregates_score, post_aggregates_stickied;
+
index fcb3a9390593830f9addd4a70e8f079b47a3cb85..7ee817dc8d25392e75be1b68319cb22804fd5e88 100644 (file)
 -- Add post aggregates
-create table post_aggregates (
-  id serial primary key,
-  post_id int references post on update cascade on delete cascade not null,
-  comments bigint not null default 0,
-  score bigint not null default 0,
-  upvotes bigint not null default 0,
-  downvotes bigint not null default 0,
-  stickied boolean not null default false,
-  published timestamp not null default now(),
-  newest_comment_time timestamp not null default now(),
-  unique (post_id)
+CREATE TABLE post_aggregates (
+    id serial PRIMARY KEY,
+    post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    comments bigint NOT NULL DEFAULT 0,
+    score bigint NOT NULL DEFAULT 0,
+    upvotes bigint NOT NULL DEFAULT 0,
+    downvotes bigint NOT NULL DEFAULT 0,
+    stickied boolean NOT NULL DEFAULT FALSE,
+    published timestamp NOT NULL DEFAULT now(),
+    newest_comment_time timestamp NOT NULL DEFAULT now(),
+    UNIQUE (post_id)
 );
 
-insert into post_aggregates (post_id, comments, score, upvotes, downvotes, stickied, published, newest_comment_time)
-  select 
+INSERT INTO post_aggregates (post_id, comments, score, upvotes, downvotes, stickied, published, newest_comment_time)
+SELECT
     p.id,
-    coalesce(ct.comments, 0::bigint) as comments,
-    coalesce(pl.score, 0::bigint) as score,
-    coalesce(pl.upvotes, 0::bigint) as upvotes,
-    coalesce(pl.downvotes, 0::bigint) as downvotes,
+    coalesce(ct.comments, 0::bigint) AS comments,
+    coalesce(pl.score, 0::bigint) AS score,
+    coalesce(pl.upvotes, 0::bigint) AS upvotes,
+    coalesce(pl.downvotes, 0::bigint) AS downvotes,
     p.stickied,
     p.published,
-    greatest(ct.recent_comment_time, p.published) as newest_activity_time
-  from post p
-  left join ( 
-    select comment.post_id,
-    count(*) as comments,
-    max(comment.published) as recent_comment_time
-    from comment
-    group by comment.post_id
-  ) ct on ct.post_id = p.id
-  left join ( 
-    select post_like.post_id,
-    sum(post_like.score) as score,
-    sum(post_like.score) filter (where post_like.score = 1) as upvotes,
-    -sum(post_like.score) filter (where post_like.score = '-1'::integer) as downvotes
-    from post_like
-    group by post_like.post_id
-  ) pl on pl.post_id = p.id;
+    greatest (ct.recent_comment_time, p.published) AS newest_activity_time
+FROM
+    post p
+    LEFT JOIN (
+        SELECT
+            comment.post_id,
+            count(*) AS comments,
+            max(comment.published) AS recent_comment_time
+        FROM
+            comment
+        GROUP BY
+            comment.post_id) ct ON ct.post_id = p.id
+    LEFT JOIN (
+        SELECT
+            post_like.post_id,
+            sum(post_like.score) AS score,
+            sum(post_like.score) FILTER (WHERE post_like.score = 1) AS upvotes,
+            - sum(post_like.score) FILTER (WHERE post_like.score = '-1'::integer) AS downvotes
+        FROM
+            post_like
+        GROUP BY
+            post_like.post_id) pl ON pl.post_id = p.id;
 
 -- Add community aggregate triggers
-
 -- initial post add
-create function post_aggregates_post()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into post_aggregates (post_id) values (NEW.id);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from post_aggregates where post_id = OLD.id;
-  END IF;
-  return null;
-end $$;
+CREATE FUNCTION post_aggregates_post ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO post_aggregates (post_id)
+            VALUES (NEW.id);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM post_aggregates
+        WHERE post_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create trigger post_aggregates_post
-after insert or delete on post
-for each row
-execute procedure post_aggregates_post();
+CREATE TRIGGER post_aggregates_post
+    AFTER INSERT OR DELETE ON post
+    FOR EACH ROW
+    EXECUTE PROCEDURE post_aggregates_post ();
 
 -- comment count
-create function post_aggregates_comment_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update post_aggregates pa
-    set comments = comments + 1
-    where pa.post_id = NEW.post_id;
-
-    -- A 2 day necro-bump limit
-    update post_aggregates pa
-    set newest_comment_time = NEW.published
-    where pa.post_id = NEW.post_id
-    and published > ('now'::timestamp - '2 days'::interval);
-  ELSIF (TG_OP = 'DELETE') THEN
-    -- Join to post because that post may not exist anymore
-    update post_aggregates pa
-    set comments = comments - 1
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = OLD.post_id;
-  END IF;
-  return null;
-end $$;
+CREATE FUNCTION post_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments + 1
+        WHERE
+            pa.post_id = NEW.post_id;
+        -- A 2 day necro-bump limit
+        UPDATE
+            post_aggregates pa
+        SET
+            newest_comment_time = NEW.published
+        WHERE
+            pa.post_id = NEW.post_id
+            AND published > ('now'::timestamp - '2 days'::interval);
+    ELSIF (TG_OP = 'DELETE') THEN
+        -- Join to post because that post may not exist anymore
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments - 1
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = OLD.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create trigger post_aggregates_comment_count
-after insert or delete on comment
-for each row
-execute procedure post_aggregates_comment_count();
+CREATE TRIGGER post_aggregates_comment_count
+    AFTER INSERT OR DELETE ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE post_aggregates_comment_count ();
 
 -- post score
-create function post_aggregates_score()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update post_aggregates pa
-    set score = score + NEW.score,
-    upvotes = case when NEW.score = 1 then upvotes + 1 else upvotes end,
-    downvotes = case when NEW.score = -1 then downvotes + 1 else downvotes end
-    where pa.post_id = NEW.post_id;
-
-  ELSIF (TG_OP = 'DELETE') THEN
-    -- Join to post because that post may not exist anymore
-    update post_aggregates pa
-    set score = score - OLD.score,
-    upvotes = case when OLD.score = 1 then upvotes - 1 else upvotes end,
-    downvotes = case when OLD.score = -1 then downvotes - 1 else downvotes end
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = OLD.post_id;
-
-  END IF;
-  return null;
-end $$;
+CREATE FUNCTION post_aggregates_score ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            post_aggregates pa
+        SET
+            score = score + NEW.score,
+            upvotes = CASE WHEN NEW.score = 1 THEN
+                upvotes + 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN NEW.score = - 1 THEN
+                downvotes + 1
+            ELSE
+                downvotes
+            END
+        WHERE
+            pa.post_id = NEW.post_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        -- Join to post because that post may not exist anymore
+        UPDATE
+            post_aggregates pa
+        SET
+            score = score - OLD.score,
+            upvotes = CASE WHEN OLD.score = 1 THEN
+                upvotes - 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN OLD.score = - 1 THEN
+                downvotes - 1
+            ELSE
+                downvotes
+            END
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = OLD.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create trigger post_aggregates_score
-after insert or delete on post_like
-for each row
-execute procedure post_aggregates_score();
+CREATE TRIGGER post_aggregates_score
+    AFTER INSERT OR DELETE ON post_like
+    FOR EACH ROW
+    EXECUTE PROCEDURE post_aggregates_score ();
 
 -- post stickied
-create function post_aggregates_stickied()
-returns trigger language plpgsql
-as $$
-begin
-  update post_aggregates pa
-  set stickied = NEW.stickied
-  where pa.post_id = NEW.id;
+CREATE FUNCTION post_aggregates_stickied ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        post_aggregates pa
+    SET
+        stickied = NEW.stickied
+    WHERE
+        pa.post_id = NEW.id;
+    RETURN NULL;
+END
+$$;
 
-  return null;
-end $$;
+CREATE TRIGGER post_aggregates_stickied
+    AFTER UPDATE ON post
+    FOR EACH ROW
+    WHEN (OLD.stickied IS DISTINCT FROM NEW.stickied)
+    EXECUTE PROCEDURE post_aggregates_stickied ();
 
-create trigger post_aggregates_stickied
-after update on post
-for each row
-when (OLD.stickied is distinct from NEW.stickied)
-execute procedure post_aggregates_stickied();
index 6fd9ddc2562e9b30108e3439ca91bfc791cbab77..1b3809ba2fe8e501663cc4ce4731c41cf500761c 100644 (file)
@@ -1,7 +1,9 @@
 -- comment aggregates
-drop table comment_aggregates;
-drop trigger comment_aggregates_comment on comment;
-drop trigger comment_aggregates_score on comment_like;
-drop function 
-  comment_aggregates_comment,
-  comment_aggregates_score;
+DROP TABLE comment_aggregates;
+
+DROP TRIGGER comment_aggregates_comment ON comment;
+
+DROP TRIGGER comment_aggregates_score ON comment_like;
+
+DROP FUNCTION comment_aggregates_comment, comment_aggregates_score;
+
index f9cae6b37c67e6c10c6caa479d9c209d9d4f7405..25a9414f822b5374a631d2ac712aa86ec17b03fa 100644 (file)
 -- Add comment aggregates
-create table comment_aggregates (
-  id serial primary key,
-  comment_id int references comment on update cascade on delete cascade not null,
-  score bigint not null default 0,
-  upvotes bigint not null default 0,
-  downvotes bigint not null default 0,
-  published timestamp not null default now(),
-  unique (comment_id)
+CREATE TABLE comment_aggregates (
+    id serial PRIMARY KEY,
+    comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    score bigint NOT NULL DEFAULT 0,
+    upvotes bigint NOT NULL DEFAULT 0,
+    downvotes bigint NOT NULL DEFAULT 0,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (comment_id)
 );
 
-insert into comment_aggregates (comment_id, score, upvotes, downvotes, published)
-  select 
+INSERT INTO comment_aggregates (comment_id, score, upvotes, downvotes, published)
+SELECT
     c.id,
     COALESCE(cl.total, 0::bigint) AS score,
     COALESCE(cl.up, 0::bigint) AS upvotes,
     COALESCE(cl.down, 0::bigint) AS downvotes,
     c.published
-  from comment c
-  left join ( select l.comment_id as id,
-    sum(l.score) as total,
-    count(
-      case
-      when l.score = 1 then 1
-      else null::integer
-      end) as up,
-    count(
-      case
-      when l.score = '-1'::integer then 1
-      else null::integer
-      end) as down
-    from comment_like l
-    group by l.comment_id) cl on cl.id = c.id;
+FROM
+    comment c
+    LEFT JOIN (
+        SELECT
+            l.comment_id AS id,
+            sum(l.score) AS total,
+            count(
+                CASE WHEN l.score = 1 THEN
+                    1
+                ELSE
+                    NULL::integer
+                END) AS up,
+            count(
+                CASE WHEN l.score = '-1'::integer THEN
+                    1
+                ELSE
+                    NULL::integer
+                END) AS down
+        FROM
+            comment_like l
+        GROUP BY
+            l.comment_id) cl ON cl.id = c.id;
 
 -- Add comment aggregate triggers
-
 -- initial comment add
-create function comment_aggregates_comment()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into comment_aggregates (comment_id) values (NEW.id);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from comment_aggregates where comment_id = OLD.id;
-  END IF;
-  return null;
-end $$;
+CREATE FUNCTION comment_aggregates_comment ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO comment_aggregates (comment_id)
+            VALUES (NEW.id);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM comment_aggregates
+        WHERE comment_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create trigger comment_aggregates_comment
-after insert or delete on comment
-for each row
-execute procedure comment_aggregates_comment();
+CREATE TRIGGER comment_aggregates_comment
+    AFTER INSERT OR DELETE ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE comment_aggregates_comment ();
 
 -- comment score
-create function comment_aggregates_score()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update comment_aggregates ca
-    set score = score + NEW.score,
-    upvotes = case when NEW.score = 1 then upvotes + 1 else upvotes end,
-    downvotes = case when NEW.score = -1 then downvotes + 1 else downvotes end
-    where ca.comment_id = NEW.comment_id;
-
-  ELSIF (TG_OP = 'DELETE') THEN
-    -- Join to comment because that comment may not exist anymore
-    update comment_aggregates ca
-    set score = score - OLD.score,
-    upvotes = case when OLD.score = 1 then upvotes - 1 else upvotes end,
-    downvotes = case when OLD.score = -1 then downvotes - 1 else downvotes end
-    from comment c
-    where ca.comment_id = c.id
-    and ca.comment_id = OLD.comment_id;
+CREATE FUNCTION comment_aggregates_score ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            comment_aggregates ca
+        SET
+            score = score + NEW.score,
+            upvotes = CASE WHEN NEW.score = 1 THEN
+                upvotes + 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN NEW.score = - 1 THEN
+                downvotes + 1
+            ELSE
+                downvotes
+            END
+        WHERE
+            ca.comment_id = NEW.comment_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        -- Join to comment because that comment may not exist anymore
+        UPDATE
+            comment_aggregates ca
+        SET
+            score = score - OLD.score,
+            upvotes = CASE WHEN OLD.score = 1 THEN
+                upvotes - 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN OLD.score = - 1 THEN
+                downvotes - 1
+            ELSE
+                downvotes
+            END
+        FROM
+            comment c
+        WHERE
+            ca.comment_id = c.id
+            AND ca.comment_id = OLD.comment_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-  END IF;
-  return null;
-end $$;
+CREATE TRIGGER comment_aggregates_score
+    AFTER INSERT OR DELETE ON comment_like
+    FOR EACH ROW
+    EXECUTE PROCEDURE comment_aggregates_score ();
 
-create trigger comment_aggregates_score
-after insert or delete on comment_like
-for each row
-execute procedure comment_aggregates_score();
index 66ded96e5f34ab981f2b88906c8730ef475f7f85..ac917269442e639e69944d430e1996728275956d 100644 (file)
@@ -1 +1,2 @@
-drop view user_alias_1, user_alias_2, comment_alias_1;
+DROP VIEW user_alias_1, user_alias_2, comment_alias_1;
+
index 3d3b1b430548184b0061b728f0b678f0f2ab5569..8eb35fc93652a66fb2eb66976e0cdeb25fea0d2d 100644 (file)
@@ -1,7 +1,21 @@
--- Some view that act as aliases 
+-- Some view that act as aliases
 -- unfortunately necessary, since diesel doesn't have self joins
 -- or alias support yet
-create view user_alias_1 as select * from user_;
-create view user_alias_2 as select * from user_;
-create view comment_alias_1 as select * from comment;
+CREATE VIEW user_alias_1 AS
+SELECT
+    *
+FROM
+    user_;
+
+CREATE VIEW user_alias_2 AS
+SELECT
+    *
+FROM
+    user_;
+
+CREATE VIEW comment_alias_1 AS
+SELECT
+    *
+FROM
+    comment;
 
index 1c5d7767013e8710c206d85c867e141973c3b5b4..275f4b61037dd2ee08911d85ae571d75c93b1b40 100644 (file)
@@ -1,4 +1,8 @@
 -- There is no restore for this, it would require every view, table, index, etc.
 -- If you want to save past this point, you should make a DB backup.
+SELECT
+    *
+FROM
+    user_
+LIMIT 1;
 
-select * from user_ limit 1;
index cafa48ce8a4ed34ba14c640b67ce25f3497ff73b..e7927737d3285f17ff2c47ab4106229dad865b57 100644 (file)
@@ -1,64 +1,26 @@
 -- Drop triggers
-drop trigger if exists refresh_comment on comment;
-drop trigger if exists refresh_comment_like on comment_like;
-drop trigger if exists refresh_community on community;
-drop trigger if exists refresh_community_follower on community_follower;
-drop trigger if exists refresh_community_user_ban on community_user_ban;
-drop trigger if exists refresh_post on post;
-drop trigger if exists refresh_post_like on post_like;
-drop trigger if exists refresh_user on user_;
+DROP TRIGGER IF EXISTS refresh_comment ON comment;
+
+DROP TRIGGER IF EXISTS refresh_comment_like ON comment_like;
+
+DROP TRIGGER IF EXISTS refresh_community ON community;
+
+DROP TRIGGER IF EXISTS refresh_community_follower ON community_follower;
+
+DROP TRIGGER IF EXISTS refresh_community_user_ban ON community_user_ban;
+
+DROP TRIGGER IF EXISTS refresh_post ON post;
+
+DROP TRIGGER IF EXISTS refresh_post_like ON post_like;
+
+DROP TRIGGER IF EXISTS refresh_user ON user_;
 
 -- Drop functions
-drop function if exists
-refresh_comment,
-refresh_comment_like,
-refresh_community,
-refresh_community_follower,
-refresh_community_user_ban,
-refresh_post,
-refresh_post_like,
-refresh_private_message,
-refresh_user
-cascade;
+DROP FUNCTION IF EXISTS refresh_comment, refresh_comment_like, refresh_community, refresh_community_follower, refresh_community_user_ban, refresh_post, refresh_post_like, refresh_private_message, refresh_user CASCADE;
 
 -- Drop views
-drop view if exists
-comment_aggregates_view, 
-comment_fast_view,
-comment_report_view,
-comment_view,
-community_aggregates_view,
-community_fast_view,
-community_follower_view,
-community_moderator_view,
-community_user_ban_view,
-community_view,
-mod_add_community_view,
-mod_add_view,
-mod_ban_from_community_view,
-mod_ban_view,
-mod_lock_post_view,
-mod_remove_comment_view,
-mod_remove_community_view,
-mod_remove_post_view,
-mod_sticky_post_view,
-post_aggregates_view,
-post_fast_view,
-post_report_view,
-post_view,
-private_message_view,
-reply_fast_view,
-site_view,
-user_mention_fast_view,
-user_mention_view,
-user_view
-cascade;
+DROP VIEW IF EXISTS comment_aggregates_view, comment_fast_view, comment_report_view, comment_view, community_aggregates_view, community_fast_view, community_follower_view, community_moderator_view, community_user_ban_view, community_view, mod_add_community_view, mod_add_view, mod_ban_from_community_view, mod_ban_view, mod_lock_post_view, mod_remove_comment_view, mod_remove_community_view, mod_remove_post_view, mod_sticky_post_view, post_aggregates_view, post_fast_view, post_report_view, post_view, private_message_view, reply_fast_view, site_view, user_mention_fast_view, user_mention_view, user_view CASCADE;
 
 -- Drop fast tables
-drop table if exists
-comment_aggregates_fast,
-community_aggregates_fast,
-post_aggregates_fast,
-user_fast
-cascade;
+DROP TABLE IF EXISTS comment_aggregates_fast, community_aggregates_fast, post_aggregates_fast, user_fast CASCADE;
 
index 55e833323f088a4cd4e9f54c8967b1acf0143ea7..85267bf73ce7a0f8a1b9a0ea7644e20a34ba05f6 100644 (file)
@@ -1,28 +1,13 @@
 -- Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
-create or replace function hot_rank(
-  score numeric,
-  published timestamp without time zone)
-returns integer as $$
-begin
-  -- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
-  return floor(10000*log(greatest(1,score+3)) / power(((EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600) + 2), 1.8))::integer;
-end; $$
+CREATE OR REPLACE FUNCTION hot_rank (score numeric, published timestamp without time zone)
+    RETURNS integer
+    AS $$
+BEGIN
+    -- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
+    RETURN floor(10000 * log(greatest (1, score + 3)) / power(((EXTRACT(EPOCH FROM (timezone('utc', now()) - published)) / 3600) + 2), 1.8))::integer;
+END;
+$$
 LANGUAGE plpgsql;
 
-drop index 
-  idx_post_aggregates_hot,
-  idx_post_aggregates_stickied_hot,
-  idx_post_aggregates_active,
-  idx_post_aggregates_stickied_active,
-  idx_post_aggregates_score,
-  idx_post_aggregates_stickied_score,
-  idx_post_aggregates_published,
-  idx_post_aggregates_stickied_published,
-  idx_comment_published,
-  idx_comment_aggregates_hot,
-  idx_comment_aggregates_score,
-  idx_user_published,
-  idx_user_aggregates_comment_score,
-  idx_community_published,
-  idx_community_aggregates_hot,
-  idx_community_aggregates_subscribers;
+DROP INDEX idx_post_aggregates_hot, idx_post_aggregates_stickied_hot, idx_post_aggregates_active, idx_post_aggregates_stickied_active, idx_post_aggregates_score, idx_post_aggregates_stickied_score, idx_post_aggregates_published, idx_post_aggregates_stickied_published, idx_comment_published, idx_comment_aggregates_hot, idx_comment_aggregates_score, idx_user_published, idx_user_aggregates_comment_score, idx_community_published, idx_community_aggregates_hot, idx_community_aggregates_subscribers;
+
index f4d4147106429b4e938f500abc7951182daa9a2e..88eb0f4be52776e23e4a6aaf69d1c1ae3adb63a7 100644 (file)
@@ -1,49 +1,52 @@
 -- Need to add immutable to the hot_rank function in order to index by it
-
 -- Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
-create or replace function hot_rank(
-  score numeric,
-  published timestamp without time zone)
-returns integer as $$
-begin
-  -- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
-  return floor(10000*log(greatest(1,score+3)) / power(((EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600) + 2), 1.8))::integer;
-end; $$
+CREATE OR REPLACE FUNCTION hot_rank (score numeric, published timestamp without time zone)
+    RETURNS integer
+    AS $$
+BEGIN
+    -- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
+    RETURN floor(10000 * log(greatest (1, score + 3)) / power(((EXTRACT(EPOCH FROM (timezone('utc', now()) - published)) / 3600) + 2), 1.8))::integer;
+END;
+$$
 LANGUAGE plpgsql
 IMMUTABLE;
 
 -- Post_aggregates
-create index idx_post_aggregates_stickied_hot on post_aggregates (stickied desc, hot_rank(score, published) desc, published desc);
-create index idx_post_aggregates_hot on post_aggregates (hot_rank(score, published) desc, published desc);
+CREATE INDEX idx_post_aggregates_stickied_hot ON post_aggregates (stickied DESC, hot_rank (score, published) DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_hot ON post_aggregates (hot_rank (score, published) DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_stickied_active ON post_aggregates (stickied DESC, hot_rank (score, newest_comment_time) DESC, newest_comment_time DESC);
+
+CREATE INDEX idx_post_aggregates_active ON post_aggregates (hot_rank (score, newest_comment_time) DESC, newest_comment_time DESC);
 
-create index idx_post_aggregates_stickied_active on post_aggregates (stickied desc, hot_rank(score, newest_comment_time) desc, newest_comment_time desc);
-create index idx_post_aggregates_active on post_aggregates (hot_rank(score, newest_comment_time) desc, newest_comment_time desc);
+CREATE INDEX idx_post_aggregates_stickied_score ON post_aggregates (stickied DESC, score DESC);
 
-create index idx_post_aggregates_stickied_score on post_aggregates (stickied desc, score desc);
-create index idx_post_aggregates_score on post_aggregates (score desc);
+CREATE INDEX idx_post_aggregates_score ON post_aggregates (score DESC);
 
-create index idx_post_aggregates_stickied_published on post_aggregates (stickied desc, published desc);
-create index idx_post_aggregates_published on post_aggregates (published desc);
+CREATE INDEX idx_post_aggregates_stickied_published ON post_aggregates (stickied DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_published ON post_aggregates (published DESC);
 
 -- Comment
-create index idx_comment_published on comment (published desc);
+CREATE INDEX idx_comment_published ON comment (published DESC);
 
 -- Comment_aggregates
-create index idx_comment_aggregates_hot on comment_aggregates (hot_rank(score, published) desc, published desc);
-create index idx_comment_aggregates_score on comment_aggregates (score desc);
+CREATE INDEX idx_comment_aggregates_hot ON comment_aggregates (hot_rank (score, published) DESC, published DESC);
+
+CREATE INDEX idx_comment_aggregates_score ON comment_aggregates (score DESC);
 
 -- User
-create index idx_user_published on user_ (published desc);
+CREATE INDEX idx_user_published ON user_ (published DESC);
 
 -- User_aggregates
-create index idx_user_aggregates_comment_score on user_aggregates (comment_score desc);
+CREATE INDEX idx_user_aggregates_comment_score ON user_aggregates (comment_score DESC);
 
 -- Community
-create index idx_community_published on community (published desc);
+CREATE INDEX idx_community_published ON community (published DESC);
 
 -- Community_aggregates
-create index idx_community_aggregates_hot on community_aggregates (hot_rank(subscribers, published) desc, published desc);
-create index idx_community_aggregates_subscribers on community_aggregates (subscribers desc);
-
+CREATE INDEX idx_community_aggregates_hot ON community_aggregates (hot_rank (subscribers, published) DESC, published DESC);
 
+CREATE INDEX idx_community_aggregates_subscribers ON community_aggregates (subscribers DESC);
 
index fb612743ea26c3400bc439806fdba814800ac507..80092cab95274aebcadebd79a4d6d951fab6a350 100644 (file)
@@ -1,6 +1,10 @@
-create or replace function generate_unique_changeme() 
-returns text language sql 
-as $$
-  select 'changeme_' || string_agg (substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil (random() * 62)::integer, 1), '')
-  from generate_series(1, 20)
-$$;
\ No newline at end of file
+CREATE OR REPLACE FUNCTION generate_unique_changeme ()
+    RETURNS text
+    LANGUAGE sql
+    AS $$
+    SELECT
+        'changeme_' || string_agg(substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil(random() * 62)::integer, 1), '')
+    FROM
+        generate_series(1, 20)
+$$;
+
index 4370c4a520fb8ceee76590547e41f047a00333a8..00aa1c719d0b71e23431425f6dd15bd8b7ac8579 100644 (file)
@@ -1,6 +1,10 @@
-create or replace function generate_unique_changeme() 
-returns text language sql 
-as $$
-  select 'http://changeme_' || string_agg (substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil (random() * 62)::integer, 1), '')
-  from generate_series(1, 20)
-$$;
\ No newline at end of file
+CREATE OR REPLACE FUNCTION generate_unique_changeme ()
+    RETURNS text
+    LANGUAGE sql
+    AS $$
+    SELECT
+        'http://changeme_' || string_agg(substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil(random() * 62)::integer, 1), '')
+    FROM
+        generate_series(1, 20)
+$$;
+
index bc3d46a548e74fa813a793b59fd4ff1c170bd891..e5d636892a670397b6b114c39c4514c2b5bc2705 100644 (file)
@@ -1,14 +1,16 @@
-alter table site_aggregates 
-  drop column users_active_day,
-  drop column users_active_week,
-  drop column users_active_month,
-  drop column users_active_half_year;
-
-alter table community_aggregates 
-  drop column users_active_day,
-  drop column users_active_week,
-  drop column users_active_month,
-  drop column users_active_half_year;
-
-drop function site_aggregates_activity(i text);
-drop function community_aggregates_activity(i text);
+ALTER TABLE site_aggregates
+    DROP COLUMN users_active_day,
+    DROP COLUMN users_active_week,
+    DROP COLUMN users_active_month,
+    DROP COLUMN users_active_half_year;
+
+ALTER TABLE community_aggregates
+    DROP COLUMN users_active_day,
+    DROP COLUMN users_active_week,
+    DROP COLUMN users_active_month,
+    DROP COLUMN users_active_half_year;
+
+DROP FUNCTION site_aggregates_activity (i text);
+
+DROP FUNCTION community_aggregates_activity (i text);
+
index 9248ae8687eb08f8f15b5a47bf4ccf245fced798..63ca51d1e4f970e525716c246758a8ee16bba67d 100644 (file)
 -- Add monthly and half yearly active columns for site and community aggregates
-
 -- These columns don't need to be updated with a trigger, so they're saved daily via queries
-alter table site_aggregates add column users_active_day bigint not null default 0;
-alter table site_aggregates add column users_active_week bigint not null default 0;
-alter table site_aggregates add column users_active_month bigint not null default 0;
-alter table site_aggregates add column users_active_half_year bigint not null default 0;
-
-alter table community_aggregates add column users_active_day bigint not null default 0;
-alter table community_aggregates add column users_active_week bigint not null default 0;
-alter table community_aggregates add column users_active_month bigint not null default 0;
-alter table community_aggregates add column users_active_half_year bigint not null default 0;
-
-create or replace function site_aggregates_activity(i text)
-returns int
-language plpgsql
-as
-$$
-declare
-   count_ integer;
-begin
-  select count(*) 
-  into count_
-  from (
-    select c.creator_id from comment c
-    inner join user_ u on c.creator_id = u.id
-    where c.published > ('now'::timestamp - i::interval) 
-    and u.local = true
-    union
-    select p.creator_id from post p
-    inner join user_ u on p.creator_id = u.id
-    where p.published > ('now'::timestamp - i::interval)
-    and u.local = true
-  ) a;
-  return count_;
-end;
+ALTER TABLE site_aggregates
+    ADD COLUMN users_active_day bigint NOT NULL DEFAULT 0;
+
+ALTER TABLE site_aggregates
+    ADD COLUMN users_active_week bigint NOT NULL DEFAULT 0;
+
+ALTER TABLE site_aggregates
+    ADD COLUMN users_active_month bigint NOT NULL DEFAULT 0;
+
+ALTER TABLE site_aggregates
+    ADD COLUMN users_active_half_year bigint NOT NULL DEFAULT 0;
+
+ALTER TABLE community_aggregates
+    ADD COLUMN users_active_day bigint NOT NULL DEFAULT 0;
+
+ALTER TABLE community_aggregates
+    ADD COLUMN users_active_week bigint NOT NULL DEFAULT 0;
+
+ALTER TABLE community_aggregates
+    ADD COLUMN users_active_month bigint NOT NULL DEFAULT 0;
+
+ALTER TABLE community_aggregates
+    ADD COLUMN users_active_half_year bigint NOT NULL DEFAULT 0;
+
+CREATE OR REPLACE FUNCTION site_aggregates_activity (i text)
+    RETURNS int
+    LANGUAGE plpgsql
+    AS $$
+DECLARE
+    count_ integer;
+BEGIN
+    SELECT
+        count(*) INTO count_
+    FROM (
+        SELECT
+            c.creator_id
+        FROM
+            comment c
+            INNER JOIN user_ u ON c.creator_id = u.id
+        WHERE
+            c.published > ('now'::timestamp - i::interval)
+            AND u.local = TRUE
+        UNION
+        SELECT
+            p.creator_id
+        FROM
+            post p
+            INNER JOIN user_ u ON p.creator_id = u.id
+        WHERE
+            p.published > ('now'::timestamp - i::interval)
+            AND u.local = TRUE) a;
+    RETURN count_;
+END;
 $$;
 
-update site_aggregates 
-set users_active_day = (select * from site_aggregates_activity('1 day'));
-
-update site_aggregates 
-set users_active_week = (select * from site_aggregates_activity('1 week'));
-
-update site_aggregates 
-set users_active_month = (select * from site_aggregates_activity('1 month'));
-
-update site_aggregates 
-set users_active_half_year = (select * from site_aggregates_activity('6 months'));
-
-create or replace function community_aggregates_activity(i text)
-returns table(count_ bigint, community_id_ integer)
-language plpgsql
-as
-$$
-begin
-  return query 
-  select count(*), community_id
-  from (
-    select c.creator_id, p.community_id from comment c
-    inner join post p on c.post_id = p.id
-    where c.published > ('now'::timestamp - i::interval)
-    union
-    select p.creator_id, p.community_id from post p
-    where p.published > ('now'::timestamp - i::interval)  
-  ) a
-  group by community_id;
-end;
+UPDATE
+    site_aggregates
+SET
+    users_active_day = (
+        SELECT
+            *
+        FROM
+            site_aggregates_activity ('1 day'));
+
+UPDATE
+    site_aggregates
+SET
+    users_active_week = (
+        SELECT
+            *
+        FROM
+            site_aggregates_activity ('1 week'));
+
+UPDATE
+    site_aggregates
+SET
+    users_active_month = (
+        SELECT
+            *
+        FROM
+            site_aggregates_activity ('1 month'));
+
+UPDATE
+    site_aggregates
+SET
+    users_active_half_year = (
+        SELECT
+            *
+        FROM
+            site_aggregates_activity ('6 months'));
+
+CREATE OR REPLACE FUNCTION community_aggregates_activity (i text)
+    RETURNS TABLE (
+        count_ bigint,
+        community_id_ integer)
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    RETURN query
+    SELECT
+        count(*),
+        community_id
+    FROM (
+        SELECT
+            c.creator_id,
+            p.community_id
+        FROM
+            comment c
+            INNER JOIN post p ON c.post_id = p.id
+        WHERE
+            c.published > ('now'::timestamp - i::interval)
+        UNION
+        SELECT
+            p.creator_id,
+            p.community_id
+        FROM
+            post p
+        WHERE
+            p.published > ('now'::timestamp - i::interval)) a
+GROUP BY
+    community_id;
+END;
 $$;
 
-update community_aggregates ca
-set users_active_day = mv.count_
-from community_aggregates_activity('1 day') mv
-where ca.community_id = mv.community_id_;
-
-update community_aggregates ca
-set users_active_week = mv.count_
-from community_aggregates_activity('1 week') mv
-where ca.community_id = mv.community_id_;
-
-update community_aggregates ca
-set users_active_month = mv.count_
-from community_aggregates_activity('1 month') mv
-where ca.community_id = mv.community_id_;
-
-update community_aggregates ca
-set users_active_half_year = mv.count_
-from community_aggregates_activity('6 months') mv
-where ca.community_id = mv.community_id_;
+UPDATE
+    community_aggregates ca
+SET
+    users_active_day = mv.count_
+FROM
+    community_aggregates_activity ('1 day') mv
+WHERE
+    ca.community_id = mv.community_id_;
+
+UPDATE
+    community_aggregates ca
+SET
+    users_active_week = mv.count_
+FROM
+    community_aggregates_activity ('1 week') mv
+WHERE
+    ca.community_id = mv.community_id_;
+
+UPDATE
+    community_aggregates ca
+SET
+    users_active_month = mv.count_
+FROM
+    community_aggregates_activity ('1 month') mv
+WHERE
+    ca.community_id = mv.community_id_;
+
+UPDATE
+    community_aggregates ca
+SET
+    users_active_half_year = mv.count_
+FROM
+    community_aggregates_activity ('6 months') mv
+WHERE
+    ca.community_id = mv.community_id_;
+
index 9de29698391cd3770b4386b0be72ffc1df1a9c70..a81fc6c0aeb848124c0fd5c358d19d30509f7c90 100644 (file)
@@ -1 +1,2 @@
-drop index idx_post_aggregates_comments;
+DROP INDEX idx_post_aggregates_comments;
+
index 610873888d5774e5befedaf8fb08d7a4f324772e..18013f48aeec4d9aa786cb8374207f3ef38346b2 100644 (file)
@@ -1 +1,2 @@
-create index idx_post_aggregates_comments on post_aggregates (comments desc);
+CREATE INDEX idx_post_aggregates_comments ON post_aggregates (comments DESC);
+
index 248eb9b80023906515530f7b8fd9bb95e7389aba..a5ff8edcf58e51fd74d996ba2acca4dad732034b 100644 (file)
@@ -1,6 +1,15 @@
-ALTER TABLE community DROP COLUMN followers_url;
-ALTER TABLE community DROP COLUMN inbox_url;
-ALTER TABLE community DROP COLUMN shared_inbox_url;
+ALTER TABLE community
+    DROP COLUMN followers_url;
+
+ALTER TABLE community
+    DROP COLUMN inbox_url;
+
+ALTER TABLE community
+    DROP COLUMN shared_inbox_url;
+
+ALTER TABLE user_
+    DROP COLUMN inbox_url;
+
+ALTER TABLE user_
+    DROP COLUMN shared_inbox_url;
 
-ALTER TABLE user_ DROP COLUMN inbox_url;
-ALTER TABLE user_ DROP COLUMN shared_inbox_url;
\ No newline at end of file
index 48f3b20e5b7777ab7f6c60acaf542c75308a4c2e..69e99cb2edcc44fc6ac56c7e285bd42921911f18 100644 (file)
@@ -1,10 +1,24 @@
-ALTER TABLE community ADD COLUMN followers_url varchar(255) NOT NULL DEFAULT generate_unique_changeme();
-ALTER TABLE community ADD COLUMN inbox_url varchar(255) NOT NULL DEFAULT generate_unique_changeme();
-ALTER TABLE community ADD COLUMN shared_inbox_url varchar(255);
+ALTER TABLE community
+    ADD COLUMN followers_url varchar(255) NOT NULL DEFAULT generate_unique_changeme ();
 
-ALTER TABLE user_ ADD COLUMN inbox_url varchar(255) NOT NULL DEFAULT generate_unique_changeme();
-ALTER TABLE user_ ADD COLUMN shared_inbox_url varchar(255);
+ALTER TABLE community
+    ADD COLUMN inbox_url varchar(255) NOT NULL DEFAULT generate_unique_changeme ();
+
+ALTER TABLE community
+    ADD COLUMN shared_inbox_url varchar(255);
+
+ALTER TABLE user_
+    ADD COLUMN inbox_url varchar(255) NOT NULL DEFAULT generate_unique_changeme ();
+
+ALTER TABLE user_
+    ADD COLUMN shared_inbox_url varchar(255);
+
+ALTER TABLE community
+    ADD CONSTRAINT idx_community_followers_url UNIQUE (followers_url);
+
+ALTER TABLE community
+    ADD CONSTRAINT idx_community_inbox_url UNIQUE (inbox_url);
+
+ALTER TABLE user_
+    ADD CONSTRAINT idx_user_inbox_url UNIQUE (inbox_url);
 
-ALTER TABLE community ADD CONSTRAINT idx_community_followers_url UNIQUE (followers_url);
-ALTER TABLE community ADD CONSTRAINT idx_community_inbox_url UNIQUE (inbox_url);
-ALTER TABLE user_ ADD CONSTRAINT idx_user_inbox_url UNIQUE (inbox_url);
index 75f9aea3aca5be5a351df18450b1462ce528679a..acfd6d12b1fb655ef3470c457c95d864087b4702 100644 (file)
@@ -1,33 +1,43 @@
-drop index idx_post_aggregates_newest_comment_time,
-idx_post_aggregates_stickied_newest_comment_time,
-idx_post_aggregates_stickied_comments;
+DROP INDEX idx_post_aggregates_newest_comment_time, idx_post_aggregates_stickied_newest_comment_time, idx_post_aggregates_stickied_comments;
 
-alter table post_aggregates drop column newest_comment_time;
+ALTER TABLE post_aggregates
+    DROP COLUMN newest_comment_time;
 
-alter table post_aggregates rename column newest_comment_time_necro to newest_comment_time;
+ALTER TABLE post_aggregates RENAME COLUMN newest_comment_time_necro TO newest_comment_time;
 
-create or replace function post_aggregates_comment_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update post_aggregates pa
-    set comments = comments + 1
-    where pa.post_id = NEW.post_id;
-
-    -- A 2 day necro-bump limit
-    update post_aggregates pa
-    set newest_comment_time = NEW.published
-    where pa.post_id = NEW.post_id
-    and published > ('now'::timestamp - '2 days'::interval);
-  ELSIF (TG_OP = 'DELETE') THEN
-    -- Join to post because that post may not exist anymore
-    update post_aggregates pa
-    set comments = comments - 1
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = OLD.post_id;
-  END IF;
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION post_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments + 1
+        WHERE
+            pa.post_id = NEW.post_id;
+        -- A 2 day necro-bump limit
+        UPDATE
+            post_aggregates pa
+        SET
+            newest_comment_time = NEW.published
+        WHERE
+            pa.post_id = NEW.post_id
+            AND published > ('now'::timestamp - '2 days'::interval);
+    ELSIF (TG_OP = 'DELETE') THEN
+        -- Join to post because that post may not exist anymore
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments - 1
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = OLD.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
index 5452fbae451d191d63ea0c716a186bddcba7ba6b..70d6f30a0b1a19c2a3ef73f32bffe791c8dad591 100644 (file)
@@ -1,43 +1,60 @@
 -- First rename current newest comment time to newest_comment_time_necro
 -- necro means that time is limited to 2 days, whereas newest_comment_time ignores that.
-alter table post_aggregates rename column newest_comment_time to newest_comment_time_necro;
+ALTER TABLE post_aggregates RENAME COLUMN newest_comment_time TO newest_comment_time_necro;
 
 -- Add the newest_comment_time column
-alter table post_aggregates add column newest_comment_time timestamp not null default now();
+ALTER TABLE post_aggregates
+    ADD COLUMN newest_comment_time timestamp NOT NULL DEFAULT now();
 
 -- Set the current newest_comment_time based on the old ones
-update post_aggregates set newest_comment_time = newest_comment_time_necro;
+UPDATE
+    post_aggregates
+SET
+    newest_comment_time = newest_comment_time_necro;
 
 -- Add the indexes for this new column
-create index idx_post_aggregates_newest_comment_time on post_aggregates (newest_comment_time desc);
-create index idx_post_aggregates_stickied_newest_comment_time on post_aggregates (stickied desc, newest_comment_time desc);
+CREATE INDEX idx_post_aggregates_newest_comment_time ON post_aggregates (newest_comment_time DESC);
+
+CREATE INDEX idx_post_aggregates_stickied_newest_comment_time ON post_aggregates (stickied DESC, newest_comment_time DESC);
 
 -- Forgot to add index w/ stickied first for most comments:
-create index idx_post_aggregates_stickied_comments on post_aggregates (stickied desc, comments desc);
+CREATE INDEX idx_post_aggregates_stickied_comments ON post_aggregates (stickied DESC, comments DESC);
 
 -- Alter the comment trigger to set the newest_comment_time, and newest_comment_time_necro
-create or replace function post_aggregates_comment_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update post_aggregates pa
-    set comments = comments + 1,
-    newest_comment_time = NEW.published
-    where pa.post_id = NEW.post_id;
+CREATE OR REPLACE FUNCTION post_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments + 1,
+            newest_comment_time = NEW.published
+        WHERE
+            pa.post_id = NEW.post_id;
+        -- A 2 day necro-bump limit
+        UPDATE
+            post_aggregates pa
+        SET
+            newest_comment_time_necro = NEW.published
+        WHERE
+            pa.post_id = NEW.post_id
+            AND published > ('now'::timestamp - '2 days'::interval);
+    ELSIF (TG_OP = 'DELETE') THEN
+        -- Join to post because that post may not exist anymore
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments - 1
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = OLD.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-    -- A 2 day necro-bump limit
-    update post_aggregates pa
-    set newest_comment_time_necro = NEW.published
-    where pa.post_id = NEW.post_id
-    and published > ('now'::timestamp - '2 days'::interval);
-  ELSIF (TG_OP = 'DELETE') THEN
-    -- Join to post because that post may not exist anymore
-    update post_aggregates pa
-    set comments = comments - 1
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = OLD.post_id;
-  END IF;
-  return null;
-end $$;
index 227c3ec220d57912b2aad9b1ca606dc8ee1f0b17..94e981dd0a2954b6f2b61ee74fbda824c6441b77 100644 (file)
@@ -1,35 +1,48 @@
-create or replace function comment_aggregates_comment()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into comment_aggregates (comment_id) values (NEW.id);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from comment_aggregates where comment_id = OLD.id;
-  END IF;
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION comment_aggregates_comment ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO comment_aggregates (comment_id)
+            VALUES (NEW.id);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM comment_aggregates
+        WHERE comment_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create or replace function post_aggregates_post()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into post_aggregates (post_id) values (NEW.id);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from post_aggregates where post_id = OLD.id;
-  END IF;
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION post_aggregates_post ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO post_aggregates (post_id)
+            VALUES (NEW.id);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM post_aggregates
+        WHERE post_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION community_aggregates_community ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO community_aggregates (community_id)
+            VALUES (NEW.id);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM community_aggregates
+        WHERE community_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create or replace function community_aggregates_community()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into community_aggregates (community_id) values (NEW.id);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from community_aggregates where community_id = OLD.id;
-  END IF;
-  return null;
-end $$;
index c195daad4d5a67c4a0970d336d5e629468970297..9a942aa46491a8347e5145b207aa276180f53895 100644 (file)
@@ -1,39 +1,51 @@
--- The published and updated columns on the aggregates tables are using now(), 
+-- The published and updated columns on the aggregates tables are using now(),
 -- when they should use the correct published or updated columns
 -- This is mainly a problem with federated posts being fetched
+CREATE OR REPLACE FUNCTION comment_aggregates_comment ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO comment_aggregates (comment_id, published)
+            VALUES (NEW.id, NEW.published);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM comment_aggregates
+        WHERE comment_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create or replace function comment_aggregates_comment()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into comment_aggregates (comment_id, published) values (NEW.id, NEW.published);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from comment_aggregates where comment_id = OLD.id;
-  END IF;
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION post_aggregates_post ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO post_aggregates (post_id, published, newest_comment_time, newest_comment_time_necro)
+            VALUES (NEW.id, NEW.published, NEW.published, NEW.published);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM post_aggregates
+        WHERE post_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create or replace function post_aggregates_post()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into post_aggregates (post_id, published, newest_comment_time, newest_comment_time_necro) values (NEW.id, NEW.published, NEW.published, NEW.published);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from post_aggregates where post_id = OLD.id;
-  END IF;
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION community_aggregates_community ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO community_aggregates (community_id, published)
+            VALUES (NEW.id, NEW.published);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM community_aggregates
+        WHERE community_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create or replace function community_aggregates_community()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into community_aggregates (community_id, published) values (NEW.id, NEW.published);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from community_aggregates where community_id = OLD.id;
-  END IF;
-  return null;
-end $$;
index 35386f795579af845fca9c52394cfda09a35096d..61d8c8d57475cd26635550ee3ce631c9c602b788 100644 (file)
@@ -1,34 +1,36 @@
-create table category (
-  id serial primary key,
-  name varchar(100) not null unique
+CREATE TABLE category (
+    id serial PRIMARY KEY,
+    name varchar(100) NOT NULL UNIQUE
 );
 
-insert into category (name) values
-('Discussion'),
-('Humor/Memes'),
-('Gaming'),
-('Movies'),
-('TV'),
-('Music'),
-('Literature'),
-('Comics'),
-('Photography'),
-('Art'),
-('Learning'),
-('DIY'),
-('Lifestyle'),
-('News'),
-('Politics'),
-('Society'),
-('Gender/Identity/Sexuality'),
-('Race/Colonisation'),
-('Religion'),
-('Science/Technology'),
-('Programming/Software'),
-('Health/Sports/Fitness'),
-('Porn'),
-('Places'),
-('Meta'),
-('Other');
+INSERT INTO category (name)
+    VALUES ('Discussion'),
+    ('Humor/Memes'),
+    ('Gaming'),
+    ('Movies'),
+    ('TV'),
+    ('Music'),
+    ('Literature'),
+    ('Comics'),
+    ('Photography'),
+    ('Art'),
+    ('Learning'),
+    ('DIY'),
+    ('Lifestyle'),
+    ('News'),
+    ('Politics'),
+    ('Society'),
+    ('Gender/Identity/Sexuality'),
+    ('Race/Colonisation'),
+    ('Religion'),
+    ('Science/Technology'),
+    ('Programming/Software'),
+    ('Health/Sports/Fitness'),
+    ('Porn'),
+    ('Places'),
+    ('Meta'),
+    ('Other');
+
+ALTER TABLE community
+    ADD category_id int REFERENCES category ON UPDATE CASCADE ON DELETE CASCADE NOT NULL DEFAULT 1;
 
-ALTER TABLE community ADD category_id int references category on update cascade on delete cascade not null default 1;
\ No newline at end of file
index ebe83e541fa3c04f325f065505b6d76dc26789e9..6b39bfe00cc9dffe2499d5d97935ad35288ad343 100644 (file)
@@ -1,2 +1,5 @@
-ALTER TABLE community DROP COLUMN category_id;
+ALTER TABLE community
+    DROP COLUMN category_id;
+
 DROP TABLE category;
+
index 7195601c9e072d8dffef4590adb4559464a550dc..0a9e8e2db65713c2d5f5065bb7e7199d8ebcb196 100644 (file)
@@ -1,4 +1,5 @@
 -- This is a clean-up migration that cannot be undone,
 -- but Diesel requires a non-empty script so run a no-op.
-SELECT 1;
+SELECT
+    1;
 
index 24e2d16b878295ef05e8b4136c8a7a1e70a61f5c..5e09164518500d737feacc383a916858021d844c 100644 (file)
@@ -1 +1,7 @@
-UPDATE post SET url = NULL where url = '';
+UPDATE
+    post
+SET
+    url = NULL
+WHERE
+    url = '';
+
index f866b155c5c41972f1ff7701908da199565e07ad..0a9e8e2db65713c2d5f5065bb7e7199d8ebcb196 100644 (file)
@@ -1,3 +1,5 @@
 -- This is a clean-up migration that cannot be undone,
 -- but Diesel requires a non-empty script so run a no-op.
-SELECT 1;
+SELECT
+    1;
+
index 8b22b33fca35aae22b678cbcc5a670d296873245..9fe28e8d4ee8fcbad67864d5460656d43d7f107d 100644 (file)
@@ -1,5 +1,29 @@
 -- If these are not urls, it will crash the server
-update user_ set avatar = NULL where avatar not like 'http%';
-update user_ set banner = NULL where banner not like 'http%';
-update community set icon = NULL where icon not like 'http%';
-update community set banner = NULL where banner not like 'http%';
+UPDATE
+    user_
+SET
+    avatar = NULL
+WHERE
+    avatar NOT LIKE 'http%';
+
+UPDATE
+    user_
+SET
+    banner = NULL
+WHERE
+    banner NOT LIKE 'http%';
+
+UPDATE
+    community
+SET
+    icon = NULL
+WHERE
+    icon NOT LIKE 'http%';
+
+UPDATE
+    community
+SET
+    banner = NULL
+WHERE
+    banner NOT LIKE 'http%';
+
index e1d55a205b3a12ccd02ab526158ddbc7e790e033..cba90ac49e64c2cfea55e1c1a75460c28b25afe4 100644 (file)
 -- post_saved
-alter table post_saved rename column person_id to user_id;
-alter table post_saved rename constraint post_saved_post_id_person_id_key to post_saved_post_id_user_id_key;
-alter table post_saved rename constraint post_saved_person_id_fkey to post_saved_user_id_fkey;
+ALTER TABLE post_saved RENAME COLUMN person_id TO user_id;
+
+ALTER TABLE post_saved RENAME CONSTRAINT post_saved_post_id_person_id_key TO post_saved_post_id_user_id_key;
+
+ALTER TABLE post_saved RENAME CONSTRAINT post_saved_person_id_fkey TO post_saved_user_id_fkey;
 
 -- post_read
-alter table post_read rename column person_id to user_id;
-alter table post_read rename constraint post_read_post_id_person_id_key to post_read_post_id_user_id_key;
-alter table post_read rename constraint post_read_person_id_fkey to post_read_user_id_fkey;
+ALTER TABLE post_read RENAME COLUMN person_id TO user_id;
+
+ALTER TABLE post_read RENAME CONSTRAINT post_read_post_id_person_id_key TO post_read_post_id_user_id_key;
+
+ALTER TABLE post_read RENAME CONSTRAINT post_read_person_id_fkey TO post_read_user_id_fkey;
 
 -- post_like
-alter table post_like rename column person_id to user_id;
-alter index idx_post_like_person rename to idx_post_like_user;
-alter table post_like rename constraint post_like_post_id_person_id_key to post_like_post_id_user_id_key;
-alter table post_like rename constraint post_like_person_id_fkey to post_like_user_id_fkey;
+ALTER TABLE post_like RENAME COLUMN person_id TO user_id;
+
+ALTER INDEX idx_post_like_person RENAME TO idx_post_like_user;
+
+ALTER TABLE post_like RENAME CONSTRAINT post_like_post_id_person_id_key TO post_like_post_id_user_id_key;
+
+ALTER TABLE post_like RENAME CONSTRAINT post_like_person_id_fkey TO post_like_user_id_fkey;
 
 -- password_reset_request
-delete from password_reset_request;
-alter table password_reset_request drop column local_user_id;
-alter table password_reset_request add column user_id integer not null references person(id) on update cascade on delete cascade;
+DELETE FROM password_reset_request;
+
+ALTER TABLE password_reset_request
+    DROP COLUMN local_user_id;
+
+ALTER TABLE password_reset_request
+    ADD COLUMN user_id integer NOT NULL REFERENCES person (id) ON UPDATE CASCADE ON DELETE CASCADE;
 
 -- mod_sticky_post
-alter table mod_sticky_post rename column mod_person_id to mod_user_id; 
-alter table mod_sticky_post rename constraint mod_sticky_post_mod_person_id_fkey to mod_sticky_post_mod_user_id_fkey;
+ALTER TABLE mod_sticky_post RENAME COLUMN mod_person_id TO mod_user_id;
+
+ALTER TABLE mod_sticky_post RENAME CONSTRAINT mod_sticky_post_mod_person_id_fkey TO mod_sticky_post_mod_user_id_fkey;
 
 -- mod_remove_post
-alter table mod_remove_post rename column mod_person_id to mod_user_id; 
-alter table mod_remove_post rename constraint mod_remove_post_mod_person_id_fkey to mod_remove_post_mod_user_id_fkey;
+ALTER TABLE mod_remove_post RENAME COLUMN mod_person_id TO mod_user_id;
+
+ALTER TABLE mod_remove_post RENAME CONSTRAINT mod_remove_post_mod_person_id_fkey TO mod_remove_post_mod_user_id_fkey;
 
 -- mod_remove_community
-alter table mod_remove_community rename column mod_person_id to mod_user_id; 
-alter table mod_remove_community rename constraint mod_remove_community_mod_person_id_fkey to mod_remove_community_mod_user_id_fkey;
+ALTER TABLE mod_remove_community RENAME COLUMN mod_person_id TO mod_user_id;
+
+ALTER TABLE mod_remove_community RENAME CONSTRAINT mod_remove_community_mod_person_id_fkey TO mod_remove_community_mod_user_id_fkey;
 
 -- mod_remove_comment
-alter table mod_remove_comment rename column mod_person_id to mod_user_id; 
-alter table mod_remove_comment rename constraint mod_remove_comment_mod_person_id_fkey to mod_remove_comment_mod_user_id_fkey;
+ALTER TABLE mod_remove_comment RENAME COLUMN mod_person_id TO mod_user_id;
+
+ALTER TABLE mod_remove_comment RENAME CONSTRAINT mod_remove_comment_mod_person_id_fkey TO mod_remove_comment_mod_user_id_fkey;
 
 -- mod_lock_post
-alter table mod_lock_post rename column mod_person_id to mod_user_id; 
-alter table mod_lock_post rename constraint mod_lock_post_mod_person_id_fkey to mod_lock_post_mod_user_id_fkey;
+ALTER TABLE mod_lock_post RENAME COLUMN mod_person_id TO mod_user_id;
+
+ALTER TABLE mod_lock_post RENAME CONSTRAINT mod_lock_post_mod_person_id_fkey TO mod_lock_post_mod_user_id_fkey;
 
 -- mod_add_community
-alter table mod_ban_from_community rename column mod_person_id to mod_user_id; 
-alter table mod_ban_from_community rename column other_person_id to other_user_id; 
-alter table mod_ban_from_community rename constraint mod_ban_from_community_mod_person_id_fkey to mod_ban_from_community_mod_user_id_fkey;
-alter table mod_ban_from_community rename constraint mod_ban_from_community_other_person_id_fkey to mod_ban_from_community_other_user_id_fkey;
+ALTER TABLE mod_ban_from_community RENAME COLUMN mod_person_id TO mod_user_id;
+
+ALTER TABLE mod_ban_from_community RENAME COLUMN other_person_id TO other_user_id;
+
+ALTER TABLE mod_ban_from_community RENAME CONSTRAINT mod_ban_from_community_mod_person_id_fkey TO mod_ban_from_community_mod_user_id_fkey;
+
+ALTER TABLE mod_ban_from_community RENAME CONSTRAINT mod_ban_from_community_other_person_id_fkey TO mod_ban_from_community_other_user_id_fkey;
 
 -- mod_ban
-alter table mod_ban rename column mod_person_id to mod_user_id; 
-alter table mod_ban rename column other_person_id to other_user_id; 
-alter table mod_ban rename constraint mod_ban_mod_person_id_fkey to mod_ban_mod_user_id_fkey;
-alter table mod_ban rename constraint mod_ban_other_person_id_fkey to mod_ban_other_user_id_fkey;
+ALTER TABLE mod_ban RENAME COLUMN mod_person_id TO mod_user_id;
+
+ALTER TABLE mod_ban RENAME COLUMN other_person_id TO other_user_id;
+
+ALTER TABLE mod_ban RENAME CONSTRAINT mod_ban_mod_person_id_fkey TO mod_ban_mod_user_id_fkey;
+
+ALTER TABLE mod_ban RENAME CONSTRAINT mod_ban_other_person_id_fkey TO mod_ban_other_user_id_fkey;
 
 -- mod_add_community
-alter table mod_add_community rename column mod_person_id to mod_user_id; 
-alter table mod_add_community rename column other_person_id to other_user_id; 
-alter table mod_add_community rename constraint mod_add_community_mod_person_id_fkey to mod_add_community_mod_user_id_fkey;
-alter table mod_add_community rename constraint mod_add_community_other_person_id_fkey to mod_add_community_other_user_id_fkey;
+ALTER TABLE mod_add_community RENAME COLUMN mod_person_id TO mod_user_id;
+
+ALTER TABLE mod_add_community RENAME COLUMN other_person_id TO other_user_id;
+
+ALTER TABLE mod_add_community RENAME CONSTRAINT mod_add_community_mod_person_id_fkey TO mod_add_community_mod_user_id_fkey;
+
+ALTER TABLE mod_add_community RENAME CONSTRAINT mod_add_community_other_person_id_fkey TO mod_add_community_other_user_id_fkey;
 
 -- mod_add
-alter table mod_add rename column mod_person_id to mod_user_id; 
-alter table mod_add rename column other_person_id to other_user_id; 
-alter table mod_add rename constraint mod_add_mod_person_id_fkey to mod_add_mod_user_id_fkey;
-alter table mod_add rename constraint mod_add_other_person_id_fkey to mod_add_other_user_id_fkey;
+ALTER TABLE mod_add RENAME COLUMN mod_person_id TO mod_user_id;
+
+ALTER TABLE mod_add RENAME COLUMN other_person_id TO other_user_id;
+
+ALTER TABLE mod_add RENAME CONSTRAINT mod_add_mod_person_id_fkey TO mod_add_mod_user_id_fkey;
+
+ALTER TABLE mod_add RENAME CONSTRAINT mod_add_other_person_id_fkey TO mod_add_other_user_id_fkey;
 
 -- community_user_ban
-alter table community_person_ban rename to community_user_ban;
-alter sequence community_person_ban_id_seq rename to community_user_ban_id_seq;
-alter table community_user_ban rename column person_id to user_id;
-alter table community_user_ban rename constraint community_person_ban_pkey to community_user_ban_pkey; 
-alter table community_user_ban rename constraint community_person_ban_community_id_fkey to community_user_ban_community_id_fkey;
-alter table community_user_ban rename constraint community_person_ban_community_id_person_id_key to community_user_ban_community_id_user_id_key;
-alter table community_user_ban rename constraint community_person_ban_person_id_fkey to community_user_ban_user_id_fkey;
+ALTER TABLE community_person_ban RENAME TO community_user_ban;
+
+ALTER SEQUENCE community_person_ban_id_seq
+    RENAME TO community_user_ban_id_seq;
+
+ALTER TABLE community_user_ban RENAME COLUMN person_id TO user_id;
+
+ALTER TABLE community_user_ban RENAME CONSTRAINT community_person_ban_pkey TO community_user_ban_pkey;
+
+ALTER TABLE community_user_ban RENAME CONSTRAINT community_person_ban_community_id_fkey TO community_user_ban_community_id_fkey;
+
+ALTER TABLE community_user_ban RENAME CONSTRAINT community_person_ban_community_id_person_id_key TO community_user_ban_community_id_user_id_key;
+
+ALTER TABLE community_user_ban RENAME CONSTRAINT community_person_ban_person_id_fkey TO community_user_ban_user_id_fkey;
 
 -- community_moderator
-alter table community_moderator rename column person_id to user_id;
-alter table community_moderator rename constraint community_moderator_community_id_person_id_key to community_moderator_community_id_user_id_key;
-alter table community_moderator rename constraint community_moderator_person_id_fkey to community_moderator_user_id_fkey;
+ALTER TABLE community_moderator RENAME COLUMN person_id TO user_id;
+
+ALTER TABLE community_moderator RENAME CONSTRAINT community_moderator_community_id_person_id_key TO community_moderator_community_id_user_id_key;
+
+ALTER TABLE community_moderator RENAME CONSTRAINT community_moderator_person_id_fkey TO community_moderator_user_id_fkey;
 
 -- community_follower
-alter table community_follower rename column person_id to user_id;
-alter table community_follower rename constraint community_follower_community_id_person_id_key to community_follower_community_id_user_id_key;
-alter table community_follower rename constraint community_follower_person_id_fkey to community_follower_user_id_fkey;
+ALTER TABLE community_follower RENAME COLUMN person_id TO user_id;
+
+ALTER TABLE community_follower RENAME CONSTRAINT community_follower_community_id_person_id_key TO community_follower_community_id_user_id_key;
+
+ALTER TABLE community_follower RENAME CONSTRAINT community_follower_person_id_fkey TO community_follower_user_id_fkey;
 
 -- comment_saved
-alter table comment_saved rename column person_id to user_id;
-alter table comment_saved rename constraint comment_saved_comment_id_person_id_key to comment_saved_comment_id_user_id_key;
-alter table comment_saved rename constraint comment_saved_person_id_fkey to comment_saved_user_id_fkey;
+ALTER TABLE comment_saved RENAME COLUMN person_id TO user_id;
+
+ALTER TABLE comment_saved RENAME CONSTRAINT comment_saved_comment_id_person_id_key TO comment_saved_comment_id_user_id_key;
+
+ALTER TABLE comment_saved RENAME CONSTRAINT comment_saved_person_id_fkey TO comment_saved_user_id_fkey;
 
 -- comment_like
-alter table comment_like rename column person_id to user_id;
-alter index idx_comment_like_person rename to idx_comment_like_user;
-alter table comment_like rename constraint comment_like_comment_id_person_id_key to comment_like_comment_id_user_id_key;
-alter table comment_like rename constraint comment_like_person_id_fkey to comment_like_user_id_fkey;
+ALTER TABLE comment_like RENAME COLUMN person_id TO user_id;
+
+ALTER INDEX idx_comment_like_person RENAME TO idx_comment_like_user;
+
+ALTER TABLE comment_like RENAME CONSTRAINT comment_like_comment_id_person_id_key TO comment_like_comment_id_user_id_key;
+
+ALTER TABLE comment_like RENAME CONSTRAINT comment_like_person_id_fkey TO comment_like_user_id_fkey;
 
 -- user_ban
-alter table person_ban rename to user_ban;
-alter sequence person_ban_id_seq rename to user_ban_id_seq;
-alter index person_ban_pkey rename to user_ban_pkey;
-alter index person_ban_person_id_key rename to user_ban_user_id_key;
-alter table user_ban rename column person_id to user_id;
-alter table user_ban rename constraint person_ban_person_id_fkey to user_ban_user_id_fkey;
+ALTER TABLE person_ban RENAME TO user_ban;
+
+ALTER SEQUENCE person_ban_id_seq
+    RENAME TO user_ban_id_seq;
+
+ALTER INDEX person_ban_pkey RENAME TO user_ban_pkey;
+
+ALTER INDEX person_ban_person_id_key RENAME TO user_ban_user_id_key;
+
+ALTER TABLE user_ban RENAME COLUMN person_id TO user_id;
+
+ALTER TABLE user_ban RENAME CONSTRAINT person_ban_person_id_fkey TO user_ban_user_id_fkey;
 
 -- user_mention
-alter table person_mention rename to user_mention;
-alter sequence person_mention_id_seq rename to user_mention_id_seq;
-alter index person_mention_pkey rename to user_mention_pkey;
-alter index person_mention_recipient_id_comment_id_key rename to user_mention_recipient_id_comment_id_key;
-alter table user_mention rename constraint person_mention_comment_id_fkey to user_mention_comment_id_fkey;
-alter table user_mention rename constraint person_mention_recipient_id_fkey to user_mention_recipient_id_fkey;
+ALTER TABLE person_mention RENAME TO user_mention;
+
+ALTER SEQUENCE person_mention_id_seq
+    RENAME TO user_mention_id_seq;
+
+ALTER INDEX person_mention_pkey RENAME TO user_mention_pkey;
+
+ALTER INDEX person_mention_recipient_id_comment_id_key RENAME TO user_mention_recipient_id_comment_id_key;
+
+ALTER TABLE user_mention RENAME CONSTRAINT person_mention_comment_id_fkey TO user_mention_comment_id_fkey;
+
+ALTER TABLE user_mention RENAME CONSTRAINT person_mention_recipient_id_fkey TO user_mention_recipient_id_fkey;
 
 -- User aggregates table
-alter table person_aggregates rename to user_aggregates;
-alter sequence person_aggregates_id_seq rename to user_aggregates_id_seq;
-alter table user_aggregates rename column person_id to user_id;
+ALTER TABLE person_aggregates RENAME TO user_aggregates;
+
+ALTER SEQUENCE person_aggregates_id_seq
+    RENAME TO user_aggregates_id_seq;
+
+ALTER TABLE user_aggregates RENAME COLUMN person_id TO user_id;
 
 -- Indexes
-alter index person_aggregates_pkey rename to user_aggregates_pkey;
-alter index idx_person_aggregates_comment_score rename to idx_user_aggregates_comment_score;
-alter index person_aggregates_person_id_key rename to user_aggregates_user_id_key;
-alter table user_aggregates rename constraint person_aggregates_person_id_fkey to user_aggregates_user_id_fkey;
+ALTER INDEX person_aggregates_pkey RENAME TO user_aggregates_pkey;
+
+ALTER INDEX idx_person_aggregates_comment_score RENAME TO idx_user_aggregates_comment_score;
+
+ALTER INDEX person_aggregates_person_id_key RENAME TO user_aggregates_user_id_key;
+
+ALTER TABLE user_aggregates RENAME CONSTRAINT person_aggregates_person_id_fkey TO user_aggregates_user_id_fkey;
 
 -- Redo the user_aggregates table
-drop trigger person_aggregates_person on person;
-drop trigger person_aggregates_post_count on post;
-drop trigger person_aggregates_post_score on post_like;
-drop trigger person_aggregates_comment_count on comment;
-drop trigger person_aggregates_comment_score on comment_like;
-drop function 
-  person_aggregates_person, 
-  person_aggregates_post_count,
-  person_aggregates_post_score,
-  person_aggregates_comment_count,
-  person_aggregates_comment_score;
+DROP TRIGGER person_aggregates_person ON person;
+
+DROP TRIGGER person_aggregates_post_count ON post;
+
+DROP TRIGGER person_aggregates_post_score ON post_like;
+
+DROP TRIGGER person_aggregates_comment_count ON comment;
+
+DROP TRIGGER person_aggregates_comment_score ON comment_like;
+
+DROP FUNCTION person_aggregates_person, person_aggregates_post_count, person_aggregates_post_score, person_aggregates_comment_count, person_aggregates_comment_score;
 
 -- user_ table
 -- Drop views
-drop view person_alias_1, person_alias_2;
+DROP VIEW person_alias_1, person_alias_2;
 
 -- Rename indexes
-alter index person__pkey rename to user__pkey;
-alter index idx_person_actor_id rename to idx_user_actor_id;
-alter index idx_person_inbox_url rename to idx_user_inbox_url;
-alter index idx_person_lower_actor_id rename to idx_user_lower_actor_id;
-alter index idx_person_published rename to idx_user_published;
+ALTER INDEX person__pkey RENAME TO user__pkey;
+
+ALTER INDEX idx_person_actor_id RENAME TO idx_user_actor_id;
+
+ALTER INDEX idx_person_inbox_url RENAME TO idx_user_inbox_url;
+
+ALTER INDEX idx_person_lower_actor_id RENAME TO idx_user_lower_actor_id;
+
+ALTER INDEX idx_person_published RENAME TO idx_user_published;
 
 -- Rename triggers
-alter trigger site_aggregates_person_delete on person rename to site_aggregates_user_delete;
-alter trigger site_aggregates_person_insert on person rename to site_aggregates_user_insert;
+ALTER TRIGGER site_aggregates_person_delete ON person RENAME TO site_aggregates_user_delete;
+
+ALTER TRIGGER site_aggregates_person_insert ON person RENAME TO site_aggregates_user_insert;
 
 -- Rename the trigger functions
-alter function site_aggregates_person_delete() rename to site_aggregates_user_delete;
-alter function site_aggregates_person_insert() rename to site_aggregates_user_insert;
+ALTER FUNCTION site_aggregates_person_delete () RENAME TO site_aggregates_user_delete;
+
+ALTER FUNCTION site_aggregates_person_insert () RENAME TO site_aggregates_user_insert;
 
 -- Rename the table back to user_
-alter table person rename to user_;
-alter sequence person_id_seq rename to user__id_seq;
+ALTER TABLE person RENAME TO user_;
+
+ALTER SEQUENCE person_id_seq
+    RENAME TO user__id_seq;
 
 -- Add the columns back in
-alter table user_
-  add column password_encrypted text not null default 'changeme',
-  add column email text,
-  add column admin boolean default false not null,
-  add column show_nsfw boolean default false not null,
-  add column theme character varying(20) default 'darkly'::character varying not null,
-  add column default_sort_type smallint default 0 not null,
-  add column default_listing_type smallint default 1 not null,
-  add column lang character varying(20) default 'browser'::character varying not null,
-  add column show_avatars boolean default true not null,
-  add column send_notifications_to_email boolean default false not null,
-  add column matrix_user_id text;
+ALTER TABLE user_
+    ADD COLUMN password_encrypted text NOT NULL DEFAULT 'changeme',
+    ADD COLUMN email text,
+    ADD COLUMN admin boolean DEFAULT FALSE NOT NULL,
+    ADD COLUMN show_nsfw boolean DEFAULT FALSE NOT NULL,
+    ADD COLUMN theme character varying(20) DEFAULT 'darkly'::character varying NOT NULL,
+    ADD COLUMN default_sort_type smallint DEFAULT 0 NOT NULL,
+    ADD COLUMN default_listing_type smallint DEFAULT 1 NOT NULL,
+    ADD COLUMN lang character varying(20) DEFAULT 'browser'::character varying NOT NULL,
+    ADD COLUMN show_avatars boolean DEFAULT TRUE NOT NULL,
+    ADD COLUMN send_notifications_to_email boolean DEFAULT FALSE NOT NULL,
+    ADD COLUMN matrix_user_id text;
 
 -- Update the user_ table with the local_user data
-update user_ u set
-  password_encrypted = lu.password_encrypted,
-  email = lu.email,
-  admin = lu.admin,
-  show_nsfw = lu.show_nsfw,
-  theme = lu.theme,
-  default_sort_type = lu.default_sort_type,
-  default_listing_type = lu.default_listing_type,
-  lang = lu.lang,
-  show_avatars = lu.show_avatars,
-  send_notifications_to_email = lu.send_notifications_to_email,
-  matrix_user_id = lu.matrix_user_id
-from local_user lu
-where lu.person_id = u.id;
-
-create view user_alias_1 as select * from user_;
-create view user_alias_2 as select * from user_;
-
-drop table local_user;
+UPDATE
+    user_ u
+SET
+    password_encrypted = lu.password_encrypted,
+    email = lu.email,
+    admin = lu.admin,
+    show_nsfw = lu.show_nsfw,
+    theme = lu.theme,
+    default_sort_type = lu.default_sort_type,
+    default_listing_type = lu.default_listing_type,
+    lang = lu.lang,
+    show_avatars = lu.show_avatars,
+    send_notifications_to_email = lu.send_notifications_to_email,
+    matrix_user_id = lu.matrix_user_id
+FROM
+    local_user lu
+WHERE
+    lu.person_id = u.id;
+
+CREATE VIEW user_alias_1 AS
+SELECT
+    *
+FROM
+    user_;
+
+CREATE VIEW user_alias_2 AS
+SELECT
+    *
+FROM
+    user_;
+
+DROP TABLE local_user;
 
 -- Add the user_aggregates table triggers
-
 -- initial user add
-create function user_aggregates_user()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into user_aggregates (user_id) values (NEW.id);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from user_aggregates where user_id = OLD.id;
-  END IF;
-  return null;
-end $$;
-
-create trigger user_aggregates_user
-after insert or delete on user_
-for each row
-execute procedure user_aggregates_user();
+CREATE FUNCTION user_aggregates_user ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO user_aggregates (user_id)
+            VALUES (NEW.id);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM user_aggregates
+        WHERE user_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER user_aggregates_user
+    AFTER INSERT OR DELETE ON user_
+    FOR EACH ROW
+    EXECUTE PROCEDURE user_aggregates_user ();
 
 -- post count
-create function user_aggregates_post_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update user_aggregates 
-    set post_count = post_count + 1 where user_id = NEW.creator_id;
-
-  ELSIF (TG_OP = 'DELETE') THEN
-    update user_aggregates 
-    set post_count = post_count - 1 where user_id = OLD.creator_id;
-
-    -- If the post gets deleted, the score calculation trigger won't fire, 
-    -- so you need to re-calculate
-    update user_aggregates ua
-    set post_score = pd.score
-    from (
-      select u.id,
-      coalesce(0, sum(pl.score)) as score
-      -- User join because posts could be empty
-      from user_ u 
-      left join post p on u.id = p.creator_id
-      left join post_like pl on p.id = pl.post_id
-      group by u.id
-    ) pd 
-    where ua.user_id = OLD.creator_id;
-
-  END IF;
-  return null;
-end $$;
-
-create trigger user_aggregates_post_count
-after insert or delete on post
-for each row
-execute procedure user_aggregates_post_count();
+CREATE FUNCTION user_aggregates_post_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            user_aggregates
+        SET
+            post_count = post_count + 1
+        WHERE
+            user_id = NEW.creator_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            user_aggregates
+        SET
+            post_count = post_count - 1
+        WHERE
+            user_id = OLD.creator_id;
+        -- If the post gets deleted, the score calculation trigger won't fire,
+        -- so you need to re-calculate
+        UPDATE
+            user_aggregates ua
+        SET
+            post_score = pd.score
+        FROM (
+            SELECT
+                u.id,
+                coalesce(0, sum(pl.score)) AS score
+                -- User join because posts could be empty
+            FROM
+                user_ u
+            LEFT JOIN post p ON u.id = p.creator_id
+            LEFT JOIN post_like pl ON p.id = pl.post_id
+        GROUP BY
+            u.id) pd
+    WHERE
+        ua.user_id = OLD.creator_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER user_aggregates_post_count
+    AFTER INSERT OR DELETE ON post
+    FOR EACH ROW
+    EXECUTE PROCEDURE user_aggregates_post_count ();
 
 -- post score
-create function user_aggregates_post_score()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    -- Need to get the post creator, not the voter
-    update user_aggregates ua
-    set post_score = post_score + NEW.score
-    from post p
-    where ua.user_id = p.creator_id and p.id = NEW.post_id;
-    
-  ELSIF (TG_OP = 'DELETE') THEN
-    update user_aggregates ua
-    set post_score = post_score - OLD.score
-    from post p
-    where ua.user_id = p.creator_id and p.id = OLD.post_id;
-  END IF;
-  return null;
-end $$;
-
-create trigger user_aggregates_post_score
-after insert or delete on post_like
-for each row
-execute procedure user_aggregates_post_score();
+CREATE FUNCTION user_aggregates_post_score ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        -- Need to get the post creator, not the voter
+        UPDATE
+            user_aggregates ua
+        SET
+            post_score = post_score + NEW.score
+        FROM
+            post p
+        WHERE
+            ua.user_id = p.creator_id
+            AND p.id = NEW.post_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            user_aggregates ua
+        SET
+            post_score = post_score - OLD.score
+        FROM
+            post p
+        WHERE
+            ua.user_id = p.creator_id
+            AND p.id = OLD.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER user_aggregates_post_score
+    AFTER INSERT OR DELETE ON post_like
+    FOR EACH ROW
+    EXECUTE PROCEDURE user_aggregates_post_score ();
 
 -- comment count
-create function user_aggregates_comment_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update user_aggregates 
-    set comment_count = comment_count + 1 where user_id = NEW.creator_id;
-  ELSIF (TG_OP = 'DELETE') THEN
-    update user_aggregates 
-    set comment_count = comment_count - 1 where user_id = OLD.creator_id;
-
-    -- If the comment gets deleted, the score calculation trigger won't fire, 
-    -- so you need to re-calculate
-    update user_aggregates ua
-    set comment_score = cd.score
-    from (
-      select u.id,
-      coalesce(0, sum(cl.score)) as score
-      -- User join because comments could be empty
-      from user_ u 
-      left join comment c on u.id = c.creator_id
-      left join comment_like cl on c.id = cl.comment_id
-      group by u.id
-    ) cd 
-    where ua.user_id = OLD.creator_id;
-  END IF;
-  return null;
-end $$;
-
-create trigger user_aggregates_comment_count
-after insert or delete on comment
-for each row
-execute procedure user_aggregates_comment_count();
+CREATE FUNCTION user_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            user_aggregates
+        SET
+            comment_count = comment_count + 1
+        WHERE
+            user_id = NEW.creator_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            user_aggregates
+        SET
+            comment_count = comment_count - 1
+        WHERE
+            user_id = OLD.creator_id;
+        -- If the comment gets deleted, the score calculation trigger won't fire,
+        -- so you need to re-calculate
+        UPDATE
+            user_aggregates ua
+        SET
+            comment_score = cd.score
+        FROM (
+            SELECT
+                u.id,
+                coalesce(0, sum(cl.score)) AS score
+                -- User join because comments could be empty
+            FROM
+                user_ u
+            LEFT JOIN comment c ON u.id = c.creator_id
+            LEFT JOIN comment_like cl ON c.id = cl.comment_id
+        GROUP BY
+            u.id) cd
+    WHERE
+        ua.user_id = OLD.creator_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER user_aggregates_comment_count
+    AFTER INSERT OR DELETE ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE user_aggregates_comment_count ();
 
 -- comment score
-create function user_aggregates_comment_score()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    -- Need to get the post creator, not the voter
-    update user_aggregates ua
-    set comment_score = comment_score + NEW.score
-    from comment c
-    where ua.user_id = c.creator_id and c.id = NEW.comment_id;
-  ELSIF (TG_OP = 'DELETE') THEN
-    update user_aggregates ua
-    set comment_score = comment_score - OLD.score
-    from comment c
-    where ua.user_id = c.creator_id and c.id = OLD.comment_id;
-  END IF;
-  return null;
-end $$;
-
-create trigger user_aggregates_comment_score
-after insert or delete on comment_like
-for each row
-execute procedure user_aggregates_comment_score();
+CREATE FUNCTION user_aggregates_comment_score ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        -- Need to get the post creator, not the voter
+        UPDATE
+            user_aggregates ua
+        SET
+            comment_score = comment_score + NEW.score
+        FROM
+            comment c
+        WHERE
+            ua.user_id = c.creator_id
+            AND c.id = NEW.comment_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            user_aggregates ua
+        SET
+            comment_score = comment_score - OLD.score
+        FROM
+            comment c
+        WHERE
+            ua.user_id = c.creator_id
+            AND c.id = OLD.comment_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER user_aggregates_comment_score
+    AFTER INSERT OR DELETE ON comment_like
+    FOR EACH ROW
+    EXECUTE PROCEDURE user_aggregates_comment_score ();
 
 -- redo site aggregates trigger
-create or replace function site_aggregates_activity(i text) returns integer
-    language plpgsql
-    as $$
-declare
-   count_ integer;
-begin
-  select count(*)
-  into count_
-  from (
-    select c.creator_id from comment c
-    inner join user_ u on c.creator_id = u.id
-    where c.published > ('now'::timestamp - i::interval) 
-    and u.local = true
-    union
-    select p.creator_id from post p
-    inner join user_ u on p.creator_id = u.id
-    where p.published > ('now'::timestamp - i::interval)
-    and u.local = true
-  ) a;
-  return count_;
-end;
+CREATE OR REPLACE FUNCTION site_aggregates_activity (i text)
+    RETURNS integer
+    LANGUAGE plpgsql
+    AS $$
+DECLARE
+    count_ integer;
+BEGIN
+    SELECT
+        count(*) INTO count_
+    FROM (
+        SELECT
+            c.creator_id
+        FROM
+            comment c
+            INNER JOIN user_ u ON c.creator_id = u.id
+        WHERE
+            c.published > ('now'::timestamp - i::interval)
+            AND u.local = TRUE
+        UNION
+        SELECT
+            p.creator_id
+        FROM
+            post p
+            INNER JOIN user_ u ON p.creator_id = u.id
+        WHERE
+            p.published > ('now'::timestamp - i::interval)
+            AND u.local = TRUE) a;
+    RETURN count_;
+END;
 $$;
+
index db3547e5fb569aade06900998865540dfd35a417..4eb020d463aea730053383e0a5fb8b6ef5f0ebb4 100644 (file)
 -- Person
 -- Drop the 2 views user_alias_1, user_alias_2
-drop view user_alias_1, user_alias_2;
+DROP VIEW user_alias_1, user_alias_2;
 
 -- rename the user_ table to person
-alter table user_ rename to person;
-alter sequence user__id_seq rename to person_id_seq;
+ALTER TABLE user_ RENAME TO person;
+
+ALTER SEQUENCE user__id_seq
+    RENAME TO person_id_seq;
 
 -- create a new table local_user
-create table local_user (
-  id serial primary key,
-  person_id int references person on update cascade on delete cascade not null,
-  password_encrypted text not null,
-  email text unique,
-  admin boolean default false not null,
-  show_nsfw boolean default false not null,
-  theme character varying(20) default 'darkly'::character varying not null,
-  default_sort_type smallint default 0 not null,
-  default_listing_type smallint default 1 not null,
-  lang character varying(20) default 'browser'::character varying not null,
-  show_avatars boolean default true not null,
-  send_notifications_to_email boolean default false not null,
-  matrix_user_id text,
-  unique (person_id)
+CREATE TABLE local_user (
+    id serial PRIMARY KEY,
+    person_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    password_encrypted text NOT NULL,
+    email text UNIQUE,
+    admin boolean DEFAULT FALSE NOT NULL,
+    show_nsfw boolean DEFAULT FALSE NOT NULL,
+    theme character varying(20) DEFAULT 'darkly' ::character varying NOT NULL,
+    default_sort_type smallint DEFAULT 0 NOT NULL,
+    default_listing_type smallint DEFAULT 1 NOT NULL,
+    lang character varying(20) DEFAULT 'browser' ::character varying NOT NULL,
+    show_avatars boolean DEFAULT TRUE NOT NULL,
+    send_notifications_to_email boolean DEFAULT FALSE NOT NULL,
+    matrix_user_id text,
+    UNIQUE (person_id)
 );
 
 -- Copy the local users over to the new table
-insert into local_user 
-(
-  person_id,
-  password_encrypted,
-  email,
-  admin,
-  show_nsfw,
-  theme,
-  default_sort_type,
-  default_listing_type,
-  lang,
-  show_avatars,
-  send_notifications_to_email,
-  matrix_user_id
-)
-select
-  id,
-  password_encrypted,
-  email,
-  admin,
-  show_nsfw,
-  theme,
-  default_sort_type,
-  default_listing_type,
-  lang,
-  show_avatars,
-  send_notifications_to_email,
-  matrix_user_id
-from person
-where local = true;
+INSERT INTO local_user (person_id, password_encrypted, email, admin, show_nsfw, theme, default_sort_type, default_listing_type, lang, show_avatars, send_notifications_to_email, matrix_user_id)
+SELECT
+    id,
+    password_encrypted,
+    email,
+    admin,
+    show_nsfw,
+    theme,
+    default_sort_type,
+    default_listing_type,
+    lang,
+    show_avatars,
+    send_notifications_to_email,
+    matrix_user_id
+FROM
+    person
+WHERE
+    local = TRUE;
 
 -- Drop those columns from person
-alter table person 
-  drop column password_encrypted,
-  drop column email,
-  drop column admin,
-  drop column show_nsfw,
-  drop column theme,
-  drop column default_sort_type,
-  drop column default_listing_type,
-  drop column lang,
-  drop column show_avatars,
-  drop column send_notifications_to_email,
-  drop column matrix_user_id;
+ALTER TABLE person
+    DROP COLUMN password_encrypted,
+    DROP COLUMN email,
+    DROP COLUMN admin,
+    DROP COLUMN show_nsfw,
+    DROP COLUMN theme,
+    DROP COLUMN default_sort_type,
+    DROP COLUMN default_listing_type,
+    DROP COLUMN lang,
+    DROP COLUMN show_avatars,
+    DROP COLUMN send_notifications_to_email,
+    DROP COLUMN matrix_user_id;
 
 -- Rename indexes
-alter index user__pkey rename to person__pkey;
-alter index idx_user_actor_id rename to idx_person_actor_id;
-alter index idx_user_inbox_url rename to idx_person_inbox_url;
-alter index idx_user_lower_actor_id rename to idx_person_lower_actor_id;
-alter index idx_user_published rename to idx_person_published;
+ALTER INDEX user__pkey RENAME TO person__pkey;
+
+ALTER INDEX idx_user_actor_id RENAME TO idx_person_actor_id;
+
+ALTER INDEX idx_user_inbox_url RENAME TO idx_person_inbox_url;
+
+ALTER INDEX idx_user_lower_actor_id RENAME TO idx_person_lower_actor_id;
+
+ALTER INDEX idx_user_published RENAME TO idx_person_published;
 
 -- Rename triggers
-alter trigger site_aggregates_user_delete on person rename to site_aggregates_person_delete;
-alter trigger site_aggregates_user_insert on person rename to site_aggregates_person_insert;
+ALTER TRIGGER site_aggregates_user_delete ON person RENAME TO site_aggregates_person_delete;
+
+ALTER TRIGGER site_aggregates_user_insert ON person RENAME TO site_aggregates_person_insert;
 
 -- Rename the trigger functions
-alter function site_aggregates_user_delete() rename to site_aggregates_person_delete;
-alter function site_aggregates_user_insert() rename to site_aggregates_person_insert;
+ALTER FUNCTION site_aggregates_user_delete () RENAME TO site_aggregates_person_delete;
+
+ALTER FUNCTION site_aggregates_user_insert () RENAME TO site_aggregates_person_insert;
 
 -- Create views
-create view person_alias_1 as select * from person;
-create view person_alias_2 as select * from person;
+CREATE VIEW person_alias_1 AS
+SELECT
+    *
+FROM
+    person;
+
+CREATE VIEW person_alias_2 AS
+SELECT
+    *
+FROM
+    person;
 
 -- Redo user aggregates into person_aggregates
-alter table user_aggregates rename to person_aggregates;
-alter sequence user_aggregates_id_seq rename to person_aggregates_id_seq;
-alter table person_aggregates rename column user_id to person_id;
+ALTER TABLE user_aggregates RENAME TO person_aggregates;
+
+ALTER SEQUENCE user_aggregates_id_seq
+    RENAME TO person_aggregates_id_seq;
+
+ALTER TABLE person_aggregates RENAME COLUMN user_id TO person_id;
 
 -- index
-alter index user_aggregates_pkey rename to person_aggregates_pkey;
-alter index idx_user_aggregates_comment_score rename to idx_person_aggregates_comment_score;
-alter index user_aggregates_user_id_key rename to person_aggregates_person_id_key;
-alter table person_aggregates rename constraint user_aggregates_user_id_fkey to person_aggregates_person_id_fkey;
+ALTER INDEX user_aggregates_pkey RENAME TO person_aggregates_pkey;
+
+ALTER INDEX idx_user_aggregates_comment_score RENAME TO idx_person_aggregates_comment_score;
 
+ALTER INDEX user_aggregates_user_id_key RENAME TO person_aggregates_person_id_key;
+
+ALTER TABLE person_aggregates RENAME CONSTRAINT user_aggregates_user_id_fkey TO person_aggregates_person_id_fkey;
 
 -- Drop all the old triggers and functions
-drop trigger user_aggregates_user on person;
-drop trigger user_aggregates_post_count on post;
-drop trigger user_aggregates_post_score on post_like;
-drop trigger user_aggregates_comment_count on comment;
-drop trigger user_aggregates_comment_score on comment_like;
-drop function 
-  user_aggregates_user, 
-  user_aggregates_post_count,
-  user_aggregates_post_score,
-  user_aggregates_comment_count,
-  user_aggregates_comment_score;
+DROP TRIGGER user_aggregates_user ON person;
+
+DROP TRIGGER user_aggregates_post_count ON post;
+
+DROP TRIGGER user_aggregates_post_score ON post_like;
+
+DROP TRIGGER user_aggregates_comment_count ON comment;
+
+DROP TRIGGER user_aggregates_comment_score ON comment_like;
+
+DROP FUNCTION user_aggregates_user, user_aggregates_post_count, user_aggregates_post_score, user_aggregates_comment_count, user_aggregates_comment_score;
 
 -- initial user add
-create function person_aggregates_person()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into person_aggregates (person_id) values (NEW.id);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from person_aggregates where person_id = OLD.id;
-  END IF;
-  return null;
-end $$;
-
-create trigger person_aggregates_person
-after insert or delete on person
-for each row
-execute procedure person_aggregates_person();
+CREATE FUNCTION person_aggregates_person ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO person_aggregates (person_id)
+            VALUES (NEW.id);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM person_aggregates
+        WHERE person_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER person_aggregates_person
+    AFTER INSERT OR DELETE ON person
+    FOR EACH ROW
+    EXECUTE PROCEDURE person_aggregates_person ();
 
 -- post count
-create function person_aggregates_post_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update person_aggregates 
-    set post_count = post_count + 1 where person_id = NEW.creator_id;
-
-  ELSIF (TG_OP = 'DELETE') THEN
-    update person_aggregates 
-    set post_count = post_count - 1 where person_id = OLD.creator_id;
-
-    -- If the post gets deleted, the score calculation trigger won't fire, 
-    -- so you need to re-calculate
-    update person_aggregates ua
-    set post_score = pd.score
-    from (
-      select u.id,
-      coalesce(0, sum(pl.score)) as score
-      -- User join because posts could be empty
-      from person u 
-      left join post p on u.id = p.creator_id
-      left join post_like pl on p.id = pl.post_id
-      group by u.id
-    ) pd 
-    where ua.person_id = OLD.creator_id;
-
-  END IF;
-  return null;
-end $$;
-
-create trigger person_aggregates_post_count
-after insert or delete on post
-for each row
-execute procedure person_aggregates_post_count();
+CREATE FUNCTION person_aggregates_post_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            person_aggregates
+        SET
+            post_count = post_count + 1
+        WHERE
+            person_id = NEW.creator_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            person_aggregates
+        SET
+            post_count = post_count - 1
+        WHERE
+            person_id = OLD.creator_id;
+        -- If the post gets deleted, the score calculation trigger won't fire,
+        -- so you need to re-calculate
+        UPDATE
+            person_aggregates ua
+        SET
+            post_score = pd.score
+        FROM (
+            SELECT
+                u.id,
+                coalesce(0, sum(pl.score)) AS score
+                -- User join because posts could be empty
+            FROM
+                person u
+            LEFT JOIN post p ON u.id = p.creator_id
+            LEFT JOIN post_like pl ON p.id = pl.post_id
+        GROUP BY
+            u.id) pd
+    WHERE
+        ua.person_id = OLD.creator_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER person_aggregates_post_count
+    AFTER INSERT OR DELETE ON post
+    FOR EACH ROW
+    EXECUTE PROCEDURE person_aggregates_post_count ();
 
 -- post score
-create function person_aggregates_post_score()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    -- Need to get the post creator, not the voter
-    update person_aggregates ua
-    set post_score = post_score + NEW.score
-    from post p
-    where ua.person_id = p.creator_id and p.id = NEW.post_id;
-    
-  ELSIF (TG_OP = 'DELETE') THEN
-    update person_aggregates ua
-    set post_score = post_score - OLD.score
-    from post p
-    where ua.person_id = p.creator_id and p.id = OLD.post_id;
-  END IF;
-  return null;
-end $$;
-
-create trigger person_aggregates_post_score
-after insert or delete on post_like
-for each row
-execute procedure person_aggregates_post_score();
+CREATE FUNCTION person_aggregates_post_score ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        -- Need to get the post creator, not the voter
+        UPDATE
+            person_aggregates ua
+        SET
+            post_score = post_score + NEW.score
+        FROM
+            post p
+        WHERE
+            ua.person_id = p.creator_id
+            AND p.id = NEW.post_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            person_aggregates ua
+        SET
+            post_score = post_score - OLD.score
+        FROM
+            post p
+        WHERE
+            ua.person_id = p.creator_id
+            AND p.id = OLD.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER person_aggregates_post_score
+    AFTER INSERT OR DELETE ON post_like
+    FOR EACH ROW
+    EXECUTE PROCEDURE person_aggregates_post_score ();
 
 -- comment count
-create function person_aggregates_comment_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update person_aggregates 
-    set comment_count = comment_count + 1 where person_id = NEW.creator_id;
-  ELSIF (TG_OP = 'DELETE') THEN
-    update person_aggregates 
-    set comment_count = comment_count - 1 where person_id = OLD.creator_id;
-
-    -- If the comment gets deleted, the score calculation trigger won't fire, 
-    -- so you need to re-calculate
-    update person_aggregates ua
-    set comment_score = cd.score
-    from (
-      select u.id,
-      coalesce(0, sum(cl.score)) as score
-      -- User join because comments could be empty
-      from person u 
-      left join comment c on u.id = c.creator_id
-      left join comment_like cl on c.id = cl.comment_id
-      group by u.id
-    ) cd 
-    where ua.person_id = OLD.creator_id;
-  END IF;
-  return null;
-end $$;
-
-create trigger person_aggregates_comment_count
-after insert or delete on comment
-for each row
-execute procedure person_aggregates_comment_count();
+CREATE FUNCTION person_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            person_aggregates
+        SET
+            comment_count = comment_count + 1
+        WHERE
+            person_id = NEW.creator_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            person_aggregates
+        SET
+            comment_count = comment_count - 1
+        WHERE
+            person_id = OLD.creator_id;
+        -- If the comment gets deleted, the score calculation trigger won't fire,
+        -- so you need to re-calculate
+        UPDATE
+            person_aggregates ua
+        SET
+            comment_score = cd.score
+        FROM (
+            SELECT
+                u.id,
+                coalesce(0, sum(cl.score)) AS score
+                -- User join because comments could be empty
+            FROM
+                person u
+            LEFT JOIN comment c ON u.id = c.creator_id
+            LEFT JOIN comment_like cl ON c.id = cl.comment_id
+        GROUP BY
+            u.id) cd
+    WHERE
+        ua.person_id = OLD.creator_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER person_aggregates_comment_count
+    AFTER INSERT OR DELETE ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE person_aggregates_comment_count ();
 
 -- comment score
-create function person_aggregates_comment_score()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    -- Need to get the post creator, not the voter
-    update person_aggregates ua
-    set comment_score = comment_score + NEW.score
-    from comment c
-    where ua.person_id = c.creator_id and c.id = NEW.comment_id;
-  ELSIF (TG_OP = 'DELETE') THEN
-    update person_aggregates ua
-    set comment_score = comment_score - OLD.score
-    from comment c
-    where ua.person_id = c.creator_id and c.id = OLD.comment_id;
-  END IF;
-  return null;
-end $$;
-
-create trigger person_aggregates_comment_score
-after insert or delete on comment_like
-for each row
-execute procedure person_aggregates_comment_score();
+CREATE FUNCTION person_aggregates_comment_score ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        -- Need to get the post creator, not the voter
+        UPDATE
+            person_aggregates ua
+        SET
+            comment_score = comment_score + NEW.score
+        FROM
+            comment c
+        WHERE
+            ua.person_id = c.creator_id
+            AND c.id = NEW.comment_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            person_aggregates ua
+        SET
+            comment_score = comment_score - OLD.score
+        FROM
+            comment c
+        WHERE
+            ua.person_id = c.creator_id
+            AND c.id = OLD.comment_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER person_aggregates_comment_score
+    AFTER INSERT OR DELETE ON comment_like
+    FOR EACH ROW
+    EXECUTE PROCEDURE person_aggregates_comment_score ();
 
 -- person_mention
-alter table user_mention rename to person_mention;
-alter sequence user_mention_id_seq rename to person_mention_id_seq;
-alter index user_mention_pkey rename to person_mention_pkey;
-alter index user_mention_recipient_id_comment_id_key rename to person_mention_recipient_id_comment_id_key;
-alter table person_mention rename constraint user_mention_comment_id_fkey to person_mention_comment_id_fkey;
-alter table person_mention rename constraint user_mention_recipient_id_fkey to person_mention_recipient_id_fkey;
+ALTER TABLE user_mention RENAME TO person_mention;
+
+ALTER SEQUENCE user_mention_id_seq
+    RENAME TO person_mention_id_seq;
+
+ALTER INDEX user_mention_pkey RENAME TO person_mention_pkey;
+
+ALTER INDEX user_mention_recipient_id_comment_id_key RENAME TO person_mention_recipient_id_comment_id_key;
+
+ALTER TABLE person_mention RENAME CONSTRAINT user_mention_comment_id_fkey TO person_mention_comment_id_fkey;
+
+ALTER TABLE person_mention RENAME CONSTRAINT user_mention_recipient_id_fkey TO person_mention_recipient_id_fkey;
 
 -- user_ban
-alter table user_ban rename to person_ban;
-alter sequence user_ban_id_seq rename to person_ban_id_seq;
-alter index user_ban_pkey rename to person_ban_pkey;
-alter index user_ban_user_id_key rename to person_ban_person_id_key;
-alter table person_ban rename column user_id to person_id;
-alter table person_ban rename constraint user_ban_user_id_fkey to person_ban_person_id_fkey;
+ALTER TABLE user_ban RENAME TO person_ban;
+
+ALTER SEQUENCE user_ban_id_seq
+    RENAME TO person_ban_id_seq;
+
+ALTER INDEX user_ban_pkey RENAME TO person_ban_pkey;
+
+ALTER INDEX user_ban_user_id_key RENAME TO person_ban_person_id_key;
+
+ALTER TABLE person_ban RENAME COLUMN user_id TO person_id;
+
+ALTER TABLE person_ban RENAME CONSTRAINT user_ban_user_id_fkey TO person_ban_person_id_fkey;
 
 -- comment_like
-alter table comment_like rename column user_id to person_id;
-alter index idx_comment_like_user rename to idx_comment_like_person;
-alter table comment_like rename constraint comment_like_comment_id_user_id_key to comment_like_comment_id_person_id_key;
-alter table comment_like rename constraint comment_like_user_id_fkey to comment_like_person_id_fkey;
+ALTER TABLE comment_like RENAME COLUMN user_id TO person_id;
+
+ALTER INDEX idx_comment_like_user RENAME TO idx_comment_like_person;
+
+ALTER TABLE comment_like RENAME CONSTRAINT comment_like_comment_id_user_id_key TO comment_like_comment_id_person_id_key;
+
+ALTER TABLE comment_like RENAME CONSTRAINT comment_like_user_id_fkey TO comment_like_person_id_fkey;
 
 -- comment_saved
-alter table comment_saved rename column user_id to person_id;
-alter table comment_saved rename constraint comment_saved_comment_id_user_id_key to comment_saved_comment_id_person_id_key;
-alter table comment_saved rename constraint comment_saved_user_id_fkey to comment_saved_person_id_fkey;
+ALTER TABLE comment_saved RENAME COLUMN user_id TO person_id;
+
+ALTER TABLE comment_saved RENAME CONSTRAINT comment_saved_comment_id_user_id_key TO comment_saved_comment_id_person_id_key;
+
+ALTER TABLE comment_saved RENAME CONSTRAINT comment_saved_user_id_fkey TO comment_saved_person_id_fkey;
 
 -- community_follower
-alter table community_follower rename column user_id to person_id;
-alter table community_follower rename constraint community_follower_community_id_user_id_key to community_follower_community_id_person_id_key;
-alter table community_follower rename constraint community_follower_user_id_fkey to community_follower_person_id_fkey;
+ALTER TABLE community_follower RENAME COLUMN user_id TO person_id;
+
+ALTER TABLE community_follower RENAME CONSTRAINT community_follower_community_id_user_id_key TO community_follower_community_id_person_id_key;
+
+ALTER TABLE community_follower RENAME CONSTRAINT community_follower_user_id_fkey TO community_follower_person_id_fkey;
 
 -- community_moderator
-alter table community_moderator rename column user_id to person_id;
-alter table community_moderator rename constraint community_moderator_community_id_user_id_key to community_moderator_community_id_person_id_key;
-alter table community_moderator rename constraint community_moderator_user_id_fkey to community_moderator_person_id_fkey;
+ALTER TABLE community_moderator RENAME COLUMN user_id TO person_id;
+
+ALTER TABLE community_moderator RENAME CONSTRAINT community_moderator_community_id_user_id_key TO community_moderator_community_id_person_id_key;
+
+ALTER TABLE community_moderator RENAME CONSTRAINT community_moderator_user_id_fkey TO community_moderator_person_id_fkey;
 
 -- community_user_ban
-alter table community_user_ban rename to community_person_ban;
-alter sequence community_user_ban_id_seq rename to community_person_ban_id_seq;
-alter table community_person_ban rename column user_id to person_id;
-alter table community_person_ban rename constraint community_user_ban_pkey to community_person_ban_pkey; 
-alter table community_person_ban rename constraint community_user_ban_community_id_fkey to community_person_ban_community_id_fkey;
-alter table community_person_ban rename constraint community_user_ban_community_id_user_id_key to community_person_ban_community_id_person_id_key;
-alter table community_person_ban rename constraint community_user_ban_user_id_fkey to community_person_ban_person_id_fkey;
+ALTER TABLE community_user_ban RENAME TO community_person_ban;
+
+ALTER SEQUENCE community_user_ban_id_seq
+    RENAME TO community_person_ban_id_seq;
+
+ALTER TABLE community_person_ban RENAME COLUMN user_id TO person_id;
+
+ALTER TABLE community_person_ban RENAME CONSTRAINT community_user_ban_pkey TO community_person_ban_pkey;
+
+ALTER TABLE community_person_ban RENAME CONSTRAINT community_user_ban_community_id_fkey TO community_person_ban_community_id_fkey;
+
+ALTER TABLE community_person_ban RENAME CONSTRAINT community_user_ban_community_id_user_id_key TO community_person_ban_community_id_person_id_key;
+
+ALTER TABLE community_person_ban RENAME CONSTRAINT community_user_ban_user_id_fkey TO community_person_ban_person_id_fkey;
 
 -- mod_add
-alter table mod_add rename column mod_user_id to mod_person_id; 
-alter table mod_add rename column other_user_id to other_person_id; 
-alter table mod_add rename constraint mod_add_mod_user_id_fkey to mod_add_mod_person_id_fkey;
-alter table mod_add rename constraint mod_add_other_user_id_fkey to mod_add_other_person_id_fkey;
+ALTER TABLE mod_add RENAME COLUMN mod_user_id TO mod_person_id;
+
+ALTER TABLE mod_add RENAME COLUMN other_user_id TO other_person_id;
+
+ALTER TABLE mod_add RENAME CONSTRAINT mod_add_mod_user_id_fkey TO mod_add_mod_person_id_fkey;
+
+ALTER TABLE mod_add RENAME CONSTRAINT mod_add_other_user_id_fkey TO mod_add_other_person_id_fkey;
 
 -- mod_add_community
-alter table mod_add_community rename column mod_user_id to mod_person_id; 
-alter table mod_add_community rename column other_user_id to other_person_id; 
-alter table mod_add_community rename constraint mod_add_community_mod_user_id_fkey to mod_add_community_mod_person_id_fkey;
-alter table mod_add_community rename constraint mod_add_community_other_user_id_fkey to mod_add_community_other_person_id_fkey;
+ALTER TABLE mod_add_community RENAME COLUMN mod_user_id TO mod_person_id;
+
+ALTER TABLE mod_add_community RENAME COLUMN other_user_id TO other_person_id;
+
+ALTER TABLE mod_add_community RENAME CONSTRAINT mod_add_community_mod_user_id_fkey TO mod_add_community_mod_person_id_fkey;
+
+ALTER TABLE mod_add_community RENAME CONSTRAINT mod_add_community_other_user_id_fkey TO mod_add_community_other_person_id_fkey;
 
 -- mod_ban
-alter table mod_ban rename column mod_user_id to mod_person_id; 
-alter table mod_ban rename column other_user_id to other_person_id; 
-alter table mod_ban rename constraint mod_ban_mod_user_id_fkey to mod_ban_mod_person_id_fkey;
-alter table mod_ban rename constraint mod_ban_other_user_id_fkey to mod_ban_other_person_id_fkey;
+ALTER TABLE mod_ban RENAME COLUMN mod_user_id TO mod_person_id;
+
+ALTER TABLE mod_ban RENAME COLUMN other_user_id TO other_person_id;
+
+ALTER TABLE mod_ban RENAME CONSTRAINT mod_ban_mod_user_id_fkey TO mod_ban_mod_person_id_fkey;
+
+ALTER TABLE mod_ban RENAME CONSTRAINT mod_ban_other_user_id_fkey TO mod_ban_other_person_id_fkey;
 
 -- mod_ban_community
-alter table mod_ban_from_community rename column mod_user_id to mod_person_id; 
-alter table mod_ban_from_community rename column other_user_id to other_person_id; 
-alter table mod_ban_from_community rename constraint mod_ban_from_community_mod_user_id_fkey to mod_ban_from_community_mod_person_id_fkey;
-alter table mod_ban_from_community rename constraint mod_ban_from_community_other_user_id_fkey to mod_ban_from_community_other_person_id_fkey;
+ALTER TABLE mod_ban_from_community RENAME COLUMN mod_user_id TO mod_person_id;
+
+ALTER TABLE mod_ban_from_community RENAME COLUMN other_user_id TO other_person_id;
+
+ALTER TABLE mod_ban_from_community RENAME CONSTRAINT mod_ban_from_community_mod_user_id_fkey TO mod_ban_from_community_mod_person_id_fkey;
+
+ALTER TABLE mod_ban_from_community RENAME CONSTRAINT mod_ban_from_community_other_user_id_fkey TO mod_ban_from_community_other_person_id_fkey;
 
 -- mod_lock_post
-alter table mod_lock_post rename column mod_user_id to mod_person_id; 
-alter table mod_lock_post rename constraint mod_lock_post_mod_user_id_fkey to mod_lock_post_mod_person_id_fkey;
+ALTER TABLE mod_lock_post RENAME COLUMN mod_user_id TO mod_person_id;
+
+ALTER TABLE mod_lock_post RENAME CONSTRAINT mod_lock_post_mod_user_id_fkey TO mod_lock_post_mod_person_id_fkey;
 
 -- mod_remove_comment
-alter table mod_remove_comment rename column mod_user_id to mod_person_id; 
-alter table mod_remove_comment rename constraint mod_remove_comment_mod_user_id_fkey to mod_remove_comment_mod_person_id_fkey;
+ALTER TABLE mod_remove_comment RENAME COLUMN mod_user_id TO mod_person_id;
+
+ALTER TABLE mod_remove_comment RENAME CONSTRAINT mod_remove_comment_mod_user_id_fkey TO mod_remove_comment_mod_person_id_fkey;
 
 -- mod_remove_community
-alter table mod_remove_community rename column mod_user_id to mod_person_id; 
-alter table mod_remove_community rename constraint mod_remove_community_mod_user_id_fkey to mod_remove_community_mod_person_id_fkey;
+ALTER TABLE mod_remove_community RENAME COLUMN mod_user_id TO mod_person_id;
+
+ALTER TABLE mod_remove_community RENAME CONSTRAINT mod_remove_community_mod_user_id_fkey TO mod_remove_community_mod_person_id_fkey;
 
 -- mod_remove_post
-alter table mod_remove_post rename column mod_user_id to mod_person_id; 
-alter table mod_remove_post rename constraint mod_remove_post_mod_user_id_fkey to mod_remove_post_mod_person_id_fkey;
+ALTER TABLE mod_remove_post RENAME COLUMN mod_user_id TO mod_person_id;
+
+ALTER TABLE mod_remove_post RENAME CONSTRAINT mod_remove_post_mod_user_id_fkey TO mod_remove_post_mod_person_id_fkey;
 
 -- mod_sticky_post
-alter table mod_sticky_post rename column mod_user_id to mod_person_id; 
-alter table mod_sticky_post rename constraint mod_sticky_post_mod_user_id_fkey to mod_sticky_post_mod_person_id_fkey;
+ALTER TABLE mod_sticky_post RENAME COLUMN mod_user_id TO mod_person_id;
+
+ALTER TABLE mod_sticky_post RENAME CONSTRAINT mod_sticky_post_mod_user_id_fkey TO mod_sticky_post_mod_person_id_fkey;
 
 -- password_reset_request
-delete from password_reset_request;
-alter table password_reset_request drop column user_id;
-alter table password_reset_request add column local_user_id integer not null references local_user(id) on update cascade on delete cascade;
+DELETE FROM password_reset_request;
+
+ALTER TABLE password_reset_request
+    DROP COLUMN user_id;
+
+ALTER TABLE password_reset_request
+    ADD COLUMN local_user_id integer NOT NULL REFERENCES local_user (id) ON UPDATE CASCADE ON DELETE CASCADE;
 
 -- post_like
-alter table post_like rename column user_id to person_id;
-alter index idx_post_like_user rename to idx_post_like_person;
-alter table post_like rename constraint post_like_post_id_user_id_key to post_like_post_id_person_id_key;
-alter table post_like rename constraint post_like_user_id_fkey to post_like_person_id_fkey;
+ALTER TABLE post_like RENAME COLUMN user_id TO person_id;
+
+ALTER INDEX idx_post_like_user RENAME TO idx_post_like_person;
+
+ALTER TABLE post_like RENAME CONSTRAINT post_like_post_id_user_id_key TO post_like_post_id_person_id_key;
+
+ALTER TABLE post_like RENAME CONSTRAINT post_like_user_id_fkey TO post_like_person_id_fkey;
 
 -- post_read
-alter table post_read rename column user_id to person_id;
-alter table post_read rename constraint post_read_post_id_user_id_key to post_read_post_id_person_id_key;
-alter table post_read rename constraint post_read_user_id_fkey to post_read_person_id_fkey;
+ALTER TABLE post_read RENAME COLUMN user_id TO person_id;
+
+ALTER TABLE post_read RENAME CONSTRAINT post_read_post_id_user_id_key TO post_read_post_id_person_id_key;
+
+ALTER TABLE post_read RENAME CONSTRAINT post_read_user_id_fkey TO post_read_person_id_fkey;
 
 -- post_saved
-alter table post_saved rename column user_id to person_id;
-alter table post_saved rename constraint post_saved_post_id_user_id_key to post_saved_post_id_person_id_key;
-alter table post_saved rename constraint post_saved_user_id_fkey to post_saved_person_id_fkey;
+ALTER TABLE post_saved RENAME COLUMN user_id TO person_id;
+
+ALTER TABLE post_saved RENAME CONSTRAINT post_saved_post_id_user_id_key TO post_saved_post_id_person_id_key;
+
+ALTER TABLE post_saved RENAME CONSTRAINT post_saved_user_id_fkey TO post_saved_person_id_fkey;
 
 -- redo site aggregates trigger
-create or replace function site_aggregates_activity(i text) returns integer
-    language plpgsql
-    as $$
-declare
-   count_ integer;
-begin
-  select count(*)
-  into count_
-  from (
-    select c.creator_id from comment c
-    inner join person u on c.creator_id = u.id
-    where c.published > ('now'::timestamp - i::interval) 
-    and u.local = true
-    union
-    select p.creator_id from post p
-    inner join person u on p.creator_id = u.id
-    where p.published > ('now'::timestamp - i::interval)
-    and u.local = true
-  ) a;
-  return count_;
-end;
+CREATE OR REPLACE FUNCTION site_aggregates_activity (i text)
+    RETURNS integer
+    LANGUAGE plpgsql
+    AS $$
+DECLARE
+    count_ integer;
+BEGIN
+    SELECT
+        count(*) INTO count_
+    FROM (
+        SELECT
+            c.creator_id
+        FROM
+            comment c
+            INNER JOIN person u ON c.creator_id = u.id
+        WHERE
+            c.published > ('now'::timestamp - i::interval)
+            AND u.local = TRUE
+        UNION
+        SELECT
+            p.creator_id
+        FROM
+            post p
+            INNER JOIN person u ON p.creator_id = u.id
+        WHERE
+            p.published > ('now'::timestamp - i::interval)
+            AND u.local = TRUE) a;
+    RETURN count_;
+END;
 $$;
+
index 657bfb3d43b833945ff0fba792465a1651751cd0..d59419bfb64741a281dc3176195d9cc60fab799b 100644 (file)
@@ -1 +1,3 @@
-alter table local_user drop column validator_time;
+ALTER TABLE local_user
+    DROP COLUMN validator_time;
+
index 4fbb5eb9162795f3f7afd061df25c218ac7a9600..3f3ac07af642574e968383a3fcda713c8d557627 100644 (file)
@@ -1 +1,3 @@
-alter table local_user add column validator_time timestamp not null default now();
+ALTER TABLE local_user
+    ADD COLUMN validator_time timestamp NOT NULL DEFAULT now();
+
index 990d8fe97777435a8ed4ae050843d49b875aa66a..548dc9a0efeb8a4b0222105b85bdcd15b3f375c8 100644 (file)
@@ -1,17 +1,37 @@
-alter table local_user add column matrix_user_id text;
-alter table local_user add column admin boolean default false not null;
+ALTER TABLE local_user
+    ADD COLUMN matrix_user_id text;
 
-update local_user lu
-set 
-  matrix_user_id = p.matrix_user_id,
-  admin = p.admin
-from person p
-where p.id = lu.person_id;
+ALTER TABLE local_user
+    ADD COLUMN admin boolean DEFAULT FALSE NOT NULL;
 
-drop view person_alias_1, person_alias_2;
-alter table person drop column matrix_user_id;
-alter table person drop column admin;
+UPDATE
+    local_user lu
+SET
+    matrix_user_id = p.matrix_user_id,
+    admin = p.admin
+FROM
+    person p
+WHERE
+    p.id = lu.person_id;
+
+DROP VIEW person_alias_1, person_alias_2;
+
+ALTER TABLE person
+    DROP COLUMN matrix_user_id;
+
+ALTER TABLE person
+    DROP COLUMN admin;
 
 -- Regenerate the person_alias views
-create view person_alias_1 as select * from person;
-create view person_alias_2 as select * from person;
+CREATE VIEW person_alias_1 AS
+SELECT
+    *
+FROM
+    person;
+
+CREATE VIEW person_alias_2 AS
+SELECT
+    *
+FROM
+    person;
+
index d9ba3dd9c2097abae78c5fbd6df58909c653f3c6..7c7c1fb4f6aa369456383ec4920be545e3a65c92 100644 (file)
@@ -1,17 +1,37 @@
-alter table person add column matrix_user_id text;
-alter table person add column admin boolean default false not null;
+ALTER TABLE person
+    ADD COLUMN matrix_user_id text;
 
-update person p
-set 
-  matrix_user_id = lu.matrix_user_id,
-  admin = lu.admin
-from local_user lu
-where p.id = lu.person_id;
+ALTER TABLE person
+    ADD COLUMN admin boolean DEFAULT FALSE NOT NULL;
 
-alter table local_user drop column matrix_user_id;
-alter table local_user drop column admin;
+UPDATE
+    person p
+SET
+    matrix_user_id = lu.matrix_user_id,
+    admin = lu.admin
+FROM
+    local_user lu
+WHERE
+    p.id = lu.person_id;
+
+ALTER TABLE local_user
+    DROP COLUMN matrix_user_id;
+
+ALTER TABLE local_user
+    DROP COLUMN admin;
 
 -- Regenerate the person_alias views
-drop view person_alias_1, person_alias_2;
-create view person_alias_1 as select * from person;
-create view person_alias_2 as select * from person;
+DROP VIEW person_alias_1, person_alias_2;
+
+CREATE VIEW person_alias_1 AS
+SELECT
+    *
+FROM
+    person;
+
+CREATE VIEW person_alias_2 AS
+SELECT
+    *
+FROM
+    person;
+
index 9d35b56383e8f92ba980bdbd6e0a49a61355a80b..20fc01c4ec4de067e41c01fbf8d69b01c6c49c2d 100644 (file)
@@ -1 +1,3 @@
-alter table local_user drop column show_scores;
+ALTER TABLE local_user
+    DROP COLUMN show_scores;
+
index 7960886a398bfa2ce8ab0882bab08a4fd09171ad..94ca9bbd278cedf8eba66bfa35251d80cd60efca 100644 (file)
@@ -1 +1,3 @@
-alter table local_user add column show_scores boolean default true not null;
+ALTER TABLE local_user
+    ADD COLUMN show_scores boolean DEFAULT TRUE NOT NULL;
+
index 4f407ce862c77a9cedfa9636b9c27d167e6d11d7..d4d0ef047b613eac3d75104a8dd4d82d98257ae1 100644 (file)
@@ -1,6 +1,20 @@
-drop view person_alias_1, person_alias_2;
-alter table person drop column bot_account;
-create view person_alias_1 as select * from person;
-create view person_alias_2 as select * from person;
+DROP VIEW person_alias_1, person_alias_2;
+
+ALTER TABLE person
+    DROP COLUMN bot_account;
+
+CREATE VIEW person_alias_1 AS
+SELECT
+    *
+FROM
+    person;
+
+CREATE VIEW person_alias_2 AS
+SELECT
+    *
+FROM
+    person;
+
+ALTER TABLE local_user
+    DROP COLUMN show_bot_accounts;
 
-alter table local_user drop column show_bot_accounts;
index a1e207a1d6a645cc1f7d262ace96eb252202beb8..7fc62811a593a507de0a2e1f5ca3fae9cf0953c3 100644 (file)
@@ -1,8 +1,22 @@
 -- Add the bot_account column to the person table
-drop view person_alias_1, person_alias_2;
-alter table person add column bot_account boolean not null default false;
-create view person_alias_1 as select * from person;
-create view person_alias_2 as select * from person;
+DROP VIEW person_alias_1, person_alias_2;
+
+ALTER TABLE person
+    ADD COLUMN bot_account boolean NOT NULL DEFAULT FALSE;
+
+CREATE VIEW person_alias_1 AS
+SELECT
+    *
+FROM
+    person;
+
+CREATE VIEW person_alias_2 AS
+SELECT
+    *
+FROM
+    person;
 
 -- Add the show_bot_accounts to the local user table as a setting
-alter table local_user add column show_bot_accounts boolean not null default true;
+ALTER TABLE local_user
+    ADD COLUMN show_bot_accounts boolean NOT NULL DEFAULT TRUE;
+
index 0035ee77002eeed266c6f248bcb1abdc9922f148..a1eab23deafddbfc9e452cddffdda3ce104fd64a 100644 (file)
@@ -1,2 +1,5 @@
-alter table site drop column description;
-alter table site rename column sidebar to description;
+ALTER TABLE site
+    DROP COLUMN description;
+
+ALTER TABLE site RENAME COLUMN sidebar TO description;
+
index 09dc36a7df967382a4186bd3a71dde8b108ecc83..14d63c880855712424ecbda6d975a677209628d7 100644 (file)
@@ -1,5 +1,7 @@
 -- Renaming description to sidebar
-alter table site rename column description to sidebar;
+ALTER TABLE site RENAME COLUMN description TO sidebar;
 
 -- Adding a short description column
-alter table site add column description varchar(150);
+ALTER TABLE site
+    ADD COLUMN description varchar(150);
+
index 844c02d38b587895c71ccea8a3f7e7822bbc05c4..112dc5b4c42a878764316a5188dde3bc151a4862 100644 (file)
@@ -1,6 +1,17 @@
-alter table person rename display_name to preferred_username;
+ALTER TABLE person RENAME display_name TO preferred_username;
 
 -- Regenerate the person_alias views
-drop view person_alias_1, person_alias_2;
-create view person_alias_1 as select * from person;
-create view person_alias_2 as select * from person;
+DROP VIEW person_alias_1, person_alias_2;
+
+CREATE VIEW person_alias_1 AS
+SELECT
+    *
+FROM
+    person;
+
+CREATE VIEW person_alias_2 AS
+SELECT
+    *
+FROM
+    person;
+
index f4b9729c11848650db9d0b00e5ebd387ec195d84..17110d329a665b64a2abaf397f307a395fcde988 100644 (file)
@@ -1,6 +1,17 @@
-alter table person rename preferred_username to display_name;
+ALTER TABLE person RENAME preferred_username TO display_name;
 
 -- Regenerate the person_alias views
-drop view person_alias_1, person_alias_2;
-create view person_alias_1 as select * from person;
-create view person_alias_2 as select * from person;
+DROP VIEW person_alias_1, person_alias_2;
+
+CREATE VIEW person_alias_1 AS
+SELECT
+    *
+FROM
+    person;
+
+CREATE VIEW person_alias_2 AS
+SELECT
+    *
+FROM
+    person;
+
index 9748bc17fb2132f0508a635626febdb255cb7dea..ba8cf93ecae7f5200eaafc93cbb55adbcfd0e693 100644 (file)
@@ -1 +1,2 @@
-drop index idx_community_aggregates_users_active_month;
+DROP INDEX idx_community_aggregates_users_active_month;
+
index 4ae90e0aba7533a9a4ceebb2fd686ea73c4029b0..576912507005fc5d622fe51b4f3a7ffab9eee264 100644 (file)
@@ -1,2 +1,2 @@
-create index idx_community_aggregates_users_active_month on community_aggregates (users_active_month desc);
+CREATE INDEX idx_community_aggregates_users_active_month ON community_aggregates (users_active_month DESC);
 
index d015f252b36af00cf62a6d759ec9c749ce8f54c0..dc94ae3610a8db8c40880f699546179d52b4337f 100644 (file)
@@ -1,24 +1,26 @@
-
 --  Add the column back
-alter table community add column creator_id int references person on update cascade on delete cascade;
+ALTER TABLE community
+    ADD COLUMN creator_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE;
 
 -- Recreate the index
-create index idx_community_creator on community (creator_id);
+CREATE INDEX idx_community_creator ON community (creator_id);
 
 -- Add the data, selecting the highest mod
-update community
-set creator_id = sub.person_id
-from (
-  select 
-  cm.community_id,
-  cm.person_id
-  from 
-  community_moderator cm
-  limit 1
-) as sub
-where id = sub.community_id;
+UPDATE
+    community
+SET
+    creator_id = sub.person_id
+FROM (
+    SELECT
+        cm.community_id,
+        cm.person_id
+    FROM
+        community_moderator cm
+    LIMIT 1) AS sub
+WHERE
+    id = sub.community_id;
 
 -- Set to not null
-alter table community alter column creator_id set not null;
-
+ALTER TABLE community
+    ALTER COLUMN creator_id SET NOT NULL;
 
index c3415c6abc4f5dde4f70a358fdb9e1b1d4a2bb45..cd41f86c589d41eba4f8e3073e524e0eddb1618e 100644 (file)
@@ -1,2 +1,4 @@
 -- Drop the column
-alter table community drop column creator_id;
+ALTER TABLE community
+    DROP COLUMN creator_id;
+
index a04036f4fe3ec0bab8c18acd69282bb9e6498b4a..a1475fbd74c155872ac9ad0c20398098e730de93 100644 (file)
@@ -1 +1,3 @@
-ALTER TABLE site DROP COLUMN community_creation_admin_only;
+ALTER TABLE site
+    DROP COLUMN community_creation_admin_only;
+
index f302f88af83fb125ecbbffcf491b67167f79100e..44d5be699e2afccd07e694b6efe139d7b7bef35a 100644 (file)
@@ -1 +1,3 @@
-ALTER TABLE site ADD COLUMN community_creation_admin_only bool NOT NULL DEFAULT false;
+ALTER TABLE site
+    ADD COLUMN community_creation_admin_only bool NOT NULL DEFAULT FALSE;
+
index 2aa275147bac4a041d13392b8b428e888d22fa00..040bbeb5c22b5f563df325acbd23924a777d8c1d 100644 (file)
@@ -1 +1,3 @@
-alter table local_user drop column show_read_posts;
+ALTER TABLE local_user
+    DROP COLUMN show_read_posts;
+
index 74b5398c359c4d0540a78fd6565407bdcc7bf7c3..a5c26245b1cc8951a01d485d4ea8b721c8eab961 100644 (file)
@@ -1 +1,3 @@
-alter table local_user add column show_read_posts boolean default true not null;
+ALTER TABLE local_user
+    ADD COLUMN show_read_posts boolean DEFAULT TRUE NOT NULL;
+
index 197c36ebc43f3ab1c7124fcc7f51fe0fdc2d1e19..60cf51064cf6db53b0a050e57f3f3c0ad93ab907 100644 (file)
@@ -1 +1,3 @@
-alter table local_user drop column show_new_post_notifs;
+ALTER TABLE local_user
+    DROP COLUMN show_new_post_notifs;
+
index 7c6960b44f5df2d71f939e4b7a0f96322b2c6878..de5c6a7292418eb97e24288184d22e7c59bfdd9b 100644 (file)
@@ -1 +1,3 @@
-alter table local_user add column show_new_post_notifs boolean default false not null;
+ALTER TABLE local_user
+    ADD COLUMN show_new_post_notifs boolean DEFAULT FALSE NOT NULL;
+
index 76cec4c91b4a1f95b04c1e94d899fa366edb26d2..c8436ad897d67026cdf3edc395522089d475dbff 100644 (file)
@@ -1,10 +1,28 @@
 DROP VIEW person_alias_1;
+
 DROP VIEW person_alias_2;
 
-ALTER TABLE community ALTER COLUMN name TYPE varchar(20);
-ALTER TABLE community ALTER COLUMN title TYPE varchar(100);
-ALTER TABLE person ALTER COLUMN name TYPE varchar(20);
-ALTER TABLE person ALTER COLUMN display_name TYPE varchar(20);
+ALTER TABLE community
+    ALTER COLUMN name TYPE varchar(20);
+
+ALTER TABLE community
+    ALTER COLUMN title TYPE varchar(100);
+
+ALTER TABLE person
+    ALTER COLUMN name TYPE varchar(20);
+
+ALTER TABLE person
+    ALTER COLUMN display_name TYPE varchar(20);
+
+CREATE VIEW person_alias_1 AS
+SELECT
+    *
+FROM
+    person;
+
+CREATE VIEW person_alias_2 AS
+SELECT
+    *
+FROM
+    person;
 
-create view person_alias_1 as select * from person;
-create view person_alias_2 as select * from person;
index 2e7bc9dfb2dfd834bbfcab450a4f942a55feeff1..2db7c3b46c67f0cbafa53e1863e8bb697f1c6a7e 100644 (file)
@@ -1,10 +1,28 @@
 DROP VIEW person_alias_1;
+
 DROP VIEW person_alias_2;
 
-ALTER TABLE community ALTER COLUMN name TYPE varchar(255);
-ALTER TABLE community ALTER COLUMN title TYPE varchar(255);
-ALTER TABLE person ALTER COLUMN name TYPE varchar(255);
-ALTER TABLE person ALTER COLUMN display_name TYPE varchar(255);
+ALTER TABLE community
+    ALTER COLUMN name TYPE varchar(255);
+
+ALTER TABLE community
+    ALTER COLUMN title TYPE varchar(255);
+
+ALTER TABLE person
+    ALTER COLUMN name TYPE varchar(255);
+
+ALTER TABLE person
+    ALTER COLUMN display_name TYPE varchar(255);
+
+CREATE VIEW person_alias_1 AS
+SELECT
+    *
+FROM
+    person;
+
+CREATE VIEW person_alias_2 AS
+SELECT
+    *
+FROM
+    person;
 
-create view person_alias_1 as select * from person;
-create view person_alias_2 as select * from person;
index 3a895f70b5ba5374305ece6bcce8d9d69c7d5c48..79578462f1347ae876d4ad5b24800bf6b51cf6cd 100644 (file)
@@ -1,28 +1,41 @@
-drop trigger post_aggregates_comment_set_deleted on comment;
-drop function post_aggregates_comment_deleted;
+DROP TRIGGER post_aggregates_comment_set_deleted ON comment;
 
-create or replace function post_aggregates_comment_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update post_aggregates pa
-    set comments = comments + 1,
-    newest_comment_time = NEW.published
-    where pa.post_id = NEW.post_id;
+DROP FUNCTION post_aggregates_comment_deleted;
+
+CREATE OR REPLACE FUNCTION post_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments + 1,
+            newest_comment_time = NEW.published
+        WHERE
+            pa.post_id = NEW.post_id;
+        -- A 2 day necro-bump limit
+        UPDATE
+            post_aggregates pa
+        SET
+            newest_comment_time_necro = NEW.published
+        WHERE
+            pa.post_id = NEW.post_id
+            AND published > ('now'::timestamp - '2 days'::interval);
+    ELSIF (TG_OP = 'DELETE') THEN
+        -- Join to post because that post may not exist anymore
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments - 1
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = OLD.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-    -- A 2 day necro-bump limit
-    update post_aggregates pa
-    set newest_comment_time_necro = NEW.published
-    where pa.post_id = NEW.post_id
-    and published > ('now'::timestamp - '2 days'::interval);
-  ELSIF (TG_OP = 'DELETE') THEN
-    -- Join to post because that post may not exist anymore
-    update post_aggregates pa
-    set comments = comments - 1
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = OLD.post_id;
-  END IF;
-  return null;
-end $$;
index b65f6008941e7c5b43570563df1f99305335bad6..81edf344b518a856fb8fb6ab21515cb396a2b7d3 100644 (file)
@@ -1,61 +1,84 @@
 -- Creating a new trigger for when comment.deleted is updated
+CREATE OR REPLACE FUNCTION post_aggregates_comment_deleted ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF NEW.deleted = TRUE THEN
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments - 1
+        WHERE
+            pa.post_id = NEW.post_id;
+    ELSE
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments + 1
+        WHERE
+            pa.post_id = NEW.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create or replace function post_aggregates_comment_deleted()
-returns trigger language plpgsql
-as $$
-begin
-  IF NEW.deleted = TRUE THEN
-    update post_aggregates pa
-    set comments = comments - 1
-    where pa.post_id = NEW.post_id;
-  ELSE 
-    update post_aggregates pa
-    set comments = comments + 1
-    where pa.post_id = NEW.post_id;
-  END IF;
-  return null;
-end $$;
-
-create trigger post_aggregates_comment_set_deleted 
-after update of deleted on comment
-for each row
-execute procedure post_aggregates_comment_deleted();
+CREATE TRIGGER post_aggregates_comment_set_deleted
+    AFTER UPDATE OF deleted ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE post_aggregates_comment_deleted ();
 
 -- Fix issue with being able to necro-bump your own post
-create or replace function post_aggregates_comment_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update post_aggregates pa
-    set comments = comments + 1,
-    newest_comment_time = NEW.published
-    where pa.post_id = NEW.post_id;
-
-    -- A 2 day necro-bump limit
-    update post_aggregates pa
-    set newest_comment_time_necro = NEW.published
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = NEW.post_id
-    -- Fix issue with being able to necro-bump your own post
-    and NEW.creator_id != p.creator_id
-    and pa.published > ('now'::timestamp - '2 days'::interval);
+CREATE OR REPLACE FUNCTION post_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments + 1,
+            newest_comment_time = NEW.published
+        WHERE
+            pa.post_id = NEW.post_id;
+        -- A 2 day necro-bump limit
+        UPDATE
+            post_aggregates pa
+        SET
+            newest_comment_time_necro = NEW.published
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = NEW.post_id
+            -- Fix issue with being able to necro-bump your own post
+            AND NEW.creator_id != p.creator_id
+            AND pa.published > ('now'::timestamp - '2 days'::interval);
+    ELSIF (TG_OP = 'DELETE') THEN
+        -- Join to post because that post may not exist anymore
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments - 1
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = OLD.post_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        -- Join to post because that post may not exist anymore
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments - 1
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = OLD.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-  ELSIF (TG_OP = 'DELETE') THEN
-    -- Join to post because that post may not exist anymore
-    update post_aggregates pa
-    set comments = comments - 1
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = OLD.post_id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    -- Join to post because that post may not exist anymore
-    update post_aggregates pa
-    set comments = comments - 1
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = OLD.post_id;
-  END IF;
-  return null;
-end $$;
index eb5a35e5a2a7f139f38509465ae6495ca7b92d25..d7a184494543a47db9815c7b765a2f25f0e21319 100644 (file)
@@ -1,2 +1,4 @@
-drop table person_block;
-drop table community_block;
+DROP TABLE person_block;
+
+DROP TABLE community_block;
+
index cbcadc284fb5e6c7cf0b72f83700384238e6ae33..4e31196c6fcd0833a0c75d2e7db6ccc6aa564df0 100644 (file)
@@ -1,15 +1,16 @@
-create table person_block (
-  id serial primary key,
-  person_id int references person on update cascade on delete cascade not null,
-  target_id int references person on update cascade on delete cascade not null,
-  published timestamp not null default now(),
-  unique(person_id, target_id)
+CREATE TABLE person_block (
+    id serial PRIMARY KEY,
+    person_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    target_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (person_id, target_id)
 );
 
-create table community_block (
-  id serial primary key,
-  person_id int references person on update cascade on delete cascade not null,
-  community_id int references community on update cascade on delete cascade not null,
-  published timestamp not null default now(),
-  unique(person_id, community_id)
+CREATE TABLE community_block (
+    id serial PRIMARY KEY,
+    person_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (person_id, community_id)
 );
+
index a0ec4bdd48362cc6823b956d724f0554d3cb34b3..50ddcb3a6073c3610204684334925031392475a0 100644 (file)
@@ -1,42 +1,64 @@
-create or replace function community_aggregates_activity(i text)
-returns table(count_ bigint, community_id_ integer)
-language plpgsql
-as
-$$
-begin
-  return query 
-  select count(*), community_id
-  from (
-    select c.creator_id, p.community_id from comment c
-    inner join post p on c.post_id = p.id
-    where c.published > ('now'::timestamp - i::interval)
-    union
-    select p.creator_id, p.community_id from post p
-    where p.published > ('now'::timestamp - i::interval)  
-  ) a
-  group by community_id;
-end;
+CREATE OR REPLACE FUNCTION community_aggregates_activity (i text)
+    RETURNS TABLE (
+        count_ bigint,
+        community_id_ integer)
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    RETURN query
+    SELECT
+        count(*),
+        community_id
+    FROM (
+        SELECT
+            c.creator_id,
+            p.community_id
+        FROM
+            comment c
+            INNER JOIN post p ON c.post_id = p.id
+        WHERE
+            c.published > ('now'::timestamp - i::interval)
+        UNION
+        SELECT
+            p.creator_id,
+            p.community_id
+        FROM
+            post p
+        WHERE
+            p.published > ('now'::timestamp - i::interval)) a
+GROUP BY
+    community_id;
+END;
 $$;
 
-create or replace function site_aggregates_activity(i text) returns integer
-    language plpgsql
-    as $$
-declare
-   count_ integer;
-begin
-  select count(*)
-  into count_
-  from (
-    select c.creator_id from comment c
-    inner join person u on c.creator_id = u.id
-    where c.published > ('now'::timestamp - i::interval) 
-    and u.local = true
-    union
-    select p.creator_id from post p
-    inner join person u on p.creator_id = u.id
-    where p.published > ('now'::timestamp - i::interval)
-    and u.local = true
-  ) a;
-  return count_;
-end;
+CREATE OR REPLACE FUNCTION site_aggregates_activity (i text)
+    RETURNS integer
+    LANGUAGE plpgsql
+    AS $$
+DECLARE
+    count_ integer;
+BEGIN
+    SELECT
+        count(*) INTO count_
+    FROM (
+        SELECT
+            c.creator_id
+        FROM
+            comment c
+            INNER JOIN person u ON c.creator_id = u.id
+        WHERE
+            c.published > ('now'::timestamp - i::interval)
+            AND u.local = TRUE
+        UNION
+        SELECT
+            p.creator_id
+        FROM
+            post p
+            INNER JOIN person u ON p.creator_id = u.id
+        WHERE
+            p.published > ('now'::timestamp - i::interval)
+            AND u.local = TRUE) a;
+    RETURN count_;
+END;
 $$;
+
index 2aafc369364726f455fa139d54747e211f40f08b..26a49bd871b499dc08eb8c1ea093bbd31f1af182 100644 (file)
@@ -1,52 +1,73 @@
 -- Make sure bots aren't included in aggregate counts
-
-create or replace function community_aggregates_activity(i text)
-returns table(count_ bigint, community_id_ integer)
-language plpgsql
-as
-$$
-begin
-  return query 
-  select count(*), community_id
-  from (
-    select c.creator_id, p.community_id from comment c
-    inner join post p on c.post_id = p.id
-    inner join person pe on c.creator_id = pe.id
-    where c.published > ('now'::timestamp - i::interval)
-    and pe.bot_account = false
-    union
-    select p.creator_id, p.community_id from post p
-    inner join person pe on p.creator_id = pe.id
-    where p.published > ('now'::timestamp - i::interval)  
-    and pe.bot_account = false
-  ) a
-  group by community_id;
-end;
+CREATE OR REPLACE FUNCTION community_aggregates_activity (i text)
+    RETURNS TABLE (
+        count_ bigint,
+        community_id_ integer)
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    RETURN query
+    SELECT
+        count(*),
+        community_id
+    FROM (
+        SELECT
+            c.creator_id,
+            p.community_id
+        FROM
+            comment c
+            INNER JOIN post p ON c.post_id = p.id
+            INNER JOIN person pe ON c.creator_id = pe.id
+        WHERE
+            c.published > ('now'::timestamp - i::interval)
+            AND pe.bot_account = FALSE
+        UNION
+        SELECT
+            p.creator_id,
+            p.community_id
+        FROM
+            post p
+            INNER JOIN person pe ON p.creator_id = pe.id
+        WHERE
+            p.published > ('now'::timestamp - i::interval)
+            AND pe.bot_account = FALSE) a
+GROUP BY
+    community_id;
+END;
 $$;
 
-create or replace function site_aggregates_activity(i text) returns integer
-    language plpgsql
-    as $$
-declare
-   count_ integer;
-begin
-  select count(*)
-  into count_
-  from (
-    select c.creator_id from comment c
-    inner join person u on c.creator_id = u.id
-    inner join person pe on c.creator_id = pe.id
-    where c.published > ('now'::timestamp - i::interval) 
-    and u.local = true
-    and pe.bot_account = false
-    union
-    select p.creator_id from post p
-    inner join person u on p.creator_id = u.id
-    inner join person pe on p.creator_id = pe.id
-    where p.published > ('now'::timestamp - i::interval)
-    and u.local = true
-    and pe.bot_account = false
-  ) a;
-  return count_;
-end;
+CREATE OR REPLACE FUNCTION site_aggregates_activity (i text)
+    RETURNS integer
+    LANGUAGE plpgsql
+    AS $$
+DECLARE
+    count_ integer;
+BEGIN
+    SELECT
+        count(*) INTO count_
+    FROM (
+        SELECT
+            c.creator_id
+        FROM
+            comment c
+            INNER JOIN person u ON c.creator_id = u.id
+            INNER JOIN person pe ON c.creator_id = pe.id
+        WHERE
+            c.published > ('now'::timestamp - i::interval)
+            AND u.local = TRUE
+            AND pe.bot_account = FALSE
+        UNION
+        SELECT
+            p.creator_id
+        FROM
+            post p
+            INNER JOIN person u ON p.creator_id = u.id
+            INNER JOIN person pe ON p.creator_id = pe.id
+        WHERE
+            p.published > ('now'::timestamp - i::interval)
+            AND u.local = TRUE
+            AND pe.bot_account = FALSE) a;
+    RETURN count_;
+END;
 $$;
+
index 8a2d8820af19f933db9f01ca2ffaf4fbf1a34f01..477454334882861dcf1a67dd8cae8c495035b923 100644 (file)
@@ -1 +1,2 @@
-drop table mod_transfer_community;
+DROP TABLE mod_transfer_community;
+
index b43cf14087da8fb59a4c78e8390660a5d94a6134..e023576dd279832ed8cfda07be124e2889193cbe 100644 (file)
@@ -1,9 +1,10 @@
 -- Add the mod_transfer_community log table
-create table mod_transfer_community (
-  id serial primary key,
-  mod_person_id int references person on update cascade on delete cascade not null,
-  other_person_id int references person on update cascade on delete cascade not null,
-  community_id int references community on update cascade on delete cascade not null,
-  removed boolean default false,
-  when_ timestamp not null default now()
+CREATE TABLE mod_transfer_community (
+    id serial PRIMARY KEY,
+    mod_person_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    other_person_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    removed boolean DEFAULT FALSE,
+    when_ timestamp NOT NULL DEFAULT now()
 );
+
index 61b21fbabe7472e1e6dc4181a9576d20e571d83e..89350d96a6f357c0c5da22755290f7f59e3ef7dd 100644 (file)
@@ -1 +1,2 @@
-drop table secret;
+DROP TABLE secret;
+
index e9af9ef55c974dce1143ad12021a50bee2cbe045..85797c7f329f6ee3b75381acbf592a9c85d84e8d 100644 (file)
@@ -1,9 +1,9 @@
 -- generate a jwt secret
-create extension if not exists pgcrypto;
+CREATE EXTENSION IF NOT EXISTS pgcrypto;
 
-create table secret(
-  id serial primary key,
-  jwt_secret varchar not null default gen_random_uuid()
+CREATE TABLE secret (
+    id serial PRIMARY KEY,
+    jwt_secret varchar NOT NULL DEFAULT gen_random_uuid ()
 );
 
-insert into secret default values;
+INSERT INTO secret DEFAULT VALUES;
index 054923f6bf3230aad19e22b5cf963a5a765a6930..c542fe779d4709fa638617b7f37876f8f27fb03b 100644 (file)
@@ -1,4 +1,8 @@
-drop table admin_purge_person;
-drop table admin_purge_community;
-drop table admin_purge_post;
-drop table admin_purge_comment;
+DROP TABLE admin_purge_person;
+
+DROP TABLE admin_purge_community;
+
+DROP TABLE admin_purge_post;
+
+DROP TABLE admin_purge_comment;
+
index 1eb6b3f17d287c0360311994b11231077287d5da..a67e12fe2b123962dd13603a79b59e81b3e31ed0 100644 (file)
@@ -1,31 +1,31 @@
 -- Add the admin_purge tables
-
-create table admin_purge_person (
-  id serial primary key,
-  admin_person_id int references person on update cascade on delete cascade not null,
-  reason text,
-  when_ timestamp not null default now()
+CREATE TABLE admin_purge_person (
+    id serial PRIMARY KEY,
+    admin_person_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    reason text,
+    when_ timestamp NOT NULL DEFAULT now()
 );
 
-create table admin_purge_community (
-  id serial primary key,
-  admin_person_id int references person on update cascade on delete cascade not null,
-  reason text,
-  when_ timestamp not null default now()
+CREATE TABLE admin_purge_community (
+    id serial PRIMARY KEY,
+    admin_person_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    reason text,
+    when_ timestamp NOT NULL DEFAULT now()
 );
 
-create table admin_purge_post (
-  id serial primary key,
-  admin_person_id int references person on update cascade on delete cascade not null,
-  community_id int references community on update cascade on delete cascade not null,
-  reason text,
-  when_ timestamp not null default now()
+CREATE TABLE admin_purge_post (
+    id serial PRIMARY KEY,
+    admin_person_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    reason text,
+    when_ timestamp NOT NULL DEFAULT now()
 );
 
-create table admin_purge_comment (
-  id serial primary key,
-  admin_person_id int references person on update cascade on delete cascade not null,
-  post_id int references post on update cascade on delete cascade not null,
-  reason text,
-  when_ timestamp not null default now()
+CREATE TABLE admin_purge_comment (
+    id serial PRIMARY KEY,
+    admin_person_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    reason text,
+    when_ timestamp NOT NULL DEFAULT now()
 );
+
index d2d3ad9dc92bb808b76a4fc6336516a2e354b3bf..664d6cfedaf0d845d3b1acbce4a4cead172ffe8c 100644 (file)
@@ -1,5 +1,7 @@
-alter table activity alter column ap_id drop not null;
+ALTER TABLE activity
+    ALTER COLUMN ap_id DROP NOT NULL;
 
-create unique index idx_activity_unique_apid on activity ((data ->> 'id'::text));
+CREATE UNIQUE INDEX idx_activity_unique_apid ON activity ((data ->> 'id'::text));
+
+DROP INDEX idx_activity_ap_id;
 
-drop index idx_activity_ap_id;
index 84530508e94d3f131ddbbc23d98ca64f449cc93e..6fa2cf5696131464c1504d57015d499a581da957 100644 (file)
@@ -1,22 +1,29 @@
-
 -- Delete the empty ap_ids
-delete from activity where ap_id is null;
+DELETE FROM activity
+WHERE ap_id IS NULL;
 
 -- Make it required
-alter table activity alter column ap_id set not null;
+ALTER TABLE activity
+    ALTER COLUMN ap_id SET NOT NULL;
 
 -- Delete dupes, keeping the first one
-delete from activity a using (
-  select min(id) as id, ap_id
-  from activity
-  group by ap_id having count(*) > 1
-) b
-where a.ap_id = b.ap_id 
-and a.id <> b.id;
+DELETE FROM activity a USING (
+    SELECT
+        min(id) AS id,
+        ap_id
+    FROM
+        activity
+    GROUP BY
+        ap_id
+    HAVING
+        count(*) > 1) b
+WHERE
+    a.ap_id = b.ap_id
+    AND a.id <> b.id;
 
 -- The index
-create unique index idx_activity_ap_id on activity(ap_id);
+CREATE UNIQUE INDEX idx_activity_ap_id ON activity (ap_id);
 
 -- Drop the old index
-drop index idx_activity_unique_apid;
+DROP INDEX idx_activity_unique_apid;
 
index 775d07d3c91e19fa366929c9e6582c943b233b6f..a6101d0a7ee462538190fb20f3a28a38d543785c 100644 (file)
@@ -1,2 +1,6 @@
-alter table community alter column public_key drop not null;
-alter table person alter column public_key drop not null;
+ALTER TABLE community
+    ALTER COLUMN public_key DROP NOT NULL;
+
+ALTER TABLE person
+    ALTER COLUMN public_key DROP NOT NULL;
+
index b2882561c9883be1ae1040e0ea58f4497bada46e..7776f88c8a8a6ce1010e54aac163ecdda49bf894 100644 (file)
@@ -1,7 +1,14 @@
 -- Delete the empty public keys
-delete from community where public_key is null;
-delete from person where public_key is null;
+DELETE FROM community
+WHERE public_key IS NULL;
+
+DELETE FROM person
+WHERE public_key IS NULL;
 
 -- Make it required
-alter table community alter column public_key set not null;
-alter table person alter column public_key set not null;
+ALTER TABLE community
+    ALTER COLUMN public_key SET NOT NULL;
+
+ALTER TABLE person
+    ALTER COLUMN public_key SET NOT NULL;
+
index b5e9b51ca278cb0987c6698db740168562be38a0..33b4fecf8ea3ecef35d8120397be89981c19b9d4 100644 (file)
@@ -1,2 +1,4 @@
-drop index idx_comment_report_published;
-drop index idx_post_report_published;
+DROP INDEX idx_comment_report_published;
+
+DROP INDEX idx_post_report_published;
+
index eb0c1220cc7ff789b82e5eab4adc241542a7e7b3..33492419ecd2655745938051b7d5402239bfad13 100644 (file)
@@ -1,2 +1,4 @@
-create index idx_comment_report_published on comment_report (published desc);
-create index idx_post_report_published on post_report (published desc);
+CREATE INDEX idx_comment_report_published ON comment_report (published DESC);
+
+CREATE INDEX idx_post_report_published ON post_report (published DESC);
+
index cd1eb7c295aa183769bf3f50ab7dbc1595d58f94..0579873daa4750a4c33782b8eb1cacf4bde338c1 100644 (file)
@@ -1,8 +1,16 @@
 -- revert defaults from db for local user init
-alter table local_user alter column theme set default 'darkly';
-alter table local_user alter column default_listing_type set default 1;
+ALTER TABLE local_user
+    ALTER COLUMN theme SET DEFAULT 'darkly';
+
+ALTER TABLE local_user
+    ALTER COLUMN default_listing_type SET DEFAULT 1;
 
 -- remove tables and columns for optional email verification
-alter table site drop column require_email_verification;
-alter table local_user drop column email_verified;
-drop table email_verification;
+ALTER TABLE site
+    DROP COLUMN require_email_verification;
+
+ALTER TABLE local_user
+    DROP COLUMN email_verified;
+
+DROP TABLE email_verification;
+
index 29a20e00cc5f78ec82986882a8fbc4d31abfe7dd..3b14b2a3f65a1ec0808da6788c41fbc00d80364f 100644 (file)
@@ -1,14 +1,21 @@
 -- use defaults from db for local user init
-alter table local_user alter column theme set default 'browser';
-alter table local_user alter column default_listing_type set default 2;
+ALTER TABLE local_user
+    ALTER COLUMN theme SET DEFAULT 'browser';
+
+ALTER TABLE local_user
+    ALTER COLUMN default_listing_type SET DEFAULT 2;
 
 -- add tables and columns for optional email verification
-alter table site add column require_email_verification boolean not null default false;
-alter table local_user add column email_verified boolean not null default false;
-
-create table email_verification (
-    id serial primary key,
-    local_user_id int references local_user(id) on update cascade on delete cascade not null,
-    email text not null,
-    verification_token text not null
+ALTER TABLE site
+    ADD COLUMN require_email_verification boolean NOT NULL DEFAULT FALSE;
+
+ALTER TABLE local_user
+    ADD COLUMN email_verified boolean NOT NULL DEFAULT FALSE;
+
+CREATE TABLE email_verification (
+    id serial PRIMARY KEY,
+    local_user_id int REFERENCES local_user (id) ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    email text NOT NULL,
+    verification_token text NOT NULL
 );
+
index 52a1a280803eaa6217ecc57b6e40aa57777b5707..22af54bf8ff955c95798275db7764dde1a5189bf 100644 (file)
@@ -1,9 +1,16 @@
 -- Add columns to site table
-alter table site drop column require_application;
-alter table site drop column application_question;
-alter table site drop column private_instance;
+ALTER TABLE site
+    DROP COLUMN require_application;
+
+ALTER TABLE site
+    DROP COLUMN application_question;
+
+ALTER TABLE site
+    DROP COLUMN private_instance;
 
 -- Add pending to local_user
-alter table local_user drop column accepted_application;
+ALTER TABLE local_user
+    DROP COLUMN accepted_application;
+
+DROP TABLE registration_application;
 
-drop table registration_application;
index b3f8a18dcfee9954874fbe492c4acbd66e3b60ab..7e611b1690fa48c61b2319a2211328c9477f4d8a 100644 (file)
@@ -1,19 +1,26 @@
 -- Add columns to site table
-alter table site add column require_application boolean not null default false;
-alter table site add column application_question text;
-alter table site add column private_instance boolean not null default false;
+ALTER TABLE site
+    ADD COLUMN require_application boolean NOT NULL DEFAULT FALSE;
+
+ALTER TABLE site
+    ADD COLUMN application_question text;
+
+ALTER TABLE site
+    ADD COLUMN private_instance boolean NOT NULL DEFAULT FALSE;
 
 -- Add pending to local_user
-alter table local_user add column accepted_application boolean not null default false;
+ALTER TABLE local_user
+    ADD COLUMN accepted_application boolean NOT NULL DEFAULT FALSE;
 
-create table registration_application (
-  id serial primary key,
-  local_user_id int references local_user on update cascade on delete cascade not null,
-  answer text not null,
-  admin_id int references person on update cascade on delete cascade,
-  deny_reason text,
-  published timestamp not null default now(),
-  unique(local_user_id)
+CREATE TABLE registration_application (
+    id serial PRIMARY KEY,
+    local_user_id int REFERENCES local_user ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    answer text NOT NULL,
+    admin_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE,
+    deny_reason text,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (local_user_id)
 );
 
-create index idx_registration_application_published on registration_application (published desc);
+CREATE INDEX idx_registration_application_published ON registration_application (published DESC);
+
index 21405db11ce4182a9c8b069d9c73723f64d54792..64aaa8e06a548785c30e92fba36a940a787fadb4 100644 (file)
@@ -1 +1,3 @@
-alter table email_verification drop column published;
+ALTER TABLE email_verification
+    DROP COLUMN published;
+
index 79dd32bfbad7327b47e0cfa4b805572292b759db..6eb47af646f4b8e48aa63f299d060a5d244e5d81 100644 (file)
@@ -1 +1,3 @@
-alter table email_verification add column published timestamp not null default now();
+ALTER TABLE email_verification
+    ADD COLUMN published timestamp NOT NULL DEFAULT now();
+
index 83a3715ec4abb6cc92124192f28992461fe08b2c..fde33ab1bce1130cdc60ad20502f2d2d36c8ee30 100644 (file)
@@ -1,7 +1,20 @@
-drop view person_alias_1, person_alias_2;
+DROP VIEW person_alias_1, person_alias_2;
 
-alter table person drop column ban_expires;
-alter table community_person_ban drop column expires;
+ALTER TABLE person
+    DROP COLUMN ban_expires;
+
+ALTER TABLE community_person_ban
+    DROP COLUMN expires;
+
+CREATE VIEW person_alias_1 AS
+SELECT
+    *
+FROM
+    person;
+
+CREATE VIEW person_alias_2 AS
+SELECT
+    *
+FROM
+    person;
 
-create view person_alias_1 as select * from person;
-create view person_alias_2 as select * from person;
index 7e6338361d84eecc2b2fe4d0439629b0bd0536e5..6b3ee246384166103531371e05d5f656146f9f35 100644 (file)
@@ -1,8 +1,21 @@
 -- Add ban_expires to person, community_person_ban
-alter table person add column ban_expires timestamp;
-alter table community_person_ban add column expires timestamp;
+ALTER TABLE person
+    ADD COLUMN ban_expires timestamp;
 
-drop view person_alias_1, person_alias_2;
-create view person_alias_1 as select * from person;
-create view person_alias_2 as select * from person;
+ALTER TABLE community_person_ban
+    ADD COLUMN expires timestamp;
+
+DROP VIEW person_alias_1, person_alias_2;
+
+CREATE VIEW person_alias_1 AS
+SELECT
+    *
+FROM
+    person;
+
+CREATE VIEW person_alias_2 AS
+SELECT
+    *
+FROM
+    person;
 
index 55a054ae3fee468d06079ae27ce74a169ff61b2f..f6e9367c77a81dfbf75a9ef7c50435503c5fd87a 100644 (file)
@@ -1,3 +1,5 @@
-alter table community drop column hidden;
+ALTER TABLE community
+    DROP COLUMN hidden;
+
+DROP TABLE mod_hide_community;
 
-drop table mod_hide_community;
index b7436616dd4b7816b3a0f28090862d7d8adc1b18..8aa7321eedfb4ec53a755cc933615a89fa025a7e 100644 (file)
@@ -1,13 +1,12 @@
-alter table community add column hidden boolean default false;
+ALTER TABLE community
+    ADD COLUMN hidden boolean DEFAULT FALSE;
 
-
-create table mod_hide_community
-(
-    id serial primary key,
-    community_id int references community on update cascade on delete cascade not null,
-    mod_person_id int references person on update cascade on delete cascade not null,
-    when_ timestamp not null default now(),
+CREATE TABLE mod_hide_community (
+    id serial PRIMARY KEY,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    mod_person_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    when_ timestamp NOT NULL DEFAULT now(),
     reason text,
-    hidden boolean default false
+    hidden boolean DEFAULT FALSE
 );
 
index 8195f719b18e483a10ed7f5cd632efa46652eee4..612a518933c60b7ecb96390c5d42f2732e7a8416 100644 (file)
@@ -1,14 +1,22 @@
 --  Add the column back
-alter table site add column creator_id int references person on update cascade on delete cascade;
+ALTER TABLE site
+    ADD COLUMN creator_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE;
 
 -- Add the data, selecting the highest admin
-update site
-set creator_id = sub.id
-from (
-  select id from person
-  where admin = true
-  limit 1
-) as sub;
+UPDATE
+    site
+SET
+    creator_id = sub.id
+FROM (
+    SELECT
+        id
+    FROM
+        person
+    WHERE
+        admin = TRUE
+    LIMIT 1) AS sub;
 
 -- Set to not null
-alter table site alter column creator_id set not null;
+ALTER TABLE site
+    ALTER COLUMN creator_id SET NOT NULL;
+
index 78774445bbb6d2b0dd32c502bd25f62c2b0db517..5f11c56c42052cbc4cfa09df7c510efdcb29356b 100644 (file)
@@ -1,2 +1,4 @@
 -- Drop the column
-alter table site drop column creator_id;
+ALTER TABLE site
+    DROP COLUMN creator_id;
+
index a258c27a469cc21a9818f04d39d941128ed11b4d..0a45515dc11b78982e62530c58ef4b6417a37005 100644 (file)
@@ -1,6 +1,7 @@
-alter table site
-    drop column actor_id,
-    drop column last_refreshed_at,
-    drop column inbox_url,
-    drop column private_key,
-    drop column public_key;
+ALTER TABLE site
+    DROP COLUMN actor_id,
+    DROP COLUMN last_refreshed_at,
+    DROP COLUMN inbox_url,
+    DROP COLUMN private_key,
+    DROP COLUMN public_key;
+
index 914ab757ea2d7fec88953921515375333573fb21..dcbb08625da87bbd3c44bbc9602ca4278794f93c 100644 (file)
@@ -1,6 +1,7 @@
-alter table site
-    add column actor_id varchar(255) not null unique default generate_unique_changeme(),
-    add column last_refreshed_at Timestamp not null default now(),
-    add column inbox_url varchar(255) not null default generate_unique_changeme(),
-    add column private_key text,
-    add column public_key text not null default generate_unique_changeme();
+ALTER TABLE site
+    ADD COLUMN actor_id varchar(255) NOT NULL UNIQUE DEFAULT generate_unique_changeme (),
+    ADD COLUMN last_refreshed_at Timestamp NOT NULL DEFAULT now(),
+    ADD COLUMN inbox_url varchar(255) NOT NULL DEFAULT generate_unique_changeme (),
+    ADD COLUMN private_key text,
+    ADD COLUMN public_key text NOT NULL DEFAULT generate_unique_changeme ();
+
index 8b927b7b3d2a9b27a42154c4fb3364ff957e7d1a..39c454ab5b8407fc5ce40657c5db176826983394 100644 (file)
@@ -1 +1,2 @@
-drop index idx_community_title;
+DROP INDEX idx_community_title;
+
index 50c267bb435d7b30088b2fed47b6aead03f519cd..4fe527181bbfccc9d328f0091674994443e94311 100644 (file)
@@ -1 +1,2 @@
-create index idx_community_title on community(title);
+CREATE INDEX idx_community_title ON community (title);
+
index fc57669a8c947465780a615c577bcafcd559c7cf..a52305af70121275243f3b48a04df7e818cbad12 100644 (file)
@@ -1 +1,3 @@
-alter table site drop column default_theme;
\ No newline at end of file
+ALTER TABLE site
+    DROP COLUMN default_theme;
+
index dd6faad48ab2ff566346d35dcaa94b671a80b0b1..f2a2822aeca7c54414ec3a7617cc6a1337aea948 100644 (file)
@@ -1 +1,3 @@
-alter table site add column default_theme text not null default 'browser';
\ No newline at end of file
+ALTER TABLE site
+    ADD COLUMN default_theme text NOT NULL DEFAULT 'browser';
+
index 8a8ea5cb2db21b87dd006043d1afa53694b7ab8b..406d459ae4cb614b73701bfe3c805b438ba6ece8 100644 (file)
-drop trigger if exists community_aggregates_post_count on post;
-drop trigger if exists community_aggregates_comment_count on comment;
-drop trigger if exists site_aggregates_comment_insert on comment;
-drop trigger if exists site_aggregates_comment_delete on comment;
-drop trigger if exists site_aggregates_post_insert on post;
-drop trigger if exists site_aggregates_post_delete on post;
-drop trigger if exists site_aggregates_community_insert on community;
-drop trigger if exists site_aggregates_community_delete on community;
-drop trigger if exists person_aggregates_post_count on post;
-drop trigger if exists person_aggregates_comment_count on comment;
-
-drop function was_removed_or_deleted(TG_OP text, OLD record, NEW record);
-drop function was_restored_or_created(TG_OP text, OLD record, NEW record);
+DROP TRIGGER IF EXISTS community_aggregates_post_count ON post;
 
--- Community aggregate functions
+DROP TRIGGER IF EXISTS community_aggregates_comment_count ON comment;
 
-create or replace function community_aggregates_post_count()
-    returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-update community_aggregates
-set posts = posts + 1 where community_id = NEW.community_id;
-ELSIF (TG_OP = 'DELETE') THEN
-update community_aggregates
-set posts = posts - 1 where community_id = OLD.community_id;
-
--- Update the counts if the post got deleted
-update community_aggregates ca
-set posts = coalesce(cd.posts, 0),
-    comments = coalesce(cd.comments, 0)
-    from ( 
-      select 
-      c.id,
-      count(distinct p.id) as posts,
-      count(distinct ct.id) as comments
-      from community c
-      left join post p on c.id = p.community_id
-      left join comment ct on p.id = ct.post_id
-      group by c.id
-    ) cd
-where ca.community_id = OLD.community_id;
-END IF;
-return null;
-end $$;
-
-create or replace function community_aggregates_comment_count()
-    returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-update community_aggregates ca
-set comments = comments + 1 from comment c, post p
-where p.id = c.post_id
-  and p.id = NEW.post_id
-  and ca.community_id = p.community_id;
-ELSIF (TG_OP = 'DELETE') THEN
-update community_aggregates ca
-set comments = comments - 1 from comment c, post p
-where p.id = c.post_id
-  and p.id = OLD.post_id
-  and ca.community_id = p.community_id;
-
-END IF;
-return null;
-end $$;
+DROP TRIGGER IF EXISTS site_aggregates_comment_insert ON comment;
 
--- Community aggregate triggers
+DROP TRIGGER IF EXISTS site_aggregates_comment_delete ON comment;
 
-create trigger community_aggregates_post_count
-    after insert or delete on post
-for each row
-execute procedure community_aggregates_post_count();
+DROP TRIGGER IF EXISTS site_aggregates_post_insert ON post;
 
-create trigger community_aggregates_comment_count
-    after insert or delete on comment
-for each row
-execute procedure community_aggregates_comment_count();
+DROP TRIGGER IF EXISTS site_aggregates_post_delete ON post;
 
--- Site aggregate functions
+DROP TRIGGER IF EXISTS site_aggregates_community_insert ON community;
 
-create or replace function site_aggregates_post_insert()
-    returns trigger language plpgsql
-as $$
-begin
-update site_aggregates
-set posts = posts + 1;
-return null;
-end $$;
-
-create or replace function site_aggregates_post_delete()
-    returns trigger language plpgsql
-as $$
-begin
-update site_aggregates sa
-set posts = posts - 1
-    from site s
-where sa.site_id = s.id;
-return null;
-end $$;
-
-create or replace function site_aggregates_comment_insert()
-    returns trigger language plpgsql
-as $$
-begin
-update site_aggregates
-set comments = comments + 1;
-return null;
-end $$;
-
-create or replace function site_aggregates_comment_delete()
-    returns trigger language plpgsql
-as $$
-begin
-update site_aggregates sa
-set comments = comments - 1
-    from site s
-where sa.site_id = s.id;
-return null;
-end $$;
-
-create or replace function site_aggregates_community_insert()
-    returns trigger language plpgsql
-as $$
-begin
-update site_aggregates
-set communities = communities + 1;
-return null;
-end $$;
-
-create or replace function site_aggregates_community_delete()
-    returns trigger language plpgsql
-as $$
-begin
-update site_aggregates sa
-set communities = communities - 1
-    from site s
-where sa.site_id = s.id;
-return null;
-end $$;
+DROP TRIGGER IF EXISTS site_aggregates_community_delete ON community;
 
+DROP TRIGGER IF EXISTS person_aggregates_post_count ON post;
 
--- Site update triggers
+DROP TRIGGER IF EXISTS person_aggregates_comment_count ON comment;
 
-create trigger site_aggregates_post_insert
-    after insert on post
-    for each row
-    when (NEW.local = true)
-    execute procedure site_aggregates_post_insert();
-
-create trigger site_aggregates_post_delete
-    after delete on post
-    for each row
-    when (OLD.local = true)
-    execute procedure site_aggregates_post_delete();
-
-create trigger site_aggregates_comment_insert
-    after insert on comment
-    for each row
-    when (NEW.local = true)
-    execute procedure site_aggregates_comment_insert();
-
-create trigger site_aggregates_comment_delete
-    after delete on comment
-    for each row
-    when (OLD.local = true)
-    execute procedure site_aggregates_comment_delete();
-
-create trigger site_aggregates_community_insert
-    after insert on community
-    for each row
-    when (NEW.local = true)
-    execute procedure site_aggregates_community_insert();
-
-create trigger site_aggregates_community_delete
-    after delete on community
-    for each row
-    when (OLD.local = true)
-    execute procedure site_aggregates_community_delete();
+DROP FUNCTION was_removed_or_deleted (TG_OP text, OLD record, NEW record);
 
--- Person aggregate functions
+DROP FUNCTION was_restored_or_created (TG_OP text, OLD record, NEW record);
 
-create or replace function person_aggregates_post_count()
-    returns trigger language plpgsql
-as $$
-begin
+-- Community aggregate functions
+CREATE OR REPLACE FUNCTION community_aggregates_post_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
     IF (TG_OP = 'INSERT') THEN
-update person_aggregates
-set post_count = post_count + 1 where person_id = NEW.creator_id;
-
-ELSIF (TG_OP = 'DELETE') THEN
-update person_aggregates
-set post_count = post_count - 1 where person_id = OLD.creator_id;
-
--- If the post gets deleted, the score calculation trigger won't fire,
--- so you need to re-calculate
-update person_aggregates ua
-set post_score = pd.score
-    from (
-                 select u.id,
-                        coalesce(0, sum(pl.score)) as score
-                        -- User join because posts could be empty
-                 from person u
-                          left join post p on u.id = p.creator_id
-                          left join post_like pl on p.id = pl.post_id
-                 group by u.id
-             ) pd
-where ua.person_id = OLD.creator_id;
-
-END IF;
-return null;
-end $$;
-
-create or replace function person_aggregates_comment_count()
-    returns trigger language plpgsql
-as $$
-begin
+        UPDATE
+            community_aggregates
+        SET
+            posts = posts + 1
+        WHERE
+            community_id = NEW.community_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            community_aggregates
+        SET
+            posts = posts - 1
+        WHERE
+            community_id = OLD.community_id;
+        -- Update the counts if the post got deleted
+        UPDATE
+            community_aggregates ca
+        SET
+            posts = coalesce(cd.posts, 0),
+            comments = coalesce(cd.comments, 0)
+        FROM (
+            SELECT
+                c.id,
+                count(DISTINCT p.id) AS posts,
+                count(DISTINCT ct.id) AS comments
+            FROM
+                community c
+            LEFT JOIN post p ON c.id = p.community_id
+            LEFT JOIN comment ct ON p.id = ct.post_id
+        GROUP BY
+            c.id) cd
+    WHERE
+        ca.community_id = OLD.community_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION community_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
     IF (TG_OP = 'INSERT') THEN
-update person_aggregates
-set comment_count = comment_count + 1 where person_id = NEW.creator_id;
-ELSIF (TG_OP = 'DELETE') THEN
-update person_aggregates
-set comment_count = comment_count - 1 where person_id = OLD.creator_id;
-
--- If the comment gets deleted, the score calculation trigger won't fire,
--- so you need to re-calculate
-update person_aggregates ua
-set comment_score = cd.score
-    from (
-                 select u.id,
-                        coalesce(0, sum(cl.score)) as score
-                        -- User join because comments could be empty
-                 from person u
-                          left join comment c on u.id = c.creator_id
-                          left join comment_like cl on c.id = cl.comment_id
-                 group by u.id
-             ) cd
-where ua.person_id = OLD.creator_id;
-END IF;
-return null;
-end $$;
+        UPDATE
+            community_aggregates ca
+        SET
+            comments = comments + 1
+        FROM
+            comment c,
+            post p
+        WHERE
+            p.id = c.post_id
+            AND p.id = NEW.post_id
+            AND ca.community_id = p.community_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            community_aggregates ca
+        SET
+            comments = comments - 1
+        FROM
+            comment c,
+            post p
+        WHERE
+            p.id = c.post_id
+            AND p.id = OLD.post_id
+            AND ca.community_id = p.community_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+-- Community aggregate triggers
+CREATE TRIGGER community_aggregates_post_count
+    AFTER INSERT OR DELETE ON post
+    FOR EACH ROW
+    EXECUTE PROCEDURE community_aggregates_post_count ();
 
+CREATE TRIGGER community_aggregates_comment_count
+    AFTER INSERT OR DELETE ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE community_aggregates_comment_count ();
+
+-- Site aggregate functions
+CREATE OR REPLACE FUNCTION site_aggregates_post_insert ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates
+    SET
+        posts = posts + 1;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION site_aggregates_post_delete ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates sa
+    SET
+        posts = posts - 1
+    FROM
+        site s
+    WHERE
+        sa.site_id = s.id;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION site_aggregates_comment_insert ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates
+    SET
+        comments = comments + 1;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION site_aggregates_comment_delete ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates sa
+    SET
+        comments = comments - 1
+    FROM
+        site s
+    WHERE
+        sa.site_id = s.id;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION site_aggregates_community_insert ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates
+    SET
+        communities = communities + 1;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION site_aggregates_community_delete ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        site_aggregates sa
+    SET
+        communities = communities - 1
+    FROM
+        site s
+    WHERE
+        sa.site_id = s.id;
+    RETURN NULL;
+END
+$$;
+
+-- Site update triggers
+CREATE TRIGGER site_aggregates_post_insert
+    AFTER INSERT ON post
+    FOR EACH ROW
+    WHEN (NEW.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_post_insert ();
+
+CREATE TRIGGER site_aggregates_post_delete
+    AFTER DELETE ON post
+    FOR EACH ROW
+    WHEN (OLD.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_post_delete ();
+
+CREATE TRIGGER site_aggregates_comment_insert
+    AFTER INSERT ON comment
+    FOR EACH ROW
+    WHEN (NEW.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_comment_insert ();
+
+CREATE TRIGGER site_aggregates_comment_delete
+    AFTER DELETE ON comment
+    FOR EACH ROW
+    WHEN (OLD.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_comment_delete ();
+
+CREATE TRIGGER site_aggregates_community_insert
+    AFTER INSERT ON community
+    FOR EACH ROW
+    WHEN (NEW.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_community_insert ();
+
+CREATE TRIGGER site_aggregates_community_delete
+    AFTER DELETE ON community
+    FOR EACH ROW
+    WHEN (OLD.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_community_delete ();
+
+-- Person aggregate functions
+CREATE OR REPLACE FUNCTION person_aggregates_post_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            person_aggregates
+        SET
+            post_count = post_count + 1
+        WHERE
+            person_id = NEW.creator_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            person_aggregates
+        SET
+            post_count = post_count - 1
+        WHERE
+            person_id = OLD.creator_id;
+        -- If the post gets deleted, the score calculation trigger won't fire,
+        -- so you need to re-calculate
+        UPDATE
+            person_aggregates ua
+        SET
+            post_score = pd.score
+        FROM (
+            SELECT
+                u.id,
+                coalesce(0, sum(pl.score)) AS score
+                -- User join because posts could be empty
+            FROM
+                person u
+            LEFT JOIN post p ON u.id = p.creator_id
+            LEFT JOIN post_like pl ON p.id = pl.post_id
+        GROUP BY
+            u.id) pd
+    WHERE
+        ua.person_id = OLD.creator_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION person_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            person_aggregates
+        SET
+            comment_count = comment_count + 1
+        WHERE
+            person_id = NEW.creator_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        UPDATE
+            person_aggregates
+        SET
+            comment_count = comment_count - 1
+        WHERE
+            person_id = OLD.creator_id;
+        -- If the comment gets deleted, the score calculation trigger won't fire,
+        -- so you need to re-calculate
+        UPDATE
+            person_aggregates ua
+        SET
+            comment_score = cd.score
+        FROM (
+            SELECT
+                u.id,
+                coalesce(0, sum(cl.score)) AS score
+                -- User join because comments could be empty
+            FROM
+                person u
+            LEFT JOIN comment c ON u.id = c.creator_id
+            LEFT JOIN comment_like cl ON c.id = cl.comment_id
+        GROUP BY
+            u.id) cd
+    WHERE
+        ua.person_id = OLD.creator_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
 -- Person aggregate triggers
+CREATE TRIGGER person_aggregates_post_count
+    AFTER INSERT OR DELETE ON post
+    FOR EACH ROW
+    EXECUTE PROCEDURE person_aggregates_post_count ();
 
-create trigger person_aggregates_post_count
-    after insert or delete on post
-    for each row
-execute procedure person_aggregates_post_count();
+CREATE TRIGGER person_aggregates_comment_count
+    AFTER INSERT OR DELETE ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE person_aggregates_comment_count ();
 
-create trigger person_aggregates_comment_count
-    after insert or delete on comment
-    for each row
-execute procedure person_aggregates_comment_count();
\ No newline at end of file
index 472be541d1bea6d86c6bd945ed8e87ae9a23967f..27fc66d170e47fb19eec63e9cd18dc549cf364ed 100644 (file)
-drop trigger if exists community_aggregates_post_count on post;
-drop trigger if exists community_aggregates_comment_count on comment;
-drop trigger if exists site_aggregates_comment_insert on comment;
-drop trigger if exists site_aggregates_comment_delete on comment;
-drop trigger if exists site_aggregates_post_insert on post;
-drop trigger if exists site_aggregates_post_delete on post;
-drop trigger if exists site_aggregates_community_insert on community;
-drop trigger if exists site_aggregates_community_delete on community;
-drop trigger if exists person_aggregates_post_count on post;
-drop trigger if exists person_aggregates_comment_count on comment;
-
-create or replace function was_removed_or_deleted(TG_OP text, OLD record, NEW record)
-RETURNS boolean
-LANGUAGE plpgsql
-as $$
-    begin
-        IF (TG_OP = 'INSERT') THEN
-            return false;
-        end if;
-
-        IF (TG_OP = 'DELETE') THEN
-            return true;
-        end if;
-
-    return TG_OP = 'UPDATE' AND (
-            (OLD.deleted = 'f' AND NEW.deleted = 't') OR
-            (OLD.removed = 'f' AND NEW.removed = 't')
-            );
-END $$;
-
-create or replace function was_restored_or_created(TG_OP text, OLD record, NEW record)
-    RETURNS boolean
-    LANGUAGE plpgsql
-as $$
-begin
-    IF (TG_OP = 'DELETE') THEN
-        return false;
-    end if;
+DROP TRIGGER IF EXISTS community_aggregates_post_count ON post;
 
-    IF (TG_OP = 'INSERT') THEN
-        return true;
-    end if;
+DROP TRIGGER IF EXISTS community_aggregates_comment_count ON comment;
 
-   return TG_OP = 'UPDATE' AND (
-        (OLD.deleted = 't' AND NEW.deleted = 'f') OR
-        (OLD.removed = 't' AND NEW.removed = 'f')
-        );
-END $$;
+DROP TRIGGER IF EXISTS site_aggregates_comment_insert ON comment;
 
--- Community aggregate functions
+DROP TRIGGER IF EXISTS site_aggregates_comment_delete ON comment;
 
-create or replace function community_aggregates_post_count()
-    returns trigger language plpgsql
-as $$
-begin
-  IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-update community_aggregates
-set posts = posts + 1 where community_id = NEW.community_id;
-
-IF (TG_OP = 'UPDATE') THEN
-    -- Post was restored, so restore comment counts as well
-    update community_aggregates ca
-    set posts = coalesce(cd.posts, 0),
-        comments = coalesce(cd.comments, 0)
-    from (
-             select
-                 c.id,
-                 count(distinct p.id) as posts,
-                 count(distinct ct.id) as comments
-             from community c
-                      left join post p on c.id = p.community_id and p.deleted = 'f' and p.removed = 'f'
-                      left join comment ct on p.id = ct.post_id and ct.deleted = 'f' and ct.removed = 'f'
-             where c.id = NEW.community_id
-             group by c.id
-         ) cd
-    where ca.community_id = NEW.community_id;
-END IF;
-
-ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-update community_aggregates
-set posts = posts - 1 where community_id = OLD.community_id;
-
--- Update the counts if the post got deleted
-update community_aggregates ca
-set posts = coalesce(cd.posts, 0),
-    comments = coalesce(cd.comments, 0)
-    from ( 
-      select 
-      c.id,
-      count(distinct p.id) as posts,
-      count(distinct ct.id) as comments
-      from community c
-      left join post p on c.id = p.community_id and p.deleted = 'f' and p.removed = 'f'
-      left join comment ct on p.id = ct.post_id and ct.deleted = 'f' and ct.removed = 'f'
-      where c.id = OLD.community_id
-      group by c.id
-    ) cd
-where ca.community_id = OLD.community_id;
-END IF;
-return null;
-end $$;
+DROP TRIGGER IF EXISTS site_aggregates_post_insert ON post;
 
--- comment count
-create or replace function community_aggregates_comment_count()
-    returns trigger language plpgsql
-as $$
-begin
-  IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-update community_aggregates ca
-set comments = comments + 1 from comment c, post p
-where p.id = c.post_id
-  and p.id = NEW.post_id
-  and ca.community_id = p.community_id;
-ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-update community_aggregates ca
-set comments = comments - 1 from comment c, post p
-where p.id = c.post_id
-  and p.id = OLD.post_id
-  and ca.community_id = p.community_id;
-
-END IF;
-return null;
-end $$;
+DROP TRIGGER IF EXISTS site_aggregates_post_delete ON post;
 
--- Community aggregate triggers
+DROP TRIGGER IF EXISTS site_aggregates_community_insert ON community;
 
-create trigger community_aggregates_post_count
-    after insert or delete or update of removed, deleted on post
-    for each row
-execute procedure community_aggregates_post_count();
+DROP TRIGGER IF EXISTS site_aggregates_community_delete ON community;
 
-create trigger community_aggregates_comment_count
-    after insert or delete or update of removed, deleted on comment
-for each row
-execute procedure community_aggregates_comment_count();
+DROP TRIGGER IF EXISTS person_aggregates_post_count ON post;
 
--- Site aggregate functions
+DROP TRIGGER IF EXISTS person_aggregates_comment_count ON comment;
 
-create or replace function site_aggregates_post_insert()
-    returns trigger language plpgsql
-as $$
-begin
-    IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-        update site_aggregates sa
-        set posts = posts + 1
-        from site s
-        where sa.site_id = s.id;
+CREATE OR REPLACE FUNCTION was_removed_or_deleted (TG_OP text, OLD record, NEW record)
+    RETURNS boolean
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        RETURN FALSE;
     END IF;
-    return null;
-end $$;
-
-create or replace function site_aggregates_post_delete()
-    returns trigger language plpgsql
-as $$
-begin
-    IF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-        update site_aggregates sa
-        set posts = posts - 1
-        from site s
-        where sa.site_id = s.id;
+    IF (TG_OP = 'DELETE') THEN
+        RETURN TRUE;
     END IF;
-    return null;
-end $$;
-
-create or replace function site_aggregates_comment_insert()
-    returns trigger language plpgsql
-as $$
-begin
-    IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-        update site_aggregates sa
-        set comments = comments + 1
-        from site s
-        where sa.site_id = s.id;
+    RETURN TG_OP = 'UPDATE'
+        AND ((OLD.deleted = 'f'
+                AND NEW.deleted = 't')
+            OR (OLD.removed = 'f'
+                AND NEW.removed = 't'));
+END
+$$;
+
+CREATE OR REPLACE FUNCTION was_restored_or_created (TG_OP text, OLD record, NEW record)
+    RETURNS boolean
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'DELETE') THEN
+        RETURN FALSE;
     END IF;
-    return null;
-end $$;
-
-create or replace function site_aggregates_comment_delete()
-    returns trigger language plpgsql
-as $$
-begin
-    IF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-        update site_aggregates sa
-        set comments = comments - 1
-        from site s
-        where sa.site_id = s.id;
+    IF (TG_OP = 'INSERT') THEN
+        RETURN TRUE;
     END IF;
-    return null;
-end $$;
-
-create or replace function site_aggregates_community_insert()
-    returns trigger language plpgsql
-as $$
-begin
-    IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-        update site_aggregates sa
-        set communities = communities + 1
-        from site s
-        where sa.site_id = s.id;
+    RETURN TG_OP = 'UPDATE'
+        AND ((OLD.deleted = 't'
+                AND NEW.deleted = 'f')
+            OR (OLD.removed = 't'
+                AND NEW.removed = 'f'));
+END
+$$;
+
+-- Community aggregate functions
+CREATE OR REPLACE FUNCTION community_aggregates_post_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            community_aggregates
+        SET
+            posts = posts + 1
+        WHERE
+            community_id = NEW.community_id;
+        IF (TG_OP = 'UPDATE') THEN
+            -- Post was restored, so restore comment counts as well
+            UPDATE
+                community_aggregates ca
+            SET
+                posts = coalesce(cd.posts, 0),
+                comments = coalesce(cd.comments, 0)
+            FROM (
+                SELECT
+                    c.id,
+                    count(DISTINCT p.id) AS posts,
+                    count(DISTINCT ct.id) AS comments
+                FROM
+                    community c
+                LEFT JOIN post p ON c.id = p.community_id
+                    AND p.deleted = 'f'
+                    AND p.removed = 'f'
+            LEFT JOIN comment ct ON p.id = ct.post_id
+                AND ct.deleted = 'f'
+                AND ct.removed = 'f'
+        WHERE
+            c.id = NEW.community_id
+        GROUP BY
+            c.id) cd
+        WHERE
+            ca.community_id = NEW.community_id;
+        END IF;
+    ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            community_aggregates
+        SET
+            posts = posts - 1
+        WHERE
+            community_id = OLD.community_id;
+        -- Update the counts if the post got deleted
+        UPDATE
+            community_aggregates ca
+        SET
+            posts = coalesce(cd.posts, 0),
+            comments = coalesce(cd.comments, 0)
+        FROM (
+            SELECT
+                c.id,
+                count(DISTINCT p.id) AS posts,
+                count(DISTINCT ct.id) AS comments
+            FROM
+                community c
+            LEFT JOIN post p ON c.id = p.community_id
+                AND p.deleted = 'f'
+                AND p.removed = 'f'
+        LEFT JOIN comment ct ON p.id = ct.post_id
+            AND ct.deleted = 'f'
+            AND ct.removed = 'f'
+    WHERE
+        c.id = OLD.community_id
+    GROUP BY
+        c.id) cd
+    WHERE
+        ca.community_id = OLD.community_id;
     END IF;
-    return null;
-end $$;
-
-create or replace function site_aggregates_community_delete()
-    returns trigger language plpgsql
-as $$
-begin
-        IF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-        update site_aggregates sa
-        set communities = communities - 1
-        from site s
-        where sa.site_id = s.id;
+    RETURN NULL;
+END
+$$;
+
+-- comment count
+CREATE OR REPLACE FUNCTION community_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            community_aggregates ca
+        SET
+            comments = comments + 1
+        FROM
+            comment c,
+            post p
+        WHERE
+            p.id = c.post_id
+            AND p.id = NEW.post_id
+            AND ca.community_id = p.community_id;
+    ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            community_aggregates ca
+        SET
+            comments = comments - 1
+        FROM
+            comment c,
+            post p
+        WHERE
+            p.id = c.post_id
+            AND p.id = OLD.post_id
+            AND ca.community_id = p.community_id;
     END IF;
-    return null;
-end $$;
+    RETURN NULL;
+END
+$$;
 
--- Site aggregate triggers
+-- Community aggregate triggers
+CREATE TRIGGER community_aggregates_post_count
+    AFTER INSERT OR DELETE OR UPDATE OF removed,
+    deleted ON post
+    FOR EACH ROW
+    EXECUTE PROCEDURE community_aggregates_post_count ();
+
+CREATE TRIGGER community_aggregates_comment_count
+    AFTER INSERT OR DELETE OR UPDATE OF removed,
+    deleted ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE community_aggregates_comment_count ();
 
-create trigger site_aggregates_post_insert
-    after insert or update of removed, deleted on post
-    for each row
-    when (NEW.local = true)
-execute procedure site_aggregates_post_insert();
-
-create trigger site_aggregates_post_delete
-    after delete or update of removed, deleted on post
-    for each row
-    when (OLD.local = true)
-execute procedure site_aggregates_post_delete();
-
-create trigger site_aggregates_comment_insert
-    after insert or update of removed, deleted on comment
-    for each row
-    when (NEW.local = true)
-execute procedure site_aggregates_comment_insert();
-
-create trigger site_aggregates_comment_delete
-    after delete or update of removed, deleted on comment
-    for each row
-    when (OLD.local = true)
-execute procedure site_aggregates_comment_delete();
-
-create trigger site_aggregates_community_insert
-    after insert or update of removed, deleted on community
-    for each row
-    when (NEW.local = true)
-execute procedure site_aggregates_community_insert();
-
-create trigger site_aggregates_community_delete
-    after delete or update of removed, deleted on community
-    for each row
-    when (OLD.local = true)
-execute procedure site_aggregates_community_delete();
+-- Site aggregate functions
+CREATE OR REPLACE FUNCTION site_aggregates_post_insert ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            site_aggregates sa
+        SET
+            posts = posts + 1
+        FROM
+            site s
+        WHERE
+            sa.site_id = s.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
--- Person aggregate functions
+CREATE OR REPLACE FUNCTION site_aggregates_post_delete ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            site_aggregates sa
+        SET
+            posts = posts - 1
+        FROM
+            site s
+        WHERE
+            sa.site_id = s.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-create or replace function person_aggregates_post_count()
-    returns trigger language plpgsql
-as $$
-begin
-    IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-        update person_aggregates
-        set post_count = post_count + 1 where person_id = NEW.creator_id;
+CREATE OR REPLACE FUNCTION site_aggregates_comment_insert ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            site_aggregates sa
+        SET
+            comments = comments + 1
+        FROM
+            site s
+        WHERE
+            sa.site_id = s.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-    ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-        update person_aggregates
-        set post_count = post_count - 1 where person_id = OLD.creator_id;
+CREATE OR REPLACE FUNCTION site_aggregates_comment_delete ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            site_aggregates sa
+        SET
+            comments = comments - 1
+        FROM
+            site s
+        WHERE
+            sa.site_id = s.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
+CREATE OR REPLACE FUNCTION site_aggregates_community_insert ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            site_aggregates sa
+        SET
+            communities = communities + 1
+        FROM
+            site s
+        WHERE
+            sa.site_id = s.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+CREATE OR REPLACE FUNCTION site_aggregates_community_delete ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            site_aggregates sa
+        SET
+            communities = communities - 1
+        FROM
+            site s
+        WHERE
+            sa.site_id = s.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+-- Site aggregate triggers
+CREATE TRIGGER site_aggregates_post_insert
+    AFTER INSERT OR UPDATE OF removed,
+    deleted ON post
+    FOR EACH ROW
+    WHEN (NEW.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_post_insert ();
+
+CREATE TRIGGER site_aggregates_post_delete
+    AFTER DELETE OR UPDATE OF removed,
+    deleted ON post
+    FOR EACH ROW
+    WHEN (OLD.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_post_delete ();
+
+CREATE TRIGGER site_aggregates_comment_insert
+    AFTER INSERT OR UPDATE OF removed,
+    deleted ON comment
+    FOR EACH ROW
+    WHEN (NEW.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_comment_insert ();
+
+CREATE TRIGGER site_aggregates_comment_delete
+    AFTER DELETE OR UPDATE OF removed,
+    deleted ON comment
+    FOR EACH ROW
+    WHEN (OLD.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_comment_delete ();
+
+CREATE TRIGGER site_aggregates_community_insert
+    AFTER INSERT OR UPDATE OF removed,
+    deleted ON community
+    FOR EACH ROW
+    WHEN (NEW.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_community_insert ();
+
+CREATE TRIGGER site_aggregates_community_delete
+    AFTER DELETE OR UPDATE OF removed,
+    deleted ON community
+    FOR EACH ROW
+    WHEN (OLD.local = TRUE)
+    EXECUTE PROCEDURE site_aggregates_community_delete ();
+
+-- Person aggregate functions
+CREATE OR REPLACE FUNCTION person_aggregates_post_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            person_aggregates
+        SET
+            post_count = post_count + 1
+        WHERE
+            person_id = NEW.creator_id;
+    ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            person_aggregates
+        SET
+            post_count = post_count - 1
+        WHERE
+            person_id = OLD.creator_id;
         -- If the post gets deleted, the score calculation trigger won't fire,
         -- so you need to re-calculate
-        update person_aggregates ua
-        set post_score = pd.score
-        from (
-                 select u.id,
-                        coalesce(0, sum(pl.score)) as score
-                        -- User join because posts could be empty
-                 from person u
-                          left join post p on u.id = p.creator_id and p.deleted = 'f' and p.removed = 'f'
-                          left join post_like pl on p.id = pl.post_id
-                 group by u.id
-             ) pd
-        where ua.person_id = OLD.creator_id;
-
+        UPDATE
+            person_aggregates ua
+        SET
+            post_score = pd.score
+        FROM (
+            SELECT
+                u.id,
+                coalesce(0, sum(pl.score)) AS score
+                -- User join because posts could be empty
+            FROM
+                person u
+            LEFT JOIN post p ON u.id = p.creator_id
+                AND p.deleted = 'f'
+                AND p.removed = 'f'
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        u.id) pd
+    WHERE
+        ua.person_id = OLD.creator_id;
     END IF;
-    return null;
-end $$;
-
-create or replace function person_aggregates_comment_count()
-    returns trigger language plpgsql
-as $$
-begin
-    IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-        update person_aggregates
-        set comment_count = comment_count + 1 where person_id = NEW.creator_id;
-    ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-        update person_aggregates
-        set comment_count = comment_count - 1 where person_id = OLD.creator_id;
+    RETURN NULL;
+END
+$$;
 
+CREATE OR REPLACE FUNCTION person_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            person_aggregates
+        SET
+            comment_count = comment_count + 1
+        WHERE
+            person_id = NEW.creator_id;
+    ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            person_aggregates
+        SET
+            comment_count = comment_count - 1
+        WHERE
+            person_id = OLD.creator_id;
         -- If the comment gets deleted, the score calculation trigger won't fire,
         -- so you need to re-calculate
-        update person_aggregates ua
-        set comment_score = cd.score
-        from (
-                 select u.id,
-                        coalesce(0, sum(cl.score)) as score
-                        -- User join because comments could be empty
-                 from person u
-                          left join comment c on u.id = c.creator_id and c.deleted = 'f' and c.removed = 'f'
-                          left join comment_like cl on c.id = cl.comment_id
-                 group by u.id
-             ) cd
-        where ua.person_id = OLD.creator_id;
+        UPDATE
+            person_aggregates ua
+        SET
+            comment_score = cd.score
+        FROM (
+            SELECT
+                u.id,
+                coalesce(0, sum(cl.score)) AS score
+                -- User join because comments could be empty
+            FROM
+                person u
+            LEFT JOIN comment c ON u.id = c.creator_id
+                AND c.deleted = 'f'
+                AND c.removed = 'f'
+        LEFT JOIN comment_like cl ON c.id = cl.comment_id
+    GROUP BY
+        u.id) cd
+    WHERE
+        ua.person_id = OLD.creator_id;
     END IF;
-    return null;
-end $$;
+    RETURN NULL;
+END
+$$;
 
 -- Person aggregate triggers
+CREATE TRIGGER person_aggregates_post_count
+    AFTER INSERT OR DELETE OR UPDATE OF removed,
+    deleted ON post
+    FOR EACH ROW
+    EXECUTE PROCEDURE person_aggregates_post_count ();
+
+CREATE TRIGGER person_aggregates_comment_count
+    AFTER INSERT OR DELETE OR UPDATE OF removed,
+    deleted ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE person_aggregates_comment_count ();
 
-create trigger person_aggregates_post_count
-    after insert or delete or update of removed, deleted on post
-    for each row
-execute procedure person_aggregates_post_count();
-
-create trigger person_aggregates_comment_count
-    after insert or delete or update of removed, deleted on comment
-    for each row
-execute procedure person_aggregates_comment_count();
index 28198e892e0df86d9dd702f39c658185e7a6c0a6..00aa1c719d0b71e23431425f6dd15bd8b7ac8579 100644 (file)
@@ -1,6 +1,10 @@
-create or replace function generate_unique_changeme() 
-returns text language sql 
-as $$
-  select 'http://changeme_' || string_agg (substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil (random() * 62)::integer, 1), '')
-  from generate_series(1, 20)
+CREATE OR REPLACE FUNCTION generate_unique_changeme ()
+    RETURNS text
+    LANGUAGE sql
+    AS $$
+    SELECT
+        'http://changeme_' || string_agg(substr('abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789', ceil(random() * 62)::integer, 1), '')
+    FROM
+        generate_series(1, 20)
 $$;
+
index 46fe55b5f5e6b194de4e04b92ba7fdd8a0ab7252..5cd927d24ba50a01b79d6386494f08be9db3f74f 100644 (file)
@@ -1,5 +1,8 @@
-create or replace function generate_unique_changeme() 
-returns text language sql 
-as $$
-  select 'http://changeme.invalid/' || substr(md5(random()::text), 0, 25);
+CREATE OR REPLACE FUNCTION generate_unique_changeme ()
+    RETURNS text
+    LANGUAGE sql
+    AS $$
+    SELECT
+        'http://changeme.invalid/' || substr(md5(random()::text), 0, 25);
 $$;
+
index b70a7dd0ccc8b97d93f0c30d754e633a8e9b86c9..9d51d7ca5bd488edbfd9c07e07fa9784384952f8 100644 (file)
@@ -1 +1,3 @@
-alter table site drop column default_post_listing_type;
+ALTER TABLE site
+    DROP COLUMN default_post_listing_type;
+
index f68e198fa162d4a9d93059e640ca35530dce0a85..7cd0602ea97215b4cd3cd4b592553235b9acbbdd 100644 (file)
@@ -1 +1,3 @@
-alter table site add column default_post_listing_type text not null default 'Local';
+ALTER TABLE site
+    ADD COLUMN default_post_listing_type text NOT NULL DEFAULT 'Local';
+
index d1d35659a384e702b80f047afc25bf58d5e2bd75..573677a9aaea06f227597b9f850839caf1e67a9b 100644 (file)
@@ -1,3 +1,4 @@
 -- 0 is All, 1 is Local, 2 is Subscribed
+ALTER TABLE ONLY local_user
+    ALTER COLUMN default_listing_type SET DEFAULT 2;
 
-alter table only local_user alter column default_listing_type set default 2;
index 15f0509e91bb93510a8b5aa47bd538a17607d702..a0054a26d511bcdbe6cb33eb358365bed7c726c1 100644 (file)
@@ -1,3 +1,4 @@
 -- 0 is All, 1 is Local, 2 is Subscribed
+ALTER TABLE ONLY local_user
+    ALTER COLUMN default_listing_type SET DEFAULT 1;
 
-alter table only local_user alter column default_listing_type set default 1;
index eaa29a3cade80a7fdac295bb7eefd45df6ac6966..1fb9934825e71acfa1faa2228a0e85b73cedd1fe 100644 (file)
@@ -1,2 +1,6 @@
-alter table site alter column require_application set default false;
-alter table site alter column application_question set default null;
\ No newline at end of file
+ALTER TABLE site
+    ALTER COLUMN require_application SET DEFAULT FALSE;
+
+ALTER TABLE site
+    ALTER COLUMN application_question SET DEFAULT NULL;
+
index 1e326f17303a47e7fa950dc0a1639e84ea3bf2f3..35722288fbdbf4585b21695b20100ea97b43fb09 100644 (file)
@@ -1,2 +1,6 @@
-alter table site alter column require_application set default true;
-alter table site alter column application_question set default 'To verify that you are human, please explain why you want to create an account on this site';
\ No newline at end of file
+ALTER TABLE site
+    ALTER COLUMN require_application SET DEFAULT TRUE;
+
+ALTER TABLE site
+    ALTER COLUMN application_question SET DEFAULT 'To verify that you are human, please explain why you want to create an account on this site';
+
index a9c95bf622429785a9bd634b94d1f5a0605a0c54..c74fe40ca973824263aff998104dff015e1cc36d 100644 (file)
@@ -1 +1,3 @@
-alter table community drop column posting_restricted_to_mods;
\ No newline at end of file
+ALTER TABLE community
+    DROP COLUMN posting_restricted_to_mods;
+
index fbc569831ae74b2e32faa185301c22c33b32b96c..176de3cb39e7ebf6b03952080462e898336297bf 100644 (file)
@@ -1 +1,3 @@
-alter table community add column posting_restricted_to_mods boolean default false;
\ No newline at end of file
+ALTER TABLE community
+    ADD COLUMN posting_restricted_to_mods boolean DEFAULT FALSE;
+
index 0df8c19efe42144c253a05df6172ca512e7ba073..f8a398e62f8441963e290a77ce056ac0586ead02 100644 (file)
@@ -1 +1,3 @@
-alter table site drop column legal_information;
\ No newline at end of file
+ALTER TABLE site
+    DROP COLUMN legal_information;
+
index 641fe63f785aeab8be1184dc0f16f8039ea08eff..01266d0f85f6eb4269c128bbd6e9e2a037840202 100644 (file)
@@ -1 +1,3 @@
-alter table site add column legal_information text;
\ No newline at end of file
+ALTER TABLE site
+    ADD COLUMN legal_information text;
+
index 1c4ff1f6113ae1cf2378033ba65d0833178c8b86..4ce7051a1127288a922abb95faa22b9829a5708c 100644 (file)
@@ -1,2 +1,6 @@
-alter table post drop column embed_url;
-alter table post add column embed_video_url text;
\ No newline at end of file
+ALTER TABLE post
+    DROP COLUMN embed_url;
+
+ALTER TABLE post
+    ADD COLUMN embed_video_url text;
+
index 47d0df683d5c632a4bef1b6e947b7a2c62e7dfdf..d69c0f31164151ea945a9e0f7dd0dc07a55b991f 100644 (file)
@@ -1,2 +1,6 @@
-alter table post drop column embed_html;
-alter table post add column embed_video_url text;
\ No newline at end of file
+ALTER TABLE post
+    DROP COLUMN embed_html;
+
+ALTER TABLE post
+    ADD COLUMN embed_video_url text;
+
index c3f658175fcb601035035624f3ea851ab506c4a2..d758752d42a775b1428389d5d2aa7991aa1178d7 100644 (file)
@@ -1 +1,3 @@
-alter table site drop column hide_modlog_mod_names;
\ No newline at end of file
+ALTER TABLE site
+    DROP COLUMN hide_modlog_mod_names;
+
index 329ad6f425485705b60ae648b2f38c22b684ad25..bb8e65644b165faa862e7410c8f2f34ca3f294b1 100644 (file)
@@ -1 +1,3 @@
-alter table site add column hide_modlog_mod_names boolean default true NOT NULL;
\ No newline at end of file
+ALTER TABLE site
+    ADD COLUMN hide_modlog_mod_names boolean DEFAULT TRUE NOT NULL;
+
index f7b3a95e647ad42a6d462c15a62309546f517431..7f99a59ca0cca93c0b67858d273e6b4f1bee88e1 100644 (file)
@@ -1 +1,3 @@
-alter table post_report alter column original_post_name type varchar(100);
+ALTER TABLE post_report
+    ALTER COLUMN original_post_name TYPE varchar(100);
+
index 7663115124976e4a45b08a0121892e76c4fff3cc..595b1337fcb99640736ecfba8f4859437357246d 100644 (file)
@@ -1,2 +1,4 @@
 -- adjust length limit to match post.name
-alter table post_report alter column original_post_name type varchar(200);
+ALTER TABLE post_report
+    ALTER COLUMN original_post_name TYPE varchar(200);
+
index 22ce261fad1cb1e9f60c025f30ab1797b8e42679..8af971d0c6be653858e02f149b69245d7dfa7ab5 100644 (file)
@@ -1,6 +1,9 @@
-alter table post drop column language_id;
-drop table local_user_language;
-drop table language;
+ALTER TABLE post
+    DROP COLUMN language_id;
 
-alter table local_user rename column interface_language to lang;
+DROP TABLE local_user_language;
+
+DROP TABLE LANGUAGE;
+
+ALTER TABLE local_user RENAME COLUMN interface_language TO lang;
 
index e670a59d4cd0a5c22c1c45f639e97a496c37604e..9deb6b479f4d8a2de9ad28d44a46822a7c45f135 100644 (file)
-create table language (
-  id serial primary key,
-  code varchar(3),
-  name text
+CREATE TABLE
+LANGUAGE (
+    id serial PRIMARY KEY,
+    code varchar(3),
+    name text
 );
-create table local_user_language (
-  id serial primary key,
-  local_user_id int references local_user on update cascade on delete cascade not null,
-  language_id int references language on update cascade on delete cascade not null,
-  unique (local_user_id, language_id)
+
+CREATE TABLE local_user_language (
+    id serial PRIMARY KEY,
+    local_user_id int REFERENCES local_user ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    language_id int REFERENCES
+    LANGUAGE ON
+    UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    UNIQUE (local_user_id, language_id)
 );
 
-alter table local_user rename column lang to interface_language;
-
-insert into language(id, code, name) values (0, 'und', 'Undetermined');
-alter table post add column language_id integer references language not null default 0;
-insert into language(code, name) values ('aa', 'Afaraf');
-insert into language(code, name) values ('ab', 'аҧсуа бызшәа');
-insert into language(code, name) values ('ae', 'avesta');
-insert into language(code, name) values ('af', 'Afrikaans');
-insert into language(code, name) values ('ak', 'Akan');
-insert into language(code, name) values ('am', 'አማርኛ');
-insert into language(code, name) values ('an', 'aragonés');
-insert into language(code, name) values ('ar', 'اَلْعَرَبِيَّةُ');
-insert into language(code, name) values ('as', 'অসমীয়া');
-insert into language(code, name) values ('av', 'авар мацӀ');
-insert into language(code, name) values ('ay', 'aymar aru');
-insert into language(code, name) values ('az', 'azərbaycan dili');
-insert into language(code, name) values ('ba', 'башҡорт теле');
-insert into language(code, name) values ('be', 'беларуская мова');
-insert into language(code, name) values ('bg', 'български език');
-insert into language(code, name) values ('bi', 'Bislama');
-insert into language(code, name) values ('bm', 'bamanankan');
-insert into language(code, name) values ('bn', 'বাংলা');
-insert into language(code, name) values ('bo', 'བོད་ཡིག');
-insert into language(code, name) values ('br', 'brezhoneg');
-insert into language(code, name) values ('bs', 'bosanski jezik');
-insert into language(code, name) values ('ca', 'Català');
-insert into language(code, name) values ('ce', 'нохчийн мотт');
-insert into language(code, name) values ('ch', 'Chamoru');
-insert into language(code, name) values ('co', 'corsu');
-insert into language(code, name) values ('cr', 'ᓀᐦᐃᔭᐍᐏᐣ');
-insert into language(code, name) values ('cs', 'čeština');
-insert into language(code, name) values ('cu', 'ѩзыкъ словѣньскъ');
-insert into language(code, name) values ('cv', 'чӑваш чӗлхи');
-insert into language(code, name) values ('cy', 'Cymraeg');
-insert into language(code, name) values ('da', 'dansk');
-insert into language(code, name) values ('de', 'Deutsch');
-insert into language(code, name) values ('dv', 'ދިވެހި');
-insert into language(code, name) values ('dz', 'རྫོང་ཁ');
-insert into language(code, name) values ('ee', 'Eʋegbe');
-insert into language(code, name) values ('el', 'Ελληνικά');
-insert into language(code, name) values ('en', 'English');
-insert into language(code, name) values ('eo', 'Esperanto');
-insert into language(code, name) values ('es', 'Español');
-insert into language(code, name) values ('et', 'eesti');
-insert into language(code, name) values ('eu', 'euskara');
-insert into language(code, name) values ('fa', 'فارسی');
-insert into language(code, name) values ('ff', 'Fulfulde');
-insert into language(code, name) values ('fi', 'suomi');
-insert into language(code, name) values ('fj', 'vosa Vakaviti');
-insert into language(code, name) values ('fo', 'føroyskt');
-insert into language(code, name) values ('fr', 'Français');
-insert into language(code, name) values ('fy', 'Frysk');
-insert into language(code, name) values ('ga', 'Gaeilge');
-insert into language(code, name) values ('gd', 'Gàidhlig');
-insert into language(code, name) values ('gl', 'galego');
-insert into language(code, name) values ('gn', E'Avañe\'ẽ');
-insert into language(code, name) values ('gu', 'ગુજરાતી');
-insert into language(code, name) values ('gv', 'Gaelg');
-insert into language(code, name) values ('ha', 'هَوُسَ');
-insert into language(code, name) values ('he', 'עברית');
-insert into language(code, name) values ('hi', 'हिन्दी');
-insert into language(code, name) values ('ho', 'Hiri Motu');
-insert into language(code, name) values ('hr', 'Hrvatski');
-insert into language(code, name) values ('ht', 'Kreyòl ayisyen');
-insert into language(code, name) values ('hu', 'magyar');
-insert into language(code, name) values ('hy', 'Հայերեն');
-insert into language(code, name) values ('hz', 'Otjiherero');
-insert into language(code, name) values ('ia', 'Interlingua');
-insert into language(code, name) values ('id', 'Bahasa Indonesia');
-insert into language(code, name) values ('ie', 'Interlingue');
-insert into language(code, name) values ('ig', 'Asụsụ Igbo');
-insert into language(code, name) values ('ii', 'ꆈꌠ꒿ Nuosuhxop');
-insert into language(code, name) values ('ik', 'Iñupiaq');
-insert into language(code, name) values ('io', 'Ido');
-insert into language(code, name) values ('is', 'Íslenska');
-insert into language(code, name) values ('it', 'Italiano');
-insert into language(code, name) values ('iu', 'ᐃᓄᒃᑎᑐᑦ');
-insert into language(code, name) values ('ja', '日本語');
-insert into language(code, name) values ('jv', 'basa Jawa');
-insert into language(code, name) values ('ka', 'ქართული');
-insert into language(code, name) values ('kg', 'Kikongo');
-insert into language(code, name) values ('ki', 'Gĩkũyũ');
-insert into language(code, name) values ('kj', 'Kuanyama');
-insert into language(code, name) values ('kk', 'қазақ тілі');
-insert into language(code, name) values ('kl', 'kalaallisut');
-insert into language(code, name) values ('km', 'ខេមរភាសា');
-insert into language(code, name) values ('kn', 'ಕನ್ನಡ');
-insert into language(code, name) values ('ko', '한국어');
-insert into language(code, name) values ('kr', 'Kanuri');
-insert into language(code, name) values ('ks', 'कश्मीरी');
-insert into language(code, name) values ('ku', 'Kurdî');
-insert into language(code, name) values ('kv', 'коми кыв');
-insert into language(code, name) values ('kw', 'Kernewek');
-insert into language(code, name) values ('ky', 'Кыргызча');
-insert into language(code, name) values ('la', 'latine');
-insert into language(code, name) values ('lb', 'Lëtzebuergesch');
-insert into language(code, name) values ('lg', 'Luganda');
-insert into language(code, name) values ('li', 'Limburgs');
-insert into language(code, name) values ('ln', 'Lingála');
-insert into language(code, name) values ('lo', 'ພາສາລາວ');
-insert into language(code, name) values ('lt', 'lietuvių kalba');
-insert into language(code, name) values ('lu', 'Kiluba');
-insert into language(code, name) values ('lv', 'latviešu valoda');
-insert into language(code, name) values ('mg', 'fiteny malagasy');
-insert into language(code, name) values ('mh', 'Kajin M̧ajeļ');
-insert into language(code, name) values ('mi', 'te reo Māori');
-insert into language(code, name) values ('mk', 'македонски јазик');
-insert into language(code, name) values ('ml', 'മലയാളം');
-insert into language(code, name) values ('mn', 'Монгол хэл');
-insert into language(code, name) values ('mr', 'मराठी');
-insert into language(code, name) values ('ms', 'Bahasa Melayu');
-insert into language(code, name) values ('mt', 'Malti');
-insert into language(code, name) values ('my', 'ဗမာစာ');
-insert into language(code, name) values ('na', 'Dorerin Naoero');
-insert into language(code, name) values ('nb', 'Norsk bokmål');
-insert into language(code, name) values ('nd', 'isiNdebele');
-insert into language(code, name) values ('ne', 'नेपाली');
-insert into language(code, name) values ('ng', 'Owambo');
-insert into language(code, name) values ('nl', 'Nederlands');
-insert into language(code, name) values ('nn', 'Norsk nynorsk');
-insert into language(code, name) values ('no', 'Norsk');
-insert into language(code, name) values ('nr', 'isiNdebele');
-insert into language(code, name) values ('nv', 'Diné bizaad');
-insert into language(code, name) values ('ny', 'chiCheŵa');
-insert into language(code, name) values ('oc', 'occitan');
-insert into language(code, name) values ('oj', 'ᐊᓂᔑᓈᐯᒧᐎᓐ');
-insert into language(code, name) values ('om', 'Afaan Oromoo');
-insert into language(code, name) values ('or', 'ଓଡ଼ିଆ');
-insert into language(code, name) values ('os', 'ирон æвзаг');
-insert into language(code, name) values ('pa', 'ਪੰਜਾਬੀ');
-insert into language(code, name) values ('pi', 'पाऴि');
-insert into language(code, name) values ('pl', 'Polski');
-insert into language(code, name) values ('ps', 'پښتو');
-insert into language(code, name) values ('pt', 'Português');
-insert into language(code, name) values ('qu', 'Runa Simi');
-insert into language(code, name) values ('rm', 'rumantsch grischun');
-insert into language(code, name) values ('rn', 'Ikirundi');
-insert into language(code, name) values ('ro', 'Română');
-insert into language(code, name) values ('ru', 'Русский');
-insert into language(code, name) values ('rw', 'Ikinyarwanda');
-insert into language(code, name) values ('sa', 'संस्कृतम्');
-insert into language(code, name) values ('sc', 'sardu');
-insert into language(code, name) values ('sd', 'सिन्धी');
-insert into language(code, name) values ('se', 'Davvisámegiella');
-insert into language(code, name) values ('sg', 'yângâ tî sängö');
-insert into language(code, name) values ('si', 'සිංහල');
-insert into language(code, name) values ('sk', 'slovenčina');
-insert into language(code, name) values ('sl', 'slovenščina');
-insert into language(code, name) values ('sm', E'gagana fa\'a Samoa');
-insert into language(code, name) values ('sn', 'chiShona');
-insert into language(code, name) values ('so', 'Soomaaliga');
-insert into language(code, name) values ('sq', 'Shqip');
-insert into language(code, name) values ('sr', 'српски језик');
-insert into language(code, name) values ('ss', 'SiSwati');
-insert into language(code, name) values ('st', 'Sesotho');
-insert into language(code, name) values ('su', 'Basa Sunda');
-insert into language(code, name) values ('sv', 'Svenska');
-insert into language(code, name) values ('sw', 'Kiswahili');
-insert into language(code, name) values ('ta', 'தமிழ்');
-insert into language(code, name) values ('te', 'తెలుగు');
-insert into language(code, name) values ('tg', 'тоҷикӣ');
-insert into language(code, name) values ('th', 'ไทย');
-insert into language(code, name) values ('ti', 'ትግርኛ');
-insert into language(code, name) values ('tk', 'Türkmençe');
-insert into language(code, name) values ('tl', 'Wikang Tagalog');
-insert into language(code, name) values ('tn', 'Setswana');
-insert into language(code, name) values ('to', 'faka Tonga');
-insert into language(code, name) values ('tr', 'Türkçe');
-insert into language(code, name) values ('ts', 'Xitsonga');
-insert into language(code, name) values ('tt', 'татар теле');
-insert into language(code, name) values ('tw', 'Twi');
-insert into language(code, name) values ('ty', 'Reo Tahiti');
-insert into language(code, name) values ('ug', 'ئۇيغۇرچە‎');
-insert into language(code, name) values ('uk', 'Українська');
-insert into language(code, name) values ('ur', 'اردو');
-insert into language(code, name) values ('uz', 'Ўзбек');
-insert into language(code, name) values ('ve', 'Tshivenḓa');
-insert into language(code, name) values ('vi', 'Tiếng Việt');
-insert into language(code, name) values ('vo', 'Volapük');
-insert into language(code, name) values ('wa', 'walon');
-insert into language(code, name) values ('wo', 'Wollof');
-insert into language(code, name) values ('xh', 'isiXhosa');
-insert into language(code, name) values ('yi', 'ייִדיש');
-insert into language(code, name) values ('yo', 'Yorùbá');
-insert into language(code, name) values ('za', 'Saɯ cueŋƅ');
-insert into language(code, name) values ('zh', '中文');
-insert into language(code, name) values ('zu', 'isiZulu');
+ALTER TABLE local_user RENAME COLUMN lang TO interface_language;
+
+INSERT INTO
+LANGUAGE (id, code, name)
+    VALUES (0, 'und', 'Undetermined');
+
+ALTER TABLE post
+    ADD COLUMN language_id integer REFERENCES LANGUAGE NOT
+    NULL DEFAULT 0;
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('aa', 'Afaraf');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ab', 'аҧсуа бызшәа');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ae', 'avesta');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('af', 'Afrikaans');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ak', 'Akan');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('am', 'አማርኛ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('an', 'aragonés');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ar', 'اَلْعَرَبِيَّةُ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('as', 'অসমীয়া');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('av', 'авар мацӀ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ay', 'aymar aru');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('az', 'azərbaycan dili');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ba', 'башҡорт теле');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('be', 'беларуская мова');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('bg', 'български език');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('bi', 'Bislama');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('bm', 'bamanankan');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('bn', 'বাংলা');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('bo', 'བོད་ཡིག');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('br', 'brezhoneg');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('bs', 'bosanski jezik');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ca', 'Català');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ce', 'нохчийн мотт');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ch', 'Chamoru');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('co', 'corsu');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('cr', 'ᓀᐦᐃᔭᐍᐏᐣ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('cs', 'čeština');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('cu', 'ѩзыкъ словѣньскъ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('cv', 'чӑваш чӗлхи');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('cy', 'Cymraeg');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('da', 'dansk');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('de', 'Deutsch');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('dv', 'ދިވެހި');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('dz', 'རྫོང་ཁ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ee', 'Eʋegbe');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('el', 'Ελληνικά');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('en', 'English');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('eo', 'Esperanto');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('es', 'Español');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('et', 'eesti');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('eu', 'euskara');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('fa', 'فارسی');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ff', 'Fulfulde');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('fi', 'suomi');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('fj', 'vosa Vakaviti');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('fo', 'føroyskt');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('fr', 'Français');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('fy', 'Frysk');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ga', 'Gaeilge');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('gd', 'Gàidhlig');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('gl', 'galego');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('gn', E'Avañe\'ẽ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('gu', 'ગુજરાતી');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('gv', 'Gaelg');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ha', 'هَوُسَ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('he', 'עברית');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('hi', 'हिन्दी');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ho', 'Hiri Motu');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('hr', 'Hrvatski');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ht', 'Kreyòl ayisyen');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('hu', 'magyar');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('hy', 'Հայերեն');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('hz', 'Otjiherero');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ia', 'Interlingua');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('id', 'Bahasa Indonesia');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ie', 'Interlingue');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ig', 'Asụsụ Igbo');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ii', 'ꆈꌠ꒿ Nuosuhxop');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ik', 'Iñupiaq');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('io', 'Ido');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('is', 'Íslenska');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('it', 'Italiano');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('iu', 'ᐃᓄᒃᑎᑐᑦ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ja', '日本語');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('jv', 'basa Jawa');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ka', 'ქართული');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('kg', 'Kikongo');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ki', 'Gĩkũyũ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('kj', 'Kuanyama');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('kk', 'қазақ тілі');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('kl', 'kalaallisut');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('km', 'ខេមរភាសា');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('kn', 'ಕನ್ನಡ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ko', '한국어');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('kr', 'Kanuri');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ks', 'कश्मीरी');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ku', 'Kurdî');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('kv', 'коми кыв');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('kw', 'Kernewek');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ky', 'Кыргызча');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('la', 'latine');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('lb', 'Lëtzebuergesch');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('lg', 'Luganda');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('li', 'Limburgs');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ln', 'Lingála');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('lo', 'ພາສາລາວ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('lt', 'lietuvių kalba');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('lu', 'Kiluba');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('lv', 'latviešu valoda');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('mg', 'fiteny malagasy');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('mh', 'Kajin M̧ajeļ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('mi', 'te reo Māori');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('mk', 'македонски јазик');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ml', 'മലയാളം');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('mn', 'Монгол хэл');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('mr', 'मराठी');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ms', 'Bahasa Melayu');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('mt', 'Malti');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('my', 'ဗမာစာ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('na', 'Dorerin Naoero');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('nb', 'Norsk bokmål');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('nd', 'isiNdebele');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ne', 'नेपाली');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ng', 'Owambo');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('nl', 'Nederlands');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('nn', 'Norsk nynorsk');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('no', 'Norsk');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('nr', 'isiNdebele');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('nv', 'Diné bizaad');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ny', 'chiCheŵa');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('oc', 'occitan');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('oj', 'ᐊᓂᔑᓈᐯᒧᐎᓐ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('om', 'Afaan Oromoo');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('or', 'ଓଡ଼ିଆ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('os', 'ирон æвзаг');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('pa', 'ਪੰਜਾਬੀ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('pi', 'पाऴि');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('pl', 'Polski');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ps', 'پښتو');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('pt', 'Português');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('qu', 'Runa Simi');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('rm', 'rumantsch grischun');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('rn', 'Ikirundi');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ro', 'Română');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ru', 'Русский');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('rw', 'Ikinyarwanda');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('sa', 'संस्कृतम्');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('sc', 'sardu');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('sd', 'सिन्धी');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('se', 'Davvisámegiella');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('sg', 'yângâ tî sängö');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('si', 'සිංහල');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('sk', 'slovenčina');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('sl', 'slovenščina');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('sm', E'gagana fa\'a Samoa');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('sn', 'chiShona');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('so', 'Soomaaliga');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('sq', 'Shqip');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('sr', 'српски језик');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ss', 'SiSwati');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('st', 'Sesotho');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('su', 'Basa Sunda');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('sv', 'Svenska');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('sw', 'Kiswahili');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ta', 'தமிழ்');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('te', 'తెలుగు');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('tg', 'тоҷикӣ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('th', 'ไทย');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ti', 'ትግርኛ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('tk', 'Türkmençe');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('tl', 'Wikang Tagalog');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('tn', 'Setswana');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('to', 'faka Tonga');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('tr', 'Türkçe');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ts', 'Xitsonga');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('tt', 'татар теле');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('tw', 'Twi');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ty', 'Reo Tahiti');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ug', 'ئۇيغۇرچە‎');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('uk', 'Українська');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ur', 'اردو');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('uz', 'Ўзбек');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('ve', 'Tshivenḓa');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('vi', 'Tiếng Việt');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('vo', 'Volapük');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('wa', 'walon');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('wo', 'Wollof');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('xh', 'isiXhosa');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('yi', 'ייִדיש');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('yo', 'Yorùbá');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('za', 'Saɯ cueŋƅ');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('zh', '中文');
+
+INSERT INTO
+LANGUAGE (code, name)
+    VALUES ('zu', 'isiZulu');
+
index 6385f62c45ac026408ebb4f8e719e1d0389f5a19..fce2e139b58951fbf4697e99dde4a5e2743b19b8 100644 (file)
@@ -1,25 +1,45 @@
-alter table comment add column parent_id integer;
+ALTER TABLE comment
+    ADD COLUMN parent_id integer;
 
 -- Constraints and index
-alter table comment add constraint comment_parent_id_fkey foreign key (parent_id) REFERENCES comment(id) ON UPDATE CASCADE ON DELETE CASCADE;
-create index idx_comment_parent on comment (parent_id);
+ALTER TABLE comment
+    ADD CONSTRAINT comment_parent_id_fkey FOREIGN KEY (parent_id) REFERENCES comment (id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+CREATE INDEX idx_comment_parent ON comment (parent_id);
 
 -- Update the parent_id column
 -- subpath(subpath(0, -1), -1) gets the immediate parent but it fails null checks
-update comment set parent_id = cast(ltree2text(nullif(subpath(nullif(subpath(path, 0, -1), '0'), -1), '0')) as INTEGER);
-
-alter table comment drop column path;
-alter table comment_aggregates drop column child_count;
+UPDATE
+    comment
+SET
+    parent_id = cast(ltree2text (nullif (subpath (nullif (subpath (path, 0, -1), '0'), -1), '0')) AS INTEGER);
 
-drop extension ltree;
+ALTER TABLE comment
+    DROP COLUMN path;
 
--- Add back in the read column
-alter table comment add column read boolean default false not null;
+ALTER TABLE comment_aggregates
+    DROP COLUMN child_count;
 
-update comment c set read = cr.read
-from comment_reply cr where cr.comment_id = c.id;
+DROP EXTENSION ltree;
 
-create view comment_alias_1 as select * from comment;    
-
-drop table comment_reply;
+-- Add back in the read column
+ALTER TABLE comment
+    ADD COLUMN read boolean DEFAULT FALSE NOT NULL;
+
+UPDATE
+    comment c
+SET
+    read = cr.read
+FROM
+    comment_reply cr
+WHERE
+    cr.comment_id = c.id;
+
+CREATE VIEW comment_alias_1 AS
+SELECT
+    *
+FROM
+    comment;
+
+DROP TABLE comment_reply;
 
index 4d2598dda77fb5888e5fa51f5682c83762e9f876..08c0141d3194781a94482ab0a651ca5243cec7b6 100644 (file)
 -- Remove the comment.read column, and create a new comment_reply table,
--- similar to the person_mention table. 
--- 
+-- similar to the person_mention table.
+--
 -- This is necessary because self-joins using ltrees would be too tough with SQL views
--- 
--- Every comment should have a row here, because all comments have a recipient, 
+--
+-- Every comment should have a row here, because all comments have a recipient,
 -- either the post creator, or the parent commenter.
-create table comment_reply(
-  id serial primary key,
-  recipient_id int references person on update cascade on delete cascade not null,
-  comment_id int references comment on update cascade on delete cascade not null,
-  read boolean default false not null,
-  published timestamp not null default now(),
-  unique(recipient_id, comment_id)
+CREATE TABLE comment_reply (
+    id serial PRIMARY KEY,
+    recipient_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    comment_id int REFERENCES COMMENT ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    read boolean DEFAULT FALSE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (recipient_id, comment_id)
 );
 
 -- Ones where parent_id is null, use the post creator recipient
-insert into comment_reply (recipient_id, comment_id, read)
-select p.creator_id, c.id, c.read from comment c
-inner join post p on c.post_id = p.id
-where c.parent_id is null;
+INSERT INTO comment_reply (recipient_id, comment_id, read)
+SELECT
+    p.creator_id,
+    c.id,
+    c.read
+FROM
+    comment c
+    INNER JOIN post p ON c.post_id = p.id
+WHERE
+    c.parent_id IS NULL;
 
 --  Ones where there is a parent_id, self join to comment to get the parent comment creator
-insert into comment_reply (recipient_id, comment_id, read)
-select c2.creator_id, c.id, c.read from comment c
-inner join comment c2 on c.parent_id = c2.id;
+INSERT INTO comment_reply (recipient_id, comment_id, read)
+SELECT
+    c2.creator_id,
+    c.id,
+    c.read
+FROM
+    comment c
+    INNER JOIN comment c2 ON c.parent_id = c2.id;
 
 -- Drop comment_alias view
-drop view comment_alias_1;
+DROP VIEW comment_alias_1;
 
-alter table comment drop column read;
+ALTER TABLE comment
+    DROP COLUMN read;
 
-create extension if not exists ltree;
+CREATE EXTENSION IF NOT EXISTS ltree;
 
-alter table comment add column path ltree not null default '0';
-alter table comment_aggregates add column child_count integer not null default 0;
+ALTER TABLE comment
+    ADD COLUMN path ltree NOT NULL DEFAULT '0';
 
--- The ltree path column should be the comment_id parent paths, separated by dots. 
+ALTER TABLE comment_aggregates
+    ADD COLUMN child_count integer NOT NULL DEFAULT 0;
+
+-- The ltree path column should be the comment_id parent paths, separated by dots.
 -- Stackoverflow: building an ltree from a parent_id hierarchical tree:
 -- https://stackoverflow.com/a/1144848/1655478
-
-create temporary table comment_temp as 
+CREATE TEMPORARY TABLE comment_temp AS
 WITH RECURSIVE q AS (
-       SELECT  h, 1 AS level, ARRAY[id] AS breadcrumb
-       FROM    comment h
-       WHERE   parent_id is null
-       UNION ALL
-       SELECT  hi, q.level + 1 AS level, breadcrumb || id
-       FROM    q
-       JOIN    comment hi
-       ON      hi.parent_id = (q.h).id
+    SELECT
+        h,
+        1 AS level,
+        ARRAY[id] AS breadcrumb
+    FROM
+        comment h
+    WHERE
+        parent_id IS NULL
+    UNION ALL
+    SELECT
+        hi,
+        q.level + 1 AS level,
+        breadcrumb || id
+    FROM
+        q
+        JOIN comment hi ON hi.parent_id = (q.h).id
 )
-SELECT  (q.h).id,
-       (q.h).parent_id,
-       level,
-       breadcrumb::VARCHAR AS path,
-       text2ltree('0.' || array_to_string(breadcrumb, '.')) as ltree_path
-FROM    q
+SELECT
+    (q.h).id,
+    (q.h).parent_id,
+    level,
+    breadcrumb::varchar AS path,
+    text2ltree ('0.' || array_to_string(breadcrumb, '.')) AS ltree_path
+FROM
+    q
 ORDER BY
-       breadcrumb;
+    breadcrumb;
 
 -- Remove indexes and foreign key constraints, and disable triggers for faster updates
-alter table comment disable trigger user;
+ALTER TABLE comment DISABLE TRIGGER USER;
+
+ALTER TABLE comment
+    DROP CONSTRAINT IF EXISTS comment_creator_id_fkey;
+
+ALTER TABLE comment
+    DROP CONSTRAINT IF EXISTS comment_parent_id_fkey;
+
+ALTER TABLE comment
+    DROP CONSTRAINT IF EXISTS comment_post_id_fkey;
+
+ALTER TABLE comment
+    DROP CONSTRAINT IF EXISTS idx_comment_ap_id;
+
+DROP INDEX IF EXISTS idx_comment_creator;
+
+DROP INDEX IF EXISTS idx_comment_parent;
 
-alter table comment drop constraint if exists comment_creator_id_fkey;
-alter table comment drop constraint if exists comment_parent_id_fkey;
-alter table comment drop constraint if exists comment_post_id_fkey;
-alter table comment drop constraint if exists idx_comment_ap_id;
+DROP INDEX IF EXISTS idx_comment_post;
 
-drop index if exists idx_comment_creator;
-drop index if exists idx_comment_parent;
-drop index if exists idx_comment_post;
-drop index if exists idx_comment_published;
+DROP INDEX IF EXISTS idx_comment_published;
 
 -- Add the ltree column
-update comment c 
-set path = ct.ltree_path
-from comment_temp ct
-where c.id = ct.id;
+UPDATE
+    comment c
+SET
+    path = ct.ltree_path
+FROM
+    comment_temp ct
+WHERE
+    c.id = ct.id;
 
 -- Update the child counts
-update comment_aggregates ca set child_count = c2.child_count
-from (
-  select c.id, c.path, count(c2.id) as child_count from comment c
-  left join comment c2 on c2.path <@ c.path and c2.path != c.path
-  group by c.id
-) as c2
-where ca.comment_id = c2.id;
+UPDATE
+    comment_aggregates ca
+SET
+    child_count = c2.child_count
+FROM (
+    SELECT
+        c.id,
+        c.path,
+        count(c2.id) AS child_count
+    FROM
+        comment c
+    LEFT JOIN comment c2 ON c2.path <@ c.path
+        AND c2.path != c.path
+GROUP BY
+    c.id) AS c2
+WHERE
+    ca.comment_id = c2.id;
 
 -- Delete comments at a depth of > 150, otherwise the index creation below will fail
-delete from comment where nlevel(path) > 150;
+DELETE FROM comment
+WHERE nlevel (path) > 150;
 
 -- Delete from comment where there is a missing post
-delete from comment c where not exists (
-  select from post p where p.id = c.post_id
-);
+DELETE FROM comment c
+WHERE NOT EXISTS (
+        SELECT
+        FROM
+            post p
+        WHERE
+            p.id = c.post_id);
 
 -- Delete from comment where there is a missing creator_id
-delete from comment c where not exists (
-  select from person p where p.id = c.creator_id
-);
+DELETE FROM comment c
+WHERE NOT EXISTS (
+        SELECT
+        FROM
+            person p
+        WHERE
+            p.id = c.creator_id);
 
 -- Re-enable old constraints and indexes
-alter table comment add constraint "comment_creator_id_fkey" FOREIGN KEY (creator_id) REFERENCES person(id) ON UPDATE CASCADE ON DELETE CASCADE;
-alter table comment add constraint "comment_post_id_fkey" FOREIGN KEY (post_id) REFERENCES post(id) ON UPDATE CASCADE ON DELETE CASCADE;
-alter table comment add constraint "idx_comment_ap_id" unique (ap_id);
+ALTER TABLE comment
+    ADD CONSTRAINT "comment_creator_id_fkey" FOREIGN KEY (creator_id) REFERENCES person (id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+ALTER TABLE comment
+    ADD CONSTRAINT "comment_post_id_fkey" FOREIGN KEY (post_id) REFERENCES post (id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+ALTER TABLE comment
+    ADD CONSTRAINT "idx_comment_ap_id" UNIQUE (ap_id);
 
-create index idx_comment_creator on comment (creator_id);
-create index idx_comment_post on comment (post_id);
-create index idx_comment_published on comment (published desc);
+CREATE INDEX idx_comment_creator ON comment (creator_id);
+
+CREATE INDEX idx_comment_post ON comment (post_id);
+
+CREATE INDEX idx_comment_published ON comment (published DESC);
 
 -- Create the index
-create index idx_path_gist on comment using gist (path);
+CREATE INDEX idx_path_gist ON comment USING gist (path);
 
 -- Drop the parent_id column
-alter table comment drop column parent_id cascade;
+ALTER TABLE comment
+    DROP COLUMN parent_id CASCADE;
+
+ALTER TABLE comment ENABLE TRIGGER USER;
 
-alter table comment enable trigger user;
index c515275aebec37d098cf8a37ef4d59468dfc7b81..0f9228e9adf7c22799ae2c7ace5438bcab611d4f 100644 (file)
@@ -1 +1,3 @@
-alter table site drop column application_email_admins;
+ALTER TABLE site
+    DROP COLUMN application_email_admins;
+
index adf8f3c11075cb0450e268b5ab1ea05040a79063..0bad529343fb7898cc4acba2a8b8b018ab63252a 100644 (file)
@@ -1,2 +1,4 @@
 -- Adding a field to email admins for new applications
-alter table site add column application_email_admins boolean not null default false;
+ALTER TABLE site
+    ADD COLUMN application_email_admins boolean NOT NULL DEFAULT FALSE;
+
index fff64498ee03976c2d74ac9d00dde16475063cc4..8af586929ba872aa1679d7b1c61ab889f70d83b0 100644 (file)
@@ -1 +1,3 @@
-alter table comment drop column distinguished;
+ALTER TABLE comment
+    DROP COLUMN distinguished;
+
index a23d2331f8fd5e7a53bd634059b5139db4f1e883..16f416ae37611930da08c3499b1e0896555960dc 100644 (file)
@@ -1 +1,3 @@
-alter table comment add column distinguished boolean not null default false;
+ALTER TABLE comment
+    ADD COLUMN distinguished boolean NOT NULL DEFAULT FALSE;
+
index f4ae52633c15476d15061919b8214a23a82f8753..e48e0a8db40a0b3916e565cc057363979383038d 100644 (file)
@@ -1 +1,2 @@
-drop table person_post_aggregates;
+DROP TABLE person_post_aggregates;
+
index 9a0a5fa5f39a4885569e9f8b29af5eff77cd0b28..4fcae3b2eac46033ea9a4fa294bb70615d094bf2 100644 (file)
@@ -1,11 +1,12 @@
 -- This table stores the # of read comments for a person, on a post
 -- It can then be joined to post_aggregates to get an unread count:
 -- unread = post_aggregates.comments - person_post_aggregates.read_comments
-create table person_post_aggregates(
-  id serial primary key,
-  person_id int references person on update cascade on delete cascade not null,
-  post_id int references post on update cascade on delete cascade not null,
-  read_comments bigint not null default 0,
-  published timestamp not null default now(),
-  unique(person_id, post_id)
+CREATE TABLE person_post_aggregates (
+    id serial PRIMARY KEY,
+    person_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    post_id int REFERENCES post ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    read_comments bigint NOT NULL DEFAULT 0,
+    published timestamp NOT NULL DEFAULT now(),
+    UNIQUE (person_id, post_id)
 );
+
index 7c15bfdc1d41ff0f12921ca742a12d55f8d64fc4..745a6b35cfc402e320cf4f426992d612adaeb35b 100644 (file)
@@ -1 +1,3 @@
-alter table comment drop column language_id;
+ALTER TABLE comment
+    DROP COLUMN language_id;
+
index 78fe0e957b6368f9e4c40f9694fe15d7e3c4b994..782931cb593d822f70e771b17b0363d0fb29aa9f 100644 (file)
@@ -1 +1,4 @@
-alter table comment add column language_id integer references language not null default 0;
+ALTER TABLE comment
+    ADD COLUMN language_id integer REFERENCES LANGUAGE NOT
+    NULL DEFAULT 0;
+
index 3fc7778a510697c50e490e542e3eb7cddcf7b1fb..eede81868793cdd83a3fd4b127b48dbeb074ffbf 100644 (file)
@@ -1 +1,2 @@
-drop function drop_ccnew_indexes;
+DROP FUNCTION drop_ccnew_indexes;
+
index 2a861aaff8e8415ebfd4f9f543e7247479583301..0314b1ddc89bb5eedba4f5627a291923ebfae1a9 100644 (file)
@@ -1,13 +1,21 @@
-CREATE OR REPLACE FUNCTION drop_ccnew_indexes() RETURNS INTEGER AS $$
+CREATE OR REPLACE FUNCTION drop_ccnew_indexes ()
+    RETURNS integer
+    AS $$
 DECLARE
-i RECORD;
+    i RECORD;
 BEGIN
-  FOR i IN
-    (SELECT relname FROM pg_class WHERE relname like '%ccnew%')
-    LOOP
-      EXECUTE 'DROP INDEX ' || i.relname;
-    END LOOP;
+    FOR i IN (
+        SELECT
+            relname
+        FROM
+            pg_class
+        WHERE
+            relname LIKE '%ccnew%')
+        LOOP
+            EXECUTE 'DROP INDEX ' || i.relname;
+        END LOOP;
     RETURN 1;
-  END;
-$$ LANGUAGE plpgsql;
+END;
+$$
+LANGUAGE plpgsql;
 
index 1db179affd7edfdf302ae4458009c0b1729b8435..46466d5e3c0275c34724e3e4186a8fab242b47fb 100644 (file)
@@ -1 +1,2 @@
-drop table private_message_report;
+DROP TABLE private_message_report;
+
index 7574f7cf4e6fca1600b27c0691a583146a622d94..aa168c33947f3b7da6eb9206144f88e4678bd715 100644 (file)
@@ -1,12 +1,13 @@
-create table private_message_report (
-  id            serial    primary key,
-  creator_id    int       references person on update cascade on delete cascade not null,   -- user reporting comment
-  private_message_id    int       references private_message on update cascade on delete cascade not null, -- comment being reported
-  original_pm_text  text      not null,
-  reason        text      not null,
-  resolved      bool      not null default false,
-  resolver_id   int       references person on update cascade on delete cascade,   -- user resolving report
-  published     timestamp not null default now(),
-  updated       timestamp null,
-  unique(private_message_id, creator_id) -- users should only be able to report a pm once
+CREATE TABLE private_message_report (
+    id serial PRIMARY KEY,
+    creator_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, -- user reporting comment
+    private_message_id int REFERENCES private_message ON UPDATE CASCADE ON DELETE CASCADE NOT NULL, -- comment being reported
+    original_pm_text text NOT NULL,
+    reason text NOT NULL,
+    resolved bool NOT NULL DEFAULT FALSE,
+    resolver_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE, -- user resolving report
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp NULL,
+    UNIQUE (private_message_id, creator_id) -- users should only be able to report a pm once
 );
+
index eeff85b2caacd41d2eb4bd656825283d165e88ea..4cebf533619972965d1af0ea0e71e3cd317110cd 100644 (file)
@@ -1,3 +1,6 @@
-drop table site_language;
-drop table community_language;
-delete from local_user_language;
+DROP TABLE site_language;
+
+DROP TABLE community_language;
+
+DELETE FROM local_user_language;
+
index 7687c1b31ddf9643ed41d689d583fc42524ff02c..0f56b4782cc50892ae7886f49e797400a43f5ae4 100644 (file)
@@ -1,38 +1,62 @@
-create table site_language (
-  id serial primary key,
-  site_id int references site on update cascade on delete cascade not null,
-  language_id int references language on update cascade on delete cascade not null,
-  unique (site_id, language_id)
+CREATE TABLE site_language (
+    id serial PRIMARY KEY,
+    site_id int REFERENCES site ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    language_id int REFERENCES
+    LANGUAGE ON
+    UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    UNIQUE (site_id, language_id)
 );
 
-create table community_language (
-  id serial primary key,
-  community_id int references community on update cascade on delete cascade not null,
-  language_id int references language on update cascade on delete cascade not null,
-  unique (community_id, language_id)
+CREATE TABLE community_language (
+    id serial PRIMARY KEY,
+    community_id int REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    language_id int REFERENCES
+    LANGUAGE ON
+    UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    UNIQUE (community_id, language_id)
 );
 
 -- update existing users, sites and communities to have all languages enabled
-do $$
-    declare
-        xid integer;
-begin
-    for xid in select id from local_user
-    loop
-        insert into local_user_language (local_user_id, language_id)
-        (select xid, language.id as lid from language);
-    end loop;
-
-    for xid in select id from site
-    loop
-        insert into site_language (site_id, language_id)
-        (select xid, language.id as lid from language);
-    end loop;
-
-    for xid in select id from community
-    loop
-        insert into community_language (community_id, language_id)
-        (select xid, language.id as lid from language);
-    end loop;
-end;
+DO $$
+DECLARE
+    xid integer;
+BEGIN
+    FOR xid IN
+    SELECT
+        id
+    FROM
+        local_user LOOP
+            INSERT INTO local_user_language (local_user_id, language_id) (
+                SELECT
+                    xid,
+                    language.id AS lid
+                FROM
+                    LANGUAGE);
+        END LOOP;
+    FOR xid IN
+    SELECT
+        id
+    FROM
+        site LOOP
+            INSERT INTO site_language (site_id, language_id) (
+                SELECT
+                    xid,
+                    language.id AS lid
+                FROM
+                    LANGUAGE);
+        END LOOP;
+    FOR xid IN
+    SELECT
+        id
+    FROM
+        community LOOP
+            INSERT INTO community_language (community_id, language_id) (
+                SELECT
+                    xid,
+                    language.id AS lid
+                FROM
+                    LANGUAGE);
+        END LOOP;
+END;
 $$;
+
index 39b439a0aa348e8ac43795d9582da2424dbdd8cd..7aac082ba5006929deb616f363c05d821d81761e 100644 (file)
@@ -1,2 +1,12 @@
-create view person_alias_1 as select * from person;
-create view person_alias_2 as select * from person;
+CREATE VIEW person_alias_1 AS
+SELECT
+    *
+FROM
+    person;
+
+CREATE VIEW person_alias_2 AS
+SELECT
+    *
+FROM
+    person;
+
index 36eabecab74b890966a9443d0acd7c923c18ce6c..f4ac9c77144164dab59ecd826489842a28dc6593 100644 (file)
@@ -1,2 +1,3 @@
 -- Drop the alias views
-drop view person_alias_1, person_alias_2;
+DROP VIEW person_alias_1, person_alias_2;
+
index 3ca49b54875f8f151e1ab951699ffe477805a382..af95b8773d81d41db3e8f98ab9d329ebda283ac4 100644 (file)
@@ -1,63 +1,78 @@
 -- Add back site columns
-alter table site
-  add column enable_downvotes boolean default true not null,
-  add column open_registration boolean default true not null,
-  add column enable_nsfw boolean default true not null,
-  add column community_creation_admin_only boolean default false not null,
-  add column require_email_verification boolean default false not null,
-  add column require_application boolean default true not null,
-  add column application_question text default 'to verify that you are human, please explain why you want to create an account on this site'::text,
-  add column private_instance boolean default false not null,
-  add column default_theme text default 'browser'::text not null,
-  add column default_post_listing_type text default 'Local'::text not null,
-  add column legal_information text,
-  add column hide_modlog_mod_names boolean default true not null,
-  add column application_email_admins boolean default false not null;
+ALTER TABLE site
+    ADD COLUMN enable_downvotes boolean DEFAULT TRUE NOT NULL,
+    ADD COLUMN open_registration boolean DEFAULT TRUE NOT NULL,
+    ADD COLUMN enable_nsfw boolean DEFAULT TRUE NOT NULL,
+    ADD COLUMN community_creation_admin_only boolean DEFAULT FALSE NOT NULL,
+    ADD COLUMN require_email_verification boolean DEFAULT FALSE NOT NULL,
+    ADD COLUMN require_application boolean DEFAULT TRUE NOT NULL,
+    ADD COLUMN application_question text DEFAULT 'to verify that you are human, please explain why you want to create an account on this site'::text,
+    ADD COLUMN private_instance boolean DEFAULT FALSE NOT NULL,
+    ADD COLUMN default_theme text DEFAULT 'browser'::text NOT NULL,
+    ADD COLUMN default_post_listing_type text DEFAULT 'Local'::text NOT NULL,
+    ADD COLUMN legal_information text,
+    ADD COLUMN hide_modlog_mod_names boolean DEFAULT TRUE NOT NULL,
+    ADD COLUMN application_email_admins boolean DEFAULT FALSE NOT NULL;
 
 -- Insert the data back from local_site
-update site set
-  enable_downvotes = ls.enable_downvotes,
-  open_registration = ls.open_registration,
-  enable_nsfw = ls.enable_nsfw,
-  community_creation_admin_only = ls.community_creation_admin_only,
-  require_email_verification = ls.require_email_verification,
-  require_application = ls.require_application,
-  application_question = ls.application_question,
-  private_instance = ls.private_instance,
-  default_theme = ls.default_theme,
-  default_post_listing_type = ls.default_post_listing_type,
-  legal_information = ls.legal_information,
-  hide_modlog_mod_names = ls.hide_modlog_mod_names,
-  application_email_admins = ls.application_email_admins,
-  published = ls.published,
-  updated = ls.updated
-from (select 
-  site_id, 
-  enable_downvotes,
-  open_registration,
-  enable_nsfw,
-  community_creation_admin_only,
-  require_email_verification,
-  require_application,
-  application_question,
-  private_instance,
-  default_theme,
-  default_post_listing_type,
-  legal_information,
-  hide_modlog_mod_names,
-  application_email_admins,
-  published,
-  updated
-from local_site) as ls
-where site.id = ls.site_id;
+UPDATE
+    site
+SET
+    enable_downvotes = ls.enable_downvotes,
+    open_registration = ls.open_registration,
+    enable_nsfw = ls.enable_nsfw,
+    community_creation_admin_only = ls.community_creation_admin_only,
+    require_email_verification = ls.require_email_verification,
+    require_application = ls.require_application,
+    application_question = ls.application_question,
+    private_instance = ls.private_instance,
+    default_theme = ls.default_theme,
+    default_post_listing_type = ls.default_post_listing_type,
+    legal_information = ls.legal_information,
+    hide_modlog_mod_names = ls.hide_modlog_mod_names,
+    application_email_admins = ls.application_email_admins,
+    published = ls.published,
+    updated = ls.updated
+FROM (
+    SELECT
+        site_id,
+        enable_downvotes,
+        open_registration,
+        enable_nsfw,
+        community_creation_admin_only,
+        require_email_verification,
+        require_application,
+        application_question,
+        private_instance,
+        default_theme,
+        default_post_listing_type,
+        legal_information,
+        hide_modlog_mod_names,
+        application_email_admins,
+        published,
+        updated
+    FROM
+        local_site) AS ls
+WHERE
+    site.id = ls.site_id;
 
 -- drop instance columns
-alter table site drop column instance_id;
-alter table person drop column instance_id;
-alter table community drop column instance_id;
-
-drop table local_site_rate_limit;
-drop table local_site;
-drop table federation_allowlist;
-drop table federation_blocklist;
-drop table instance;
+ALTER TABLE site
+    DROP COLUMN instance_id;
+
+ALTER TABLE person
+    DROP COLUMN instance_id;
+
+ALTER TABLE community
+    DROP COLUMN instance_id;
+
+DROP TABLE local_site_rate_limit;
+
+DROP TABLE local_site;
+
+DROP TABLE federation_allowlist;
+
+DROP TABLE federation_blocklist;
+
+DROP TABLE instance;
+
index e814ce27a157afcea260123b612f1c89b3f38bca..3b97daccd8a809be100195c59d39833746e7f446 100644 (file)
 -- Create an instance table
 -- Holds any connected or unconnected domain
-create table instance (
-  id serial primary key,
-  domain varchar(255) not null unique,
-  published timestamp not null default now(),
-  updated timestamp null
+CREATE TABLE instance (
+    id serial PRIMARY KEY,
+    domain varchar(255) NOT NULL UNIQUE,
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp NULL
 );
 
 -- Insert all the domains to the instance table
-insert into instance (domain)
-select distinct substring(p.actor_id from '(?:.*://)?(?:www\.)?([^/?]*)') from ( 
-  select actor_id from site 
-  union 
-  select actor_id from person 
-  union 
-  select actor_id from community
-) as p;
+INSERT INTO instance (DOMAIN)
+SELECT DISTINCT
+    substring(p.actor_id FROM '(?:.*://)?(?:www\.)?([^/?]*)')
+FROM (
+    SELECT
+        actor_id
+    FROM
+        site
+    UNION
+    SELECT
+        actor_id
+    FROM
+        person
+    UNION
+    SELECT
+        actor_id
+    FROM
+        community) AS p;
 
 -- Alter site, person, and community tables to reference the instance table.
-alter table site add column 
-instance_id int references instance on update cascade on delete cascade;
+ALTER TABLE site
+    ADD COLUMN instance_id int REFERENCES instance ON UPDATE CASCADE ON DELETE CASCADE;
 
-alter table person add column 
-instance_id int references instance on update cascade on delete cascade;
+ALTER TABLE person
+    ADD COLUMN instance_id int REFERENCES instance ON UPDATE CASCADE ON DELETE CASCADE;
 
-alter table community add column 
-instance_id int references instance on update cascade on delete cascade;
+ALTER TABLE community
+    ADD COLUMN instance_id int REFERENCES instance ON UPDATE CASCADE ON DELETE CASCADE;
 
 -- Add those columns
-update site set instance_id = i.id 
-from instance i
-where substring(actor_id from '(?:.*://)?(?:www\.)?([^/?]*)') = i.domain;
+UPDATE
+    site
+SET
+    instance_id = i.id
+FROM
+    instance i
+WHERE
+    substring(actor_id FROM '(?:.*://)?(?:www\.)?([^/?]*)') = i.domain;
+
+UPDATE
+    person
+SET
+    instance_id = i.id
+FROM
+    instance i
+WHERE
+    substring(actor_id FROM '(?:.*://)?(?:www\.)?([^/?]*)') = i.domain;
+
+UPDATE
+    community
+SET
+    instance_id = i.id
+FROM
+    instance i
+WHERE
+    substring(actor_id FROM '(?:.*://)?(?:www\.)?([^/?]*)') = i.domain;
 
-update person set instance_id = i.id 
-from instance i
-where substring(actor_id from '(?:.*://)?(?:www\.)?([^/?]*)') = i.domain;
+-- Make those columns unique not null now
+ALTER TABLE site
+    ALTER COLUMN instance_id SET NOT NULL;
 
-update community set instance_id = i.id 
-from instance i
-where substring(actor_id from '(?:.*://)?(?:www\.)?([^/?]*)') = i.domain;
+ALTER TABLE site
+    ADD CONSTRAINT idx_site_instance_unique UNIQUE (instance_id);
 
--- Make those columns unique not null now
-alter table site alter column instance_id set not null;
-alter table site add constraint idx_site_instance_unique unique (instance_id);
+ALTER TABLE person
+    ALTER COLUMN instance_id SET NOT NULL;
 
-alter table person alter column instance_id set not null;
-alter table community alter column instance_id set not null;
+ALTER TABLE community
+    ALTER COLUMN instance_id SET NOT NULL;
 
 -- Create allowlist and blocklist tables
-create table federation_allowlist (
-  id serial primary key,
-  instance_id int references instance on update cascade on delete cascade not null unique,
-  published timestamp not null default now(),
-  updated timestamp null
+CREATE TABLE federation_allowlist (
+    id serial PRIMARY KEY,
+    instance_id int REFERENCES instance ON UPDATE CASCADE ON DELETE CASCADE NOT NULL UNIQUE,
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp NULL
 );
 
-create table federation_blocklist (
-  id serial primary key,
-  instance_id int references instance on update cascade on delete cascade not null unique,
-  published timestamp not null default now(),
-  updated timestamp null
+CREATE TABLE federation_blocklist (
+    id serial PRIMARY KEY,
+    instance_id int REFERENCES instance ON UPDATE CASCADE ON DELETE CASCADE NOT NULL UNIQUE,
+    published timestamp NOT NULL DEFAULT now(),
+    updated timestamp NULL
 );
 
 -- Move all the extra site settings-type columns to a local_site table
 -- Add a lot of other fields currently in the lemmy.hjson
-create table local_site (
-  id serial primary key,
-  site_id int references site on update cascade on delete cascade not null unique,
-
-  -- Site table fields
-  site_setup boolean default false not null,
-  enable_downvotes boolean default true not null,
-  open_registration boolean default true not null,
-  enable_nsfw boolean default true not null,
-  community_creation_admin_only boolean default false not null,
-  require_email_verification boolean default false not null,
-  require_application boolean default true not null,
-  application_question text default 'to verify that you are human, please explain why you want to create an account on this site'::text,
-  private_instance boolean default false not null,
-  default_theme text default 'browser'::text not null,
-  default_post_listing_type text default 'Local'::text not null,
-  legal_information text,
-  hide_modlog_mod_names boolean default true not null,
-  application_email_admins boolean default false not null,
-
-  -- Fields from lemmy.hjson
-  slur_filter_regex text,
-  actor_name_max_length int default 20 not null,
-  federation_enabled boolean default true not null,
-  federation_debug boolean default false not null,
-  federation_strict_allowlist boolean default true not null,
-  federation_http_fetch_retry_limit int default 25 not null,
-  federation_worker_count int default 64 not null,
-  captcha_enabled boolean default false not null,
-  captcha_difficulty varchar(255) default 'medium' not null,
-
-  -- Time fields
-  published timestamp without time zone default now() not null,
-  updated timestamp without time zone
+CREATE TABLE local_site (
+    id serial PRIMARY KEY,
+    site_id int REFERENCES site ON UPDATE CASCADE ON DELETE CASCADE NOT NULL UNIQUE,
+    -- Site table fields
+    site_setup boolean DEFAULT FALSE NOT NULL,
+    enable_downvotes boolean DEFAULT TRUE NOT NULL,
+    open_registration boolean DEFAULT TRUE NOT NULL,
+    enable_nsfw boolean DEFAULT TRUE NOT NULL,
+    community_creation_admin_only boolean DEFAULT FALSE NOT NULL,
+    require_email_verification boolean DEFAULT FALSE NOT NULL,
+    require_application boolean DEFAULT TRUE NOT NULL,
+    application_question text DEFAULT 'to verify that you are human, please explain why you want to create an account on this site' ::text,
+    private_instance boolean DEFAULT FALSE NOT NULL,
+    default_theme text DEFAULT 'browser' ::text NOT NULL,
+    default_post_listing_type text DEFAULT 'Local' ::text NOT NULL,
+    legal_information text,
+    hide_modlog_mod_names boolean DEFAULT TRUE NOT NULL,
+    application_email_admins boolean DEFAULT FALSE NOT NULL,
+    -- Fields from lemmy.hjson
+    slur_filter_regex text,
+    actor_name_max_length int DEFAULT 20 NOT NULL,
+    federation_enabled boolean DEFAULT TRUE NOT NULL,
+    federation_debug boolean DEFAULT FALSE NOT NULL,
+    federation_strict_allowlist boolean DEFAULT TRUE NOT NULL,
+    federation_http_fetch_retry_limit int DEFAULT 25 NOT NULL,
+    federation_worker_count int DEFAULT 64 NOT NULL,
+    captcha_enabled boolean DEFAULT FALSE NOT NULL,
+    captcha_difficulty varchar(255) DEFAULT 'medium' NOT NULL,
+    -- Time fields
+    published timestamp without time zone DEFAULT now() NOT NULL,
+    updated timestamp without time zone
 );
 
 -- local_site_rate_limit is its own table, so as to not go over 32 columns, and force diesel to use the 64-column-tables feature
-create table local_site_rate_limit (
-  id serial primary key,
-  local_site_id int references local_site on update cascade on delete cascade not null unique,
-  message int default 180 not null,
-  message_per_second int default 60 not null,
-  post int default 6 not null,
-  post_per_second int default 600 not null,
-  register int default 3 not null,
-  register_per_second int default 3600 not null,
-  image int default 6 not null,
-  image_per_second int default 3600 not null,
-  comment int default 6 not null,
-  comment_per_second int default 600 not null,
-  search int default 60 not null,
-  search_per_second int default 600 not null,
-  published timestamp without time zone default now() not null,
-  updated timestamp without time zone
+CREATE TABLE local_site_rate_limit (
+    id serial PRIMARY KEY,
+    local_site_id int REFERENCES local_site ON UPDATE CASCADE ON DELETE CASCADE NOT NULL UNIQUE,
+    message int DEFAULT 180 NOT NULL,
+    message_per_second int DEFAULT 60 NOT NULL,
+    post int DEFAULT 6 NOT NULL,
+    post_per_second int DEFAULT 600 NOT NULL,
+    register int DEFAULT 3 NOT NULL,
+    register_per_second int DEFAULT 3600 NOT NULL,
+    image int DEFAULT 6 NOT NULL,
+    image_per_second int DEFAULT 3600 NOT NULL,
+    comment int DEFAULT 6 NOT NULL,
+    comment_per_second int DEFAULT 600 NOT NULL,
+    search int DEFAULT 60 NOT NULL,
+    search_per_second int DEFAULT 600 NOT NULL,
+    published timestamp without time zone DEFAULT now() NOT NULL,
+    updated timestamp without time zone
 );
 
 -- Insert the data into local_site
-insert into local_site (
-  site_id, 
-  site_setup,
-  enable_downvotes,
-  open_registration,
-  enable_nsfw,
-  community_creation_admin_only,
-  require_email_verification,
-  require_application,
-  application_question,
-  private_instance,
-  default_theme,
-  default_post_listing_type,
-  legal_information,
-  hide_modlog_mod_names,
-  application_email_admins,
-  published,
-  updated
-) 
-select 
-  id, 
-  true, -- Assume site if setup if there's already a site row
-  enable_downvotes,
-  open_registration,
-  enable_nsfw,
-  community_creation_admin_only,
-  require_email_verification,
-  require_application,
-  application_question,
-  private_instance,
-  default_theme,
-  default_post_listing_type,
-  legal_information,
-  hide_modlog_mod_names,
-  application_email_admins,
-  published,
-  updated
-from site
-order by id limit 1;
+INSERT INTO local_site (site_id, site_setup, enable_downvotes, open_registration, enable_nsfw, community_creation_admin_only, require_email_verification, require_application, application_question, private_instance, default_theme, default_post_listing_type, legal_information, hide_modlog_mod_names, application_email_admins, published, updated)
+SELECT
+    id,
+    TRUE, -- Assume site if setup if there's already a site row
+    enable_downvotes,
+    open_registration,
+    enable_nsfw,
+    community_creation_admin_only,
+    require_email_verification,
+    require_application,
+    application_question,
+    private_instance,
+    default_theme,
+    default_post_listing_type,
+    legal_information,
+    hide_modlog_mod_names,
+    application_email_admins,
+    published,
+    updated
+FROM
+    site
+ORDER BY
+    id
+LIMIT 1;
 
 -- Default here
-insert into local_site_rate_limit (
-  local_site_id
-)
-select id from local_site
-order by id limit 1;
+INSERT INTO local_site_rate_limit (local_site_id)
+SELECT
+    id
+FROM
+    local_site
+ORDER BY
+    id
+LIMIT 1;
 
 -- Drop all those columns from site
-alter table site
-  drop column enable_downvotes,
-  drop column open_registration,
-  drop column enable_nsfw,
-  drop column community_creation_admin_only,
-  drop column require_email_verification,
-  drop column require_application,
-  drop column application_question,
-  drop column private_instance,
-  drop column default_theme,
-  drop column default_post_listing_type,
-  drop column legal_information,
-  drop column hide_modlog_mod_names,
-  drop column application_email_admins;
+ALTER TABLE site
+    DROP COLUMN enable_downvotes,
+    DROP COLUMN open_registration,
+    DROP COLUMN enable_nsfw,
+    DROP COLUMN community_creation_admin_only,
+    DROP COLUMN require_email_verification,
+    DROP COLUMN require_application,
+    DROP COLUMN application_question,
+    DROP COLUMN private_instance,
+    DROP COLUMN default_theme,
+    DROP COLUMN default_post_listing_type,
+    DROP COLUMN legal_information,
+    DROP COLUMN hide_modlog_mod_names,
+    DROP COLUMN application_email_admins;
 
index 4867e5f1565484fba3e2ddc433b83a93f7820ba6..5471b4b7303c88aa69619cab4fd2b642a309218b 100644 (file)
@@ -1 +1,2 @@
-drop table tagline;
\ No newline at end of file
+DROP TABLE tagline;
+
index ff88cf4c2c9f0e91a390118ac6a72d4d7045d1ea..ed4daa63a0199af9e67d77b465bb77bc5ca906f2 100644 (file)
@@ -1,7 +1,8 @@
-create table tagline (
-  id serial primary key,
-  local_site_id int references local_site on update cascade on delete cascade not null,
-  content text not null,
-  published timestamp without time zone default now() not null,
-  updated timestamp without time zone
-);
\ No newline at end of file
+CREATE TABLE tagline (
+    id serial PRIMARY KEY,
+    local_site_id int REFERENCES local_site ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    content text NOT NULL,
+    published timestamp without time zone DEFAULT now() NOT NULL,
+    updated timestamp without time zone
+);
+
index 46a2b6a9b5a2f0a81ec891124b5e86f26f0f9d3c..767c9aa7f09bbbd99c6121414b99669e95566c8f 100644 (file)
@@ -1,47 +1,67 @@
-
 DROP TRIGGER IF EXISTS post_aggregates_featured_local ON post;
+
 DROP TRIGGER IF EXISTS post_aggregates_featured_community ON post;
-drop function post_aggregates_featured_community;
-drop function post_aggregates_featured_local;
-
-
-alter table post ADD stickied boolean NOT NULL DEFAULT false;
-Update post
-set stickied = featured_community;
-alter table post DROP COLUMN featured_community;
-alter table post DROP COLUMN featured_local;
-
-alter table post_aggregates ADD stickied boolean NOT NULL DEFAULT false;
-Update post_aggregates
-set stickied = featured_community;
-alter table post_aggregates DROP COLUMN featured_community;
-alter table post_aggregates DROP COLUMN featured_local;
-
-alter table mod_feature_post
-rename column featured TO stickied;
-
-alter table mod_feature_post
-DROP COLUMN is_featured_community;
-
-alter table mod_feature_post
-alter column stickied DROP NOT NULL;
-
-alter table mod_feature_post
-Rename To mod_sticky_post;
-
-create function post_aggregates_stickied()
-returns trigger language plpgsql
-as $$
-begin
-  update post_aggregates pa
-  set stickied = NEW.stickied
-  where pa.post_id = NEW.id;
-
-  return null;
-end $$;
-
-create trigger post_aggregates_stickied
-after update on post
-for each row
-when (OLD.stickied is distinct from NEW.stickied)
-execute procedure post_aggregates_stickied();
\ No newline at end of file
+
+DROP FUNCTION post_aggregates_featured_community;
+
+DROP FUNCTION post_aggregates_featured_local;
+
+ALTER TABLE post
+    ADD stickied boolean NOT NULL DEFAULT FALSE;
+
+UPDATE
+    post
+SET
+    stickied = featured_community;
+
+ALTER TABLE post
+    DROP COLUMN featured_community;
+
+ALTER TABLE post
+    DROP COLUMN featured_local;
+
+ALTER TABLE post_aggregates
+    ADD stickied boolean NOT NULL DEFAULT FALSE;
+
+UPDATE
+    post_aggregates
+SET
+    stickied = featured_community;
+
+ALTER TABLE post_aggregates
+    DROP COLUMN featured_community;
+
+ALTER TABLE post_aggregates
+    DROP COLUMN featured_local;
+
+ALTER TABLE mod_feature_post RENAME COLUMN featured TO stickied;
+
+ALTER TABLE mod_feature_post
+    DROP COLUMN is_featured_community;
+
+ALTER TABLE mod_feature_post
+    ALTER COLUMN stickied DROP NOT NULL;
+
+ALTER TABLE mod_feature_post RENAME TO mod_sticky_post;
+
+CREATE FUNCTION post_aggregates_stickied ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        post_aggregates pa
+    SET
+        stickied = NEW.stickied
+    WHERE
+        pa.post_id = NEW.id;
+    RETURN NULL;
+END
+$$;
+
+CREATE TRIGGER post_aggregates_stickied
+    AFTER UPDATE ON post
+    FOR EACH ROW
+    WHEN (OLD.stickied IS DISTINCT FROM NEW.stickied)
+    EXECUTE PROCEDURE post_aggregates_stickied ();
+
index 202c4c4b05a5102dd56a1254448cb44d5926d5f5..0411256d8439fdb3b60fb3e0e62ce44effe1669f 100644 (file)
@@ -1,63 +1,84 @@
-
 DROP TRIGGER IF EXISTS post_aggregates_stickied ON post;
-drop function 
-  post_aggregates_stickied;
-
-
-alter table post ADD featured_community boolean NOT NULL DEFAULT false;
-alter table post ADD featured_local boolean NOT NULL DEFAULT false;
-update post
-set featured_community = stickied;
-alter table post DROP COLUMN stickied;
-
-alter table post_aggregates ADD featured_community boolean NOT NULL DEFAULT false;
-alter table post_aggregates ADD featured_local boolean NOT NULL DEFAULT false;
-update post_aggregates
-set featured_community = stickied;
-alter table post_aggregates DROP COLUMN stickied;
-
-alter table mod_sticky_post
-rename column stickied TO featured;
-
-alter table mod_sticky_post
-alter column featured SET NOT NULL;
-
-alter table mod_sticky_post
-ADD is_featured_community boolean NOT NULL DEFAULT true;
-
-alter table mod_sticky_post
-Rename To mod_feature_post;
-
-create function post_aggregates_featured_community()
-returns trigger language plpgsql
-as $$
-begin
-  update post_aggregates pa
-  set featured_community = NEW.featured_community
-  where pa.post_id = NEW.id;
-  return null;
-end $$;
-
-create function post_aggregates_featured_local()
-returns trigger language plpgsql
-as $$
-begin
-  update post_aggregates pa
-  set featured_local = NEW.featured_local
-  where pa.post_id = NEW.id;
-  return null;
-end $$;
+
+DROP FUNCTION post_aggregates_stickied;
+
+ALTER TABLE post
+    ADD featured_community boolean NOT NULL DEFAULT FALSE;
+
+ALTER TABLE post
+    ADD featured_local boolean NOT NULL DEFAULT FALSE;
+
+UPDATE
+    post
+SET
+    featured_community = stickied;
+
+ALTER TABLE post
+    DROP COLUMN stickied;
+
+ALTER TABLE post_aggregates
+    ADD featured_community boolean NOT NULL DEFAULT FALSE;
+
+ALTER TABLE post_aggregates
+    ADD featured_local boolean NOT NULL DEFAULT FALSE;
+
+UPDATE
+    post_aggregates
+SET
+    featured_community = stickied;
+
+ALTER TABLE post_aggregates
+    DROP COLUMN stickied;
+
+ALTER TABLE mod_sticky_post RENAME COLUMN stickied TO featured;
+
+ALTER TABLE mod_sticky_post
+    ALTER COLUMN featured SET NOT NULL;
+
+ALTER TABLE mod_sticky_post
+    ADD is_featured_community boolean NOT NULL DEFAULT TRUE;
+
+ALTER TABLE mod_sticky_post RENAME TO mod_feature_post;
+
+CREATE FUNCTION post_aggregates_featured_community ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        post_aggregates pa
+    SET
+        featured_community = NEW.featured_community
+    WHERE
+        pa.post_id = NEW.id;
+    RETURN NULL;
+END
+$$;
+
+CREATE FUNCTION post_aggregates_featured_local ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    UPDATE
+        post_aggregates pa
+    SET
+        featured_local = NEW.featured_local
+    WHERE
+        pa.post_id = NEW.id;
+    RETURN NULL;
+END
+$$;
 
 CREATE TRIGGER post_aggregates_featured_community
-    AFTER UPDATE 
-    ON public.post
+    AFTER UPDATE ON public.post
     FOR EACH ROW
     WHEN (old.featured_community IS DISTINCT FROM new.featured_community)
-    EXECUTE FUNCTION public.post_aggregates_featured_community();
+    EXECUTE FUNCTION public.post_aggregates_featured_community ();
 
 CREATE TRIGGER post_aggregates_featured_local
-    AFTER UPDATE 
-    ON public.post
+    AFTER UPDATE ON public.post
     FOR EACH ROW
     WHEN (old.featured_local IS DISTINCT FROM new.featured_local)
-    EXECUTE FUNCTION public.post_aggregates_featured_local();
\ No newline at end of file
+    EXECUTE FUNCTION public.post_aggregates_featured_local ();
+
index 51452e195b5d5daf34dbd1386963d372ed9f7d54..066775ea9ffd046da779e289d372dfdf0fb40411 100644 (file)
@@ -1,2 +1,6 @@
-alter table local_site add column federation_strict_allowlist bool default true not null;
-alter table local_site add column federation_http_fetch_retry_limit int not null default 25;
+ALTER TABLE local_site
+    ADD COLUMN federation_strict_allowlist bool DEFAULT TRUE NOT NULL;
+
+ALTER TABLE local_site
+    ADD COLUMN federation_http_fetch_retry_limit int NOT NULL DEFAULT 25;
+
index e17563042ca3d2bbaad27f91ba2d89a8e73f1f67..6b2b3593652184ebeac6a42b1061ec7db7813537 100644 (file)
@@ -1,2 +1,6 @@
-alter table local_site drop column federation_strict_allowlist;
-alter table local_site drop column federation_http_fetch_retry_limit;
+ALTER TABLE local_site
+    DROP COLUMN federation_strict_allowlist;
+
+ALTER TABLE local_site
+    DROP COLUMN federation_http_fetch_retry_limit;
+
index 10118ba98f2984cd5249a4fe97d10404149b68c4..1803d4d69c7a42b63465b2ee4d0d7849e9416219 100644 (file)
@@ -1,3 +1,5 @@
-drop table person_follower;
+DROP TABLE person_follower;
+
+ALTER TABLE community_follower
+    ALTER COLUMN pending DROP NOT NULL;
 
-alter table community_follower alter column pending drop not null;
index 0ef6e8be71c2cfa17f9c9b711366b71ffa749b69..26456b84e57881a46e44601124bc712737c34b10 100644 (file)
@@ -1,12 +1,20 @@
 -- create user follower table with two references to persons
-create table person_follower (
-    id serial primary key,
-    person_id int references person on update cascade on delete cascade not null,
-    follower_id int references person on update cascade on delete cascade not null,
-    published timestamp not null default now(),
-    pending boolean not null,
-    unique (follower_id, person_id)
+CREATE TABLE person_follower (
+    id serial PRIMARY KEY,
+    person_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    follower_id int REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    published timestamp NOT NULL DEFAULT now(),
+    pending boolean NOT NULL,
+    UNIQUE (follower_id, person_id)
 );
 
-update community_follower set pending = false where pending is null;
-alter table community_follower alter column pending set not null;
+UPDATE
+    community_follower
+SET
+    pending = FALSE
+WHERE
+    pending IS NULL;
+
+ALTER TABLE community_follower
+    ALTER COLUMN pending SET NOT NULL;
+
index 25bb36c0c5906e87de8ec6c8823bdc03811b68e5..9b118ecdc98353ded640d1dec3d0b96f8af663e7 100644 (file)
@@ -1,31 +1,48 @@
 -- add back old registration columns
-alter table local_site add column open_registration boolean not null default true;
-alter table local_site add column require_application boolean not null default true;
+ALTER TABLE local_site
+    ADD COLUMN open_registration boolean NOT NULL DEFAULT TRUE;
+
+ALTER TABLE local_site
+    ADD COLUMN require_application boolean NOT NULL DEFAULT TRUE;
 
 -- regenerate their values
-with subquery as (
-    select registration_mode,
-        case
-            when registration_mode='closed' then false
-            else true
-        end
-    from local_site
-)
-update local_site
-set open_registration = subquery.case
-from subquery;
-with subquery as (
-    select registration_mode,
-        case
-            when registration_mode='open' then false
-            else true
-        end
-    from local_site
-)
-update local_site
-set require_application = subquery.case
-from subquery;
+WITH subquery AS (
+    SELECT
+        registration_mode,
+        CASE WHEN registration_mode = 'closed' THEN
+            FALSE
+        ELSE
+            TRUE
+        END
+    FROM
+        local_site)
+UPDATE
+    local_site
+SET
+    open_registration = subquery.case
+FROM
+    subquery;
+
+WITH subquery AS (
+    SELECT
+        registration_mode,
+        CASE WHEN registration_mode = 'open' THEN
+            FALSE
+        ELSE
+            TRUE
+        END
+    FROM
+        local_site)
+UPDATE
+    local_site
+SET
+    require_application = subquery.case
+FROM
+    subquery;
 
 -- drop new column and type
-alter table local_site drop column registration_mode;
-drop type registration_mode_enum;
\ No newline at end of file
+ALTER TABLE local_site
+    DROP COLUMN registration_mode;
+
+DROP TYPE registration_mode_enum;
+
index 34515e2cb9770e55464208e8ba8be0c50357d694..7f4d45ff8d12a62d07afd34dde2cd7c10bb4bcfc 100644 (file)
@@ -1,25 +1,40 @@
 -- create enum for registration modes
-create type registration_mode_enum as enum
-    ('closed', 'require_application', 'open');
+CREATE TYPE registration_mode_enum AS enum (
+    'closed',
+    'require_application',
+    'open'
+);
 
 -- use this enum for registration mode setting
-alter table local_site add column
-    registration_mode registration_mode_enum not null default 'require_application';
+ALTER TABLE local_site
+    ADD COLUMN registration_mode registration_mode_enum NOT NULL DEFAULT 'require_application';
 
 -- generate registration mode value from previous settings
-with subquery as (
-    select open_registration, require_application,
-        case
-            when open_registration=false then 'closed'::registration_mode_enum
-            when open_registration=true and require_application=true then 'require_application'
-            else 'open'
-        end
-    from local_site
-)
-update local_site
-set registration_mode = subquery.case
-from subquery;
+WITH subquery AS (
+    SELECT
+        open_registration,
+        require_application,
+        CASE WHEN open_registration = FALSE THEN
+            'closed'::registration_mode_enum
+        WHEN open_registration = TRUE
+            AND require_application = TRUE THEN
+            'require_application'
+        ELSE
+            'open'
+        END
+    FROM
+        local_site)
+UPDATE
+    local_site
+SET
+    registration_mode = subquery.case
+FROM
+    subquery;
 
 -- drop old registration settings
-alter table local_site drop column open_registration;
-alter table local_site drop column require_application;
+ALTER TABLE local_site
+    DROP COLUMN open_registration;
+
+ALTER TABLE local_site
+    DROP COLUMN require_application;
+
index d91843e1918e299e3d5fb82815bb221244376117..9064ea7602c08a082bf24992acd4a6fdbd594cf6 100644 (file)
@@ -1,22 +1,16 @@
 -- Drop the new indexes
-drop index idx_post_aggregates_featured_local_newest_comment_time,
-  idx_post_aggregates_featured_community_newest_comment_time,
-  idx_post_aggregates_featured_local_comments,
-  idx_post_aggregates_featured_community_comments,
-  idx_post_aggregates_featured_local_hot,
-  idx_post_aggregates_featured_community_hot,
-  idx_post_aggregates_featured_local_active,
-  idx_post_aggregates_featured_community_active,
-  idx_post_aggregates_featured_local_score,
-  idx_post_aggregates_featured_community_score,
-  idx_post_aggregates_featured_local_published,
-  idx_post_aggregates_featured_community_published;
+DROP INDEX idx_post_aggregates_featured_local_newest_comment_time, idx_post_aggregates_featured_community_newest_comment_time, idx_post_aggregates_featured_local_comments, idx_post_aggregates_featured_community_comments, idx_post_aggregates_featured_local_hot, idx_post_aggregates_featured_community_hot, idx_post_aggregates_featured_local_active, idx_post_aggregates_featured_community_active, idx_post_aggregates_featured_local_score, idx_post_aggregates_featured_community_score, idx_post_aggregates_featured_local_published, idx_post_aggregates_featured_community_published;
 
 -- Create the old indexes
-create index idx_post_aggregates_newest_comment_time on post_aggregates (newest_comment_time desc);
-create index idx_post_aggregates_comments on post_aggregates (comments desc);
-create index idx_post_aggregates_hot on post_aggregates (hot_rank(score, published) desc, published desc);
-create index idx_post_aggregates_active on post_aggregates (hot_rank(score, newest_comment_time) desc, newest_comment_time desc);
-create index idx_post_aggregates_score on post_aggregates (score desc);
-create index idx_post_aggregates_published on post_aggregates (published desc);
+CREATE INDEX idx_post_aggregates_newest_comment_time ON post_aggregates (newest_comment_time DESC);
+
+CREATE INDEX idx_post_aggregates_comments ON post_aggregates (comments DESC);
+
+CREATE INDEX idx_post_aggregates_hot ON post_aggregates (hot_rank (score, published) DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_active ON post_aggregates (hot_rank (score, newest_comment_time) DESC, newest_comment_time DESC);
+
+CREATE INDEX idx_post_aggregates_score ON post_aggregates (score DESC);
+
+CREATE INDEX idx_post_aggregates_published ON post_aggregates (published DESC);
 
index 881089293541622f1bc1cc1c6406aa3326ddc230..29f571ccda656a50a3ed83c9730c5dbcd9b1c7fe 100644 (file)
@@ -1,30 +1,30 @@
 -- Drop the old indexes
-drop index idx_post_aggregates_newest_comment_time,
-  idx_post_aggregates_comments,
-  idx_post_aggregates_hot,
-  idx_post_aggregates_active,
-  idx_post_aggregates_score,
-  idx_post_aggregates_published;
-
--- All of the post fetching queries now start with either 
+DROP INDEX idx_post_aggregates_newest_comment_time, idx_post_aggregates_comments, idx_post_aggregates_hot, idx_post_aggregates_active, idx_post_aggregates_score, idx_post_aggregates_published;
+
+-- All of the post fetching queries now start with either
 -- featured_local desc, or featured_community desc, then the other sorts.
 -- So you now need to double these indexes
+CREATE INDEX idx_post_aggregates_featured_local_newest_comment_time ON post_aggregates (featured_local DESC, newest_comment_time DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_newest_comment_time ON post_aggregates (featured_community DESC, newest_comment_time DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local_comments ON post_aggregates (featured_local DESC, comments DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_comments ON post_aggregates (featured_community DESC, comments DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local_hot ON post_aggregates (featured_local DESC, hot_rank (score, published) DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_hot ON post_aggregates (featured_community DESC, hot_rank (score, published) DESC, published DESC);
 
-create index idx_post_aggregates_featured_local_newest_comment_time on post_aggregates (featured_local desc, newest_comment_time desc);
-create index idx_post_aggregates_featured_community_newest_comment_time on post_aggregates (featured_community desc, newest_comment_time desc);
+CREATE INDEX idx_post_aggregates_featured_local_active ON post_aggregates (featured_local DESC, hot_rank (score, newest_comment_time) DESC, newest_comment_time DESC);
 
-create index idx_post_aggregates_featured_local_comments on post_aggregates (featured_local desc, comments desc);
-create index idx_post_aggregates_featured_community_comments on post_aggregates (featured_community desc, comments desc);
+CREATE INDEX idx_post_aggregates_featured_community_active ON post_aggregates (featured_community DESC, hot_rank (score, newest_comment_time) DESC, newest_comment_time DESC);
 
-create index idx_post_aggregates_featured_local_hot on post_aggregates (featured_local desc, hot_rank(score, published) desc, published desc);
-create index idx_post_aggregates_featured_community_hot on post_aggregates (featured_community desc, hot_rank(score, published) desc, published desc);
+CREATE INDEX idx_post_aggregates_featured_local_score ON post_aggregates (featured_local DESC, score DESC);
 
-create index idx_post_aggregates_featured_local_active on post_aggregates (featured_local desc, hot_rank(score, newest_comment_time) desc, newest_comment_time desc);
-create index idx_post_aggregates_featured_community_active on post_aggregates (featured_community desc, hot_rank(score, newest_comment_time) desc, newest_comment_time desc);
+CREATE INDEX idx_post_aggregates_featured_community_score ON post_aggregates (featured_community DESC, score DESC);
 
-create index idx_post_aggregates_featured_local_score on post_aggregates (featured_local desc, score desc);
-create index idx_post_aggregates_featured_community_score on post_aggregates (featured_community desc, score desc);
+CREATE INDEX idx_post_aggregates_featured_local_published ON post_aggregates (featured_local DESC, published DESC);
 
-create index idx_post_aggregates_featured_local_published on post_aggregates (featured_local desc, published desc);
-create index idx_post_aggregates_featured_community_published on post_aggregates (featured_community desc, published desc);
+CREATE INDEX idx_post_aggregates_featured_community_published ON post_aggregates (featured_community DESC, published DESC);
 
index 356e3b8c470cea443dfde1fb79e1df58a7217de1..20be3105b0528a895e8c553743cffb858e887b48 100644 (file)
@@ -1,6 +1,6 @@
-drop index 
-  idx_post_aggregates_featured_local_active,
-  idx_post_aggregates_featured_community_active;
+DROP INDEX idx_post_aggregates_featured_local_active, idx_post_aggregates_featured_community_active;
+
+CREATE INDEX idx_post_aggregates_featured_local_active ON post_aggregates (featured_local DESC, hot_rank (score, newest_comment_time) DESC, newest_comment_time DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_active ON post_aggregates (featured_community DESC, hot_rank (score, newest_comment_time) DESC, newest_comment_time DESC);
 
-create index idx_post_aggregates_featured_local_active on post_aggregates (featured_local desc, hot_rank(score, newest_comment_time) desc, newest_comment_time desc);
-create index idx_post_aggregates_featured_community_active on post_aggregates (featured_community desc, hot_rank(score, newest_comment_time) desc, newest_comment_time desc);
index 5673cf4528d24133470690c3eaffc73e8de712c5..2219ebc7691dfcdc35f16aaa899ee14ef34a98d3 100644 (file)
@@ -1,7 +1,7 @@
 -- This should use the newest_comment_time_necro, not the newest_comment_time for the hot_rank
-drop index 
-  idx_post_aggregates_featured_local_active,
-  idx_post_aggregates_featured_community_active;
+DROP INDEX idx_post_aggregates_featured_local_active, idx_post_aggregates_featured_community_active;
+
+CREATE INDEX idx_post_aggregates_featured_local_active ON post_aggregates (featured_local DESC, hot_rank (score, newest_comment_time_necro) DESC, newest_comment_time_necro DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_active ON post_aggregates (featured_community DESC, hot_rank (score, newest_comment_time_necro) DESC, newest_comment_time_necro DESC);
 
-create index idx_post_aggregates_featured_local_active on post_aggregates (featured_local desc, hot_rank(score, newest_comment_time_necro) desc, newest_comment_time_necro desc);
-create index idx_post_aggregates_featured_community_active on post_aggregates (featured_community desc, hot_rank(score, newest_comment_time_necro) desc, newest_comment_time_necro desc);
index 1ac320f2f78938ccc5f72a32990d56965822d594..4e141e4232025b74e8dc213387030e9d2bcec4f6 100644 (file)
@@ -1 +1,3 @@
-alter table local_site add column federation_debug int default 0;
\ No newline at end of file
+ALTER TABLE local_site
+    ADD COLUMN federation_debug int DEFAULT 0;
+
index c7286d09c7fdeb63fedce03f1f1663a75c19a061..cad1c8975487ce7000f127f31061971afedd25fe 100644 (file)
@@ -1 +1,3 @@
-alter table local_site drop column federation_debug;
\ No newline at end of file
+ALTER TABLE local_site
+    DROP COLUMN federation_debug;
+
index 8f7b531f0c4a06109bd09ced714881ccec1a4abd..e3aa8bad998953a1c37deb9258096ed4fa8cd454 100644 (file)
@@ -1,2 +1,6 @@
-alter table community drop column moderators_url;
-alter table community drop column featured_url;
\ No newline at end of file
+ALTER TABLE community
+    DROP COLUMN moderators_url;
+
+ALTER TABLE community
+    DROP COLUMN featured_url;
+
index 78e7e52b862c778e1d3b76c6aeb0e239a1bcdfdb..f8be41a96ac7601e00eba2d0cba4eda27cae771e 100644 (file)
@@ -1,2 +1,6 @@
-alter table community add column moderators_url varchar(255) unique;
-alter table community add column featured_url varchar(255) unique;
\ No newline at end of file
+ALTER TABLE community
+    ADD COLUMN moderators_url varchar(255) UNIQUE;
+
+ALTER TABLE community
+    ADD COLUMN featured_url varchar(255) UNIQUE;
+
index 8994bb3bed11b968686665dae4560d6db37fc325..01a68d2d20ba6d6b0feed74129fec0bbe6855d6d 100644 (file)
@@ -1,2 +1,4 @@
-drop table custom_emoji_keyword;
-drop table custom_emoji;
\ No newline at end of file
+DROP TABLE custom_emoji_keyword;
+
+DROP TABLE custom_emoji;
+
index 79a21b206a336571fdde9e71668ff02cee572223..98b6fa59cb79e74b887d3e7158aa65245dda7313 100644 (file)
@@ -1,19 +1,20 @@
-create table custom_emoji (
-  id serial primary key,
-  local_site_id int references local_site on update cascade on delete cascade not null,
-  shortcode varchar(128) not null UNIQUE,
-  image_url text not null UNIQUE,
-  alt_text text not null,
-  category text not null,
-  published timestamp without time zone default now() not null,
-  updated timestamp without time zone
+CREATE TABLE custom_emoji (
+    id serial PRIMARY KEY,
+    local_site_id int REFERENCES local_site ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    shortcode varchar(128) NOT NULL UNIQUE,
+    image_url text NOT NULL UNIQUE,
+    alt_text text NOT NULL,
+    category text NOT NULL,
+    published timestamp without time zone DEFAULT now() NOT NULL,
+    updated timestamp without time zone
 );
 
-create table custom_emoji_keyword (
-  id serial primary key,
-  custom_emoji_id int references custom_emoji on update cascade on delete cascade not null,
-  keyword varchar(128) not null,
-  UNIQUE (custom_emoji_id, keyword)
+CREATE TABLE custom_emoji_keyword (
+    id serial PRIMARY KEY,
+    custom_emoji_id int REFERENCES custom_emoji ON UPDATE CASCADE ON DELETE CASCADE NOT NULL,
+    keyword varchar(128) NOT NULL,
+    UNIQUE (custom_emoji_id, keyword)
 );
 
-create index idx_custom_emoji_category on custom_emoji (id,category);
+CREATE INDEX idx_custom_emoji_category ON custom_emoji (id, category);
+
index fa69bbaf4df8eab48f9530ef29a6ff7f8fa3781e..148f35d93847fdabd7597a7f60c89a463cca60e2 100644 (file)
@@ -1 +1,3 @@
-alter table local_site drop column reports_email_admins;
+ALTER TABLE local_site
+    DROP COLUMN reports_email_admins;
+
index 7de8a284209109b13977480bd90783ab12b87739..069df14538e84df801fffa31129083a34c0ded98 100644 (file)
@@ -1,2 +1,4 @@
 -- Adding a field to email admins for new reports
-alter table local_site add column reports_email_admins boolean not null default false;
+ALTER TABLE local_site
+    ADD COLUMN reports_email_admins boolean NOT NULL DEFAULT FALSE;
+
index 07179de1da6b32867bf43fae7c62d4d030521806..fbac63e1864a9d05278693dbc4274a316ee8aa13 100644 (file)
@@ -1,2 +1,6 @@
-alter table instance drop column software;
-alter table instance drop column version;
+ALTER TABLE instance
+    DROP COLUMN software;
+
+ALTER TABLE instance
+    DROP COLUMN version;
+
index abfeb85007894a5d40522259643620ac842dac68..a7f7233cb6bb444be01954584956b89d9b5b371b 100644 (file)
@@ -1,4 +1,7 @@
 -- Add Software and Version columns from nodeinfo to the instance table
+ALTER TABLE instance
+    ADD COLUMN software varchar(255);
+
+ALTER TABLE instance
+    ADD COLUMN version varchar(255);
 
-alter table instance add column software varchar(255);
-alter table instance add column version varchar(255);
index 3ef1500c6f7a2c4b9f4897a6448cb9416090e729..bd9b6bba66f6630d9976969ba8603015cb430714 100644 (file)
@@ -1 +1,2 @@
-drop index idx_post_saved_person_id, idx_comment_saved_person_id;
+DROP INDEX idx_post_saved_person_id, idx_comment_saved_person_id;
+
index 8899431218069f11be27b8a824677b2f4efc3d1f..98db27798f95d67e1faa56a1bd21a1ef2f1ad737 100644 (file)
@@ -1,2 +1,4 @@
-create index idx_post_saved_person_id on post_saved (person_id);
-create index idx_comment_saved_person_id on comment_saved (person_id);
+CREATE INDEX idx_post_saved_person_id ON post_saved (person_id);
+
+CREATE INDEX idx_comment_saved_person_id ON comment_saved (person_id);
+
index b7f38c45771e9f81e6ab6352d2213c4fcc1c1a86..7adf97e66457a5a6e5198021ba9031c4b935d67f 100644 (file)
@@ -1,2 +1,6 @@
-alter table local_user drop column totp_2fa_secret;
-alter table local_user drop column totp_2fa_url;
+ALTER TABLE local_user
+    DROP COLUMN totp_2fa_secret;
+
+ALTER TABLE local_user
+    DROP COLUMN totp_2fa_url;
+
index e40c1c66ff3b9b6bf02a28d2f1ead98a442f63d7..43ff8dc9040ba87cfdc0723149c46126a7f3b184 100644 (file)
@@ -1,2 +1,6 @@
-alter table local_user add column totp_2fa_secret text;
-alter table local_user add column totp_2fa_url text;
+ALTER TABLE local_user
+    ADD COLUMN totp_2fa_secret text;
+
+ALTER TABLE local_user
+    ADD COLUMN totp_2fa_url text;
+
index 5d5f2f3a56d110c75b252acb7e031687e523c8aa..ceb36f53882d311c54ea7ed35605407389efc0ef 100644 (file)
 -- Some fixes
-alter table community alter column hidden drop not null;
-alter table community alter column posting_restricted_to_mods drop not null;
-alter table activity alter column sensitive drop not null;
-alter table mod_add alter column removed drop not null;
-alter table mod_add_community alter column removed drop not null;
-alter table mod_ban alter column banned drop not null;
-alter table mod_ban_from_community alter column banned drop not null;
-alter table mod_hide_community alter column hidden drop not null;
-alter table mod_lock_post alter column locked drop not null;
-alter table mod_remove_comment alter column removed drop not null;
-alter table mod_remove_community alter column removed drop not null;
-alter table mod_remove_post alter column removed drop not null;
-alter table mod_transfer_community add column removed boolean default false;
-alter table language alter column code drop not null;
-alter table language alter column name drop not null;
+ALTER TABLE community
+    ALTER COLUMN hidden DROP NOT NULL;
+
+ALTER TABLE community
+    ALTER COLUMN posting_restricted_to_mods DROP NOT NULL;
+
+ALTER TABLE activity
+    ALTER COLUMN sensitive DROP NOT NULL;
+
+ALTER TABLE mod_add
+    ALTER COLUMN removed DROP NOT NULL;
+
+ALTER TABLE mod_add_community
+    ALTER COLUMN removed DROP NOT NULL;
+
+ALTER TABLE mod_ban
+    ALTER COLUMN banned DROP NOT NULL;
+
+ALTER TABLE mod_ban_from_community
+    ALTER COLUMN banned DROP NOT NULL;
+
+ALTER TABLE mod_hide_community
+    ALTER COLUMN hidden DROP NOT NULL;
+
+ALTER TABLE mod_lock_post
+    ALTER COLUMN LOCKED DROP NOT NULL;
+
+ALTER TABLE mod_remove_comment
+    ALTER COLUMN removed DROP NOT NULL;
+
+ALTER TABLE mod_remove_community
+    ALTER COLUMN removed DROP NOT NULL;
+
+ALTER TABLE mod_remove_post
+    ALTER COLUMN removed DROP NOT NULL;
+
+ALTER TABLE mod_transfer_community
+    ADD COLUMN removed boolean DEFAULT FALSE;
+
+ALTER TABLE LANGUAGE
+    ALTER COLUMN code DROP NOT NULL;
+
+ALTER TABLE LANGUAGE
+    ALTER COLUMN name DROP NOT NULL;
 
 -- Fix the registration mode enums
 ALTER TYPE registration_mode_enum RENAME VALUE 'Closed' TO 'closed';
+
 ALTER TYPE registration_mode_enum RENAME VALUE 'RequireApplication' TO 'require_application';
+
 ALTER TYPE registration_mode_enum RENAME VALUE 'Open' TO 'open';
 
 -- add back old columns
-
 -- Alter the local_user table
-alter table local_user alter column default_sort_type drop default;
-alter table local_user alter column default_sort_type type smallint using
-    case default_sort_type
-        when 'Active' then 0
-        when 'Hot' then 1
-        when 'New' then 2
-        when 'Old' then 3
-        when 'TopDay' then 4
-        when 'TopWeek' then 5
-        when 'TopMonth' then 6
-        when 'TopYear' then 7
-        when 'TopAll' then 8
-        when 'MostComments' then 9
-        when 'NewComments' then 10
-        else 0
-    end;
-alter table local_user alter column default_sort_type set default 0;
-
-alter table local_user alter column default_listing_type drop default;
-alter table local_user alter column default_listing_type type smallint using
-    case default_listing_type
-        when 'All' then 0
-        when 'Local' then 1
-        when 'Subscribed' then 2
-        else 1
-    end;
-alter table local_user alter column default_listing_type set default 1;
+ALTER TABLE local_user
+    ALTER COLUMN default_sort_type DROP DEFAULT;
+
+ALTER TABLE local_user
+    ALTER COLUMN default_sort_type TYPE smallint
+    USING
+        CASE default_sort_type
+        WHEN 'Active' THEN
+            0
+        WHEN 'Hot' THEN
+            1
+        WHEN 'New' THEN
+            2
+        WHEN 'Old' THEN
+            3
+        WHEN 'TopDay' THEN
+            4
+        WHEN 'TopWeek' THEN
+            5
+        WHEN 'TopMonth' THEN
+            6
+        WHEN 'TopYear' THEN
+            7
+        WHEN 'TopAll' THEN
+            8
+        WHEN 'MostComments' THEN
+            9
+        WHEN 'NewComments' THEN
+            10
+        ELSE
+            0
+        END;
+
+ALTER TABLE local_user
+    ALTER COLUMN default_sort_type SET DEFAULT 0;
+
+ALTER TABLE local_user
+    ALTER COLUMN default_listing_type DROP DEFAULT;
+
+ALTER TABLE local_user
+    ALTER COLUMN default_listing_type TYPE smallint
+    USING
+        CASE default_listing_type
+        WHEN 'All' THEN
+            0
+        WHEN 'Local' THEN
+            1
+        WHEN 'Subscribed' THEN
+            2
+        ELSE
+            1
+        END;
+
+ALTER TABLE local_user
+    ALTER COLUMN default_listing_type SET DEFAULT 1;
 
 -- Alter the local site column
+ALTER TABLE local_site
+    ALTER COLUMN default_post_listing_type DROP DEFAULT;
 
-alter table local_site alter column default_post_listing_type drop default;
-alter table local_site alter column default_post_listing_type type text;
-alter table local_site alter column default_post_listing_type set default 1;
+ALTER TABLE local_site
+    ALTER COLUMN default_post_listing_type TYPE text;
+
+ALTER TABLE local_site
+    ALTER COLUMN default_post_listing_type SET DEFAULT 1;
 
 -- Drop the types
-drop type listing_type_enum;
-drop type sort_type_enum;
+DROP TYPE listing_type_enum;
+
+DROP TYPE sort_type_enum;
+
index 8474f34eddce93c64ab36c8aa3568b63ca29f25a..bfce9a6a16a87df52f6db806b5c183f533e68fb5 100644 (file)
 -- A few DB fixes
-alter table community alter column hidden set not null;
-alter table community alter column posting_restricted_to_mods set not null;
-alter table activity alter column sensitive set not null;
-alter table mod_add alter column removed set not null;
-alter table mod_add_community alter column removed set not null;
-alter table mod_ban alter column banned set not null;
-alter table mod_ban_from_community alter column banned set not null;
-alter table mod_hide_community alter column hidden set not null;
-alter table mod_lock_post alter column locked set not null;
-alter table mod_remove_comment alter column removed set not null;
-alter table mod_remove_community alter column removed set not null;
-alter table mod_remove_post alter column removed set not null;
-alter table mod_transfer_community drop column removed;
-alter table language alter column code set not null;
-alter table language alter column name set not null;
+ALTER TABLE community
+    ALTER COLUMN hidden SET NOT NULL;
+
+ALTER TABLE community
+    ALTER COLUMN posting_restricted_to_mods SET NOT NULL;
+
+ALTER TABLE activity
+    ALTER COLUMN sensitive SET NOT NULL;
+
+ALTER TABLE mod_add
+    ALTER COLUMN removed SET NOT NULL;
+
+ALTER TABLE mod_add_community
+    ALTER COLUMN removed SET NOT NULL;
+
+ALTER TABLE mod_ban
+    ALTER COLUMN banned SET NOT NULL;
+
+ALTER TABLE mod_ban_from_community
+    ALTER COLUMN banned SET NOT NULL;
+
+ALTER TABLE mod_hide_community
+    ALTER COLUMN hidden SET NOT NULL;
+
+ALTER TABLE mod_lock_post
+    ALTER COLUMN LOCKED SET NOT NULL;
+
+ALTER TABLE mod_remove_comment
+    ALTER COLUMN removed SET NOT NULL;
+
+ALTER TABLE mod_remove_community
+    ALTER COLUMN removed SET NOT NULL;
+
+ALTER TABLE mod_remove_post
+    ALTER COLUMN removed SET NOT NULL;
+
+ALTER TABLE mod_transfer_community
+    DROP COLUMN removed;
+
+ALTER TABLE LANGUAGE
+    ALTER COLUMN code SET NOT NULL;
+
+ALTER TABLE LANGUAGE
+    ALTER COLUMN name SET NOT NULL;
 
 -- Fix the registration mode enums
 ALTER TYPE registration_mode_enum RENAME VALUE 'closed' TO 'Closed';
+
 ALTER TYPE registration_mode_enum RENAME VALUE 'require_application' TO 'RequireApplication';
+
 ALTER TYPE registration_mode_enum RENAME VALUE 'open' TO 'Open';
 
 -- Create the enums
+CREATE TYPE sort_type_enum AS ENUM (
+    'Active',
+    'Hot',
+    'New',
+    'Old',
+    'TopDay',
+    'TopWeek',
+    'TopMonth',
+    'TopYear',
+    'TopAll',
+    'MostComments',
+    'NewComments'
+);
 
-CREATE TYPE sort_type_enum AS ENUM ('Active', 'Hot', 'New', 'Old', 'TopDay', 'TopWeek', 'TopMonth', 'TopYear', 'TopAll', 'MostComments', 'NewComments');
-  
-CREATE TYPE listing_type_enum AS ENUM ('All', 'Local', 'Subscribed');
+CREATE TYPE listing_type_enum AS ENUM (
+    'All',
+    'Local',
+    'Subscribed'
+);
 
 -- Alter the local_user table
-alter table local_user alter column default_sort_type drop default;
-alter table local_user alter column default_sort_type type sort_type_enum using
-    case default_sort_type
-        when 0 then 'Active'
-        when 1 then 'Hot'
-        when 2 then 'New'
-        when 3 then 'Old'
-        when 4 then 'TopDay'
-        when 5 then 'TopWeek'
-        when 6 then 'TopMonth'
-        when 7 then 'TopYear'
-        when 8 then 'TopAll'
-        when 9 then 'MostComments'
-        when 10 then 'NewComments'
-        else 'Active'
-    end :: sort_type_enum;
-alter table local_user alter column default_sort_type set default 'Active';
-
-alter table local_user alter column default_listing_type drop default;
-alter table local_user alter column default_listing_type type listing_type_enum using
-    case default_listing_type
-        when 0 then 'All'
-        when 1 then 'Local'
-        when 2 then 'Subscribed'
-        else 'Local'
-    end :: listing_type_enum;
-alter table local_user alter column default_listing_type set default 'Local';
+ALTER TABLE local_user
+    ALTER COLUMN default_sort_type DROP DEFAULT;
+
+ALTER TABLE local_user
+    ALTER COLUMN default_sort_type TYPE sort_type_enum
+    USING
+        CASE default_sort_type
+        WHEN 0 THEN
+            'Active'
+        WHEN 1 THEN
+            'Hot'
+        WHEN 2 THEN
+            'New'
+        WHEN 3 THEN
+            'Old'
+        WHEN 4 THEN
+            'TopDay'
+        WHEN 5 THEN
+            'TopWeek'
+        WHEN 6 THEN
+            'TopMonth'
+        WHEN 7 THEN
+            'TopYear'
+        WHEN 8 THEN
+            'TopAll'
+        WHEN 9 THEN
+            'MostComments'
+        WHEN 10 THEN
+            'NewComments'
+        ELSE
+            'Active'
+        END::sort_type_enum;
+
+ALTER TABLE local_user
+    ALTER COLUMN default_sort_type SET DEFAULT 'Active';
+
+ALTER TABLE local_user
+    ALTER COLUMN default_listing_type DROP DEFAULT;
+
+ALTER TABLE local_user
+    ALTER COLUMN default_listing_type TYPE listing_type_enum
+    USING
+        CASE default_listing_type
+        WHEN 0 THEN
+            'All'
+        WHEN 1 THEN
+            'Local'
+        WHEN 2 THEN
+            'Subscribed'
+        ELSE
+            'Local'
+        END::listing_type_enum;
+
+ALTER TABLE local_user
+    ALTER COLUMN default_listing_type SET DEFAULT 'Local';
 
 -- Alter the local site column
-alter table local_site alter column default_post_listing_type drop default;
-alter table local_site alter column default_post_listing_type type listing_type_enum using default_post_listing_type::listing_type_enum;
-alter table local_site alter column default_post_listing_type set default 'Local';
+ALTER TABLE local_site
+    ALTER COLUMN default_post_listing_type DROP DEFAULT;
+
+ALTER TABLE local_site
+    ALTER COLUMN default_post_listing_type TYPE listing_type_enum
+    USING default_post_listing_type::listing_type_enum;
+
+ALTER TABLE local_site
+    ALTER COLUMN default_post_listing_type SET DEFAULT 'Local';
+
index 67879de4356cd4bccb82f36b697a39545d49be8c..d32b21ecb0844a9beccf0ee697daee7a8f8cbf9f 100644 (file)
@@ -1,21 +1,42 @@
-drop index idx_person_lower_name;
-drop index idx_community_lower_name;
-drop index idx_community_moderator_published;
-drop index idx_community_moderator_community;
-drop index idx_community_moderator_person;
-drop index idx_comment_saved_comment;
-drop index idx_comment_saved_person;
-drop index idx_community_block_community;
-drop index idx_community_block_person;
-drop index idx_community_follower_community;
-drop index idx_community_follower_person;
-drop index idx_person_block_person;
-drop index idx_person_block_target;
-drop index idx_post_language;
-drop index idx_comment_language;
-drop index idx_person_aggregates_person;
-drop index idx_person_post_aggregates_post;
-drop index idx_person_post_aggregates_person;
-drop index idx_comment_reply_comment;
-drop index idx_comment_reply_recipient;
-drop index idx_comment_reply_published;
\ No newline at end of file
+DROP INDEX idx_person_lower_name;
+
+DROP INDEX idx_community_lower_name;
+
+DROP INDEX idx_community_moderator_published;
+
+DROP INDEX idx_community_moderator_community;
+
+DROP INDEX idx_community_moderator_person;
+
+DROP INDEX idx_comment_saved_comment;
+
+DROP INDEX idx_comment_saved_person;
+
+DROP INDEX idx_community_block_community;
+
+DROP INDEX idx_community_block_person;
+
+DROP INDEX idx_community_follower_community;
+
+DROP INDEX idx_community_follower_person;
+
+DROP INDEX idx_person_block_person;
+
+DROP INDEX idx_person_block_target;
+
+DROP INDEX idx_post_language;
+
+DROP INDEX idx_comment_language;
+
+DROP INDEX idx_person_aggregates_person;
+
+DROP INDEX idx_person_post_aggregates_post;
+
+DROP INDEX idx_person_post_aggregates_person;
+
+DROP INDEX idx_comment_reply_comment;
+
+DROP INDEX idx_comment_reply_recipient;
+
+DROP INDEX idx_comment_reply_published;
+
index df3dde7e3556230c1291395009256ed121af11d5..c6a340f172fd8b7612b1cfb38211f013db1375df 100644 (file)
@@ -1,31 +1,43 @@
 -- Add a few indexes to speed up person details queries
-create index idx_person_lower_name on person (lower(name));
-create index idx_community_lower_name on community (lower(name));
+CREATE INDEX idx_person_lower_name ON person (lower(name));
 
-create index idx_community_moderator_published on community_moderator (published);
-create index idx_community_moderator_community on community_moderator (community_id);
-create index idx_community_moderator_person on community_moderator (person_id);
+CREATE INDEX idx_community_lower_name ON community (lower(name));
 
-create index idx_comment_saved_comment on comment_saved (comment_id);
-create index idx_comment_saved_person on comment_saved (person_id);
+CREATE INDEX idx_community_moderator_published ON community_moderator (published);
 
-create index idx_community_block_community on community_block (community_id);
-create index idx_community_block_person on community_block (person_id);
+CREATE INDEX idx_community_moderator_community ON community_moderator (community_id);
 
-create index idx_community_follower_community on community_follower (community_id);
-create index idx_community_follower_person on community_follower (person_id);
+CREATE INDEX idx_community_moderator_person ON community_moderator (person_id);
 
-create index idx_person_block_person on person_block (person_id);
-create index idx_person_block_target on person_block (target_id);
+CREATE INDEX idx_comment_saved_comment ON comment_saved (comment_id);
 
-create index idx_post_language on post (language_id);
-create index idx_comment_language on comment (language_id);
+CREATE INDEX idx_comment_saved_person ON comment_saved (person_id);
 
-create index idx_person_aggregates_person on person_aggregates (person_id);
+CREATE INDEX idx_community_block_community ON community_block (community_id);
 
-create index idx_person_post_aggregates_post on person_post_aggregates (post_id);
-create index idx_person_post_aggregates_person on person_post_aggregates (person_id);
+CREATE INDEX idx_community_block_person ON community_block (person_id);
+
+CREATE INDEX idx_community_follower_community ON community_follower (community_id);
+
+CREATE INDEX idx_community_follower_person ON community_follower (person_id);
+
+CREATE INDEX idx_person_block_person ON person_block (person_id);
+
+CREATE INDEX idx_person_block_target ON person_block (target_id);
+
+CREATE INDEX idx_post_language ON post (language_id);
+
+CREATE INDEX idx_comment_language ON comment (language_id);
+
+CREATE INDEX idx_person_aggregates_person ON person_aggregates (person_id);
+
+CREATE INDEX idx_person_post_aggregates_post ON person_post_aggregates (post_id);
+
+CREATE INDEX idx_person_post_aggregates_person ON person_post_aggregates (person_id);
+
+CREATE INDEX idx_comment_reply_comment ON comment_reply (comment_id);
+
+CREATE INDEX idx_comment_reply_recipient ON comment_reply (recipient_id);
+
+CREATE INDEX idx_comment_reply_published ON comment_reply (published DESC);
 
-create index idx_comment_reply_comment on comment_reply (comment_id);
-create index idx_comment_reply_recipient on comment_reply (recipient_id);
-create index idx_comment_reply_published on comment_reply (published desc);
index b320ecad476152810daa860b574df95d8fe50726..8b501d5b0aadd06c40656ee5eb72c3bbc5b2303e 100644 (file)
@@ -1,4 +1,11 @@
 -- force enable undetermined language for all users
-insert into local_user_language (local_user_id, language_id)
-    select id, 0 from local_user
-    on conflict (local_user_id, language_id) do nothing;
+INSERT INTO local_user_language (local_user_id, language_id)
+SELECT
+    id,
+    0
+FROM
+    local_user
+ON CONFLICT (local_user_id,
+    language_id)
+    DO NOTHING;
+
index ccc9e938f4d5b19a090b70f82fb8a971137d6bdc..7ddea79cc559804b7c5e7b59a1dd4c5df13a010c 100644 (file)
@@ -1,5 +1,7 @@
 -- Change back the column type
-alter table post alter column url type text;
+ALTER TABLE post
+    ALTER COLUMN url TYPE text;
 
 -- Drop the index
-drop index idx_post_url;
+DROP INDEX idx_post_url;
+
index ce3532887214ca2ff17deb7a938891399026a144..5e0f114636f70c9c8107107143e8354579a28b6e 100644 (file)
@@ -1,9 +1,18 @@
 -- Make a hard limit of 512 for the post.url column
 -- Truncate existing long rows.
-update post set url = left(url, 512) where length(url) > 512;
+UPDATE
+    post
+SET
+    url =
+    LEFT (url,
+        512)
+WHERE
+    length(url) > 512;
 
 -- Enforce the limit
-alter table post alter column url type varchar (512);
+ALTER TABLE post
+    ALTER COLUMN url TYPE varchar(512);
 
 -- Add the index
-create index idx_post_url on post(url);
+CREATE INDEX idx_post_url ON post (url);
+
index 71529bdf0358e843e0de18ced9a7368f4905f01e..e82fdff3867015df638c5275a78728dbf399b848 100644 (file)
@@ -1,35 +1,55 @@
-
 -- Remove the new columns
+ALTER TABLE post_aggregates
+    DROP COLUMN hot_rank;
 
-alter table post_aggregates drop column hot_rank;
-alter table post_aggregates drop column hot_rank_active;
+ALTER TABLE post_aggregates
+    DROP COLUMN hot_rank_active;
 
-alter table comment_aggregates drop column hot_rank;
+ALTER TABLE comment_aggregates
+    DROP COLUMN hot_rank;
 
-alter table community_aggregates drop column hot_rank;
+ALTER TABLE community_aggregates
+    DROP COLUMN hot_rank;
 
 -- Drop some new indexes
-drop index idx_post_aggregates_score;
-drop index idx_post_aggregates_published;
-drop index idx_post_aggregates_newest_comment_time;
-drop index idx_post_aggregates_newest_comment_time_necro;
-drop index idx_post_aggregates_featured_community;
-drop index idx_post_aggregates_featured_local;
+DROP INDEX idx_post_aggregates_score;
+
+DROP INDEX idx_post_aggregates_published;
+
+DROP INDEX idx_post_aggregates_newest_comment_time;
+
+DROP INDEX idx_post_aggregates_newest_comment_time_necro;
+
+DROP INDEX idx_post_aggregates_featured_community;
+
+DROP INDEX idx_post_aggregates_featured_local;
 
 -- Recreate the old indexes
 CREATE INDEX idx_post_aggregates_featured_local_newest_comment_time ON public.post_aggregates USING btree (featured_local DESC, newest_comment_time DESC);
+
 CREATE INDEX idx_post_aggregates_featured_community_newest_comment_time ON public.post_aggregates USING btree (featured_community DESC, newest_comment_time DESC);
+
 CREATE INDEX idx_post_aggregates_featured_local_comments ON public.post_aggregates USING btree (featured_local DESC, comments DESC);
+
 CREATE INDEX idx_post_aggregates_featured_community_comments ON public.post_aggregates USING btree (featured_community DESC, comments DESC);
-CREATE INDEX idx_post_aggregates_featured_local_hot ON public.post_aggregates USING btree (featured_local DESC, hot_rank((score)::numeric, published) DESC, published DESC);
-CREATE INDEX idx_post_aggregates_featured_community_hot ON public.post_aggregates USING btree (featured_community DESC, hot_rank((score)::numeric, published) DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local_hot ON public.post_aggregates USING btree (featured_local DESC, hot_rank ((score)::numeric, published) DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_hot ON public.post_aggregates USING btree (featured_community DESC, hot_rank ((score)::numeric, published) DESC, published DESC);
+
 CREATE INDEX idx_post_aggregates_featured_local_score ON public.post_aggregates USING btree (featured_local DESC, score DESC);
+
 CREATE INDEX idx_post_aggregates_featured_community_score ON public.post_aggregates USING btree (featured_community DESC, score DESC);
+
 CREATE INDEX idx_post_aggregates_featured_local_published ON public.post_aggregates USING btree (featured_local DESC, published DESC);
+
 CREATE INDEX idx_post_aggregates_featured_community_published ON public.post_aggregates USING btree (featured_community DESC, published DESC);
-CREATE INDEX idx_post_aggregates_featured_local_active ON public.post_aggregates USING btree (featured_local DESC, hot_rank((score)::numeric, newest_comment_time_necro) DESC, newest_comment_time_necro DESC);
-CREATE INDEX idx_post_aggregates_featured_community_active ON public.post_aggregates USING btree (featured_community DESC, hot_rank((score)::numeric, newest_comment_time_necro) DESC, newest_comment_time_necro DESC);
 
-CREATE INDEX idx_comment_aggregates_hot ON public.comment_aggregates USING btree (hot_rank((score)::numeric, published) DESC, published DESC);
+CREATE INDEX idx_post_aggregates_featured_local_active ON public.post_aggregates USING btree (featured_local DESC, hot_rank ((score)::numeric, newest_comment_time_necro) DESC, newest_comment_time_necro DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_active ON public.post_aggregates USING btree (featured_community DESC, hot_rank ((score)::numeric, newest_comment_time_necro) DESC, newest_comment_time_necro DESC);
+
+CREATE INDEX idx_comment_aggregates_hot ON public.comment_aggregates USING btree (hot_rank ((score)::numeric, published) DESC, published DESC);
+
+CREATE INDEX idx_community_aggregates_hot ON public.community_aggregates USING btree (hot_rank ((subscribers)::numeric, published) DESC, published DESC);
 
-CREATE INDEX idx_community_aggregates_hot ON public.community_aggregates USING btree (hot_rank((subscribers)::numeric, published) DESC, published DESC);
index 92f03e5f334d2b0f1ad0bd44effa52fafc537cf1..00c8fae2db416b9711248ad2cefd40a57901c937 100644 (file)
@@ -1,17 +1,27 @@
 -- This converts the old hot_rank functions, to columns
-
 -- Remove the old compound indexes
 DROP INDEX idx_post_aggregates_featured_local_newest_comment_time;
+
 DROP INDEX idx_post_aggregates_featured_community_newest_comment_time;
+
 DROP INDEX idx_post_aggregates_featured_local_comments;
+
 DROP INDEX idx_post_aggregates_featured_community_comments;
+
 DROP INDEX idx_post_aggregates_featured_local_hot;
+
 DROP INDEX idx_post_aggregates_featured_community_hot;
+
 DROP INDEX idx_post_aggregates_featured_local_score;
+
 DROP INDEX idx_post_aggregates_featured_community_score;
+
 DROP INDEX idx_post_aggregates_featured_local_published;
+
 DROP INDEX idx_post_aggregates_featured_community_published;
+
 DROP INDEX idx_post_aggregates_featured_local_active;
+
 DROP INDEX idx_post_aggregates_featured_community_active;
 
 DROP INDEX idx_comment_aggregates_hot;
@@ -21,31 +31,59 @@ DROP INDEX idx_community_aggregates_hot;
 -- Add the new hot rank columns for post and comment aggregates
 -- Note: 1728 is the result of the hot_rank function, with a score of 1, posted now
 -- hot_rank = 10000*log10(1 + 3)/Power(2, 1.8)
-alter table post_aggregates add column hot_rank integer not null default 1728;
-alter table post_aggregates add column hot_rank_active integer not null default 1728;
+ALTER TABLE post_aggregates
+    ADD COLUMN hot_rank integer NOT NULL DEFAULT 1728;
+
+ALTER TABLE post_aggregates
+    ADD COLUMN hot_rank_active integer NOT NULL DEFAULT 1728;
 
-alter table comment_aggregates add column hot_rank integer not null default 1728;
+ALTER TABLE comment_aggregates
+    ADD COLUMN hot_rank integer NOT NULL DEFAULT 1728;
 
-alter table community_aggregates add column hot_rank integer not null default 1728;
+ALTER TABLE community_aggregates
+    ADD COLUMN hot_rank integer NOT NULL DEFAULT 1728;
 
 -- Populate them initially
--- Note: After initial population, these are updated in a periodic scheduled job, 
+-- Note: After initial population, these are updated in a periodic scheduled job,
 -- with only the last week being updated.
-update post_aggregates set hot_rank_active = hot_rank(score::numeric, newest_comment_time_necro);
-update post_aggregates set hot_rank = hot_rank(score::numeric, published);
-update comment_aggregates set hot_rank = hot_rank(score::numeric, published);
-update community_aggregates set hot_rank = hot_rank(subscribers::numeric, published);
+UPDATE
+    post_aggregates
+SET
+    hot_rank_active = hot_rank (score::numeric, newest_comment_time_necro);
+
+UPDATE
+    post_aggregates
+SET
+    hot_rank = hot_rank (score::numeric, published);
+
+UPDATE
+    comment_aggregates
+SET
+    hot_rank = hot_rank (score::numeric, published);
+
+UPDATE
+    community_aggregates
+SET
+    hot_rank = hot_rank (subscribers::numeric, published);
 
 -- Create single column indexes
-create index idx_post_aggregates_score on post_aggregates (score desc);
-create index idx_post_aggregates_published on post_aggregates (published desc);
-create index idx_post_aggregates_newest_comment_time on post_aggregates (newest_comment_time desc);
-create index idx_post_aggregates_newest_comment_time_necro on post_aggregates (newest_comment_time_necro desc);
-create index idx_post_aggregates_featured_community on post_aggregates (featured_community desc);
-create index idx_post_aggregates_featured_local on post_aggregates (featured_local desc);
-create index idx_post_aggregates_hot on post_aggregates (hot_rank desc);
-create index idx_post_aggregates_active on post_aggregates (hot_rank_active desc);
-
-create index idx_comment_aggregates_hot on comment_aggregates (hot_rank desc);
-
-create index idx_community_aggregates_hot on community_aggregates (hot_rank desc);
+CREATE INDEX idx_post_aggregates_score ON post_aggregates (score DESC);
+
+CREATE INDEX idx_post_aggregates_published ON post_aggregates (published DESC);
+
+CREATE INDEX idx_post_aggregates_newest_comment_time ON post_aggregates (newest_comment_time DESC);
+
+CREATE INDEX idx_post_aggregates_newest_comment_time_necro ON post_aggregates (newest_comment_time_necro DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community ON post_aggregates (featured_community DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local ON post_aggregates (featured_local DESC);
+
+CREATE INDEX idx_post_aggregates_hot ON post_aggregates (hot_rank DESC);
+
+CREATE INDEX idx_post_aggregates_active ON post_aggregates (hot_rank_active DESC);
+
+CREATE INDEX idx_comment_aggregates_hot ON comment_aggregates (hot_rank DESC);
+
+CREATE INDEX idx_community_aggregates_hot ON community_aggregates (hot_rank DESC);
+
index 006bb1083409c3e0d4d2b9fb781e510e3c088d5e..bdf20b8eb09a0db0ab8b2183288e8782dc62dff5 100644 (file)
@@ -1,14 +1,34 @@
 -- update the default sort type
-update local_user set default_sort_type = 'TopDay' where default_sort_type in ('TopHour', 'TopSixHour', 'TopTwelveHour');
+UPDATE
+    local_user
+SET
+    default_sort_type = 'TopDay'
+WHERE
+    default_sort_type IN ('TopHour', 'TopSixHour', 'TopTwelveHour');
 
 -- rename the old enum
-alter type sort_type_enum rename to sort_type_enum__;
+ALTER TYPE sort_type_enum RENAME TO sort_type_enum__;
+
 -- create the new enum
-CREATE TYPE sort_type_enum AS ENUM ('Active', 'Hot', 'New', 'Old', 'TopDay', 'TopWeek', 'TopMonth', 'TopYear', 'TopAll', 'MostComments', 'NewComments');
+CREATE TYPE sort_type_enum AS ENUM (
+    'Active',
+    'Hot',
+    'New',
+    'Old',
+    'TopDay',
+    'TopWeek',
+    'TopMonth',
+    'TopYear',
+    'TopAll',
+    'MostComments',
+    'NewComments'
+);
 
 -- alter all you enum columns
-alter table local_user
-  alter column default_sort_type type sort_type_enum using default_sort_type::text::sort_type_enum;
+ALTER TABLE local_user
+    ALTER COLUMN default_sort_type TYPE sort_type_enum
+    USING default_sort_type::text::sort_type_enum;
 
 -- drop the old enum
-drop type sort_type_enum__;
\ No newline at end of file
+DROP TYPE sort_type_enum__;
+
index 7e4b6fbd6ed615b18e22f7a35ca51ccba8ff3640..2068269e7abba4ec79b249d2be72bd73fecc7bab 100644 (file)
@@ -1,4 +1,10 @@
 -- Update the enums
-ALTER TYPE sort_type_enum ADD VALUE 'TopHour';
-ALTER TYPE sort_type_enum ADD VALUE 'TopSixHour';
-ALTER TYPE sort_type_enum ADD VALUE 'TopTwelveHour';
\ No newline at end of file
+ALTER TYPE sort_type_enum
+    ADD VALUE 'TopHour';
+
+ALTER TYPE sort_type_enum
+    ADD VALUE 'TopSixHour';
+
+ALTER TYPE sort_type_enum
+    ADD VALUE 'TopTwelveHour';
+
index e3c200a15b296782c69341b37f6ec9ed7499053a..948d044feb056518b2f1d7c53b384fa0bf592778 100644 (file)
@@ -1 +1,3 @@
-alter table local_site add column federation_worker_count int default 64 not null;
\ No newline at end of file
+ALTER TABLE local_site
+    ADD COLUMN federation_worker_count int DEFAULT 64 NOT NULL;
+
index 2aac86f8555ae8d3763d9e090e6ba4323aeb60f6..e4eb058efd57e6adc50d89df845a869b38a6f505 100644 (file)
@@ -1 +1,3 @@
-alter table local_site drop column federation_worker_count;
\ No newline at end of file
+ALTER TABLE local_site
+    DROP COLUMN federation_worker_count;
+
index 9472780239b474f4651612fcded1dadd8dabf985..aec7c653aa756a93932ab5c05a432b3e6a573a7f 100644 (file)
@@ -1,19 +1,20 @@
 -- This file should undo anything in `up.sql`
-create or replace function was_removed_or_deleted(TG_OP text, OLD record, NEW record)
-RETURNS boolean
-LANGUAGE plpgsql
-as $$
-    begin
-        IF (TG_OP = 'INSERT') THEN
-            return false;
-        end if;
+CREATE OR REPLACE FUNCTION was_removed_or_deleted (TG_OP text, OLD record, NEW record)
+    RETURNS boolean
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        RETURN FALSE;
+    END IF;
+    IF (TG_OP = 'DELETE') THEN
+        RETURN TRUE;
+    END IF;
+    RETURN TG_OP = 'UPDATE'
+        AND ((OLD.deleted = 'f'
+                AND NEW.deleted = 't')
+            OR (OLD.removed = 'f'
+                AND NEW.removed = 't'));
+END
+$$;
 
-        IF (TG_OP = 'DELETE') THEN
-            return true;
-        end if;
-
-    return TG_OP = 'UPDATE' AND (
-            (OLD.deleted = 'f' AND NEW.deleted = 't') OR
-            (OLD.removed = 'f' AND NEW.removed = 't')
-            );
-END $$;
index bd6d1e58f5b489cbdb453bd6779989c1c269cd84..38ab5c7b5e38681057cab7df11b140bd77c95bc7 100644 (file)
 -- Deleting after removing should not decrement the count twice.
-create or replace function was_removed_or_deleted(TG_OP text, OLD record, NEW record)
-RETURNS boolean
-LANGUAGE plpgsql
-as $$
-    begin
-        IF (TG_OP = 'INSERT') THEN
-            return false;
-        end if;
-
-        IF (TG_OP = 'DELETE' AND OLD.deleted = 'f' AND OLD.removed = 'f') THEN
-            return true;
-        end if;
-
-    return TG_OP = 'UPDATE' AND (
-            (OLD.deleted = 'f' AND NEW.deleted = 't') OR
-            (OLD.removed = 'f' AND NEW.removed = 't')
-            );
-END $$;
+CREATE OR REPLACE FUNCTION was_removed_or_deleted (TG_OP text, OLD record, NEW record)
+    RETURNS boolean
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        RETURN FALSE;
+    END IF;
+    IF (TG_OP = 'DELETE' AND OLD.deleted = 'f' AND OLD.removed = 'f') THEN
+        RETURN TRUE;
+    END IF;
+    RETURN TG_OP = 'UPDATE'
+        AND ((OLD.deleted = 'f'
+                AND NEW.deleted = 't')
+            OR (OLD.removed = 'f'
+                AND NEW.removed = 't'));
+END
+$$;
 
 -- Recalculate proper comment count.
-UPDATE person_aggregates
-    SET comment_count = cnt.count
-    FROM (
-    SELECT creator_id, count(*) AS count FROM comment
-    WHERE deleted='f' AND removed='f'
-    GROUP BY creator_id
-        ) cnt
-    WHERE person_aggregates.person_id = cnt.creator_id;
+UPDATE
+    person_aggregates
+SET
+    comment_count = cnt.count
+FROM (
+    SELECT
+        creator_id,
+        count(*) AS count
+    FROM
+        comment
+    WHERE
+        deleted = 'f'
+        AND removed = 'f'
+    GROUP BY
+        creator_id) cnt
+WHERE
+    person_aggregates.person_id = cnt.creator_id;
 
 -- Recalculate proper comment score.
-UPDATE person_aggregates ua
-    SET comment_score = cd.score
-    FROM (
-        SELECT u.id AS creator_id,
-            coalesce(0, sum(cl.score)) as score
+UPDATE
+    person_aggregates ua
+SET
+    comment_score = cd.score
+FROM (
+    SELECT
+        u.id AS creator_id,
+        coalesce(0, sum(cl.score)) AS score
         -- User join because comments could be empty
-        FROM person u
-            LEFT JOIN comment c ON u.id = c.creator_id AND c.deleted = 'f' AND c.removed = 'f'
-            LEFT JOIN comment_like cl ON c.id = cl.comment_id
-        GROUP BY u.id
-    ) cd
-    WHERE ua.person_id = cd.creator_id;
+    FROM
+        person u
+    LEFT JOIN comment c ON u.id = c.creator_id
+        AND c.deleted = 'f'
+        AND c.removed = 'f'
+    LEFT JOIN comment_like cl ON c.id = cl.comment_id
+GROUP BY
+    u.id) cd
+WHERE
+    ua.person_id = cd.creator_id;
 
 -- Recalculate proper post count.
-UPDATE person_aggregates
-    SET post_count = cnt.count
-    FROM (
-    SELECT creator_id, count(*) AS count FROM post
-    WHERE deleted='f' AND removed='f'
-    GROUP BY creator_id
-        ) cnt
-    WHERE person_aggregates.person_id = cnt.creator_id;
+UPDATE
+    person_aggregates
+SET
+    post_count = cnt.count
+FROM (
+    SELECT
+        creator_id,
+        count(*) AS count
+    FROM
+        post
+    WHERE
+        deleted = 'f'
+        AND removed = 'f'
+    GROUP BY
+        creator_id) cnt
+WHERE
+    person_aggregates.person_id = cnt.creator_id;
 
 -- Recalculate proper post score.
- UPDATE person_aggregates ua
-    SET post_score = pd.score
-    FROM (
-        SELECT u.id AS creator_id,
-            coalesce(0, sum(pl.score)) AS score
-            -- User join because posts could be empty
-        FROM person u
-            LEFT JOIN post p ON u.id = p.creator_id AND p.deleted = 'f' AND p.removed = 'f'
-            LEFT JOIN post_like pl ON p.id = pl.post_id
-        GROUP BY u.id
-    ) pd
-    WHERE ua.person_id = pd.creator_id;
+UPDATE
+    person_aggregates ua
+SET
+    post_score = pd.score
+FROM (
+    SELECT
+        u.id AS creator_id,
+        coalesce(0, sum(pl.score)) AS score
+        -- User join because posts could be empty
+    FROM
+        person u
+    LEFT JOIN post p ON u.id = p.creator_id
+        AND p.deleted = 'f'
+        AND p.removed = 'f'
+    LEFT JOIN post_like pl ON p.id = pl.post_id
+GROUP BY
+    u.id) pd
+WHERE
+    ua.person_id = pd.creator_id;
+
index 5b135223e2410846e9186d1a01a27923a27053df..694b62eee89746ebd0e7bd342209c2bb99e0138f 100644 (file)
@@ -1,14 +1,37 @@
 -- update the default sort type
-update local_user set default_sort_type = 'TopDay' where default_sort_type in ('TopThreeMonths', 'TopSixMonths', 'TopNineMonths');
+UPDATE
+    local_user
+SET
+    default_sort_type = 'TopDay'
+WHERE
+    default_sort_type IN ('TopThreeMonths', 'TopSixMonths', 'TopNineMonths');
 
 -- rename the old enum
-alter type sort_type_enum rename to sort_type_enum__;
+ALTER TYPE sort_type_enum RENAME TO sort_type_enum__;
+
 -- create the new enum
-CREATE TYPE sort_type_enum AS ENUM ('Active', 'Hot', 'New', 'Old', 'TopDay', 'TopWeek', 'TopMonth', 'TopYear', 'TopAll', 'MostComments', 'NewComments', 'TopHour', 'TopSixHour', 'TopTwelveHour');
+CREATE TYPE sort_type_enum AS ENUM (
+    'Active',
+    'Hot',
+    'New',
+    'Old',
+    'TopDay',
+    'TopWeek',
+    'TopMonth',
+    'TopYear',
+    'TopAll',
+    'MostComments',
+    'NewComments',
+    'TopHour',
+    'TopSixHour',
+    'TopTwelveHour'
+);
 
 -- alter all you enum columns
-alter table local_user
-  alter column default_sort_type type sort_type_enum using default_sort_type::text::sort_type_enum;
+ALTER TABLE local_user
+    ALTER COLUMN default_sort_type TYPE sort_type_enum
+    USING default_sort_type::text::sort_type_enum;
 
 -- drop the old enum
-drop type sort_type_enum__;
+DROP TYPE sort_type_enum__;
+
index 85bcfad7c12425a012658354c0b625f327f9e579..7e5fcf77f00bd05acc9ebc1e6901b5c6a9281fdf 100644 (file)
@@ -1,4 +1,10 @@
 -- Update the enums
-ALTER TYPE sort_type_enum ADD VALUE 'TopThreeMonths';
-ALTER TYPE sort_type_enum ADD VALUE 'TopSixMonths';
-ALTER TYPE sort_type_enum ADD VALUE 'TopNineMonths';
+ALTER TYPE sort_type_enum
+    ADD VALUE 'TopThreeMonths';
+
+ALTER TYPE sort_type_enum
+    ADD VALUE 'TopSixMonths';
+
+ALTER TYPE sort_type_enum
+    ADD VALUE 'TopNineMonths';
+
index 4e5b83042c6af0d60bd33a1b3be1c1574824100b..ab0fc231e322273e5f8d843e2b768585bd3e2e76 100644 (file)
@@ -1 +1,2 @@
-drop table captcha_answer;
\ No newline at end of file
+DROP TABLE captcha_answer;
+
index 5c566bc929e6e75ada9ba9a162c10ae4b426e596..ede6f71feffe207bcd0f9544cc111e464e6ccd3e 100644 (file)
@@ -1,6 +1,7 @@
-create table captcha_answer (
-    id serial primary key,
-    uuid uuid not null unique default gen_random_uuid(),
-    answer text not null,
-    published timestamp not null default now()
+CREATE TABLE captcha_answer (
+    id serial PRIMARY KEY,
+    uuid uuid NOT NULL UNIQUE DEFAULT gen_random_uuid (),
+    answer text NOT NULL,
+    published timestamp NOT NULL DEFAULT now()
 );
+
index ae8c4c786c21f84678d4587fb1c0ad175a061f22..e7836108d54e6d0675b3927ce95b37296560770c 100644 (file)
@@ -1,2 +1,4 @@
 -- Add a no-op statement to prevent `diesel migration redo` errors
-SELECT 1;
+SELECT
+    1;
+
index cbe3be9e53cc0851aa48d805aa245785a5f65f5b..605f0ca93de2764b55e097ea0c0b9cec4f56a826 100644 (file)
@@ -1,5 +1,11 @@
-update community c
-set local=true
-from local_site ls
-  join site s on ls.site_id=s.id
-where c.instance_id=s.instance_id and not c.local;
+UPDATE
+    community c
+SET
+    local = TRUE
+FROM
+    local_site ls
+    JOIN site s ON ls.site_id = s.id
+WHERE
+    c.instance_id = s.instance_id
+    AND NOT c.local;
+
index 0731e06820d9ef0fb26c16e99cdd7a6e94029143..64480d540a021a2bc576f63973a31c211accf52d 100644 (file)
@@ -1,2 +1,6 @@
-alter table only local_user alter column theme TYPE character varying(20);
-alter table only local_user alter column theme set default 'browser'::character varying;
\ No newline at end of file
+ALTER TABLE ONLY local_user
+    ALTER COLUMN theme TYPE character varying(20);
+
+ALTER TABLE ONLY local_user
+    ALTER COLUMN theme SET DEFAULT 'browser'::character varying;
+
index cbab2566323b5496f792ef7920e07fb8687d6867..44f7dcf858df498c13fa38f0475888d9011be900 100644 (file)
@@ -1,2 +1,6 @@
-alter table only local_user alter column theme type text;
-alter table only local_user alter column theme set default 'browser'::text;
+ALTER TABLE ONLY local_user
+    ALTER COLUMN theme TYPE text;
+
+ALTER TABLE ONLY local_user
+    ALTER COLUMN theme SET DEFAULT 'browser'::text;
+
index a4dfd50b2636062b398c9baa8bbc1050cad32b1d..72ac5f2f31c8704a84da746407b2556a9fb13f88 100644 (file)
@@ -1 +1,3 @@
-alter table local_user drop column open_links_in_new_tab;
+ALTER TABLE local_user
+    DROP COLUMN open_links_in_new_tab;
+
index 39a4b44a1ed2c1268feeec90f2243a1c22a9eaf9..9bc1985584fca1c9b7ff94082d0397d98a4a0899 100644 (file)
@@ -1 +1,3 @@
-alter table local_user add column open_links_in_new_tab boolean default false not null;
+ALTER TABLE local_user
+    ADD COLUMN open_links_in_new_tab boolean DEFAULT FALSE NOT NULL;
+
index fa7f7d48f05ab7ff414d2077bc7fd56816c484d8..840d54b01285f374d0052ddabd563814173bf273 100644 (file)
@@ -1,2 +1,4 @@
-drop index idx_comment_aggregates_published;
-drop index idx_community_aggregates_published;
+DROP INDEX idx_comment_aggregates_published;
+
+DROP INDEX idx_community_aggregates_published;
+
index 42230af109260352d5b9dbc54ce403eb240045a0..b7f9d30e6dca67e4b8ccd92c4b3f483a1ebec6d8 100644 (file)
@@ -1,4 +1,5 @@
 -- Add indexes on published column (needed for hot_rank updates)
+CREATE INDEX idx_community_aggregates_published ON community_aggregates (published DESC);
+
+CREATE INDEX idx_comment_aggregates_published ON comment_aggregates (published DESC);
 
-create index idx_community_aggregates_published on community_aggregates (published desc);
-create index idx_comment_aggregates_published on comment_aggregates (published desc);
\ No newline at end of file
index 51fa70467f80bb9dcc2587ae4c656477408032da..9f56764d64c5e09ca959386d508e5f44ba479e8a 100644 (file)
@@ -1,2 +1,6 @@
-alter table local_user drop column blur_nsfw;
-alter table local_user drop column auto_expand;
+ALTER TABLE local_user
+    DROP COLUMN blur_nsfw;
+
+ALTER TABLE local_user
+    DROP COLUMN auto_expand;
+
index 3c66c9c79d3867b8de449989bcc09dd4bd3730a3..cff39601fd1371c60c5e0836ef2f25f8e93b5df9 100644 (file)
@@ -1,6 +1,8 @@
-
 -- Add the blur_nsfw to the local user table as a setting
-alter table local_user add column blur_nsfw boolean not null default true;
+ALTER TABLE local_user
+    ADD COLUMN blur_nsfw boolean NOT NULL DEFAULT TRUE;
 
 -- Add the auto_expand to the local user table as a setting
-alter table local_user add column auto_expand boolean not null default false;
+ALTER TABLE local_user
+    ADD COLUMN auto_expand boolean NOT NULL DEFAULT FALSE;
+
index 9b4996e36f5521517227a1e8ddbaf109b5656a71..e69f0493ef5abd8ba506bfd33422ec346e00fb79 100644 (file)
@@ -1,27 +1,46 @@
 -- Drop the new indexes
-drop index idx_person_admin;
-
-drop index idx_post_aggregates_featured_local_score;
-drop index idx_post_aggregates_featured_local_newest_comment_time;
-drop index idx_post_aggregates_featured_local_newest_comment_time_necro;
-drop index idx_post_aggregates_featured_local_hot;
-drop index idx_post_aggregates_featured_local_active;
-drop index idx_post_aggregates_featured_local_published;
-drop index idx_post_aggregates_published;
-
-drop index idx_post_aggregates_featured_community_score;
-drop index idx_post_aggregates_featured_community_newest_comment_time;
-drop index idx_post_aggregates_featured_community_newest_comment_time_necro;
-drop index idx_post_aggregates_featured_community_hot;
-drop index idx_post_aggregates_featured_community_active;
-drop index idx_post_aggregates_featured_community_published;
+DROP INDEX idx_person_admin;
+
+DROP INDEX idx_post_aggregates_featured_local_score;
+
+DROP INDEX idx_post_aggregates_featured_local_newest_comment_time;
+
+DROP INDEX idx_post_aggregates_featured_local_newest_comment_time_necro;
+
+DROP INDEX idx_post_aggregates_featured_local_hot;
+
+DROP INDEX idx_post_aggregates_featured_local_active;
+
+DROP INDEX idx_post_aggregates_featured_local_published;
+
+DROP INDEX idx_post_aggregates_published;
+
+DROP INDEX idx_post_aggregates_featured_community_score;
+
+DROP INDEX idx_post_aggregates_featured_community_newest_comment_time;
+
+DROP INDEX idx_post_aggregates_featured_community_newest_comment_time_necro;
+
+DROP INDEX idx_post_aggregates_featured_community_hot;
+
+DROP INDEX idx_post_aggregates_featured_community_active;
+
+DROP INDEX idx_post_aggregates_featured_community_published;
 
 -- Create single column indexes again
-create index idx_post_aggregates_score on post_aggregates (score desc);
-create index idx_post_aggregates_published on post_aggregates (published desc);
-create index idx_post_aggregates_newest_comment_time on post_aggregates (newest_comment_time desc);
-create index idx_post_aggregates_newest_comment_time_necro on post_aggregates (newest_comment_time_necro desc);
-create index idx_post_aggregates_featured_community on post_aggregates (featured_community desc);
-create index idx_post_aggregates_featured_local on post_aggregates (featured_local desc);
-create index idx_post_aggregates_hot on post_aggregates (hot_rank desc);
-create index idx_post_aggregates_active on post_aggregates (hot_rank_active desc);
+CREATE INDEX idx_post_aggregates_score ON post_aggregates (score DESC);
+
+CREATE INDEX idx_post_aggregates_published ON post_aggregates (published DESC);
+
+CREATE INDEX idx_post_aggregates_newest_comment_time ON post_aggregates (newest_comment_time DESC);
+
+CREATE INDEX idx_post_aggregates_newest_comment_time_necro ON post_aggregates (newest_comment_time_necro DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community ON post_aggregates (featured_community DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local ON post_aggregates (featured_local DESC);
+
+CREATE INDEX idx_post_aggregates_hot ON post_aggregates (hot_rank DESC);
+
+CREATE INDEX idx_post_aggregates_active ON post_aggregates (hot_rank_active DESC);
+
index c55ecee9d34b484924c2c908e5e71e8752081aab..85c5827f21e9ea6ceb318c7f5f711a6ab48f2ece 100644 (file)
@@ -1,32 +1,49 @@
 -- Create an admin person index
-create index if not exists idx_person_admin on person (admin);
+CREATE INDEX IF NOT EXISTS idx_person_admin ON person (admin);
 
 -- Compound indexes, using featured_, then the other sorts, proved to be much faster
 -- Drop the old indexes
-drop index idx_post_aggregates_score;
-drop index idx_post_aggregates_published;
-drop index idx_post_aggregates_newest_comment_time;
-drop index idx_post_aggregates_newest_comment_time_necro;
-drop index idx_post_aggregates_featured_community;
-drop index idx_post_aggregates_featured_local;
-drop index idx_post_aggregates_hot;
-drop index idx_post_aggregates_active;
+DROP INDEX idx_post_aggregates_score;
+
+DROP INDEX idx_post_aggregates_published;
+
+DROP INDEX idx_post_aggregates_newest_comment_time;
+
+DROP INDEX idx_post_aggregates_newest_comment_time_necro;
+
+DROP INDEX idx_post_aggregates_featured_community;
+
+DROP INDEX idx_post_aggregates_featured_local;
+
+DROP INDEX idx_post_aggregates_hot;
+
+DROP INDEX idx_post_aggregates_active;
 
 -- featured_local
-create index idx_post_aggregates_featured_local_score on post_aggregates (featured_local desc, score desc);
-create index idx_post_aggregates_featured_local_newest_comment_time on post_aggregates (featured_local desc, newest_comment_time desc);
-create index idx_post_aggregates_featured_local_newest_comment_time_necro on post_aggregates (featured_local desc, newest_comment_time_necro desc);
-create index idx_post_aggregates_featured_local_hot on post_aggregates (featured_local desc, hot_rank desc);
-create index idx_post_aggregates_featured_local_active on post_aggregates (featured_local desc, hot_rank_active desc);
-create index idx_post_aggregates_featured_local_published on post_aggregates (featured_local desc, published desc);
-create index idx_post_aggregates_published on post_aggregates (published desc);
+CREATE INDEX idx_post_aggregates_featured_local_score ON post_aggregates (featured_local DESC, score DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local_newest_comment_time ON post_aggregates (featured_local DESC, newest_comment_time DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local_newest_comment_time_necro ON post_aggregates (featured_local DESC, newest_comment_time_necro DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local_hot ON post_aggregates (featured_local DESC, hot_rank DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local_active ON post_aggregates (featured_local DESC, hot_rank_active DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local_published ON post_aggregates (featured_local DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_published ON post_aggregates (published DESC);
 
 -- featured_community
-create index idx_post_aggregates_featured_community_score on post_aggregates (featured_community desc, score desc);
-create index idx_post_aggregates_featured_community_newest_comment_time on post_aggregates (featured_community desc, newest_comment_time desc);
-create index idx_post_aggregates_featured_community_newest_comment_time_necro on post_aggregates (featured_community desc, newest_comment_time_necro desc);
-create index idx_post_aggregates_featured_community_hot on post_aggregates (featured_community desc, hot_rank desc);
-create index idx_post_aggregates_featured_community_active on post_aggregates (featured_community desc, hot_rank_active desc);
-create index idx_post_aggregates_featured_community_published on post_aggregates (featured_community desc, published desc);
+CREATE INDEX idx_post_aggregates_featured_community_score ON post_aggregates (featured_community DESC, score DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_newest_comment_time ON post_aggregates (featured_community DESC, newest_comment_time DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_newest_comment_time_necro ON post_aggregates (featured_community DESC, newest_comment_time_necro DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_hot ON post_aggregates (featured_community DESC, hot_rank DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_active ON post_aggregates (featured_community DESC, hot_rank_active DESC);
 
+CREATE INDEX idx_post_aggregates_featured_community_published ON post_aggregates (featured_community DESC, published DESC);
 
index b77e174750918494a3727376d0790a258ca3960e..9eba8f581aa51a10a3d576ee5aac45d75d501101 100644 (file)
@@ -1,2 +1,4 @@
-drop index idx_person_admin;
-create index idx_person_admin on person(admin);
\ No newline at end of file
+DROP INDEX idx_person_admin;
+
+CREATE INDEX idx_person_admin ON person (admin);
+
index c71052ec7c153dd5a24c6eff9e8872949bda27a3..7188480288f9b79d60e280d7875522042048df86 100644 (file)
@@ -1,2 +1,7 @@
-drop index if exists idx_person_admin;
-create index idx_person_admin on person(admin) where admin; -- allow quickly finding all admins (PersonView::admins)
\ No newline at end of file
+DROP INDEX IF EXISTS idx_person_admin;
+
+CREATE INDEX idx_person_admin ON person (admin)
+WHERE
+    admin;
+
+-- allow quickly finding all admins (PersonView::admins)
index b8f9a63f9a9f2e2d1f8e442537a00d1761fe78ab..4816c616e39a6f31fc2ab9e57cc1926962e34003 100644 (file)
@@ -1,9 +1,9 @@
-CREATE OR REPLACE FUNCTION hot_rank(score numeric, published timestamp without time zone)
+CREATE OR REPLACE FUNCTION hot_rank (score numeric, published timestamp without time zone)
     RETURNS integer
     AS $$
 BEGIN
     -- hours_diff:=EXTRACT(EPOCH FROM (timezone('utc',now()) - published))/3600
-    RETURN floor(10000 * log(greatest(1, score + 3)) / power(((EXTRACT(EPOCH FROM(timezone('utc', now()) - published)) / 3600) + 2), 1.8))::integer;
+    RETURN floor(10000 * log(greatest (1, score + 3)) / power(((EXTRACT(EPOCH FROM (timezone('utc', now()) - published)) / 3600) + 2), 1.8))::integer;
 END;
 $$
 LANGUAGE plpgsql
index 41baa673cb013f93d196622a4be5cdc5abd8e851..83b5f0f0b8bce5f25fce61004ab24187c7f106e8 100644 (file)
@@ -1,11 +1,11 @@
-CREATE OR REPLACE FUNCTION hot_rank(score numeric, published timestamp without time zone)
+CREATE OR REPLACE FUNCTION hot_rank (score numeric, published timestamp without time zone)
     RETURNS integer
     AS $$
 DECLARE
     hours_diff numeric := EXTRACT(EPOCH FROM (timezone('utc', now()) - published)) / 3600;
 BEGIN
     IF (hours_diff > 0) THEN
-        RETURN floor(10000 * log(greatest(1, score + 3)) / power((hours_diff + 2), 1.8))::integer;
+        RETURN floor(10000 * log(greatest (1, score + 3)) / power((hours_diff + 2), 1.8))::integer;
     ELSE
         RETURN 0;
     END IF;
index b9616dee778914c7736c85a05500fe3fab46ed66..d9fce66e39039cf76f3c2dd9140fdf2f03ad7dd4 100644 (file)
 -- 2023-06-19-120700_no_double_deletion/up.sql
-create or replace function was_removed_or_deleted(TG_OP text, OLD record, NEW record)
-RETURNS boolean
-LANGUAGE plpgsql
-as $$
-    begin
-        IF (TG_OP = 'INSERT') THEN
-            return false;
-        end if;
-
-        IF (TG_OP = 'DELETE' AND OLD.deleted = 'f' AND OLD.removed = 'f') THEN
-            return true;
-        end if;
-
-    return TG_OP = 'UPDATE' AND (
-            (OLD.deleted = 'f' AND NEW.deleted = 't') OR
-            (OLD.removed = 'f' AND NEW.removed = 't')
-            );
-END $$;
+CREATE OR REPLACE FUNCTION was_removed_or_deleted (TG_OP text, OLD record, NEW record)
+    RETURNS boolean
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        RETURN FALSE;
+    END IF;
+    IF (TG_OP = 'DELETE' AND OLD.deleted = 'f' AND OLD.removed = 'f') THEN
+        RETURN TRUE;
+    END IF;
+    RETURN TG_OP = 'UPDATE'
+        AND ((OLD.deleted = 'f'
+                AND NEW.deleted = 't')
+            OR (OLD.removed = 'f'
+                AND NEW.removed = 't'));
+END
+$$;
 
 -- 2022-04-04-183652_update_community_aggregates_on_soft_delete/up.sql
-create or replace function was_restored_or_created(TG_OP text, OLD record, NEW record)
+CREATE OR REPLACE FUNCTION was_restored_or_created (TG_OP text, OLD record, NEW record)
     RETURNS boolean
     LANGUAGE plpgsql
-as $$
-begin
+    AS $$
+BEGIN
     IF (TG_OP = 'DELETE') THEN
-        return false;
-    end if;
-
+        RETURN FALSE;
+    END IF;
     IF (TG_OP = 'INSERT') THEN
-        return true;
-    end if;
-
-   return TG_OP = 'UPDATE' AND (
-        (OLD.deleted = 't' AND NEW.deleted = 'f') OR
-        (OLD.removed = 't' AND NEW.removed = 'f')
-        );
-END $$;
+        RETURN TRUE;
+    END IF;
+    RETURN TG_OP = 'UPDATE'
+        AND ((OLD.deleted = 't'
+                AND NEW.deleted = 'f')
+            OR (OLD.removed = 't'
+                AND NEW.removed = 'f'));
+END
+$$;
 
 -- 2021-08-02-002342_comment_count_fixes/up.sql
-create or replace function post_aggregates_comment_deleted()
-returns trigger language plpgsql
-as $$
-begin
-  IF NEW.deleted = TRUE THEN
-    update post_aggregates pa
-    set comments = comments - 1
-    where pa.post_id = NEW.post_id;
-  ELSE
-    update post_aggregates pa
-    set comments = comments + 1
-    where pa.post_id = NEW.post_id;
-  END IF;
-  return null;
-end $$;
-
-create trigger post_aggregates_comment_set_deleted
-after update of deleted on comment
-for each row
-execute procedure post_aggregates_comment_deleted();
-
-create or replace function post_aggregates_comment_count()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update post_aggregates pa
-    set comments = comments + 1,
-    newest_comment_time = NEW.published
-    where pa.post_id = NEW.post_id;
+CREATE OR REPLACE FUNCTION post_aggregates_comment_deleted ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF NEW.deleted = TRUE THEN
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments - 1
+        WHERE
+            pa.post_id = NEW.post_id;
+    ELSE
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments + 1
+        WHERE
+            pa.post_id = NEW.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-    -- A 2 day necro-bump limit
-    update post_aggregates pa
-    set newest_comment_time_necro = NEW.published
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = NEW.post_id
-    -- Fix issue with being able to necro-bump your own post
-    and NEW.creator_id != p.creator_id
-    and pa.published > ('now'::timestamp - '2 days'::interval);
+CREATE TRIGGER post_aggregates_comment_set_deleted
+    AFTER UPDATE OF deleted ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE post_aggregates_comment_deleted ();
 
-  ELSIF (TG_OP = 'DELETE') THEN
-    -- Join to post because that post may not exist anymore
-    update post_aggregates pa
-    set comments = comments - 1
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = OLD.post_id;
-  ELSIF (TG_OP = 'UPDATE') THEN
-    -- Join to post because that post may not exist anymore
-    update post_aggregates pa
-    set comments = comments - 1
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = OLD.post_id;
-  END IF;
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION post_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments + 1,
+            newest_comment_time = NEW.published
+        WHERE
+            pa.post_id = NEW.post_id;
+        -- A 2 day necro-bump limit
+        UPDATE
+            post_aggregates pa
+        SET
+            newest_comment_time_necro = NEW.published
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = NEW.post_id
+            -- Fix issue with being able to necro-bump your own post
+            AND NEW.creator_id != p.creator_id
+            AND pa.published > ('now'::timestamp - '2 days'::interval);
+    ELSIF (TG_OP = 'DELETE') THEN
+        -- Join to post because that post may not exist anymore
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments - 1
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = OLD.post_id;
+    ELSIF (TG_OP = 'UPDATE') THEN
+        -- Join to post because that post may not exist anymore
+        UPDATE
+            post_aggregates pa
+        SET
+            comments = comments - 1
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = OLD.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
 -- 2020-12-10-152350_create_post_aggregates/up.sql
-create or replace trigger post_aggregates_comment_count
-after insert or delete on comment
-for each row
-execute procedure post_aggregates_comment_count();
+CREATE OR REPLACE TRIGGER post_aggregates_comment_count
+    AFTER INSERT OR DELETE ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE post_aggregates_comment_count ();
+
index abc89d283db5fc39f6811871b57e036b59290fb3..2884f934f837ec8954cc0c6f1d56db151051573f 100644 (file)
 -- Fix for duplicated decrementations when both `deleted` and `removed` fields are set subsequently
-create or replace function was_removed_or_deleted(TG_OP text, OLD record, NEW record)
-RETURNS boolean
-LANGUAGE plpgsql
-as $$
-    begin
-        IF (TG_OP = 'INSERT') THEN
-            return false;
-        end if;
-
-        IF (TG_OP = 'DELETE' AND OLD.deleted = 'f' AND OLD.removed = 'f') THEN
-            return true;
-        end if;
-
-    return TG_OP = 'UPDATE' AND OLD.deleted = 'f' AND OLD.removed = 'f' AND (
-            NEW.deleted = 't' OR NEW.removed = 't'
-            );
-END $$;
+CREATE OR REPLACE FUNCTION was_removed_or_deleted (TG_OP text, OLD record, NEW record)
+    RETURNS boolean
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        RETURN FALSE;
+    END IF;
+    IF (TG_OP = 'DELETE' AND OLD.deleted = 'f' AND OLD.removed = 'f') THEN
+        RETURN TRUE;
+    END IF;
+    RETURN TG_OP = 'UPDATE'
+        AND OLD.deleted = 'f'
+        AND OLD.removed = 'f'
+        AND (NEW.deleted = 't'
+            OR NEW.removed = 't');
+END
+$$;
 
-create or replace function was_restored_or_created(TG_OP text, OLD record, NEW record)
+CREATE OR REPLACE FUNCTION was_restored_or_created (TG_OP text, OLD record, NEW record)
     RETURNS boolean
     LANGUAGE plpgsql
-as $$
-begin
+    AS $$
+BEGIN
     IF (TG_OP = 'DELETE') THEN
-        return false;
-    end if;
-
+        RETURN FALSE;
+    END IF;
     IF (TG_OP = 'INSERT') THEN
-        return true;
-    end if;
-
-   return TG_OP = 'UPDATE' AND NEW.deleted = 'f' AND NEW.removed = 'f' AND (
-            OLD.deleted = 't' OR OLD.removed = 't'
-            );
-END $$;
+        RETURN TRUE;
+    END IF;
+    RETURN TG_OP = 'UPDATE'
+        AND NEW.deleted = 'f'
+        AND NEW.removed = 'f'
+        AND (OLD.deleted = 't'
+            OR OLD.removed = 't');
+END
+$$;
 
 -- Fix for post's comment count not updating after setting `removed` to 't'
-drop trigger if exists post_aggregates_comment_set_deleted on comment;
-drop function post_aggregates_comment_deleted();
+DROP TRIGGER IF EXISTS post_aggregates_comment_set_deleted ON comment;
+
+DROP FUNCTION post_aggregates_comment_deleted ();
 
-create or replace function post_aggregates_comment_count()
-    returns trigger language plpgsql
-as $$
-begin
+CREATE OR REPLACE FUNCTION post_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
     -- Check for post existence - it may not exist anymore
     IF TG_OP = 'INSERT' OR EXISTS (
-        select 1 from post p where p.id = OLD.post_id
-    ) THEN
-        IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-            update post_aggregates pa
-            set comments = comments + 1 where pa.post_id = NEW.post_id;
-        ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-            update post_aggregates pa
-            set comments = comments - 1 where pa.post_id = OLD.post_id;
+        SELECT
+            1
+        FROM
+            post p
+        WHERE
+            p.id = OLD.post_id) THEN
+        IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+            UPDATE
+                post_aggregates pa
+            SET
+                comments = comments + 1
+            WHERE
+                pa.post_id = NEW.post_id;
+        ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+            UPDATE
+                post_aggregates pa
+            SET
+                comments = comments - 1
+            WHERE
+                pa.post_id = OLD.post_id;
         END IF;
     END IF;
-
     IF TG_OP = 'INSERT' THEN
-        update post_aggregates pa
-        set newest_comment_time = NEW.published
-        where pa.post_id = NEW.post_id;
-
+        UPDATE
+            post_aggregates pa
+        SET
+            newest_comment_time = NEW.published
+        WHERE
+            pa.post_id = NEW.post_id;
         -- A 2 day necro-bump limit
-        update post_aggregates pa
-        set newest_comment_time_necro = NEW.published
-        from post p
-        where pa.post_id = p.id
-        and pa.post_id = NEW.post_id
-        -- Fix issue with being able to necro-bump your own post
-        and NEW.creator_id != p.creator_id
-        and pa.published > ('now'::timestamp - '2 days'::interval);
+        UPDATE
+            post_aggregates pa
+        SET
+            newest_comment_time_necro = NEW.published
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = NEW.post_id
+            -- Fix issue with being able to necro-bump your own post
+            AND NEW.creator_id != p.creator_id
+            AND pa.published > ('now'::timestamp - '2 days'::interval);
     END IF;
+    RETURN NULL;
+END
+$$;
 
-    return null;
-end $$;
+CREATE OR REPLACE TRIGGER post_aggregates_comment_count
+    AFTER INSERT OR DELETE OR UPDATE OF removed,
+    deleted ON comment
+    FOR EACH ROW
+    EXECUTE PROCEDURE post_aggregates_comment_count ();
 
-create or replace trigger post_aggregates_comment_count
-    after insert or delete or update of removed, deleted on comment
-    for each row
-execute procedure post_aggregates_comment_count();
\ No newline at end of file
index 66ff507d9d416a2f5f69255b83495de942e1195b..721922db074fc7178c4645e86f44d88055a78ab5 100644 (file)
@@ -1 +1,3 @@
-alter table local_user drop column infinite_scroll_enabled;
+ALTER TABLE local_user
+    DROP COLUMN infinite_scroll_enabled;
+
index 905bc710970f76856e9b14010a168949ba65fcad..b435e2e2724e29d3fded9516822cc1a471c0de2c 100644 (file)
@@ -1 +1,3 @@
-alter table local_user add column infinite_scroll_enabled boolean default false not null;
+ALTER TABLE local_user
+    ADD COLUMN infinite_scroll_enabled boolean DEFAULT FALSE NOT NULL;
+
index ea4f4d4a39728c7217fa2a2686f0b869d2459d7a..188b4afa4c0b718fbf441d7ddac0ad2c2fcc4a2c 100644 (file)
@@ -1,21 +1,28 @@
-create table activity (
-    id serial primary key,
-    data jsonb not null,
-    local boolean not null default true,
-    published timestamp not null default now(),
+CREATE TABLE activity (
+    id serial PRIMARY KEY,
+    data jsonb NOT NULL,
+    local boolean NOT NULL DEFAULT TRUE,
+    published timestamp NOT NULL DEFAULT now(),
     updated timestamp,
-    ap_id text not null,
-    sensitive boolean not null default true
+    ap_id text NOT NULL,
+    sensitive boolean NOT NULL DEFAULT TRUE
 );
 
-insert into activity(ap_id, data, sensitive, published)
-    select ap_id, data, sensitive, published
-    from sent_activity
-    order by id desc
-    limit 100000;
+INSERT INTO activity (ap_id, data, sensitive, published)
+SELECT
+    ap_id,
+    data,
+    sensitive,
+    published
+FROM
+    sent_activity
+ORDER BY
+    id DESC
+LIMIT 100000;
 
 -- We cant copy received_activity entries back into activities table because we dont have data
 -- which is mandatory.
+DROP TABLE sent_activity;
+
+DROP TABLE received_activity;
 
-drop table sent_activity;
-drop table received_activity;
\ No newline at end of file
index c6b30b7b7c3557f18faae3266b4345358b353777..4d723491a09d8f112f5574ebba94f7e6dca066fa 100644 (file)
@@ -1,35 +1,48 @@
 -- outgoing activities, need to be stored to be later server over http
 -- we change data column from jsonb to json for decreased size
 -- https://stackoverflow.com/a/22910602
-create table sent_activity (
-    id bigserial primary key,
-    ap_id text unique not null,
-    data json not null,
-    sensitive boolean not null,
-    published timestamp not null default now()
+CREATE TABLE sent_activity (
+    id bigserial PRIMARY KEY,
+    ap_id text UNIQUE NOT NULL,
+    data json NOT NULL,
+    sensitive boolean NOT NULL,
+    published timestamp NOT NULL DEFAULT now()
 );
 
 -- incoming activities, we only need the id to avoid processing the same activity multiple times
-create table received_activity (
-    id bigserial primary key,
-    ap_id text unique not null,
-    published timestamp not null default now()
+CREATE TABLE received_activity (
+    id bigserial PRIMARY KEY,
+    ap_id text UNIQUE NOT NULL,
+    published timestamp NOT NULL DEFAULT now()
 );
 
 -- copy sent activities to new table. only copy last 100k for faster migration
-insert into sent_activity(ap_id, data, sensitive, published)
-    select ap_id, data, sensitive, published
-    from activity
-    where local = true
-    order by id desc
-    limit 100000;
+INSERT INTO sent_activity (ap_id, data, sensitive, published)
+SELECT
+    ap_id,
+    data,
+    sensitive,
+    published
+FROM
+    activity
+WHERE
+    local = TRUE
+ORDER BY
+    id DESC
+LIMIT 100000;
 
 -- copy received activities to new table. only last 1m for faster migration
-insert into received_activity(ap_id, published)
-    select ap_id, published
-    from activity
-    where local = false
-    order by id desc
-    limit 1000000;
+INSERT INTO received_activity (ap_id, published)
+SELECT
+    ap_id,
+    published
+FROM
+    activity
+WHERE
+    local = FALSE
+ORDER BY
+    id DESC
+LIMIT 1000000;
+
+DROP TABLE activity;
 
-drop table activity;
index 5661a3146d641812475694c1964559a17d22a464..7bc306f9137f2db2236d24c33e1d8957665ce450 100644 (file)
@@ -1,26 +1,40 @@
 -- Drop the new indexes
-drop index idx_post_aggregates_featured_local_most_comments;
-drop index idx_post_aggregates_featured_local_hot;
-drop index idx_post_aggregates_featured_local_active;
-drop index idx_post_aggregates_featured_local_score;
-drop index idx_post_aggregates_featured_community_hot;
-drop index idx_post_aggregates_featured_community_active;
-drop index idx_post_aggregates_featured_community_score;
-drop index idx_post_aggregates_featured_community_most_comments;
-drop index idx_comment_aggregates_hot;
-drop index idx_comment_aggregates_score;
+DROP INDEX idx_post_aggregates_featured_local_most_comments;
+
+DROP INDEX idx_post_aggregates_featured_local_hot;
+
+DROP INDEX idx_post_aggregates_featured_local_active;
+
+DROP INDEX idx_post_aggregates_featured_local_score;
+
+DROP INDEX idx_post_aggregates_featured_community_hot;
+
+DROP INDEX idx_post_aggregates_featured_community_active;
+
+DROP INDEX idx_post_aggregates_featured_community_score;
+
+DROP INDEX idx_post_aggregates_featured_community_most_comments;
+
+DROP INDEX idx_comment_aggregates_hot;
+
+DROP INDEX idx_comment_aggregates_score;
 
 -- Add the old ones back in
 -- featured_local
-create index idx_post_aggregates_featured_local_hot on post_aggregates (featured_local desc, hot_rank desc);
-create index idx_post_aggregates_featured_local_active on post_aggregates (featured_local desc, hot_rank_active desc);
-create index idx_post_aggregates_featured_local_score on post_aggregates (featured_local desc, score desc);
+CREATE INDEX idx_post_aggregates_featured_local_hot ON post_aggregates (featured_local DESC, hot_rank DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local_active ON post_aggregates (featured_local DESC, hot_rank_active DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local_score ON post_aggregates (featured_local DESC, score DESC);
 
 -- featured_community
-create index idx_post_aggregates_featured_community_hot on post_aggregates (featured_community desc, hot_rank desc);
-create index idx_post_aggregates_featured_community_active on post_aggregates (featured_community desc, hot_rank_active desc);
-create index idx_post_aggregates_featured_community_score on post_aggregates (featured_community desc, score desc);
+CREATE INDEX idx_post_aggregates_featured_community_hot ON post_aggregates (featured_community DESC, hot_rank DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_active ON post_aggregates (featured_community DESC, hot_rank_active DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_score ON post_aggregates (featured_community DESC, score DESC);
+
+CREATE INDEX idx_comment_aggregates_hot ON comment_aggregates (hot_rank DESC);
 
-create index idx_comment_aggregates_hot on comment_aggregates (hot_rank desc);
-create index idx_comment_aggregates_score on comment_aggregates (score desc);
+CREATE INDEX idx_comment_aggregates_score ON comment_aggregates (score DESC);
 
index 94e426fc7c66255447aa8d2833fdbfbe0cdcf306..728ca38d4581bb0ebb254077b6a32081fcde9ceb 100644 (file)
@@ -1,30 +1,42 @@
 -- Drop the old indexes
-drop index idx_post_aggregates_featured_local_hot;
-drop index idx_post_aggregates_featured_local_active;
-drop index idx_post_aggregates_featured_local_score;
-drop index idx_post_aggregates_featured_community_hot;
-drop index idx_post_aggregates_featured_community_active;
-drop index idx_post_aggregates_featured_community_score;
-drop index idx_comment_aggregates_hot;
-drop index idx_comment_aggregates_score;
+DROP INDEX idx_post_aggregates_featured_local_hot;
 
--- Add a published desc, to the end of the hot and active ranks
+DROP INDEX idx_post_aggregates_featured_local_active;
+
+DROP INDEX idx_post_aggregates_featured_local_score;
+
+DROP INDEX idx_post_aggregates_featured_community_hot;
+
+DROP INDEX idx_post_aggregates_featured_community_active;
+
+DROP INDEX idx_post_aggregates_featured_community_score;
+
+DROP INDEX idx_comment_aggregates_hot;
+
+DROP INDEX idx_comment_aggregates_score;
 
+-- Add a published desc, to the end of the hot and active ranks
 -- Add missing most comments index
-create index idx_post_aggregates_featured_local_most_comments on post_aggregates (featured_local desc, comments desc, published desc);
-create index idx_post_aggregates_featured_community_most_comments on post_aggregates (featured_community desc, comments desc, published desc);
+CREATE INDEX idx_post_aggregates_featured_local_most_comments ON post_aggregates (featured_local DESC, comments DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_most_comments ON post_aggregates (featured_community DESC, comments DESC, published DESC);
 
 -- featured_local
-create index idx_post_aggregates_featured_local_hot on post_aggregates (featured_local desc, hot_rank desc, published desc);
-create index idx_post_aggregates_featured_local_active on post_aggregates (featured_local desc, hot_rank_active desc, published desc);
-create index idx_post_aggregates_featured_local_score on post_aggregates (featured_local desc, score desc, published desc);
+CREATE INDEX idx_post_aggregates_featured_local_hot ON post_aggregates (featured_local DESC, hot_rank DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local_active ON post_aggregates (featured_local DESC, hot_rank_active DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_featured_local_score ON post_aggregates (featured_local DESC, score DESC, published DESC);
 
 -- featured_community
-create index idx_post_aggregates_featured_community_hot on post_aggregates (featured_community desc, hot_rank desc, published desc);
-create index idx_post_aggregates_featured_community_active on post_aggregates (featured_community desc, hot_rank_active desc, published desc);
-create index idx_post_aggregates_featured_community_score on post_aggregates (featured_community desc, score desc, published desc);
+CREATE INDEX idx_post_aggregates_featured_community_hot ON post_aggregates (featured_community DESC, hot_rank DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_active ON post_aggregates (featured_community DESC, hot_rank_active DESC, published DESC);
+
+CREATE INDEX idx_post_aggregates_featured_community_score ON post_aggregates (featured_community DESC, score DESC, published DESC);
 
 -- Fixing some comment aggregates ones
-create index idx_comment_aggregates_hot on comment_aggregates (hot_rank desc, published desc);
-create index idx_comment_aggregates_score on comment_aggregates (score desc, published desc);
+CREATE INDEX idx_comment_aggregates_hot ON comment_aggregates (hot_rank DESC, published DESC);
+
+CREATE INDEX idx_comment_aggregates_score ON comment_aggregates (score DESC, published DESC);
 
index 3e247b58c5af2776ce415b7caa06c30ec80de322..658853eb317ee7028a22284f27e541f8e9615dab 100644 (file)
@@ -1,4 +1,7 @@
 -- This file should undo anything in `up.sql`
 DROP INDEX idx_community_aggregates_nonzero_hotrank;
+
 DROP INDEX idx_comment_aggregates_nonzero_hotrank;
-DROP INDEX idx_post_aggregates_nonzero_hotrank;
\ No newline at end of file
+
+DROP INDEX idx_post_aggregates_nonzero_hotrank;
+
index 2d3cd3b295d9fb8ad06e64695c750e786196e001..d51aa0dc4ac254af965539e65af104a6b600441a 100644 (file)
@@ -1,4 +1,13 @@
 -- Your SQL goes here
-CREATE INDEX idx_community_aggregates_nonzero_hotrank ON community_aggregates (published) WHERE hot_rank != 0;
-CREATE INDEX idx_comment_aggregates_nonzero_hotrank ON comment_aggregates (published) WHERE hot_rank != 0;
-CREATE INDEX idx_post_aggregates_nonzero_hotrank ON post_aggregates (published DESC) WHERE hot_rank != 0 OR hot_rank_active != 0;
\ No newline at end of file
+CREATE INDEX idx_community_aggregates_nonzero_hotrank ON community_aggregates (published)
+WHERE
+    hot_rank != 0;
+
+CREATE INDEX idx_comment_aggregates_nonzero_hotrank ON comment_aggregates (published)
+WHERE
+    hot_rank != 0;
+
+CREATE INDEX idx_post_aggregates_nonzero_hotrank ON post_aggregates (published DESC)
+WHERE
+    hot_rank != 0 OR hot_rank_active != 0;
+
index 91e2dc86271764cd1977c219a5a6cbfa5f351efd..8e221465648cde3f50a8c7c959e234742f8827ea 100644 (file)
@@ -1,20 +1,21 @@
 -- This file should undo anything in `up.sql`
-
-CREATE OR REPLACE FUNCTION post_aggregates_post()
-    RETURNS trigger
+CREATE OR REPLACE FUNCTION post_aggregates_post ()
+    RETURNS TRIGGER
     LANGUAGE plpgsql
-AS
-$$
+    AS $$
 BEGIN
     IF (TG_OP = 'INSERT') THEN
         INSERT INTO post_aggregates (post_id, published, newest_comment_time, newest_comment_time_necro)
-        VALUES (NEW.id, NEW.published, NEW.published, NEW.published);
+            VALUES (NEW.id, NEW.published, NEW.published, NEW.published);
     ELSIF (TG_OP = 'DELETE') THEN
-        DELETE FROM post_aggregates WHERE post_id = OLD.id;
+        DELETE FROM post_aggregates
+        WHERE post_id = OLD.id;
     END IF;
     RETURN NULL;
 END
 $$;
 
-ALTER TABLE post_aggregates DROP COLUMN community_id, DROP COLUMN creator_id;
+ALTER TABLE post_aggregates
+    DROP COLUMN community_id,
+    DROP COLUMN creator_id;
 
index f28701da0c4d593cf00231dc27228728eb0ef676..d909ce0224f55e93d3ffbadc1e8b0d7677964bbf 100644 (file)
@@ -3,33 +3,33 @@ ALTER TABLE post_aggregates
     ADD COLUMN community_id integer REFERENCES community ON UPDATE CASCADE ON DELETE CASCADE,
     ADD COLUMN creator_id integer REFERENCES person ON UPDATE CASCADE ON DELETE CASCADE;
 
-CREATE OR REPLACE FUNCTION post_aggregates_post()
-    RETURNS trigger
+CREATE OR REPLACE FUNCTION post_aggregates_post ()
+    RETURNS TRIGGER
     LANGUAGE plpgsql
-AS
-$$
+    AS $$
 BEGIN
     IF (TG_OP = 'INSERT') THEN
-        INSERT INTO post_aggregates (post_id,
-                                     published,
-                                     newest_comment_time,
-                                     newest_comment_time_necro,
-                                     community_id,
-                                     creator_id)
-        VALUES (NEW.id, NEW.published, NEW.published, NEW.published, NEW.community_id, NEW.creator_id);
+        INSERT INTO post_aggregates (post_id, published, newest_comment_time, newest_comment_time_necro, community_id, creator_id)
+            VALUES (NEW.id, NEW.published, NEW.published, NEW.published, NEW.community_id, NEW.creator_id);
     ELSIF (TG_OP = 'DELETE') THEN
-        DELETE FROM post_aggregates WHERE post_id = OLD.id;
+        DELETE FROM post_aggregates
+        WHERE post_id = OLD.id;
     END IF;
     RETURN NULL;
 END
 $$;
 
-UPDATE post_aggregates
-SET community_id=post.community_id,
-    creator_id=post.creator_id
-FROM post
-WHERE post.id = post_aggregates.post_id;
+UPDATE
+    post_aggregates
+SET
+    community_id = post.community_id,
+    creator_id = post.creator_id
+FROM
+    post
+WHERE
+    post.id = post_aggregates.post_id;
 
 ALTER TABLE post_aggregates
     ALTER COLUMN community_id SET NOT NULL,
-    ALTER COLUMN creator_id SET NOT NULL;
\ No newline at end of file
+    ALTER COLUMN creator_id SET NOT NULL;
+
index 9f784c067d179c5268425edf3c06c9f83ef1b127..f7cdf93eaf309af0fd78c34c3dea3f68b8ec1be4 100644 (file)
@@ -1,4 +1,6 @@
-drop index idx_comment_aggregates_hot, idx_comment_aggregates_score;
+DROP INDEX idx_comment_aggregates_hot, idx_comment_aggregates_score;
+
+CREATE INDEX idx_comment_aggregates_hot ON comment_aggregates (hot_rank DESC, published DESC);
+
+CREATE INDEX idx_comment_aggregates_score ON comment_aggregates (score DESC, published DESC);
 
-create index idx_comment_aggregates_hot on comment_aggregates (hot_rank desc, published desc);
-create index idx_comment_aggregates_score on comment_aggregates (score desc, published desc);
index 02eff3ed2b34dbd0af56ea8506a28ffe3795a909..94ad1cc6646124bd6ce421e713a4c0c7e7de07b1 100644 (file)
@@ -1,10 +1,10 @@
 -- Alter the comment_aggregates hot sort to sort by score after hot_rank.
--- Reason being, is that hot_ranks go to zero after a few days, 
+-- Reason being, is that hot_ranks go to zero after a few days,
 -- and then comments should be sorted by score, not published.
+DROP INDEX idx_comment_aggregates_hot, idx_comment_aggregates_score;
 
-drop index idx_comment_aggregates_hot, idx_comment_aggregates_score;
-
-create index idx_comment_aggregates_hot on comment_aggregates (hot_rank desc, score desc);
+CREATE INDEX idx_comment_aggregates_hot ON comment_aggregates (hot_rank DESC, score DESC);
 
 -- Remove published from this sort, its pointless
-create index idx_comment_aggregates_score on comment_aggregates (score desc);
+CREATE INDEX idx_comment_aggregates_score ON comment_aggregates (score DESC);
+
index ed648b6e54322b22aa25701f8bbb5670741de425..4875d28013701ef9856c8d7a5b1831fb036ba49c 100644 (file)
@@ -1,10 +1,10 @@
 CREATE EXTENSION IF NOT EXISTS pg_trgm;
 
-CREATE INDEX IF NOT EXISTS idx_comment_content_trigram ON comment USING gin(content gin_trgm_ops);
+CREATE INDEX IF NOT EXISTS idx_comment_content_trigram ON comment USING gin (content gin_trgm_ops);
 
-CREATE INDEX IF NOT EXISTS idx_post_trigram ON post USING gin(name gin_trgm_ops, body gin_trgm_ops);
+CREATE INDEX IF NOT EXISTS idx_post_trigram ON post USING gin (name gin_trgm_ops, body gin_trgm_ops);
 
-CREATE INDEX IF NOT EXISTS idx_person_trigram ON person USING gin(name gin_trgm_ops, display_name gin_trgm_ops);
+CREATE INDEX IF NOT EXISTS idx_person_trigram ON person USING gin (name gin_trgm_ops, display_name gin_trgm_ops);
 
-CREATE INDEX IF NOT EXISTS idx_community_trigram ON community USING gin(name gin_trgm_ops, title gin_trgm_ops);
+CREATE INDEX IF NOT EXISTS idx_community_trigram ON community USING gin (name gin_trgm_ops, title gin_trgm_ops);
 
index a355546d1443de66da3fb4b824a30413bbe92d91..f4c5b4a18e8e09a360546046ecb0ff4d30d8d6ea 100644 (file)
 -- Update comment_aggregates_score trigger function to exclude controversy_rank update
-create or replace function comment_aggregates_score()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update comment_aggregates ca
-    set score = score + NEW.score,
-    upvotes = case when NEW.score = 1 then upvotes + 1 else upvotes end,
-    downvotes = case when NEW.score = -1 then downvotes + 1 else downvotes end
-    where ca.comment_id = NEW.comment_id;
-
-  ELSIF (TG_OP = 'DELETE') THEN
-    -- Join to comment because that comment may not exist anymore
-    update comment_aggregates ca
-    set score = score - OLD.score,
-    upvotes = case when OLD.score = 1 then upvotes - 1 else upvotes end,
-    downvotes = case when OLD.score = -1 then downvotes - 1 else downvotes end
-    from comment c
-    where ca.comment_id = c.id
-    and ca.comment_id = OLD.comment_id;
-
-  END IF;
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION comment_aggregates_score ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            comment_aggregates ca
+        SET
+            score = score + NEW.score,
+            upvotes = CASE WHEN NEW.score = 1 THEN
+                upvotes + 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN NEW.score = - 1 THEN
+                downvotes + 1
+            ELSE
+                downvotes
+            END
+        WHERE
+            ca.comment_id = NEW.comment_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        -- Join to comment because that comment may not exist anymore
+        UPDATE
+            comment_aggregates ca
+        SET
+            score = score - OLD.score,
+            upvotes = CASE WHEN OLD.score = 1 THEN
+                upvotes - 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN OLD.score = - 1 THEN
+                downvotes - 1
+            ELSE
+                downvotes
+            END
+        FROM
+            comment c
+        WHERE
+            ca.comment_id = c.id
+            AND ca.comment_id = OLD.comment_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
 -- Update post_aggregates_score trigger function to exclude controversy_rank update
-create or replace function post_aggregates_score()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update post_aggregates pa
-    set score = score + NEW.score,
-    upvotes = case when NEW.score = 1 then upvotes + 1 else upvotes end,
-    downvotes = case when NEW.score = -1 then downvotes + 1 else downvotes end
-    where pa.post_id = NEW.post_id;
-
-  ELSIF (TG_OP = 'DELETE') THEN
-    -- Join to post because that post may not exist anymore
-    update post_aggregates pa
-    set score = score - OLD.score,
-    upvotes = case when OLD.score = 1 then upvotes - 1 else upvotes end,
-    downvotes = case when OLD.score = -1 then downvotes - 1 else downvotes end
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = OLD.post_id;
-  END IF;
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION post_aggregates_score ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            post_aggregates pa
+        SET
+            score = score + NEW.score,
+            upvotes = CASE WHEN NEW.score = 1 THEN
+                upvotes + 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN NEW.score = - 1 THEN
+                downvotes + 1
+            ELSE
+                downvotes
+            END
+        WHERE
+            pa.post_id = NEW.post_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        -- Join to post because that post may not exist anymore
+        UPDATE
+            post_aggregates pa
+        SET
+            score = score - OLD.score,
+            upvotes = CASE WHEN OLD.score = 1 THEN
+                upvotes - 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN OLD.score = - 1 THEN
+                downvotes - 1
+            ELSE
+                downvotes
+            END
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = OLD.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
 -- Drop the indexes
-drop index if exists idx_post_aggregates_featured_local_controversy;
-drop index if exists idx_post_aggregates_featured_community_controversy;
-drop index if exists idx_comment_aggregates_controversy;
+DROP INDEX IF EXISTS idx_post_aggregates_featured_local_controversy;
+
+DROP INDEX IF EXISTS idx_post_aggregates_featured_community_controversy;
+
+DROP INDEX IF EXISTS idx_comment_aggregates_controversy;
 
 -- Remove the added columns from the tables
-alter table post_aggregates drop column controversy_rank;
-alter table comment_aggregates drop column controversy_rank;
+ALTER TABLE post_aggregates
+    DROP COLUMN controversy_rank;
+
+ALTER TABLE comment_aggregates
+    DROP COLUMN controversy_rank;
 
 -- Remove function
-drop function controversy_rank(numeric, numeric);
+DROP FUNCTION controversy_rank (numeric, numeric);
 
index f50a706bcb9589dee7991db46729f7182d727f24..eb1d712ed1524c84448382e105af6a4fe5c1dc28 100644 (file)
 -- Need to add immutable to the controversy_rank function in order to index by it
-
 -- Controversy Rank:
 --      if downvotes <= 0 or upvotes <= 0:
---          0 
+--          0
 --      else:
---          (upvotes + downvotes) * min(upvotes, downvotes) / max(upvotes, downvotes) 
-create or replace function controversy_rank(upvotes numeric, downvotes numeric)
-returns float as $$
-begin
-    if downvotes <= 0 or upvotes <= 0 then
-        return 0;
-    else
-        return (upvotes + downvotes) *
-            case when upvotes > downvotes
-                then downvotes::float / upvotes::float
-                else upvotes::float / downvotes::float
-            end;
-    end if;
-end; $$
+--          (upvotes + downvotes) * min(upvotes, downvotes) / max(upvotes, downvotes)
+CREATE OR REPLACE FUNCTION controversy_rank (upvotes numeric, downvotes numeric)
+    RETURNS float
+    AS $$
+BEGIN
+    IF downvotes <= 0 OR upvotes <= 0 THEN
+        RETURN 0;
+    ELSE
+        RETURN (upvotes + downvotes) * CASE WHEN upvotes > downvotes THEN
+            downvotes::float / upvotes::float
+        ELSE
+            upvotes::float / downvotes::float
+        END;
+    END IF;
+END;
+$$
 LANGUAGE plpgsql
 IMMUTABLE;
 
 -- Aggregates
-alter table post_aggregates add column controversy_rank float not null default 0;
-alter table comment_aggregates add column controversy_rank float not null default 0;
+ALTER TABLE post_aggregates
+    ADD COLUMN controversy_rank float NOT NULL DEFAULT 0;
+
+ALTER TABLE comment_aggregates
+    ADD COLUMN controversy_rank float NOT NULL DEFAULT 0;
 
 -- Populate them initially
 -- Note: After initial population, these are updated with vote triggers
-update post_aggregates set controversy_rank = controversy_rank(upvotes::numeric, downvotes::numeric);
-update comment_aggregates set controversy_rank = controversy_rank(upvotes::numeric, downvotes::numeric);
+UPDATE
+    post_aggregates
+SET
+    controversy_rank = controversy_rank (upvotes::numeric, downvotes::numeric);
+
+UPDATE
+    comment_aggregates
+SET
+    controversy_rank = controversy_rank (upvotes::numeric, downvotes::numeric);
 
 -- Create single column indexes
-create index idx_post_aggregates_featured_local_controversy on post_aggregates (featured_local desc, controversy_rank desc);
-create index idx_post_aggregates_featured_community_controversy on post_aggregates (featured_community desc, controversy_rank desc);
-create index idx_comment_aggregates_controversy on comment_aggregates (controversy_rank desc);
+CREATE INDEX idx_post_aggregates_featured_local_controversy ON post_aggregates (featured_local DESC, controversy_rank DESC);
 
--- Update post_aggregates_score trigger function to include controversy_rank update
-create or replace function post_aggregates_score()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update post_aggregates pa
-    set score = score + NEW.score,
-    upvotes = case when NEW.score = 1 then upvotes + 1 else upvotes end,
-    downvotes = case when NEW.score = -1 then downvotes + 1 else downvotes end,
-    controversy_rank = controversy_rank(pa.upvotes + case when NEW.score = 1 then 1 else 0 end::numeric, 
-                                         pa.downvotes + case when NEW.score = -1 then 1 else 0 end::numeric)
-    where pa.post_id = NEW.post_id;
+CREATE INDEX idx_post_aggregates_featured_community_controversy ON post_aggregates (featured_community DESC, controversy_rank DESC);
 
-  ELSIF (TG_OP = 'DELETE') THEN
-    -- Join to post because that post may not exist anymore
-    update post_aggregates pa
-    set score = score - OLD.score,
-    upvotes = case when OLD.score = 1 then upvotes - 1 else upvotes end,
-    downvotes = case when OLD.score = -1 then downvotes - 1 else downvotes end,
-    controversy_rank = controversy_rank(pa.upvotes + case when NEW.score = 1 then 1 else 0 end::numeric, 
-                                         pa.downvotes + case when NEW.score = -1 then 1 else 0 end::numeric)
-    from post p
-    where pa.post_id = p.id
-    and pa.post_id = OLD.post_id;
+CREATE INDEX idx_comment_aggregates_controversy ON comment_aggregates (controversy_rank DESC);
 
-  END IF;
-  return null;
-end $$;
+-- Update post_aggregates_score trigger function to include controversy_rank update
+CREATE OR REPLACE FUNCTION post_aggregates_score ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            post_aggregates pa
+        SET
+            score = score + NEW.score,
+            upvotes = CASE WHEN NEW.score = 1 THEN
+                upvotes + 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN NEW.score = - 1 THEN
+                downvotes + 1
+            ELSE
+                downvotes
+            END,
+            controversy_rank = controversy_rank (pa.upvotes + CASE WHEN NEW.score = 1 THEN
+                    1
+                ELSE
+                    0
+                END::numeric, pa.downvotes + CASE WHEN NEW.score = - 1 THEN
+                    1
+                ELSE
+                    0
+                END::numeric)
+        WHERE
+            pa.post_id = NEW.post_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        -- Join to post because that post may not exist anymore
+        UPDATE
+            post_aggregates pa
+        SET
+            score = score - OLD.score,
+            upvotes = CASE WHEN OLD.score = 1 THEN
+                upvotes - 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN OLD.score = - 1 THEN
+                downvotes - 1
+            ELSE
+                downvotes
+            END,
+            controversy_rank = controversy_rank (pa.upvotes + CASE WHEN NEW.score = 1 THEN
+                    1
+                ELSE
+                    0
+                END::numeric, pa.downvotes + CASE WHEN NEW.score = - 1 THEN
+                    1
+                ELSE
+                    0
+                END::numeric)
+        FROM
+            post p
+        WHERE
+            pa.post_id = p.id
+            AND pa.post_id = OLD.post_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
 -- Update comment_aggregates_score trigger function to include controversy_rank update
-create or replace function comment_aggregates_score()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    update comment_aggregates ca
-    set score = score + NEW.score,
-    upvotes = case when NEW.score = 1 then upvotes + 1 else upvotes end,
-    downvotes = case when NEW.score = -1 then downvotes + 1 else downvotes end,
-    controversy_rank = controversy_rank(ca.upvotes + case when NEW.score = 1 then 1 else 0 end::numeric, 
-                                         ca.downvotes + case when NEW.score = -1 then 1 else 0 end::numeric)
-    where ca.comment_id = NEW.comment_id;
-
-  ELSIF (TG_OP = 'DELETE') THEN
-    -- Join to comment because that comment may not exist anymore
-    update comment_aggregates ca
-    set score = score - OLD.score,
-    upvotes = case when OLD.score = 1 then upvotes - 1 else upvotes end,
-    downvotes = case when OLD.score = -1 then downvotes - 1 else downvotes end,
-    controversy_rank = controversy_rank(ca.upvotes + case when NEW.score = 1 then 1 else 0 end::numeric, 
-                                         ca.downvotes + case when NEW.score = -1 then 1 else 0 end::numeric)
-    from comment c
-    where ca.comment_id = c.id
-    and ca.comment_id = OLD.comment_id;
-
-  END IF;
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION comment_aggregates_score ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        UPDATE
+            comment_aggregates ca
+        SET
+            score = score + NEW.score,
+            upvotes = CASE WHEN NEW.score = 1 THEN
+                upvotes + 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN NEW.score = - 1 THEN
+                downvotes + 1
+            ELSE
+                downvotes
+            END,
+            controversy_rank = controversy_rank (ca.upvotes + CASE WHEN NEW.score = 1 THEN
+                    1
+                ELSE
+                    0
+                END::numeric, ca.downvotes + CASE WHEN NEW.score = - 1 THEN
+                    1
+                ELSE
+                    0
+                END::numeric)
+        WHERE
+            ca.comment_id = NEW.comment_id;
+    ELSIF (TG_OP = 'DELETE') THEN
+        -- Join to comment because that comment may not exist anymore
+        UPDATE
+            comment_aggregates ca
+        SET
+            score = score - OLD.score,
+            upvotes = CASE WHEN OLD.score = 1 THEN
+                upvotes - 1
+            ELSE
+                upvotes
+            END,
+            downvotes = CASE WHEN OLD.score = - 1 THEN
+                downvotes - 1
+            ELSE
+                downvotes
+            END,
+            controversy_rank = controversy_rank (ca.upvotes + CASE WHEN NEW.score = 1 THEN
+                    1
+                ELSE
+                    0
+                END::numeric, ca.downvotes + CASE WHEN NEW.score = - 1 THEN
+                    1
+                ELSE
+                    0
+                END::numeric)
+        FROM
+            comment c
+        WHERE
+            ca.comment_id = c.id
+            AND ca.comment_id = OLD.comment_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
index f384785f40cda378d6fdae6e5118713995129f1e..90dbbd7f1bef34a57e4cdcf19f231048b073979f 100644 (file)
@@ -1,11 +1,16 @@
-create or replace function site_aggregates_site()
-returns trigger language plpgsql
-as $$
-begin
-  IF (TG_OP = 'INSERT') THEN
-    insert into site_aggregates (site_id) values (NEW.id);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from site_aggregates where site_id = OLD.id;
-  END IF;
-  return null;
-end $$;
\ No newline at end of file
+CREATE OR REPLACE FUNCTION site_aggregates_site ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (TG_OP = 'INSERT') THEN
+        INSERT INTO site_aggregates (site_id)
+            VALUES (NEW.id);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM site_aggregates
+        WHERE site_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
index 7ed40316d4f5a4fac19c9925d36aa3dd58d79fad..7d3c6c9b627a1f4362c5aa36ff5620265da3278a 100644 (file)
@@ -1,15 +1,32 @@
-create or replace function site_aggregates_site()
-returns trigger language plpgsql
-as $$
-begin
-  -- we only ever want to have a single value in site_aggregate because the site_aggregate triggers update all rows in that table.
-  -- a cleaner check would be to insert it for the local_site but that would break assumptions at least in the tests
-  IF (TG_OP = 'INSERT') AND NOT EXISTS (select id from site_aggregates limit 1) THEN
-    insert into site_aggregates (site_id) values (NEW.id);
-  ELSIF (TG_OP = 'DELETE') THEN
-    delete from site_aggregates where site_id = OLD.id;
-  END IF;
-  return null;
-end $$;
+CREATE OR REPLACE FUNCTION site_aggregates_site ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    -- we only ever want to have a single value in site_aggregate because the site_aggregate triggers update all rows in that table.
+    -- a cleaner check would be to insert it for the local_site but that would break assumptions at least in the tests
+    IF (TG_OP = 'INSERT') AND NOT EXISTS (
+    SELECT
+        id
+    FROM
+        site_aggregates
+    LIMIT 1) THEN
+        INSERT INTO site_aggregates (site_id)
+            VALUES (NEW.id);
+    ELSIF (TG_OP = 'DELETE') THEN
+        DELETE FROM site_aggregates
+        WHERE site_id = OLD.id;
+    END IF;
+    RETURN NULL;
+END
+$$;
+
+DELETE FROM site_aggregates a
+WHERE NOT EXISTS (
+        SELECT
+            id
+        FROM
+            local_site s
+        WHERE
+            s.site_id = a.site_id);
 
-delete from site_aggregates a where not exists (select id from local_site s where s.site_id = a.site_id);
\ No newline at end of file
index 1a5c4ec4ff56f9822da6e94f006e4d91349467ba..3db25c4b0583d346ef791e061c56ba788dc0ad0c 100644 (file)
-create or replace function person_aggregates_comment_count()
-    returns trigger language plpgsql
-as $$
-begin
-    IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-        update person_aggregates
-        set comment_count = comment_count + 1 where person_id = NEW.creator_id;
-    ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-        update person_aggregates
-        set comment_count = comment_count - 1 where person_id = OLD.creator_id;
-
+CREATE OR REPLACE FUNCTION person_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            person_aggregates
+        SET
+            comment_count = comment_count + 1
+        WHERE
+            person_id = NEW.creator_id;
+    ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            person_aggregates
+        SET
+            comment_count = comment_count - 1
+        WHERE
+            person_id = OLD.creator_id;
         -- If the comment gets deleted, the score calculation trigger won't fire,
         -- so you need to re-calculate
-        update person_aggregates ua
-        set comment_score = cd.score
-        from (
-                 select u.id,
-                        coalesce(0, sum(cl.score)) as score
-                        -- User join because comments could be empty
-                 from person u
-                          left join comment c on u.id = c.creator_id and c.deleted = 'f' and c.removed = 'f'
-                          left join comment_like cl on c.id = cl.comment_id
-                 group by u.id
-             ) cd
-        where ua.person_id = OLD.creator_id;
+        UPDATE
+            person_aggregates ua
+        SET
+            comment_score = cd.score
+        FROM (
+            SELECT
+                u.id,
+                coalesce(0, sum(cl.score)) AS score
+                -- User join because comments could be empty
+            FROM
+                person u
+            LEFT JOIN comment c ON u.id = c.creator_id
+                AND c.deleted = 'f'
+                AND c.removed = 'f'
+        LEFT JOIN comment_like cl ON c.id = cl.comment_id
+    GROUP BY
+        u.id) cd
+    WHERE
+        ua.person_id = OLD.creator_id;
     END IF;
-    return null;
-end $$;
-
-create or replace function person_aggregates_post_count()
-    returns trigger language plpgsql
-as $$
-begin
-    IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-        update person_aggregates
-        set post_count = post_count + 1 where person_id = NEW.creator_id;
-
-    ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-        update person_aggregates
-        set post_count = post_count - 1 where person_id = OLD.creator_id;
+    RETURN NULL;
+END
+$$;
 
+CREATE OR REPLACE FUNCTION person_aggregates_post_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            person_aggregates
+        SET
+            post_count = post_count + 1
+        WHERE
+            person_id = NEW.creator_id;
+    ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            person_aggregates
+        SET
+            post_count = post_count - 1
+        WHERE
+            person_id = OLD.creator_id;
         -- If the post gets deleted, the score calculation trigger won't fire,
         -- so you need to re-calculate
-        update person_aggregates ua
-        set post_score = pd.score
-        from (
-                 select u.id,
-                        coalesce(0, sum(pl.score)) as score
-                        -- User join because posts could be empty
-                 from person u
-                          left join post p on u.id = p.creator_id and p.deleted = 'f' and p.removed = 'f'
-                          left join post_like pl on p.id = pl.post_id
-                 group by u.id
-             ) pd
-        where ua.person_id = OLD.creator_id;
-
+        UPDATE
+            person_aggregates ua
+        SET
+            post_score = pd.score
+        FROM (
+            SELECT
+                u.id,
+                coalesce(0, sum(pl.score)) AS score
+                -- User join because posts could be empty
+            FROM
+                person u
+            LEFT JOIN post p ON u.id = p.creator_id
+                AND p.deleted = 'f'
+                AND p.removed = 'f'
+        LEFT JOIN post_like pl ON p.id = pl.post_id
+    GROUP BY
+        u.id) pd
+    WHERE
+        ua.person_id = OLD.creator_id;
     END IF;
-    return null;
-end $$;
+    RETURN NULL;
+END
+$$;
 
-create or replace function community_aggregates_comment_count()
-    returns trigger language plpgsql
-as $$
-begin
-  IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-update community_aggregates ca
-set comments = comments + 1 from comment c, post p
-where p.id = c.post_id
-  and p.id = NEW.post_id
-  and ca.community_id = p.community_id;
-ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-update community_aggregates ca
-set comments = comments - 1 from comment c, post p
-where p.id = c.post_id
-  and p.id = OLD.post_id
-  and ca.community_id = p.community_id;
+CREATE OR REPLACE FUNCTION community_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            community_aggregates ca
+        SET
+            comments = comments + 1
+        FROM
+            comment c,
+            post p
+        WHERE
+            p.id = c.post_id
+            AND p.id = NEW.post_id
+            AND ca.community_id = p.community_id;
+    ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            community_aggregates ca
+        SET
+            comments = comments - 1
+        FROM
+            comment c,
+            post p
+        WHERE
+            p.id = c.post_id
+            AND p.id = OLD.post_id
+            AND ca.community_id = p.community_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-END IF;
-return null;
-end $$;
\ No newline at end of file
index 66a78371b69cd0893a58686d2692bfbf35c26fb8..90385dd69e366eefee6b5ddb74ccced62ac8d6b8 100644 (file)
@@ -1,47 +1,78 @@
-create or replace function person_aggregates_comment_count()
-    returns trigger language plpgsql
-as $$
-begin
-    IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-        update person_aggregates
-        set comment_count = comment_count + 1 where person_id = NEW.creator_id;
-    ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-        update person_aggregates
-        set comment_count = comment_count - 1 where person_id = OLD.creator_id;
+CREATE OR REPLACE FUNCTION person_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            person_aggregates
+        SET
+            comment_count = comment_count + 1
+        WHERE
+            person_id = NEW.creator_id;
+    ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            person_aggregates
+        SET
+            comment_count = comment_count - 1
+        WHERE
+            person_id = OLD.creator_id;
     END IF;
-    return null;
-end $$;
+    RETURN NULL;
+END
+$$;
 
-create or replace function person_aggregates_post_count()
-    returns trigger language plpgsql
-as $$
-begin
-    IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-        update person_aggregates
-        set post_count = post_count + 1 where person_id = NEW.creator_id;
-
-    ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-        update person_aggregates
-        set post_count = post_count - 1 where person_id = OLD.creator_id;
+CREATE OR REPLACE FUNCTION person_aggregates_post_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            person_aggregates
+        SET
+            post_count = post_count + 1
+        WHERE
+            person_id = NEW.creator_id;
+    ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            person_aggregates
+        SET
+            post_count = post_count - 1
+        WHERE
+            person_id = OLD.creator_id;
     END IF;
-    return null;
-end $$;
+    RETURN NULL;
+END
+$$;
 
-create or replace function community_aggregates_comment_count()
-    returns trigger language plpgsql
-as $$
-begin
-  IF (was_restored_or_created(TG_OP, OLD, NEW)) THEN
-update community_aggregates ca
-set comments = comments + 1 from post p
-where p.id = NEW.post_id
-  and ca.community_id = p.community_id;
-ELSIF (was_removed_or_deleted(TG_OP, OLD, NEW)) THEN
-update community_aggregates ca
-set comments = comments - 1 from post p
-where p.id = OLD.post_id
-  and ca.community_id = p.community_id;
+CREATE OR REPLACE FUNCTION community_aggregates_comment_count ()
+    RETURNS TRIGGER
+    LANGUAGE plpgsql
+    AS $$
+BEGIN
+    IF (was_restored_or_created (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            community_aggregates ca
+        SET
+            comments = comments + 1
+        FROM
+            post p
+        WHERE
+            p.id = NEW.post_id
+            AND ca.community_id = p.community_id;
+    ELSIF (was_removed_or_deleted (TG_OP, OLD, NEW)) THEN
+        UPDATE
+            community_aggregates ca
+        SET
+            comments = comments - 1
+        FROM
+            post p
+        WHERE
+            p.id = OLD.post_id
+            AND ca.community_id = p.community_id;
+    END IF;
+    RETURN NULL;
+END
+$$;
 
-END IF;
-return null;
-end $$;
\ No newline at end of file
similarity index 87%
rename from scripts/fix-clippy.sh
rename to scripts/lint.sh
index 5ad3ca8b425c8d9940a603e4577b7978abde40b5..b06c5541fcc456713928cabd49ff5712a91a4601 100755 (executable)
@@ -21,5 +21,11 @@ cargo clippy --workspace --fix --allow-staged --allow-dirty --tests --all-target
   -D clippy::unwrap_used \
   -D clippy::indexing_slicing
 
+# Format rust files
 cargo +nightly fmt
+
+# Format toml files
 taplo format
+
+# Format sql files
+find migrations -type f -name '*.sql' -exec pg_format -i {} +
diff --git a/scripts/sql_format_check.sh b/scripts/sql_format_check.sh
new file mode 100755 (executable)
index 0000000..8a92eef
--- /dev/null
@@ -0,0 +1,15 @@
+#!/usr/bin/env bash
+set -e
+
+# This check is only used for CI.
+
+CWD="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
+
+cd $CWD/../
+
+find migrations -type f -name "*.sql" -print0 | while read -d $'\0' FILE
+do
+  TMP_FILE="/tmp/tmp_pg_format.sql"
+  pg_format $FILE > $TMP_FILE
+  diff $FILE $TMP_FILE
+done