DEV: Switch over category settings to new table - Part 2 (#20580)

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 num_auto_bump_daily from custom fields to the new CategorySetting model.

In addition it sets the default value to 0, which exhibits the same behaviour as when the value is NULL.
This commit is contained in:
Ted Johansson
2023-08-04 10:53:22 +08:00
committed by GitHub
parent 0fb86e6dc1
commit 1f7e5e8e75
7 changed files with 83 additions and 32 deletions

View File

@ -0,0 +1,67 @@
# frozen_string_literal: true
class ChangeCategorySettingNumAutoBumpDailyDefault < ActiveRecord::Migration[7.0]
def up
change_column_default :category_settings, :num_auto_bump_daily, 0
execute(<<~SQL)
WITH custom_fields AS (
SELECT
category_id,
MAX(
CASE WHEN (name = 'require_topic_approval')
THEN value ELSE NULL END
)::boolean AS require_topic_approval,
MAX(
CASE WHEN (name = 'require_reply_approval')
THEN value ELSE NULL END
)::boolean AS require_reply_approval,
MAX(
CASE WHEN (name = 'num_auto_bump_daily')
THEN value ELSE NULL END
)::integer AS num_auto_bump_daily,
NOW() AS created_at,
NOW() AS updated_at
FROM category_custom_fields
WHERE name IN (
'require_topic_approval',
'require_reply_approval',
'num_auto_bump_daily'
)
GROUP BY category_id
)
INSERT INTO
category_settings(
category_id,
require_topic_approval,
require_reply_approval,
num_auto_bump_daily,
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,
num_auto_bump_daily = EXCLUDED.num_auto_bump_daily,
updated_at = NOW()
SQL
execute(<<~SQL)
UPDATE category_settings
SET num_auto_bump_daily = 0
WHERE num_auto_bump_daily IS NULL;
SQL
end
def down
change_column_default :category_settings, :num_auto_bump_daily, nil
execute(<<~SQL)
UPDATE category_settings
SET num_auto_bump_daily = NULL
WHERE num_auto_bump_daily = 0;
SQL
end
end