Commit 8d7e07d0 authored by Toon Claes's avatar Toon Claes

Rename Gitlab::Database.readonly? to read_only?

parent 7746792a
......@@ -3,23 +3,23 @@
# Automatically sets the layout and ensures an administrator is logged in
class Admin::ApplicationController < ApplicationController
before_action :authenticate_admin!
before_action :display_readonly_information
before_action :display_read_only_information
layout 'admin'
def authenticate_admin!
render_404 unless current_user.admin?
end
def display_readonly_information
return unless Gitlab::Database.readonly?
def display_read_only_information
return unless Gitlab::Database.read_only?
flash.now[:notice] = readonly_message
flash.now[:notice] = read_only_message
end
private
# Overridden in EE
def readonly_message
_('You are on a read-only GitLab instance.').html_safe
def read_only_message
_('You are on a read-only GitLab instance.')
end
end
......@@ -12,7 +12,7 @@ module Boards
def index
issues = Boards::Issues::ListService.new(board_parent, current_user, filter_params).execute
issues = issues.page(params[:page]).per(params[:per] || 20)
make_sure_position_is_set(issues) unless Gitlab::Database.readonly?
make_sure_position_is_set(issues) if Gitlab::Database.read_write?
issues = issues.preload(:project,
:milestone,
:assignees,
......
......@@ -98,10 +98,10 @@ class Projects::LfsApiController < Projects::GitHttpClientController
end
def lfs_check_batch_operation!
if upload_request? && Gitlab::Database.readonly?
if upload_request? && Gitlab::Database.read_only?
render(
json: {
message: lfs_readonly_message
message: lfs_read_only_message
},
content_type: 'application/vnd.git-lfs+json',
status: 403
......@@ -110,7 +110,7 @@ class Projects::LfsApiController < Projects::GitHttpClientController
end
# Overridden in EE
def lfs_readonly_message
def lfs_read_only_message
_('You cannot write to this read-only GitLab instance.')
end
end
......@@ -15,7 +15,7 @@ class Projects::MergeRequests::ApplicationController < Projects::ApplicationCont
# Make sure merge requests created before 8.0
# have head file in refs/merge-requests/
def ensure_ref_fetched
@merge_request.ensure_ref_fetched unless Gitlab::Database.readonly?
@merge_request.ensure_ref_fetched if Gitlab::Database.read_write?
end
def merge_request_params
......
......@@ -152,7 +152,7 @@ module Routable
end
def update_route
return if Gitlab::Database.readonly?
return if Gitlab::Database.read_only?
prepare_route
route.save
......
......@@ -498,7 +498,7 @@ class MergeRequest < ActiveRecord::Base
end
def check_if_can_be_merged
return unless unchecked? && !Gitlab::Database.readonly?
return unless unchecked? && Gitlab::Database.read_write?
can_be_merged =
!broken? && project.repository.can_be_merged?(diff_head_sha, target_branch)
......
......@@ -811,7 +811,7 @@ class Project < ActiveRecord::Base
end
def cache_has_external_issue_tracker
update_column(:has_external_issue_tracker, services.external_issue_trackers.any?) unless Gitlab::Database.readonly?
update_column(:has_external_issue_tracker, services.external_issue_trackers.any?) if Gitlab::Database.read_write?
end
def has_wiki?
......@@ -831,7 +831,7 @@ class Project < ActiveRecord::Base
end
def cache_has_external_wiki
update_column(:has_external_wiki, services.external_wikis.any?) unless Gitlab::Database.readonly?
update_column(:has_external_wiki, services.external_wikis.any?) if Gitlab::Database.read_write?
end
def find_or_initialize_services(exceptions: [])
......
......@@ -478,15 +478,11 @@ class User < ActiveRecord::Base
end
def remember_me!
raise NotImplementedError unless defined?(super)
super unless ::Gitlab::Database.readonly?
super if ::Gitlab::Database.read_write?
end
def forget_me!
raise NotImplementedError unless defined?(super)
super unless ::Gitlab::Database.readonly?
super if ::Gitlab::Database.read_write?
end
def disable_two_factor!
......@@ -1079,7 +1075,7 @@ class User < ActiveRecord::Base
# we do this on read since migrating all existing users is not a feasible
# solution.
def rss_token
return read_attribute(:rss_token) if Gitlab::Database.readonly?
return read_attribute(:rss_token) if Gitlab::Database.read_only?
ensure_rss_token!
end
......
......@@ -16,7 +16,7 @@ module Keys
end
def update?
return false if ::Gitlab::Database.readonly?
return false if ::Gitlab::Database.read_only?
last_used = key.last_used_at
......
......@@ -14,7 +14,7 @@ module Users
private
def record_activity
Gitlab::UserActivities.record(@author.id) unless Gitlab::Database.readonly?
Gitlab::UserActivities.record(@author.id) if Gitlab::Database.read_write?
Rails.logger.debug("Recorded activity: #{@activity} for User ID: #{@author.id} (username: #{@author.username})")
end
......
......@@ -84,7 +84,7 @@
%p
.js-health
- unless Gitlab::Database.readonly?
- if Gitlab::Database.read_write?
.node-actions
- if Gitlab::Geo.license_allows?
- if node.missing_oauth_application?
......
......@@ -89,7 +89,7 @@ and prevent user from trying to update the database and getting a 500 error
Database will already be read-only in a replicated setup, so we don't need to
take any extra step for that.
We do use our feature toggle `Gitlab::Database.readonly?` to deny write operations
We do use our feature toggle `Gitlab::Database.read_only?` to deny write operations
on a node that is in read-only mode. This toggle is also available in CE.
## File Transfers
......
module EE
module Admin
module ApplicationController
def readonly_message
def read_only_message
raise NotImplementedError unless defined?(super)
return super unless Gitlab::Geo.secondary_with_primary?
......
module EE
module Projects
module LfsApiController
def lfs_readonly_message
def lfs_read_only_message
raise NotImplementedError unless defined?(super)
return super unless ::Gitlab::Geo.secondary_with_primary?
......
module EE
module Gitlab
module Database
def self.readonly?
def self.read_only?
raise NotImplementedError unless defined?(super)
Gitlab::Geo.secondary? || super
......
......@@ -165,7 +165,7 @@ module Banzai
# GitLab needs to disable updates on GET requests if database is readonly
def self.update_object?(object)
!Gitlab::Database.readonly?
Gitlab::Database.read_write?
end
end
end
......@@ -31,10 +31,15 @@ module Gitlab
adapter_name.casecmp('postgresql').zero?
end
def self.readonly?
# Overridden in EE
def self.read_only?
false
end
def self.read_write?
!self.read_only?
end
def self.version
database_version.match(/\A(?:PostgreSQL |)([^\s]+).*\z/)[1]
end
......
......@@ -176,7 +176,7 @@ module Gitlab
raise UnauthorizedError, ERROR_MESSAGES[:readonly]
end
if Gitlab::Database.readonly?
if Gitlab::Database.read_only?
raise UnauthorizedError, ERROR_MESSAGES[:cannot_push_to_readonly]
end
......
module Gitlab
class GitAccessWiki < GitAccess
ERROR_MESSAGES = {
readonly: "You can't push code to this read-only GitLab instance.",
read_only: "You can't push code to a read-only GitLab instance.",
write_to_wiki: "You are not allowed to write to this project's wiki."
}.freeze
......@@ -18,8 +18,8 @@ module Gitlab
raise UnauthorizedError, ERROR_MESSAGES[:write_to_wiki]
end
if Gitlab::Geo.enabled? && Gitlab::Database.readonly?
raise UnauthorizedError, ERROR_MESSAGES[:readonly]
if Gitlab::Database.read_only?
raise UnauthorizedError, ERROR_MESSAGES[:read_only]
end
true
......
......@@ -13,7 +13,7 @@ module Gitlab
def call(env)
@env = env
if disallowed_request? && Gitlab::Database.readonly?
if disallowed_request? && Gitlab::Database.read_only?
Rails.logger.debug('GitLab Readonly: preventing possible non readonly operation')
error_message = 'You cannot do writing operations on a read-only GitLab instance'
......
......@@ -84,7 +84,7 @@ describe EE::User do
end
it 'does not clear remember_created_at when in a GitLab readonly instance' do
allow(Gitlab::Database).to receive(:readonly?) { true }
allow(Gitlab::Database).to receive(:read_only?) { true }
expect { subject.forget_me! }.not_to change(subject, :remember_created_at)
end
......@@ -100,7 +100,7 @@ describe EE::User do
end
it 'does not update remember_created_at when in a Geo readonly instance' do
allow(Gitlab::Database).to receive(:readonly?) { true }
allow(Gitlab::Database).to receive(:read_only?) { true }
expect { subject.remember_me! }.not_to change(subject, :remember_created_at)
end
......
......@@ -5,7 +5,7 @@ describe Keys::LastUsedService do
key = create(:key, last_used_at: 1.year.ago)
original_time = key.last_used_at
allow(::Gitlab::Database).to receive(:readonly?).and_return(true)
allow(::Gitlab::Database).to receive(:read_only?).and_return(true)
described_class.new(key).execute
expect(key.reload.last_used_at).to be_like_time(original_time)
......
......@@ -37,7 +37,7 @@ describe Banzai::Renderer do
end
it "skips database caching on a GitLab readonly instance" do
allow(Gitlab::Database).to receive(:readonly?).and_return(true)
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
expect(object).to receive(:refresh_markdown_cache!).with(do_update: false)
is_expected.to eq('field_html')
......
......@@ -110,7 +110,7 @@ describe Gitlab::Geo do
end
end
describe 'readonly?' do
describe 'secondary?' do
context 'when current node is secondary' do
it 'returns true' do
stub_current_geo_node(secondary_node)
......
......@@ -747,7 +747,7 @@ describe Gitlab::GitAccess do
context "when in a read-only GitLab instance" do
before do
create(:protected_branch, name: 'feature', project: project)
allow(Gitlab::Database).to receive(:readonly?) { true }
allow(Gitlab::Database).to receive(:read_only?) { true }
end
# Only check admin; if an admin can't do it, other roles can't either
......
......@@ -27,7 +27,7 @@ describe Gitlab::GitAccessWiki do
context 'when in a read-only GitLab instance' do
before do
allow(Gitlab::Database).to receive(:readonly?) { true }
allow(Gitlab::Database).to receive(:read_only?) { true }
end
it 'does not give access to upload wiki code' do
......
......@@ -42,7 +42,7 @@ describe Gitlab::Middleware::Readonly do
let(:fake_app) { lambda { |env| [200, { 'Content-Type' => 'text/plain' }, ['OK']] } }
before do
allow(Gitlab::Database).to receive(:readonly?) { true }
allow(Gitlab::Database).to receive(:read_only?) { true }
end
it 'expects PATCH requests to be disallowed' do
......@@ -112,7 +112,7 @@ describe Gitlab::Middleware::Readonly do
let(:content_json) { { 'CONTENT_TYPE' => 'application/json' } }
before do
allow(Gitlab::Database).to receive(:readonly?) { true }
allow(Gitlab::Database).to receive(:read_only?) { true }
end
it 'expects PATCH requests to be disallowed' do
......
......@@ -15,7 +15,7 @@ describe Group, 'Routable' do
describe 'GitLab read-only instance' do
it 'does not save route if route is not present' do
group.route.path = ''
allow(Gitlab::Database).to receive(:readonly?).and_return(true)
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
expect(group).to receive(:update_route).and_call_original
expect { group.full_path }.to change { Route.count }.by(0)
......
......@@ -823,7 +823,7 @@ describe Project do
end
it 'does not cache data when in a read-only GitLab instance' do
allow(Gitlab::Database).to receive(:readonly?) { true }
allow(Gitlab::Database).to receive(:read_only?) { true }
expect do
project.cache_has_external_issue_tracker
......@@ -853,7 +853,7 @@ describe Project do
end
it 'does not cache data when in a read-only GitLab instance' do
allow(Gitlab::Database).to receive(:readonly?) { true }
allow(Gitlab::Database).to receive(:read_only?) { true }
expect do
project.cache_has_external_wiki
......
......@@ -877,8 +877,7 @@ describe 'Git LFS API and storage' do
end
end
describe 'when handling lfs batch request on a secondary Geo node' do
let!(:primary) { create(:geo_node, :primary) }
describe 'when handling lfs batch request on a read-only GitLab instance' do
let(:authorization) { authorize_user }
let(:project) { create(:project) }
let(:path) { "#{project.http_url_to_repo}/info/lfs/objects/batch" }
......@@ -887,7 +886,7 @@ describe 'Git LFS API and storage' do
end
before do
allow(Gitlab::Database).to receive(:readonly?) { true }
allow(Gitlab::Database).to receive(:read_only?) { true }
project.team << [user, :master]
enable_lfs
end
......
......@@ -41,7 +41,7 @@ describe Users::ActivityService do
context 'when in GitLab read-only instance' do
before do
allow(Gitlab::Database).to receive(:readonly?).and_return(true)
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
end
it 'does not update last_activity_at' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment