discourse/lib/system_themes_manager.rb
Martin Brennan 7504805f89
FIX: allow out-of-sequence theme settings migrations on re-sync (#38705)
When SystemThemesManager.sync! re-syncs an existing core theme,
the theme settings migration runner would raise an error because
migrations had already been recorded in the DB from a prior sync:

```
ThemeSettingsMigrationsRunner#raise_error (raise_error)
ThemeSettingsMigrationsRunner#run (out_of_sequence check)
Theme#migrate_settings
RemoteTheme#update_from_remote
RemoteTheme.import_theme_from_directory
SystemThemesManager.sync_theme!
SeedFu::Runner#run_file
```

Thread the existing `allow_out_of_sequence_migration` parameter
through `import_theme_from_directory` → `update_theme` →
`update_from_remote` → `migrate_settings`, and set it to true
when re-syncing an already-installed system theme.
2026-03-20 11:16:22 +10:00

47 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class SystemThemesManager
def self.sync!
Theme::CORE_THEMES.keys.each { |theme_name| sync_theme!(theme_name) }
end
def self.sync_theme!(theme_name)
theme_id = Theme::CORE_THEMES[theme_name]
raise Discourse::InvalidParameters unless theme_id
theme_dir = "#{Rails.root}/themes/#{theme_name}"
is_initial_install = !Theme.exists?(id: theme_id)
theme =
RemoteTheme.import_theme_from_directory(
theme_dir,
theme_id: theme_id,
allow_out_of_sequence_migration: !is_initial_install,
)
theme.update_column(:enabled, true)
if is_initial_install
if theme_id == Theme::CORE_THEMES["horizon"]
theme.update!(dark_color_scheme: theme.color_schemes.find_by(name: "Horizon Dark"))
end
end
theme.save!
Stylesheet::Manager.clear_theme_cache!
end
# Don't want user history created from theme site setting changes
# from system themes polluting specs.
def self.clear_system_theme_user_history!
return if !Rails.env.test?
Theme::CORE_THEMES.each_key do |theme_name|
UserHistory
.where(action: UserHistory.actions[:change_theme_site_setting])
.where("subject ILIKE :theme_name", theme_name: "#{theme_name}:%")
.destroy_all
end
end
end