-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 #26939)
- Loading branch information
1 parent
4e22b50
commit 3be4924
Showing
17 changed files
with
264 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...utomation/db/post_migrate/20240507112651_switch_topic_automation_custom_fields_to_json.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
32 changes: 32 additions & 0 deletions
32
...automation/db/post_migrate/20240507112751_switch_user_automation_custom_fields_to_json.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
32 changes: 32 additions & 0 deletions
32
...ost_migrate/20240507112851_switch_topic_automation_triggered_ids_custom_fields_to_json.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
15 changes: 15 additions & 0 deletions
15
...ins/automation/db/post_migrate/20240507112951_drop_automation_ids_custom_field_indexes.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.