mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Add dedicated category settings model - Part 1 (#20211)
This is the first in a multi-part change to move the custom fields to a new table. It includes: - Adding a new CategorySetting model and corresponding table. - Populating it with data from the category_custom_fields table.
This commit is contained in:
17
db/migrate/20230207093514_create_category_settings.rb
Normal file
17
db/migrate/20230207093514_create_category_settings.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class CreateCategorySettings < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
# Moving the custom fields in core into a dedicated table for
|
||||
# better type casting, validations, etc.
|
||||
create_table :category_settings do |t|
|
||||
t.references :category, null: false, index: { unique: true }
|
||||
|
||||
t.boolean :require_topic_approval, null: true
|
||||
t.boolean :require_reply_approval, null: true
|
||||
t.integer :num_auto_bump_daily, null: true
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
46
db/migrate/20230208020404_populate_category_settings.rb
Normal file
46
db/migrate/20230208020404_populate_category_settings.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class PopulateCategorySettings < ActiveRecord::Migration[7.0]
|
||||
def up
|
||||
execute(<<~SQL)
|
||||
INSERT INTO
|
||||
category_settings(
|
||||
category_id,
|
||||
require_topic_approval,
|
||||
require_reply_approval,
|
||||
num_auto_bump_daily,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
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;
|
||||
SQL
|
||||
end
|
||||
|
||||
def down
|
||||
execute(<<~SQL)
|
||||
TRUNCATE category_settings;
|
||||
SQL
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user