Class: Code0::ZeroTrack::Database::Partitioning::Strategy::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/code0/zero_track/database/partitioning/strategy/base.rb

Direct Known Subclasses

Time

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, partitioning_column, headroom: default_headroom, retain_for: nil, retain_detached_for: 7.days) ⇒ Base

Returns a new instance of Base.



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

def initialize(
  model,
  partitioning_column,
  headroom: default_headroom,
  retain_for: nil,
  retain_detached_for: 7.days
)
  @model = model
  @partitioning_column = partitioning_column
  @headroom = headroom
  @retain_for = retain_for
  @retain_detached_for = retain_detached_for
end

Instance Attribute Details

#headroomObject (readonly)

Returns the value of attribute headroom.



9
10
11
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 9

def headroom
  @headroom
end

#modelObject (readonly)

Returns the value of attribute model.



9
10
11
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 9

def model
  @model
end

#partitioning_columnObject (readonly)

Returns the value of attribute partitioning_column.



9
10
11
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 9

def partitioning_column
  @partitioning_column
end

#retain_detached_forObject (readonly)

Returns the value of attribute retain_detached_for.



9
10
11
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 9

def retain_detached_for
  @retain_detached_for
end

#retain_forObject (readonly)

Returns the value of attribute retain_for.



9
10
11
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 9

def retain_for
  @retain_for
end

Instance Method Details

#current_partitionsObject

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 25

def current_partitions
  raise NotImplementedError
end

#default_headroomObject

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 41

def default_headroom
  raise NotImplementedError
end

#desired_partitionsObject

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 29

def desired_partitions
  raise NotImplementedError
end

#oldest_active_dateObject

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 33

def oldest_active_date
  raise NotImplementedError
end

#partition_name(lower_bound) ⇒ Object

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 37

def partition_name(lower_bound)
  raise NotImplementedError
end

#partitions_to_createObject



45
46
47
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 45

def partitions_to_create
  desired_partitions - current_partitions
end

#partitions_to_detachObject



49
50
51
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 49

def partitions_to_detach
  current_partitions - desired_partitions
end

#partitions_to_dropObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 53

def partitions_to_drop
  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_detached_partitions.detached_before(retain_detached_for.ago)
end

#retention_enabled?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/code0/zero_track/database/partitioning/strategy/base.rb', line 64

def retention_enabled?
  retain_for.present?
end