Skip to content

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

DEV: Add support for converting and importing `badge_groupings` (#34190)

Selase Krakanif34a476c203f
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.yml
index 964261230b1..732d875a69c 100644
--- a/migrations/config/intermediate_db.yml
+++ b/migrations/config/intermediate_db.yml
@@ -15,6 +15,7 @@ schema:
exclude:
- "grant_count"
- "system"
+ badge_groupings: {}
categories:
columns:
add:
@@ -171,7 +172,6 @@ schema:
- "backup_draft_posts"
- "backup_draft_topics"
- "backup_metadata"
- - "badge_groupings"
- "badge_types"
- "bookmarks"
- "calendar_events"
diff --git a/migrations/db/intermediate_db_schema/100-base-schema.sql b/migrations/db/intermediate_db_schema/100-base-schema.sql
index cd380af147d..1685e5695ae 100644
--- a/migrations/db/intermediate_db_schema/100-base-schema.sql
+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql
@@ -2,6 +2,15 @@
-- update the "config/intermediate_db.yml" configuration file and then run
-- `bin/cli schema generate` to regenerate this file.
+CREATE TABLE badge_groupings
+(
+ original_id NUMERIC NOT NULL PRIMARY KEY,
+ created_at DATETIME,
+ description TEXT,
+ name TEXT NOT NULL,
+ position INTEGER NOT NULL
+);
+
CREATE TABLE badges
(
original_id NUMERIC NOT NULL PRIMARY KEY,
diff --git a/migrations/lib/converters/discourse/steps/badge_groupings.rb b/migrations/lib/converters/discourse/steps/badge_groupings.rb
new file mode 100644
index 00000000000..6ff6a877e79
--- /dev/null
+++ b/migrations/lib/converters/discourse/steps/badge_groupings.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+module Migrations::Converters::Discourse
+ class BadgeGroupings < ::Migrations::Converters::Base::ProgressStep
+ attr_accessor :source_db
+
+ def max_progress
+ @source_db.count <<~SQL
+ SELECT COUNT(*)
+ FROM badge_groupings
+ WHERE id NOT IN (1, 2, 3, 4, 5) -- Exclude system groupings
+ SQL
+ end
+
+ def items
+ @source_db.query <<~SQL
+ SELECT *
+ FROM badge_groupings
+ WHERE id NOT IN (1, 2, 3, 4, 5) -- Exclude system groupings
+ ORDER BY id
+ SQL
+ end
+
+ def process_item(item)
+ IntermediateDB::BadgeGrouping.create(
+ original_id: item[:id],
+ name: item[:name],
+ description: item[:description],
+ created_at: item[:created_at],
+ position: item[:position],
+ )
+ end
+ end
+end
diff --git a/migrations/lib/database/intermediate_db/badge_grouping.rb b/migrations/lib/database/intermediate_db/badge_grouping.rb
new file mode 100644
index 00000000000..982128b1fba
--- /dev/null
+++ b/migrations/lib/database/intermediate_db/badge_grouping.rb
@@ -0,0 +1,33 @@
+# 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 BadgeGrouping
+ SQL = <<~SQL
+ INSERT INTO badge_groupings (
+ original_id,
+ created_at,
+ description,
+ name,
+ position
+ )
+ VALUES (
+ ?, ?, ?, ?, ?
+ )
+ SQL
+
+ def self.create(original_id:, created_at: nil, description: nil, name:, position:)
+ ::Migrations::Database::IntermediateDB.insert(
+ SQL,
+ original_id,
+ ::Migrations::Database.format_datetime(created_at),
+ description,
+ name,
+ position,
+ )
+ end
+ end
+end
diff --git a/migrations/lib/importer/mapping_type.rb b/migrations/lib/importer/mapping_type.rb
index e023eb81896..934e671bff7 100644
--- a/migrations/lib/importer/mapping_type.rb
+++ b/migrations/lib/importer/mapping_type.rb
@@ -8,6 +8,7 @@ module Migrations::Importer
GROUPS = 4
TAGS = 5
TAG_GROUPS = 7
+ BADGE_GROUPINGS = 6
UPLOADS = 10
end
end
diff --git a/migrations/lib/importer/steps/badge_groupings.rb b/migrations/lib/importer/steps/badge_groupings.rb
new file mode 100644
index 00000000000..711e22961b1
--- /dev/null
+++ b/migrations/lib/importer/steps/badge_groupings.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class BadgeGroupings < ::Migrations::Importer::CopyStep
+ MAX_NAME_LENGTH = 100
+ MAX_DESCRIPTION_LENGTH = 500
+
+ store_mapped_ids true
+
+ requires_mapping :ids_by_name, "SELECT LOWER(name), id FROM badge_groupings"
+
+ column_names %i[id name description position created_at updated_at]
+
+ total_rows_query <<~SQL, MappingType::BADGE_GROUPINGS
+ SELECT COUNT(*)
+ FROM badge_groupings
+ LEFT JOIN mapped.ids mapped_badge_grouping
+ ON badge_groupings.original_id = mapped_badge_grouping.original_id
+ AND mapped_badge_grouping.type = ?
+ WHERE mapped_badge_grouping.original_id IS NULL
+ SQL
+
+ rows_query <<~SQL, MappingType::BADGE_GROUPINGS
+ SELECT badge_groupings.*,
+ ROW_NUMBER() OVER (
+ ORDER BY COALESCE(badge_groupings.position, 0), badge_groupings.ROWID
+ ) AS normalized_position
+ FROM badge_groupings
+ LEFT JOIN mapped.ids mapped_badge_grouping
+ ON badge_groupings.original_id = mapped_badge_grouping.original_id
+ AND mapped_badge_grouping.type = ?
+ WHERE mapped_badge_grouping.original_id IS NULL
+ ORDER BY normalized_position
+ SQL
+
+ def execute
+ @max_position = BadgeGrouping.maximum(:position) || 0
+
+ super
+ end
+
+ private
+
+ def transform_row(row)
+ if (existing_id = @ids_by_name[row[:name].downcase])
+ row[:id] = existing_id
+
+ return nil
+ end
+
+ name = row[:name]
+ description = row[:description]
+
+ row[:name] = name[0, MAX_NAME_LENGTH] if name.length > MAX_NAME_LENGTH
+ if description && description.length > MAX_DESCRIPTION_LENGTH
+ row[:description] = description[0, MAX_DESCRIPTION_LENGTH]
+ end
+ row[:position] = @max_position + row[:normalized_position]
+
+ super
+ end
+ end
+end
diff --git a/migrations/spec/lib/database/intermediate_db/badge_grouping_spec.rb b/migrations/spec/lib/database/intermediate_db/badge_grouping_spec.rb
new file mode 100644
index 00000000000..6ab3ec2eafd
--- /dev/null
+++ b/migrations/spec/lib/database/intermediate_db/badge_grouping_spec.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+RSpec.describe ::Migrations::Database::IntermediateDB::BadgeGrouping do
+ it_behaves_like "a database entity"
+end