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 dfd7ca4 commit 0b741d8
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 @@ -30,6 +30,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 @@ -81,7 +82,8 @@ def version_metadata_for(version:)
check_version(version:)

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

def version_metadata
Expand Down Expand Up @@ -110,12 +112,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] = { withdrawn: true, date: nil }
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' => { 'withdrawn' => true, 'date' => nil },
'2' => { 'withdrawn' => true, 'date' => nil },
'3' => { 'withdrawn' => false, 'date' => now.iso8601 }
}
}
end

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

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

0 comments on commit 0b741d8

Please sign in to comment.