Use data_source_exists? instead of table_exists?

Use data_source_exists? where possible instead of table_exists? in order to be Rails5 compatible
This commit is contained in:
Jarka Kadlecová 2018-06-05 11:29:33 +02:00
parent 760b12dc6b
commit fa36101a7f
7 changed files with 26 additions and 11 deletions

View file

@ -68,7 +68,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

View file

@ -0,0 +1,10 @@
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

View file

@ -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

View file

@ -188,8 +188,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

View file

@ -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

View file

@ -314,8 +314,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

View file

@ -10,10 +10,6 @@ module MigrationsHelpers
ActiveRecord::Migrator.migrations_paths
end
def table_exists?(name)
ActiveRecord::Base.connection.table_exists?(name)
end
def migrations
ActiveRecord::Migrator.migrations(migrations_paths)
end