Class: Gitlab::Database::Partitioning::TimePartition
- Inherits:
-
Object
- Object
- Gitlab::Database::Partitioning::TimePartition
- Includes:
- Comparable
- Defined in:
- lib/gitlab/database/partitioning/time_partition.rb
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
- #holds_data? ⇒ Boolean
-
#initialize(table, from, to, partition_name: nil) ⇒ TimePartition
constructor
A new instance of TimePartition.
- #partition_name ⇒ Object
- #to_detach_sql ⇒ Object
- #to_sql ⇒ Object
Constructor Details
#initialize(table, from, to, partition_name: nil) ⇒ TimePartition
Returns a new instance of TimePartition.
24 25 26 27 28 29 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 24 def initialize(table, from, to, partition_name: nil) @table = table.to_s @from = date_or_nil(from) @to = date_or_nil(to) @partition_name = partition_name end |
Instance Attribute Details
#from ⇒ Object (readonly)
Returns the value of attribute from.
22 23 24 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 22 def from @from end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
22 23 24 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 22 def table @table end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
22 23 24 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 22 def to @to end |
Class Method Details
.from_sql(table, partition_name, definition) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 9 def self.from_sql(table, partition_name, definition) matches = definition.match(/FOR VALUES FROM \('?(?<from>.+)'?\) TO \('?(?<to>.+)'?\)/) raise ArgumentError, "Unknown partition definition: #{definition}" unless matches raise NotImplementedError, "Open-end time partitions with MAXVALUE are not supported yet" if matches[:to] == 'MAXVALUE' from = matches[:from] == 'MINVALUE' ? nil : matches[:from] to = matches[:to] new(table, from, to, partition_name: partition_name) end |
Instance Method Details
#<=>(other) ⇒ Object
66 67 68 69 70 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 66 def <=>(other) return if table != other.table partition_name <=> other.partition_name end |
#==(other) ⇒ Object Also known as: eql?
57 58 59 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 57 def ==(other) table == other.table && partition_name == other.partition_name && from == other.from && to == other.to end |
#hash ⇒ Object
62 63 64 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 62 def hash [table, partition_name, from, to].hash end |
#holds_data? ⇒ Boolean
72 73 74 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 72 def holds_data? conn.execute("SELECT 1 FROM #{fully_qualified_partition} LIMIT 1").ntuples > 0 end |
#partition_name ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 31 def partition_name return @partition_name if @partition_name suffix = from&.strftime('%Y%m') || '000000' "#{table}_#{suffix}" end |
#to_detach_sql ⇒ Object
50 51 52 53 54 55 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 50 def to_detach_sql <<~SQL ALTER TABLE #{conn.quote_table_name(table)} DETACH PARTITION #{fully_qualified_partition} SQL end |
#to_sql ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 39 def to_sql from_sql = from ? conn.quote(from.strftime('%Y-%m-%d')) : 'MINVALUE' to_sql = conn.quote(to.strftime('%Y-%m-%d')) <<~SQL CREATE TABLE IF NOT EXISTS #{fully_qualified_partition} PARTITION OF #{conn.quote_table_name(table)} FOR VALUES FROM (#{from_sql}) TO (#{to_sql}) SQL end |