Commit 472a9a3e authored by Toon Claes's avatar Toon Claes Committed by Stan Hu

Make the UploadRegistry searchable

Search the Geo Upload registry by path of the actual upload.
parent b3dfc1da
......@@ -3,6 +3,7 @@
module Geo
module Fdw
class Upload < ::Geo::BaseFdw
include Gitlab::SQL::Pattern
include ObjectStorable
STORE_COLUMN = :store
......@@ -10,6 +11,18 @@ module Geo
self.table_name = Gitlab::Geo::Fdw.foreign_table_name('uploads')
scope :geo_syncable, -> { with_files_stored_locally }
class << self
# Searches for a list of uploads based on the query given in `query`.
#
# On PostgreSQL this method uses "ILIKE" to perform a case-insensitive
# search.
#
# query - The search query as a String.
def search(query)
fuzzy_search(query, [:path])
end
end
end
end
end
......@@ -15,6 +15,12 @@ class Geo::UploadRegistry < Geo::FileRegistry
sti_column.in(sti_names)
end
def self.with_search(query)
return all if query.nil?
where(file_id: Geo::Fdw::Upload.search(query))
end
def file
upload&.path || s_('Removed %{type} with id %{id}') % { type: file_type, id: file_id }
end
......
......@@ -2,15 +2,15 @@
require 'spec_helper'
describe Geo::UploadRegistry, :geo do
set(:lfs_registry) { create(:geo_file_registry, :lfs) }
set(:attachment_registry) { create(:geo_file_registry, :attachment, :with_file) }
set(:avatar_registry) { create(:geo_file_registry, :avatar) }
set(:file_registry) { create(:geo_file_registry, :file) }
set(:namespace_file_registry) { create(:geo_file_registry, :namespace_file) }
set(:personal_file_registry) { create(:geo_file_registry, :personal_file) }
set(:favicon_registry) { create(:geo_file_registry, :favicon) }
set(:import_export_registry) { create(:geo_file_registry, :import_export) }
describe Geo::UploadRegistry, :geo, :delete do
let!(:lfs_registry) { create(:geo_file_registry, :lfs) }
let!(:attachment_registry) { create(:geo_file_registry, :attachment, :with_file) }
let!(:avatar_registry) { create(:geo_file_registry, :avatar) }
let!(:file_registry) { create(:geo_file_registry, :file) }
let!(:namespace_file_registry) { create(:geo_file_registry, :namespace_file) }
let!(:personal_file_registry) { create(:geo_file_registry, :personal_file) }
let!(:favicon_registry) { create(:geo_file_registry, :favicon) }
let!(:import_export_registry) { create(:geo_file_registry, :import_export) }
it 'finds all upload registries' do
expected = [attachment_registry,
......@@ -28,9 +28,18 @@ describe Geo::UploadRegistry, :geo do
expect(described_class.find(attachment_registry.id).upload).to be_an_instance_of(Upload)
end
describe '.with_search', :geo_fdw do
it 'searches registries on path' do
upload = create(:upload, path: 'uploads/-/system/project/avatar/my-awesome-avatar.png')
upload_registry = create(:geo_upload_registry, file_id: upload.id, file_type: :avatar)
expect(described_class.with_search('awesome-avatar')).to match_ids(upload_registry)
end
end
describe '#file' do
it 'returns the path of the upload of a registry' do
registry = create(:geo_upload_registry, :avatar, :with_file)
registry = create(:geo_upload_registry, :file, :with_file)
expect(registry.file).to eq('uploads/-/system/project/avatar/avatar.jpg')
end
......
......@@ -18,4 +18,8 @@ RSpec.configure do |config|
config.around(:each, :geo_tracking_db) do |example|
example.run if Gitlab::Geo.geo_database_configured?
end
config.around(:each, :geo_fdw) do |example|
example.run if Gitlab::Geo::Fdw.enabled?
end
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