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.
-
#partition_name ⇒ Object
readonly
Returns the value of attribute partition_name.
-
#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:) ⇒ TimePartition
constructor
A new instance of TimePartition.
- #to_attach_sql ⇒ Object
- #to_create_sql ⇒ Object
- #to_detach_sql ⇒ Object
Constructor Details
#initialize(table, from, to, partition_name:) ⇒ TimePartition
Returns a new instance of TimePartition.
24 25 26 27 28 29 30 31 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 24 def initialize(table, from, to, partition_name:) raise ArgumentError, "partition_name required but none given" unless partition_name @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 |
#partition_name ⇒ Object (readonly)
Returns the value of attribute partition_name.
22 23 24 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 22 def partition_name @partition_name 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
67 68 69 70 71 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 67 def <=>(other) return if table != other.table partition_name <=> other.partition_name end |
#==(other) ⇒ Object Also known as: eql?
58 59 60 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 58 def ==(other) table == other.table && partition_name == other.partition_name && from == other.from && to == other.to end |
#hash ⇒ Object
63 64 65 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 63 def hash [table, partition_name, from, to].hash end |
#holds_data? ⇒ Boolean
73 74 75 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 73 def holds_data? conn.execute("SELECT 1 FROM #{fully_qualified_partition} LIMIT 1").ntuples > 0 end |
#to_attach_sql ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 40 def to_attach_sql from_sql = from ? conn.quote(from.to_date.iso8601) : 'MINVALUE' to_sql = conn.quote(to.to_date.iso8601) <<~SQL ALTER TABLE #{conn.quote_table_name(table)} ATTACH PARTITION #{fully_qualified_partition} FOR VALUES FROM (#{from_sql}) TO (#{to_sql}) SQL end |
#to_create_sql ⇒ Object
33 34 35 36 37 38 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 33 def to_create_sql <<~SQL CREATE TABLE IF NOT EXISTS #{fully_qualified_partition} (LIKE #{conn.quote_table_name(table)} INCLUDING ALL) SQL end |
#to_detach_sql ⇒ Object
51 52 53 54 55 56 |
# File 'lib/gitlab/database/partitioning/time_partition.rb', line 51 def to_detach_sql <<~SQL ALTER TABLE #{conn.quote_table_name(table)} DETACH PARTITION #{fully_qualified_partition} SQL end |