Class: Mysql::Partitioner::Operation::Range

Inherits:
Abstract
  • Object
show all
Defined in:
lib/mysql/partitioner/operation/range.rb

Instance Attribute Summary

Attributes inherited from Abstract

#dry_run, #session, #table

Instance Method Summary collapse

Methods inherited from Abstract

#database, #empty?, #get_max_val, #get_partition_type, #initialize, #of_max_val, #partitionated?

Constructor Details

This class inherits a constructor from Mysql::Partitioner::Operation::Abstract

Instance Method Details

#add_partitions(partitions) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/mysql/partitioner/operation/range.rb', line 28

def add_partitions(partitions)
  return if partitions.empty?
  partition_defs = partitions.select.map {|item|
    item.to_partition_def
  }.join(",\n  ")
  self.session.alter("ALTER TABLE #{self.table} REORGANIZE PARTITION pmax INTO ( #{partition_defs }, PARTITION pmax VALUES LESS THAN MAXVALUE )" )
end

#check!Object



7
8
9
10
11
12
# File 'lib/mysql/partitioner/operation/range.rb', line 7

def check!
  type = get_partition_type()
  if type != "RANGE" and type != nil
    raise "Partition type mismatch #{type}"
  end
end

#create_partitions(key, partitions) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mysql/partitioner/operation/range.rb', line 36

def create_partitions(key, partitions)
  
  partition_defs = partitions.select.map {|item|
    item.to_partition_def
  }.join(",\n  ")
  self.session.alter("ALTER TABLE \#{self.table} PARTITION BY RANGE (\#{key}) (\n  \#{partition_defs},\n  PARTITION pmax VALUES LESS THAN MAXVALUE)\n")

end

#drop_partitions(partitions) ⇒ Object



49
50
51
52
53
# File 'lib/mysql/partitioner/operation/range.rb', line 49

def drop_partitions(partitions)
  return if partitions.empty?
  names = partitions.map{|item| item.name }.join(",")
  self.session.alter("ALTER TABLE #{self.table} DROP PARTITION #{names}" )
end

#get_current_bounded_partitionsObject



14
15
16
# File 'lib/mysql/partitioner/operation/range.rb', line 14

def get_current_bounded_partitions
  get_current_partitions.select {| item| item.bounded? }
end

#get_current_partitionsObject



18
19
20
21
22
23
24
25
26
# File 'lib/mysql/partitioner/operation/range.rb', line 18

def get_current_partitions
  results = self.session.query("SELECT PARTITION_EXPRESSION, PARTITION_DESCRIPTION, PARTITION_ORDINAL_POSITION, PARTITION_METHOD, SUBPARTITION_EXPRESSION\nFROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME=\"\#{ self.table }\" AND TABLE_SCHEMA=\"\#{ self.database }\" ORDER BY PARTITION_ORDINAL_POSITION ASC\n")
  results.map {|item|
    Mysql::Partitioner::Partition::Range.new(item["PARTITION_DESCRIPTION"])
  }
end

#migrate_partitions(old_partitions, new_partitions) ⇒ Object



55
56
57
58
# File 'lib/mysql/partitioner/operation/range.rb', line 55

def migrate_partitions(old_partitions, new_partitions) 
  self.drop_partitions(old_partitions - new_partitions)
  self.add_partitions(new_partitions - old_partitions)
end