Class: Code0::ZeroTrack::Database::Partitioning::Strategy::Time

Inherits:
Base
  • Object
show all
Includes:
Loggable
Defined in:
lib/code0/zero_track/database/partitioning/strategy/time.rb

Direct Known Subclasses

Daily, Monthly

Instance Attribute Summary

Attributes inherited from Base

#headroom, #model, #partitioning_column, #retain_detached_for, #retain_for

Instance Method Summary collapse

Methods included from Loggable

#logger

Methods inherited from Base

#default_headroom, #initialize, #partition_name, #partitions_to_create, #partitions_to_detach, #partitions_to_drop, #retention_enabled?

Constructor Details

This class inherits a constructor from Code0::ZeroTrack::Database::Partitioning::Strategy::Base

Instance Method Details

#advance_date(date) ⇒ Object

Raises:

  • (NotImplementedError)


66
67
68
# File 'lib/code0/zero_track/database/partitioning/strategy/time.rb', line 66

def advance_date(date)
  raise NotImplementedError
end

#current_partitionsObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/code0/zero_track/database/partitioning/strategy/time.rb', line 11

def current_partitions
  partitioned_table = PostgresPartitionedTable.find_by(identifier: model.table_name)

  if partitioned_table.nil?
    logger.warn(message: 'Failed to find partitioned table', identifier: model.table_name)
    return []
  end

  partitioned_table.postgres_partitions.map do |partition|
    TimePartition.from_sql(model, partition.name, partition.condition)
  end
end

#desired_partitionsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/code0/zero_track/database/partitioning/strategy/time.rb', line 24

def desired_partitions
  partitions = []

  min_date, max_date = desired_range

  while min_date < max_date
    next_date = advance_date(min_date)

    partitions << TimePartition.new(
      model,
      min_date,
      next_date,
      partition_name: partition_name(min_date)
    )

    min_date = next_date
  end

  partitions
end

#desired_rangeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/code0/zero_track/database/partitioning/strategy/time.rb', line 45

def desired_range
  if retention_enabled?
    min_date = oldest_active_date
  else
    first_partition = current_partitions.min

    min_date = first_partition.from || first_partition.to if first_partition
    min_date ||= Date.current
  end

  min_date = normalize_date(min_date)

  max_date = advance_date(Date.current) + headroom

  [min_date, max_date]
end

#normalize_date(date) ⇒ Object

Raises:

  • (NotImplementedError)


70
71
72
# File 'lib/code0/zero_track/database/partitioning/strategy/time.rb', line 70

def normalize_date(date)
  raise NotImplementedError
end

#oldest_active_dateObject



62
63
64
# File 'lib/code0/zero_track/database/partitioning/strategy/time.rb', line 62

def oldest_active_date
  normalize_date(retain_for.ago)
end