summaryrefslogtreecommitdiff
path: root/lib/gitlab/fake_application_settings.rb
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2017-06-17 07:35:30 -0700
committerStan Hu <stanhu@gmail.com>2017-06-19 09:54:48 -0700
commit575dced5d777e2e0db58ba8dbec6438456c9ff93 (patch)
treebce5df62399a6be5501e23a8771a047d2f26fdb6 /lib/gitlab/fake_application_settings.rb
parent84cfb33fbb60898f9b56137832b2c5abb622465c (diff)
downloadgitlab-ce-575dced5d777e2e0db58ba8dbec6438456c9ff93.tar.gz
If migrations are pending, make CurrentSettings use existing values and populate missing columns with defaultssh-refactor-current-settings
master was failing because `ApplicationSetting.create_from_defaults` attempted to write to a column that did not exist in the database. This occurred in a `rake db:migrate` task, which was unable to perform the migration that would have added the missing column in the first place. In 9.3 RC2, we also had a bug where password sign-ins were disabled because there were many pending migrations. The problem occurred because `fake_application_settings` was being returned with an OpenStruct that did not include the predicate method `signup_enabled?`. As a result, the value would erroneously return `nil` instead of `true`. This commit uses the values of the defaults to mimic this behavior. This commit also refactors some of the logic to be clearer.
Diffstat (limited to 'lib/gitlab/fake_application_settings.rb')
-rw-r--r--lib/gitlab/fake_application_settings.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/gitlab/fake_application_settings.rb b/lib/gitlab/fake_application_settings.rb
new file mode 100644
index 00000000000..bb14a8cd9e7
--- /dev/null
+++ b/lib/gitlab/fake_application_settings.rb
@@ -0,0 +1,27 @@
+# This class extends an OpenStruct object by adding predicate methods to mimic
+# ActiveRecord access. We rely on the initial values being true or false to
+# determine whether to define a predicate method because for a newly-added
+# column that has not been migrated yet, there is no way to determine the
+# column type without parsing db/schema.rb.
+module Gitlab
+ class FakeApplicationSettings < OpenStruct
+ def initialize(options = {})
+ super
+
+ FakeApplicationSettings.define_predicate_methods(options)
+ end
+
+ # Mimic ActiveRecord predicate methods for boolean values
+ def self.define_predicate_methods(options)
+ options.each do |key, value|
+ next if key.to_s.end_with?('?')
+ next unless [true, false].include?(value)
+
+ define_method "#{key}?" do
+ actual_key = key.to_s.chomp('?')
+ self[actual_key]
+ end
+ end
+ end
+ end
+end