When modifying site settings during operations like imports, jobs, or plugin initialization, implement proper safeguards to prevent unintended permanent changes. Consider the impact on existing sites and provide mechanisms for restoration or warnings.
Key practices:
max_tag_length
)USER_OPTION_DEFAULTS
instead of inline hardcoded valuesExample from tag import:
# Instead of hardcoding:
SiteSetting.max_tag_length = 100 if SiteSetting.max_tag_length < 100
# Query actual requirements:
max_tag_length_needed = source_db.query("SELECT MAX(LENGTH(name)) FROM tags").first
SiteSetting.max_tag_length = max_tag_length_needed if SiteSetting.max_tag_length < max_tag_length_needed
Example for conditional defaults:
# In base configuration:
USER_OPTION_DEFAULTS = {
hide_profile: SiteSetting.default_hide_profile,
assignable_level: Group::ALIAS_LEVELS[:nobody] # Set unconditionally with comment explaining why
}
This approach prevents configuration drift, reduces surprises for site administrators, and makes operations more predictable across different environments.
Enter the URL of a public GitHub repository