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

@ -34,41 +34,38 @@ module DiscourseAutomation
MAX_NAME_LENGTH = 30
validates :name, length: { in: MIN_NAME_LENGTH..MAX_NAME_LENGTH }
def attach_custom_field(target)
def add_id_to_custom_field(target, custom_field_key)
if ![Topic, Post, User].any? { |m| target.is_a?(m) }
raise "Expected an instance of Topic/Post/User."
end
now = Time.now
fk = target.custom_fields_fk
row = {
fk => target.id,
:name => DiscourseAutomation::CUSTOM_FIELD,
:value => id,
:created_at => now,
:updated_at => now,
}
relation = "#{target.class.name}CustomField".constantize
relation.upsert(
row,
unique_by:
"idx_#{target.class.name.downcase}_custom_fields_discourse_automation_unique_id_partial",
)
change_automation_ids_custom_field_in_mutex(target, custom_field_key) do
target.reload
ids = Array(target.custom_fields[custom_field_key])
if !ids.include?(self.id)
ids << self.id
ids = ids.compact.uniq
target.custom_fields[custom_field_key] = ids
target.save_custom_fields
end
end
end
def detach_custom_field(target)
def remove_id_from_custom_field(target, custom_field_key)
if ![Topic, Post, User].any? { |m| target.is_a?(m) }
raise "Expected an instance of Topic/Post/User."
end
fk = target.custom_fields_fk
relation = "#{target.class.name}CustomField".constantize
relation.where(
fk => target.id,
:name => DiscourseAutomation::CUSTOM_FIELD,
:value => id,
).delete_all
change_automation_ids_custom_field_in_mutex(target, custom_field_key) do
target.reload
ids = Array(target.custom_fields[custom_field_key])
if ids.include?(self.id)
ids = ids.compact.uniq
ids.delete(self.id)
target.custom_fields[custom_field_key] = ids
target.save_custom_fields
end
end
end
def trigger_field(name)
@ -187,5 +184,12 @@ module DiscourseAutomation
def validate_trigger_fields
!triggerable || triggerable.valid?(self)
end
def change_automation_ids_custom_field_in_mutex(target, key)
DistributedMutex.synchronize(
"automation_custom_field_#{key}_#{target.class.table_name}_#{target.id}",
validity: 5.seconds,
) { yield }
end
end
end