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

Rename Gitlab::Database.readonly? to read_only?

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