forked from opf/openproject
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request opf#9609 from opf/user-preferences-as-json
Migrate UserPreference to JSON storage
- Loading branch information
Showing
7 changed files
with
146 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
require_relative './migration_utils/utils' | ||
|
||
class MakeUserPreferencesJson < ActiveRecord::Migration[6.1] | ||
include ::Migration::Utils | ||
|
||
class UserPreferenceWithOthers < ::UserPreference | ||
self.table_name = 'user_preferences' | ||
serialize :others, Hash | ||
serialize :settings, ::Serializers::IndifferentHashSerializer | ||
end | ||
|
||
def up | ||
add_column :user_preferences, :settings, :jsonb, default: {} | ||
|
||
UserPreferenceWithOthers.reset_column_information | ||
in_configurable_batches(UserPreferenceWithOthers).each_record do |pref| | ||
migrate_yaml_to_json(pref) | ||
pref.save!(validate: false) | ||
end | ||
|
||
change_table :user_preferences, bulk: true do |t| | ||
t.remove :others, :hide_mail, :time_zone | ||
end | ||
end | ||
|
||
def down | ||
change_table :user_preferences, bulk: true do |t| | ||
t.text :others | ||
t.boolean :hide_mail, default: true | ||
t.text :time_zone | ||
end | ||
|
||
UserPreferenceWithOthers.reset_column_information | ||
in_configurable_batches(UserPreferenceWithOthers).each_record do |pref| | ||
migrate_json_to_yaml(pref) | ||
pref.save!(validate: false) | ||
end | ||
|
||
remove_column :user_preferences, :settings, :jsonb | ||
end | ||
|
||
private | ||
|
||
def migrate_yaml_to_json(pref) | ||
pref.settings = pref.others | ||
pref.settings[:hide_mail] = pref.hide_mail | ||
pref.settings[:time_zone] = pref.time_zone | ||
end | ||
|
||
def migrate_json_to_yaml(pref) | ||
pref.others = pref.settings | ||
pref.hide_mail = pref.settings[:hide_mail] | ||
pref.time_zone = pref.settings[:time_zone] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.