DEV: Move array type custom fields to JSON type in automation (#26939)

The automation plugin has 4 custom field types that are array typed. However, array typed custom fields are deprecated and should be migrated to JSON type.

This commit does a couple of things:

1. Migrate all four custom fields to JSON
2. Fix a couple of small bugs that have been discovered while migrating the custom fields to JSON (see the comments on this commit's PR for details https://github.com/discourse/discourse/pull/26939)
This commit is contained in:
Osama Sayegh
2024-05-10 18:47:12 +03:00
committed by GitHub
parent 4e22b505c5
commit 3be4924b99
17 changed files with 264 additions and 103 deletions

View File

@ -0,0 +1,32 @@
# frozen_string_literal: true
class SwitchTopicAutomationCustomFieldsToJson < ActiveRecord::Migration[7.0]
def up
results = DB.query(<<~SQL)
SELECT topic_id, ARRAY_AGG(value) AS values
FROM topic_custom_fields
WHERE name = 'discourse_automation_ids'
GROUP BY topic_id
SQL
execute(<<~SQL)
DELETE FROM topic_custom_fields
WHERE name = 'discourse_automation_ids'
SQL
results.each do |row|
parsed = row.values.map(&:to_i).uniq
DB.exec(<<~SQL, topic_id: row.topic_id, value: parsed.to_json)
INSERT INTO topic_custom_fields
(topic_id, name, value, created_at, updated_at)
VALUES
(:topic_id, 'discourse_automation_ids_json', :value, NOW(), NOW())
SQL
end
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@ -0,0 +1,32 @@
# frozen_string_literal: true
class SwitchUserAutomationCustomFieldsToJson < ActiveRecord::Migration[7.0]
def up
results = DB.query(<<~SQL)
SELECT user_id, ARRAY_AGG(value) AS values
FROM user_custom_fields
WHERE name = 'discourse_automation_ids'
GROUP BY user_id
SQL
execute(<<~SQL)
DELETE FROM user_custom_fields
WHERE name = 'discourse_automation_ids'
SQL
results.each do |row|
parsed = row.values.map(&:to_i).uniq
DB.exec(<<~SQL, user_id: row.user_id, value: parsed.to_json)
INSERT INTO user_custom_fields
(user_id, name, value, created_at, updated_at)
VALUES
(:user_id, 'discourse_automation_ids_json', :value, NOW(), NOW())
SQL
end
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@ -0,0 +1,32 @@
# frozen_string_literal: true
class SwitchTopicAutomationTriggeredIdsCustomFieldsToJson < ActiveRecord::Migration[7.0]
def up
results = DB.query(<<~SQL)
SELECT topic_id, ARRAY_AGG(value) AS values
FROM topic_custom_fields
WHERE name = 'auto_responder_triggered_ids'
GROUP BY topic_id
SQL
execute(<<~SQL)
DELETE FROM topic_custom_fields
WHERE name = 'auto_responder_triggered_ids'
SQL
results.each do |row|
parsed = row.values.map(&:to_i).uniq
DB.exec(<<~SQL, topic_id: row.topic_id, value: parsed.to_json)
INSERT INTO topic_custom_fields
(topic_id, name, value, created_at, updated_at)
VALUES
(:topic_id, 'auto_responder_triggered_ids_json', :value, NOW(), NOW())
SQL
end
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@ -0,0 +1,15 @@
# frozen_string_literal: true
class DropAutomationIdsCustomFieldIndexes < ActiveRecord::Migration[7.0]
def change
remove_index :topic_custom_fields,
name: :idx_topic_custom_fields_discourse_automation_unique_id_partial,
if_exists: true
remove_index :user_custom_fields,
name: :idx_user_custom_fields_discourse_automation_unique_id_partial,
if_exists: true
remove_index :post_custom_fields,
name: :idx_post_custom_fields_discourse_automation_unique_id_partial,
if_exists: true
end
end