Class: Gitlab::Database::Partitioning::IntRangePartition

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/gitlab/database/partitioning/int_range_partition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, from, to, partition_name: nil) ⇒ IntRangePartition

Returns a new instance of IntRangePartition.



22
23
24
25
26
27
28
29
# File 'lib/gitlab/database/partitioning/int_range_partition.rb', line 22

def initialize(table, from, to, partition_name: nil)
  @table = table.to_s
  @from = from
  @to = to
  @partition_name = partition_name

  validate!
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



20
21
22
# File 'lib/gitlab/database/partitioning/int_range_partition.rb', line 20

def from
  @from
end

#tableObject (readonly)

Returns the value of attribute table.



20
21
22
# File 'lib/gitlab/database/partitioning/int_range_partition.rb', line 20

def table
  @table
end

#toObject (readonly)

Returns the value of attribute to.



20
21
22
# File 'lib/gitlab/database/partitioning/int_range_partition.rb', line 20

def to
  @to
end

Class Method Details

.from_sql(table, partition_name, definition) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
# File 'lib/gitlab/database/partitioning/int_range_partition.rb', line 9

def self.from_sql(table, partition_name, definition)
  matches = definition.match(/FOR VALUES FROM \('?(?<from>\d+)'?\) TO \('?(?<to>\d+)'?\)/)

  raise ArgumentError, "Unknown partition definition: #{definition}" unless matches

  to = matches[:to].to_i
  from = matches[:from].to_i

  new(table, from, to, partition_name: partition_name)
end

Instance Method Details

#<=>(other) ⇒ Object



62
63
64
65
66
# File 'lib/gitlab/database/partitioning/int_range_partition.rb', line 62

def <=>(other)
  return if table != other.table

  [from.to_i, to.to_i] <=> [other.from.to_i, other.to.to_i]
end

#==(other) ⇒ Object Also known as: eql?



53
54
55
# File 'lib/gitlab/database/partitioning/int_range_partition.rb', line 53

def ==(other)
  table == other.table && partition_name == other.partition_name && from == other.from && to == other.to
end

#hashObject



58
59
60
# File 'lib/gitlab/database/partitioning/int_range_partition.rb', line 58

def hash
  [table, partition_name, from, to].hash
end

#holds_data?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/gitlab/database/partitioning/int_range_partition.rb', line 68

def holds_data?
  conn.execute("SELECT 1 FROM #{fully_qualified_partition} LIMIT 1").ntuples > 0
end

#partition_nameObject



31
32
33
# File 'lib/gitlab/database/partitioning/int_range_partition.rb', line 31

def partition_name
  @partition_name || "#{table}_#{from}"
end

#to_attach_sqlObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/database/partitioning/int_range_partition.rb', line 42

def to_attach_sql
  from_sql = conn.quote(from)
  to_sql = conn.quote(to)

  <<~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_sqlObject



35
36
37
38
39
40
# File 'lib/gitlab/database/partitioning/int_range_partition.rb', line 35

def to_create_sql
  <<~SQL
    CREATE TABLE IF NOT EXISTS #{fully_qualified_partition}
    (LIKE #{conn.quote_table_name(table)} INCLUDING ALL)
  SQL
end