20160308212903_add_default_group_visibility_to_application_settings.rb 1.23 KB
Newer Older
1
# rubocop:disable all
Douwe Maan's avatar
Douwe Maan committed
2 3 4 5
# Create visibility level field on DB
# Sets default_visibility_level to value on settings if not restricted
# If value is restricted takes higher visibility level allowed

6
class AddDefaultGroupVisibilityToApplicationSettings < ActiveRecord::Migration[4.2]
Douwe Maan's avatar
Douwe Maan committed
7 8
  def up
    add_column :application_settings, :default_group_visibility, :integer
Douwe Maan's avatar
Douwe Maan committed
9 10
    # Unfortunately, this can't be a `default`, since we don't want the configuration specific
    # `allowed_visibility_level` to end up in schema.rb
11 12 13

    visibility_level = allowed_visibility_level || Gitlab::VisibilityLevel::PRIVATE
    execute("UPDATE application_settings SET default_group_visibility = #{visibility_level}")
Douwe Maan's avatar
Douwe Maan committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  end

  def down
    remove_column :application_settings, :default_group_visibility
  end

  private

  def allowed_visibility_level
    application_settings = select_one("SELECT restricted_visibility_levels FROM application_settings ORDER BY id DESC LIMIT 1")
    if application_settings
      restricted_visibility_levels = YAML.safe_load(application_settings["restricted_visibility_levels"]) rescue nil
    end
    restricted_visibility_levels ||= []

    allowed_levels = Gitlab::VisibilityLevel.values - restricted_visibility_levels
    allowed_levels.max
  end
end