Skip to content

Commit

Permalink
Backfill missing versions in versions manifest.
Browse files Browse the repository at this point in the history
closes #907
  • Loading branch information
justinlittman committed Aug 7, 2024
1 parent 5c4a4bf commit 968d719
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/services/versioned_files_service/versions_manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def update_version(version:, version_metadata:)
manifest[:versions][version] = version_metadata.as_json

update_head_version if !version_metadata.withdrawn? && (head_version.nil? || version >= head_version)
backfill_withdrawn_versions

write!
end
Expand Down Expand Up @@ -89,7 +90,8 @@ def version_metadata_for(version:)
check_version(version:)

version_data = manifest[:versions][version]
VersionMetadata.new(version: version.to_i, state: version_data[:state], date: DateTime.iso8601(version_data[:date]))
date = version_data[:date].present? ? DateTime.iso8601(version_data[:date]) : nil
VersionMetadata.new(version: version.to_i, state: version_data[:state], date:)
end

def version_metadata
Expand Down Expand Up @@ -118,12 +120,23 @@ def manifest
attr_reader :path

def write!
manifest[:versions] = manifest[:versions].sort.to_h if manifest.key?(:versions)
FileUtils.mkdir_p(path.dirname)
path.write(manifest.to_json)
end

def check_version(version:)
raise UnknownVersionError, "Version #{version} not found" unless version?(version:)
end

def backfill_withdrawn_versions
return unless head_version

(1..head_version).each do |version|
next if version?(version:)

manifest[:versions][version] = { state: 'permanently_withdrawn' }
end
end
end
end
37 changes: 37 additions & 0 deletions spec/services/versioned_files_service/versions_manifest_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe VersionedFilesService::VersionsManifest do
subject(:manifest) { described_class.new(path: pathname) }

let(:pathname) { Pathname.new('tmp/versions_manifest.json') }

after do
pathname.delete if pathname.exist?
end

describe '.update_version' do
context 'when backfilling withdrawn versions' do
let(:now) { DateTime.now }

let(:expected_manifest) do
{
"$schemaVersion" => 1,
'head' => 3,
'versions' => {
'1' => { 'state' => 'permanently_withdrawn' },
'2' => { 'state' => 'permanently_withdrawn' },
'3' => { 'state' => 'available', 'date' => now.iso8601 }
}
}
end

it 'backfills withdrawn versions' do
manifest.update_version(version: 3, version_metadata: described_class::VersionMetadata.new(version: 3, state: 'available', date: now))

expect(JSON.parse(pathname.read)).to eq expected_manifest
end
end
end
end

0 comments on commit 968d719

Please sign in to comment.