DEV: Switch over category settings to new table - Part 3 (#20657)

In #20135 we prevented invalid inputs from being accepted in category setting form fields on the front-end. We didn't do anything on the back-end at that time, because we were still discussing which path we wanted to take. Eventually we decided we want to move this to a new CategorySetting model.

This PR moves the require_topic_approval and require_reply_approval from custom fields to the new CategorySetting model.

This PR is nearly identical to #20580, which migrated num_auto_bump_daily, but since these are slightly more sensitive, they are moved after the previous one is verified.
This commit is contained in:
Ted Johansson
2023-09-12 09:51:49 +08:00
committed by GitHub
parent 07c29f3066
commit f08c6d2756
13 changed files with 178 additions and 51 deletions

View File

@ -0,0 +1,74 @@
# frozen_string_literal: true
class UpdateCategorySettingApprovalValues < ActiveRecord::Migration[7.0]
def up
change_column_default :category_settings, :require_topic_approval, false
change_column_default :category_settings, :require_reply_approval, false
execute(<<~SQL)
WITH custom_fields AS (
SELECT
category_id,
MAX(
CASE WHEN (name = 'require_topic_approval')
THEN NULLIF(value, '') ELSE NULL END
)::boolean AS require_topic_approval,
MAX(
CASE WHEN (name = 'require_reply_approval')
THEN NULLIF(value, '') ELSE NULL END
)::boolean AS require_reply_approval,
NOW() AS created_at,
NOW() AS updated_at
FROM category_custom_fields
WHERE name IN (
'require_topic_approval',
'require_reply_approval'
)
GROUP BY category_id
)
INSERT INTO
category_settings(
category_id,
require_topic_approval,
require_reply_approval,
created_at,
updated_at
)
SELECT * FROM custom_fields
ON CONFLICT (category_id) DO
UPDATE SET
require_topic_approval = EXCLUDED.require_topic_approval,
require_reply_approval = EXCLUDED.require_reply_approval,
updated_at = NOW()
SQL
execute(<<~SQL)
UPDATE category_settings
SET require_topic_approval = false
WHERE require_topic_approval IS NULL;
SQL
execute(<<~SQL)
UPDATE category_settings
SET require_reply_approval = false
WHERE require_reply_approval IS NULL;
SQL
end
def down
change_column_default :category_settings, :require_topic_approval, nil
change_column_default :category_settings, :require_reply_approval, nil
execute(<<~SQL)
UPDATE category_settings
SET require_topic_approval = NULL
WHERE require_topic_approval = false;
SQL
execute(<<~SQL)
UPDATE category_settings
SET require_reply_approval = NULL
WHERE require_reply_approval = false;
SQL
end
end