Commit 4c0595c4 authored by Jarka Kadlecová's avatar Jarka Kadlecová

Use data_source_exists? instead of table_exists?

Use data_source_exists? where possible instead of table_exists? in order to be Rails5 compatible
parent 1a58a90b
......@@ -72,7 +72,7 @@ class Project < ActiveRecord::Base
add_authentication_token_field :runners_token
before_validation :mark_remote_mirrors_for_removal, if: -> { ActiveRecord::Base.connection.table_exists?(:remote_mirrors) }
before_validation :mark_remote_mirrors_for_removal, if: -> { RemoteMirror.table_exists? }
before_save :ensure_runners_token
......
......@@ -7,7 +7,7 @@ rescue
end
# Needed to run migration
if ActiveRecord::Base.connected? && ActiveRecord::Base.connection.table_exists?('licenses')
if ActiveRecord::Base.connected? && ActiveRecord::Base.connection.data_source_exists?('licenses')
message = LicenseHelper.license_message(signed_in: true, is_admin: true, in_html: false)
if ::License.block_changes? && message.present?
warn "WARNING: #{message}"
......
require 'active_record/migration'
module ActiveRecord
class Migration
# data_source_exists? is not available in 4.2.10, table_exists deprecated in 5.0
def table_exists?(table_name)
ActiveRecord::Base.connection.data_source_exists?(table_name)
end
end
end
# We need to run this initializer after migrations are done so it doesn't fail on CI
if ActiveRecord::Base.connected? && ActiveRecord::Base.connection.table_exists?('licenses')
if ActiveRecord::Base.connected? && ActiveRecord::Base.connection.data_source_exists?('licenses')
if Gitlab::Database::LoadBalancing.enable?
Gitlab::Database.disable_prepared_statements
......
......@@ -45,7 +45,7 @@ module Gitlab
connection = ::Geo::BaseRegistry.connection
schema_migrations_table_name = ActiveRecord::Base.schema_migrations_table_name
if connection.table_exists?(schema_migrations_table_name)
if connection.data_source_exists?(schema_migrations_table_name)
connection.execute("SELECT MAX(version) AS version FROM #{schema_migrations_table_name}")
.first
.fetch('version')
......
......@@ -54,7 +54,8 @@ module Gitlab
def ensure_temporary_tracking_table_exists
table_name = :untracked_files_for_uploads
unless UntrackedFile.connection.table_exists?(table_name)
unless ActiveRecord::Base.connection.data_source_exists?(table_name)
UntrackedFile.connection.create_table table_name do |t|
t.string :path, limit: 600, null: false
t.index :path, unique: true
......
......@@ -226,8 +226,11 @@ module Gitlab
end
def self.cached_table_exists?(table_name)
# Rails 5 uses data_source_exists? instead of table_exists?
connection.schema_cache.table_exists?(table_name)
if Gitlab.rails5?
connection.schema_cache.data_source_exists?(table_name)
else
connection.schema_cache.table_exists?(table_name)
end
end
private_class_method :connection
......
......@@ -114,7 +114,7 @@ describe Gitlab::BackgroundMigration::PopulateUntrackedUploads, :sidekiq, :migra
it 'does not drop the temporary tracking table after processing the batch, if there are still untracked rows' do
subject.perform(1, untracked_files_for_uploads.last.id - 1)
expect(ActiveRecord::Base.connection.table_exists?(:untracked_files_for_uploads)).to be_truthy
expect(ActiveRecord::Base.connection.data_source_exists?(:untracked_files_for_uploads)).to be_truthy
end
it 'drops the temporary tracking table after processing the batch, if there are no untracked rows left' do
......
......@@ -400,8 +400,13 @@ describe Gitlab::Database do
describe '.cached_table_exists?' do
it 'only retrieves data once per table' do
expect(ActiveRecord::Base.connection).to receive(:table_exists?).with(:projects).once.and_call_original
expect(ActiveRecord::Base.connection).to receive(:table_exists?).with(:bogus_table_name).once.and_call_original
if Gitlab.rails5?
expect(ActiveRecord::Base.connection).to receive(:data_source_exists?).with(:projects).once.and_call_original
expect(ActiveRecord::Base.connection).to receive(:data_source_exists?).with(:bogus_table_name).once.and_call_original
else
expect(ActiveRecord::Base.connection).to receive(:table_exists?).with(:projects).once.and_call_original
expect(ActiveRecord::Base.connection).to receive(:table_exists?).with(:bogus_table_name).once.and_call_original
end
2.times do
expect(described_class.cached_table_exists?(:projects)).to be_truthy
......
......@@ -16,10 +16,6 @@ module MigrationsHelpers
ActiveRecord::Migrator.migrations_paths
end
def table_exists?(name)
active_record_base.connection.table_exists?(name)
end
def migrations
ActiveRecord::Migrator.migrations(migrations_paths)
end
......
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