Commit ce4bcf7c authored by Vladimir Shushlin's avatar Vladimir Shushlin

Move migrated? logic to PagesDeployment

* also add scope for finding migrated deployments
parent 7a57eef4
...@@ -5,7 +5,6 @@ module Pages ...@@ -5,7 +5,6 @@ module Pages
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
LegacyStorageDisabledError = Class.new(::StandardError) LegacyStorageDisabledError = Class.new(::StandardError)
MIGRATED_FILE_NAME = "_migrated.zip"
def initialize(project, trim_prefix: nil, domain: nil) def initialize(project, trim_prefix: nil, domain: nil)
@project = project @project = project
...@@ -55,7 +54,7 @@ module Pages ...@@ -55,7 +54,7 @@ module Pages
return if deployment.file.file_storage? && !Feature.enabled?(:pages_serve_with_zip_file_protocol, project) return if deployment.file.file_storage? && !Feature.enabled?(:pages_serve_with_zip_file_protocol, project)
return if deployment.file.filename == MIGRATED_FILE_NAME && !Feature.enabled?(:pages_serve_from_migrated_zip, project) return if deployment.migrated? && !Feature.enabled?(:pages_serve_from_migrated_zip, project)
global_id = ::Gitlab::GlobalId.build(deployment, id: deployment.id).to_s global_id = ::Gitlab::GlobalId.build(deployment, id: deployment.id).to_s
......
...@@ -4,12 +4,15 @@ ...@@ -4,12 +4,15 @@
class PagesDeployment < ApplicationRecord class PagesDeployment < ApplicationRecord
include FileStoreMounter include FileStoreMounter
MIGRATED_FILE_NAME = "_migrated.zip"
attribute :file_store, :integer, default: -> { ::Pages::DeploymentUploader.default_store } attribute :file_store, :integer, default: -> { ::Pages::DeploymentUploader.default_store }
belongs_to :project, optional: false belongs_to :project, optional: false
belongs_to :ci_build, class_name: 'Ci::Build', optional: true belongs_to :ci_build, class_name: 'Ci::Build', optional: true
scope :older_than, -> (id) { where('id < ?', id) } scope :older_than, -> (id) { where('id < ?', id) }
scope :migrated_from_legacy_storage, -> { where(file: MIGRATED_FILE_NAME) }
validates :file, presence: true validates :file, presence: true
validates :file_store, presence: true, inclusion: { in: ObjectStorage::SUPPORTED_STORES } validates :file_store, presence: true, inclusion: { in: ObjectStorage::SUPPORTED_STORES }
...@@ -25,6 +28,10 @@ class PagesDeployment < ApplicationRecord ...@@ -25,6 +28,10 @@ class PagesDeployment < ApplicationRecord
# this is to be adressed in https://gitlab.com/groups/gitlab-org/-/epics/589 # this is to be adressed in https://gitlab.com/groups/gitlab-org/-/epics/589
end end
def migrated?
file.filename == MIGRATED_FILE_NAME
end
private private
def set_size def set_size
......
...@@ -124,22 +124,12 @@ RSpec.describe Pages::LookupPath do ...@@ -124,22 +124,12 @@ RSpec.describe Pages::LookupPath do
include_examples 'uses disk storage' include_examples 'uses disk storage'
end end
end
context 'when deployment were created during migration' do context 'when deployment were created during migration' do
before do before do
FileUtils.mkdir_p File.join(project.pages_path, "public") allow(deployment).to receive(:migrated?).and_return(true)
File.open(File.join(project.pages_path, "public/index.html"), "w") do |f|
f.write("Hello!")
end
expect(::Pages::MigrateLegacyStorageToDeploymentService.new(project).execute[:status]).to eq(:success)
project.reload
end end
let(:deployment) { project.pages_metadatum.pages_deployment }
it 'uses deployment from object storage' do it 'uses deployment from object storage' do
freeze_time do freeze_time do
expect(source).to( expect(source).to(
...@@ -164,6 +154,7 @@ RSpec.describe Pages::LookupPath do ...@@ -164,6 +154,7 @@ RSpec.describe Pages::LookupPath do
end end
end end
end end
end
describe '#prefix' do describe '#prefix' do
it 'returns "/" for pages group root projects' do it 'returns "/" for pages group root projects' do
......
...@@ -26,6 +26,43 @@ RSpec.describe PagesDeployment do ...@@ -26,6 +26,43 @@ RSpec.describe PagesDeployment do
end end
end end
describe '.migrated_from_legacy_storage' do
it 'only returns migrated deployments' do
project = create(:project)
migrated_deployment = create_migrated_deployment(project)
# create one other deployment
create(:pages_deployment, project: project)
expect(described_class.migrated_from_legacy_storage).to eq([migrated_deployment])
end
end
describe '#migrated?' do
it 'returns false for normal deployment' do
deployment = create(:pages_deployment)
expect(deployment.migrated?).to eq(false)
end
it 'returns true for migrated deployment' do
project = create(:project)
deployment = create_migrated_deployment(project)
expect(deployment.migrated?).to eq(true)
end
end
def create_migrated_deployment(project)
FileUtils.mkdir_p File.join(project.pages_path, "public")
File.open(File.join(project.pages_path, "public/index.html"), "w") do |f|
f.write("Hello!")
end
expect(::Pages::MigrateLegacyStorageToDeploymentService.new(project).execute[:status]).to eq(:success)
project.reload.pages_metadatum.pages_deployment
end
describe 'default for file_store' do describe 'default for file_store' do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:deployment) do let(:deployment) 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