Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add `custom_category_fields` converter and importer steps (#33362)

Selase Krakani53c557ff6c8a
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.yml
index a62e66235d2..9ef0a35a538 100644
--- a/migrations/config/intermediate_db.yml
+++ b/migrations/config/intermediate_db.yml
@@ -42,6 +42,12 @@ schema:
primary_key_column_names: [ "category_id", "user_id" ]
columns:
exclude:
+ - "id"
+ category_custom_fields:
+ primary_key_column_names: [ "category_id", "name" ]
+ columns:
+ exclude:
+ - "created_at"
- "id"
user_emails:
columns:
@@ -110,7 +116,6 @@ schema:
- "bookmarks"
- "calendar_events"
- "categories_web_hooks"
- - "category_custom_fields"
- "category_featured_topics"
- "category_form_templates"
- "category_groups"
diff --git a/migrations/db/intermediate_db_schema/100-base-schema.sql b/migrations/db/intermediate_db_schema/100-base-schema.sql
index f7a9a4916d5..2cd1c964d26 100644
--- a/migrations/db/intermediate_db_schema/100-base-schema.sql
+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql
@@ -76,6 +76,14 @@ CREATE TABLE categories
user_id NUMERIC NOT NULL
);
+CREATE TABLE category_custom_fields
+(
+ category_id NUMERIC NOT NULL,
+ name TEXT NOT NULL,
+ value TEXT,
+ PRIMARY KEY (category_id, name)
+);
+
CREATE TABLE category_users
(
category_id NUMERIC NOT NULL,
diff --git a/migrations/lib/converters/discourse/steps/category_custom_fields.rb b/migrations/lib/converters/discourse/steps/category_custom_fields.rb
new file mode 100644
index 00000000000..6cda45b6d0d
--- /dev/null
+++ b/migrations/lib/converters/discourse/steps/category_custom_fields.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module Migrations::Converters::Discourse
+ class CategoryCustomFields < ::Migrations::Converters::Base::ProgressStep
+ attr_accessor :source_db
+
+ def max_progress
+ @source_db.count <<~SQL
+ SELECT COUNT(*) FROM category_custom_fields
+ SQL
+ end
+
+ def items
+ @source_db.query <<~SQL
+ SELECT category_id, name, value
+ FROM category_custom_fields
+ ORDER BY category_id
+ SQL
+ end
+
+ def process_item(item)
+ IntermediateDB::CategoryCustomField.create(
+ category_id: item[:category_id],
+ name: item[:name],
+ value: item[:value],
+ )
+ end
+ end
+end
diff --git a/migrations/lib/database/intermediate_db/category_custom_field.rb b/migrations/lib/database/intermediate_db/category_custom_field.rb
new file mode 100644
index 00000000000..c7d6a4bb58e
--- /dev/null
+++ b/migrations/lib/database/intermediate_db/category_custom_field.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+# This file is auto-generated from the IntermediateDB schema. To make changes,
+# update the "config/intermediate_db.yml" configuration file and then run
+# `bin/cli schema generate` to regenerate this file.
+
+module Migrations::Database::IntermediateDB
+ module CategoryCustomField
+ SQL = <<~SQL
+ INSERT INTO category_custom_fields (
+ category_id,
+ name,
+ value
+ )
+ VALUES (
+ ?, ?, ?
+ )
+ SQL
+
+ def self.create(category_id:, name:, value: nil)
+ ::Migrations::Database::IntermediateDB.insert(SQL, category_id, name, value)
+ end
+ end
+end
diff --git a/migrations/lib/importer/steps/category_custom_fields.rb b/migrations/lib/importer/steps/category_custom_fields.rb
new file mode 100644
index 00000000000..366fe074775
--- /dev/null
+++ b/migrations/lib/importer/steps/category_custom_fields.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class CategoryCustomFields < ::Migrations::Importer::CopyStep
+ depends_on :categories
+
+ requires_set :existing_custom_fields,
+ "SELECT category_id || ':' || name FROM category_custom_fields"
+
+ column_names %i[category_id name value created_at updated_at]
+
+ total_rows_query <<~SQL, MappingType::CATEGORIES
+ SELECT COUNT(*)
+ FROM category_custom_fields custom_fields
+ JOIN mapped.ids mapped_categories
+ ON custom_fields.category_id = mapped_categories.original_id AND mapped_categories.type = ?
+ SQL
+
+ rows_query <<~SQL, MappingType::CATEGORIES
+ SELECT custom_fields.*,
+ mapped_categories.discourse_id AS discourse_category_id
+ FROM category_custom_fields custom_fields
+ JOIN mapped.ids mapped_categories
+ ON custom_fields.category_id = mapped_categories.original_id AND mapped_categories.type = ?
+ SQL
+
+ private
+
+ def transform_row(row)
+ category_id = row[:discourse_category_id]
+
+ return nil unless @existing_custom_fields.add?("#{category_id}:#{row[:name]}")
+
+ row[:category_id] = category_id
+
+ super
+ end
+ end
+end
diff --git a/migrations/spec/lib/database/intermediate_db/category_custom_field_spec.rb b/migrations/spec/lib/database/intermediate_db/category_custom_field_spec.rb
new file mode 100644
index 00000000000..1cbe0014c6b
--- /dev/null
+++ b/migrations/spec/lib/database/intermediate_db/category_custom_field_spec.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+RSpec.describe ::Migrations::Database::IntermediateDB::CategoryCustomField do
+ it_behaves_like "a database entity"
+end