Skip to content

Add default database values for user_preferences table

What does this MR do and why?

We replaced default_value_for gem with the native rails implementation in f124341c. We added some setters and getters to ensure the same behavior. After adding the default for columns, we need to backfill the null values and then remove the setters and getters which is a bit hacky way to make this work.

Migration output

up
bin/rails db:migrate
bin/rails db:rollback:main
bin/rails db:roWARNING: This version of GitLab depends on gitlab-shell 14.22.0, but you're running 14.20.0. Please update gitlab-shell.
llback:ci
main: == [advisory_lock_connection] object_id: 227860, pg_backend_pid: 57776
main: == 20230602112904 ChangeUserPreferencesDefaults: migrating ====================
main: -- change_column_default(:user_preferences, :tab_width, {:from=>nil, :to=>8})
main:    -> 0.1158s
main: -- change_column_default(:user_preferences, :time_display_relative, {:from=>nil, :to=>true})
main:    -> 0.0024s
main: -- change_column_default(:user_preferences, :render_whitespace_in_code, {:from=>nil, :to=>false})
main:    -> 0.0020s
main: == 20230602112904 ChangeUserPreferencesDefaults: migrated (0.1239s) ===========

main: == [advisory_lock_connection] object_id: 227860, pg_backend_pid: 57776
ci: == [advisory_lock_connection] object_id: 228080, pg_backend_pid: 57778
ci: == 20230602112904 ChangeUserPreferencesDefaults: migrating ====================
ci: -- change_column_default(:user_preferences, :tab_width, {:from=>nil, :to=>8})
ci:    -> 0.0041s
ci: -- change_column_default(:user_preferences, :time_display_relative, {:from=>nil, :to=>true})
ci:    -> 0.0021s
ci: -- change_column_default(:user_preferences, :render_whitespace_in_code, {:from=>nil, :to=>false})
ci:    -> 0.0022s
ci: == 20230602112904 ChangeUserPreferencesDefaults: migrated (0.0188s) ===========

ci: == [advisory_lock_connection] object_id: 228080, pg_backend_pid: 57778
down
bin/rails db:rollback:main
WARNING: This version of GitLab depends on gitlab-shell 14.22.0, but you're running 14.20.0. Please update gitlab-shell.
main: == [advisory_lock_connection] object_id: 227640, pg_backend_pid: 58191
main: == 20230602112904 ChangeUserPreferencesDefaults: reverting ====================
main: -- change_column_default(:user_preferences, :render_whitespace_in_code, {:from=>false, :to=>nil})
main:    -> 0.1017s
main: -- change_column_default(:user_preferences, :time_display_relative, {:from=>true, :to=>nil})
main:    -> 0.0025s
main: -- change_column_default(:user_preferences, :tab_width, {:from=>8, :to=>nil})
main:    -> 0.0023s
main: == 20230602112904 ChangeUserPreferencesDefaults: reverted (0.1124s) ===========

main: == [advisory_lock_connection] object_id: 227640, pg_backend_pid: 58191

➜ bin/rails db:rollback:ci
WARNING: This version of GitLab depends on gitlab-shell 14.22.0, but you're running 14.20.0. Please update gitlab-shell.
ci: == [advisory_lock_connection] object_id: 227560, pg_backend_pid: 58600
ci: == 20230602112904 ChangeUserPreferencesDefaults: reverting ====================
ci: -- change_column_default(:user_preferences, :render_whitespace_in_code, {:from=>false, :to=>nil})
ci:    -> 0.0997s
ci: -- change_column_default(:user_preferences, :time_display_relative, {:from=>true, :to=>nil})
ci:    -> 0.0032s
ci: -- change_column_default(:user_preferences, :tab_width, {:from=>8, :to=>nil})
ci:    -> 0.0028s
ci: == 20230602112904 ChangeUserPreferencesDefaults: reverted (0.1258s) ===========

ci: == [advisory_lock_connection] object_id: 227560, pg_backend_pid: 58600

How to set up and validate locally

bin/rails db:migrate

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #382704 (closed)

Edited by Abdul Wadood

Merge request reports

Loading