Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Migrate UserPreference settings with the "default" theme to "light"

Aaron Contreras908e08e5ed1b
diff --git a/db/migrate/20240909151818_migrate_legacy_default_theme_to_light_theme.rb b/db/migrate/20240909151818_migrate_legacy_default_theme_to_light_theme.rb
new file mode 100644
index 00000000000..96bc3606af2
--- /dev/null
+++ b/db/migrate/20240909151818_migrate_legacy_default_theme_to_light_theme.rb
@@ -0,0 +1,59 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+class MigrateLegacyDefaultThemeToLightTheme < ActiveRecord::Migration[7.1]
+ class MigrationUserPreference < ActiveRecord::Base
+ self.table_name = "user_preferences"
+ end
+
+ # Migrate legacy `default` theme to `light` theme
+ #
+ # Moving from:
+ # {
+ # "settings": {
+ # "theme": "default"
+ # }
+ # }
+ #
+ # To:
+ # {
+ # "settings": {
+ # "theme": "light" # current default theme
+ # }
+ # }
+ def up
+ MigrationUserPreference
+ .where(settings: { theme: "default" })
+ .update_all(settings: { theme: "light" })
+ end
+
+ # no-op
+ def down; end
+end
diff --git a/spec/migrations/migrate_legacy_default_theme_to_light_theme_spec.rb b/spec/migrations/migrate_legacy_default_theme_to_light_theme_spec.rb
new file mode 100644
index 00000000000..5deb6f9479a
--- /dev/null
+++ b/spec/migrations/migrate_legacy_default_theme_to_light_theme_spec.rb
@@ -0,0 +1,56 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+require Rails.root.join("db/migrate/20240909151818_migrate_legacy_default_theme_to_light_theme.rb")
+
+RSpec.describe MigrateLegacyDefaultThemeToLightTheme, type: :model do
+ RSpec::Matchers.define_negated_matcher :not_change, :change
+
+ # Silencing migration logs, since we are not interested in that during testing
+ subject(:run_migration) { ActiveRecord::Migration.suppress_messages { described_class.new.up } }
+
+ shared_let(:user_with_default_theme) { create(:user, preferences: { settings: { theme: "default" } }) }
+ shared_let(:user_with_light_theme) { create(:user, preferences: { settings: { theme: "light" } }) }
+ shared_let(:user_with_light_high_contrast_theme) { create(:user, preferences: { settings: { theme: "light_high_contrast" } }) }
+ shared_let(:user_with_dark_theme) { create(:user, preferences: { settings: { theme: "dark" } }) }
+
+ it "sets the theme to light for users with the 'default' theme" do
+ expect { run_migration }
+ .to change { user_with_default_theme.reload.pref.settings["theme"] }
+ .from("default")
+ .to("light")
+ end
+
+ it "does not change the theme for users with another theme that isn't 'default'" do
+ expect { run_migration }
+ .to not_change { user_with_light_theme.reload.pref.settings["theme"] }
+ .and(not_change { user_with_light_high_contrast_theme.reload.pref.settings["theme"] })
+ .and(not_change { user_with_dark_theme.reload.pref.settings["theme"] })
+ end
+end