Class: Freyja::Persister

Inherits:
Valkyrie::Persistence::Postgres::Persister
  • Object
show all
Defined in:
lib/freyja/persister.rb

Overview

Persister for Postgres MetadataAdapter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:, wings_service:) ⇒ Persister

Returns a new instance of Persister.



8
9
10
11
# File 'lib/freyja/persister.rb', line 8

def initialize(adapter:, wings_service:)
  @wings_service = wings_service
  super(adapter: adapter)
end

Instance Attribute Details

#wings_serviceObject (readonly)

Returns the value of attribute wings_service.



6
7
8
# File 'lib/freyja/persister.rb', line 6

def wings_service
  @wings_service
end

Instance Method Details

#convert_and_migrate_resource(orm_object, was_wings) ⇒ Object

rubocop:enable Lint/UnusedMethodArgument



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/freyja/persister.rb', line 41

def convert_and_migrate_resource(orm_object, was_wings)
  new_resource = resource_factory.to_resource(object: orm_object)
  # if the resource was wings and is now a Valkyrie resource, we need to migrate sipity, files, and members
  if Hyrax.config.valkyrie_transition? && was_wings && !new_resource.wings?
    # Check if all file_ids match the '/files/' pattern
    MigrateFilesToValkyrieJob.perform_later(new_resource) if new_resource.is_a?(Hyrax::FileSet) && new_resource.file_ids.all? { |id_holder| id_holder.to_s.match('/files/') }
    # migrate any members if the resource is a Hyrax work
    if new_resource.is_a?(Hyrax::Work)
      member_ids = new_resource.member_ids.map(&:to_s)
      MigrateResourcesJob.perform_later(ids: member_ids) unless member_ids.empty?
      MigrateSipityEntityJob.perform_now(id: new_resource.id.to_s)
    end
  end
  new_resource
end

#delete(resource:) ⇒ Valkyrie::Resource

Deletes a resource persisted within the database from both postgres and fedora

Parameters:

  • resource (Valkyrie::Resource)

Returns:

  • (Valkyrie::Resource)

    the deleted resource



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/freyja/persister.rb', line 60

def delete(resource:)
  # call super to delete from postgres
  super(resource: resource)
  # After deletion, we need to ensure that the resource is no longer found
  # because it could still exist in fedora.
  fedora_record = Hyrax.query_service.find_by(id: resource.id)
  wings_service.delete(resource: fedora_record) if fedora_record
  resource
rescue Valkyrie::Persistence::ObjectNotFoundError
  # If the resource is not found, we return the original resource
  resource
end

#save(resource:, external_resource: false, perform_af_validation: false) ⇒ Valkyrie::Resource

Persists a resource within the database

Modified from the upstream to skip previously persisted check

rubocop:disable Lint/UnusedMethodArgument

Parameters:

  • resource (Valkyrie::Resource)

Returns:

  • (Valkyrie::Resource)

    the persisted/updated resource

Raises:

  • (Valkyrie::Persistence::StaleObjectError)

    raised if the resource was modified in the database between been read into memory and persisted



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/freyja/persister.rb', line 22

def save(resource:, external_resource: false, perform_af_validation: false)
  was_wings = resource.respond_to?(:wings?) && resource.wings?
  orm_object = resource_factory.from_resource(resource: resource)
  orm_object.transaction do
    orm_object.save!
    if resource.id && resource.id.to_s != orm_object.id
      raise Valkyrie::Persistence::UnsupportedDatatype,
            "Postgres' primary key column can not save with the given ID #{resource.id}. " \
            "To avoid this error, set the ID to be nil via `resource.id = nil` before you save it. \n" \
            "Called from #{Gem.location_of_caller.join(':')}"
    end
  end
  convert_and_migrate_resource(orm_object, was_wings)

rescue ActiveRecord::StaleObjectError
  raise Valkyrie::Persistence::StaleObjectError, "The object #{resource.id} has been updated by another process."
end