Module: PartitionGardener::MaintenanceBackend

Defined in:
lib/partition_gardener/maintenance_backend.rb

Defined Under Namespace

Classes: ValidationError

Constant Summary collapse

GARDENER =
:gardener
PG_PARTMAN =
:pg_partman
HYBRID_LAYOUT_ONLY =
:hybrid_layout_only

Class Method Summary collapse

Class Method Details

.gardener_owned?(config) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/partition_gardener/maintenance_backend.rb', line 15

def gardener_owned?(config)
  normalize(config[:maintenance_backend]) != PG_PARTMAN
end

.hybrid?(config) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/partition_gardener/maintenance_backend.rb', line 23

def hybrid?(config)
  normalize(config[:maintenance_backend]) == HYBRID_LAYOUT_ONLY
end

.normalize(backend) ⇒ Object



11
12
13
# File 'lib/partition_gardener/maintenance_backend.rb', line 11

def normalize(backend)
  backend&.to_sym || GARDENER
end

.partman_parent_configured?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/partition_gardener/maintenance_backend.rb', line 27

def partman_parent_configured?(table_name)
  Connection.partman_parent_configured?(table_name)
rescue
  false
end

.skipped?(config) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/partition_gardener/maintenance_backend.rb', line 19

def skipped?(config)
  normalize(config[:maintenance_backend]) == PG_PARTMAN
end

.validate!(config) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/partition_gardener/maintenance_backend.rb', line 33

def validate!(config)
  violations = validation_messages(config)
  return if violations.empty?

  violations.each do |message|
    if PartitionGardener.configuration.strict_maintenance_backend_validation
      raise ValidationError, message
    end

    PartitionGardener.configuration.notify(
      "[PartitionGardener] #{message}",
      context: {
        table_name: config[:table_name],
        maintenance_backend: normalize(config[:maintenance_backend])
      }
    )
  end
end

.validation_messages(config) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/partition_gardener/maintenance_backend.rb', line 52

def validation_messages(config)
  backend = normalize(config[:maintenance_backend])
  table_name = config[:table_name]
  partman_row = partman_parent_configured?(table_name)
  messages = []

  if backend == PG_PARTMAN && !partman_row
    messages << "#{table_name} maintenance_backend is pg_partman but partman.part_config has no row"
  end

  if backend == GARDENER && partman_row
    messages << "#{table_name} is registered for gardener but partman.part_config also lists this parent; pick one maintainer"
  end

  if hybrid?(config) && !partman_row
    messages << "#{table_name} hybrid_layout_only expects partman premake on the same parent"
  end

  messages
end