From 16c20eea4645bb4608e36b74bbc0324a2d424813 Mon Sep 17 00:00:00 2001 From: Aaron Contreras Date: Tue, 10 Sep 2024 08:11:21 -0500 Subject: [PATCH] Fix the way in which the conditions are queried and key is set Thanks Ivan! --- ...rate_legacy_default_theme_to_light_theme.rb | 18 +++++++++--------- ...legacy_default_theme_to_light_theme_spec.rb | 10 ++++++---- 2 files changed, 15 insertions(+), 13 deletions(-) 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 index 96bc3606af23..5dd235432153 100644 --- a/db/migrate/20240909151818_migrate_legacy_default_theme_to_light_theme.rb +++ b/db/migrate/20240909151818_migrate_legacy_default_theme_to_light_theme.rb @@ -29,29 +29,29 @@ #++ 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" + # "theme": "default", + # "foo": "bar" # } # } # # To: # { # "settings": { - # "theme": "light" # current default theme + # "theme": "light", # current default theme + # "foo": "bar" # } # } def up - MigrationUserPreference - .where(settings: { theme: "default" }) - .update_all(settings: { theme: "light" }) + execute <<-SQL.squish + UPDATE user_preferences + SET settings = jsonb_set(settings, '{theme}', '"light"') + WHERE settings->>'theme' = 'default'; + SQL end # no-op 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 index 5deb6f9479a4..7cac0fd2cf91 100644 --- a/spec/migrations/migrate_legacy_default_theme_to_light_theme_spec.rb +++ b/spec/migrations/migrate_legacy_default_theme_to_light_theme_spec.rb @@ -35,10 +35,12 @@ # 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" } }) } + shared_let(:user_with_default_theme) { create(:user, preferences: { settings: { theme: "default", foo: "bar" } }) } + shared_let(:user_with_light_theme) { create(:user, preferences: { settings: { theme: "light", foo: "bar" } }) } + shared_let(:user_with_light_high_contrast_theme) do + create(:user, preferences: { settings: { theme: "light_high_contrast", foo: "bar" } }) + end + shared_let(:user_with_dark_theme) { create(:user, preferences: { settings: { theme: "dark", foo: "bar" } }) } it "sets the theme to light for users with the 'default' theme" do expect { run_migration }