Class: Zonesync::Sync

Inherits:
Struct
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/zonesync/sync.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#destinationObject

Returns the value of attribute destination

Returns:

  • (Object)

    the current value of destination



7
8
9
# File 'lib/zonesync/sync.rb', line 7

def destination
  @destination
end

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



7
8
9
# File 'lib/zonesync/sync.rb', line 7

def source
  @source
end

Instance Method Details

#call(dry_run: false, force: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/zonesync/sync.rb', line 11

def call dry_run: false, force: false
  operations = destination.diff!(source, force: force)

  smanifest = source.manifest.generate
  dmanifest = destination.manifest.existing
  if smanifest != dmanifest
    if dmanifest
      operations << [:change, [dmanifest, smanifest]]
    else
      operations << [:add, [smanifest]]
    end
  end

  # Only sync checksums for v1 manifests (v2 manifests provide integrity via hashes)
  source_will_generate_v2 = !source.manifest.existing? # No existing manifest = will generate v2
  dest_has_checksum = destination.manifest.existing_checksum
  dest_has_v1_manifest = destination.manifest.existing? && destination.manifest.v1_format?

  if source_will_generate_v2 && dest_has_checksum
    # Transitioning to v2: remove old checksum
    operations << [:remove, [dest_has_checksum]]
  elsif !source_will_generate_v2 && (source.manifest.v1_format? || dest_has_v1_manifest)
    # Both source and dest use v1 format: sync checksum
    schecksum = source.manifest.generate_checksum
    dchecksum = destination.manifest.existing_checksum
    if schecksum != dchecksum
      if dchecksum
        operations << [:change, [dchecksum, schecksum]]
      else
        operations << [:add, [schecksum]]
      end
    end
  end

  operations.each do |method, records|
    Logger.log(method, records, dry_run: dry_run)
    destination.send(method, *records) unless dry_run
  end
end