Class: SlidingPartition::PartitionDDLChanger
- Inherits:
-
Object
- Object
- SlidingPartition::PartitionDDLChanger
- Extended by:
- Forwardable
- Includes:
- Util
- Defined in:
- lib/sliding_partition/partition_ddl_changer.rb
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
-
#partitions ⇒ Object
readonly
Returns the value of attribute partitions.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
- #clone_new_table! ⇒ Object
- #create_tables! ⇒ Object
- #expire_tables! ⇒ Object
- #final_copy! ⇒ Object
-
#initialize(definition, time, quiet: false) ⇒ PartitionDDLChanger
constructor
A new instance of PartitionDDLChanger.
- #migrate! ⇒ Object
- #migrate_data!(from:, to:) ⇒ Object
- #rotate! ⇒ Object
- #setup! ⇒ Object
- #swap_tables! ⇒ Object
Methods included from Util
Constructor Details
#initialize(definition, time, quiet: false) ⇒ PartitionDDLChanger
Returns a new instance of PartitionDDLChanger.
14 15 16 17 18 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 14 def initialize(definition, time, quiet: false) @definition, @time = definition, time @partitions = @definition.partitions(at: time) @quiet = quiet end |
Instance Attribute Details
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
9 10 11 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 9 def definition @definition end |
#partitions ⇒ Object (readonly)
Returns the value of attribute partitions.
9 10 11 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 9 def partitions @partitions end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
9 10 11 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 9 def time @time end |
Instance Method Details
#clone_new_table! ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 68 def clone_new_table! connection.execute <<-SQL CREATE TABLE #{new_table} ( LIKE #{inherited_table_name} INCLUDING ALL ); SQL end |
#create_tables! ⇒ Object
56 57 58 59 60 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 56 def create_tables! partitions.each do |partition| create_partition_table(partition) end end |
#expire_tables! ⇒ Object
62 63 64 65 66 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 62 def expire_tables! candidate_tables.each do |table| drop_partition_table(table) unless partitions.map(&:table_name).include?(table) end end |
#final_copy! ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 48 def final_copy! connection.execute(<<-SQL) INSERT INTO #{inherited_table_name} ( SELECT * FROM #{retired_table} WHERE id NOT IN (SELECT id FROM #{inherited_table_name}) ) SQL end |
#migrate! ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 36 def migrate! connection.transaction do @new_table_name = new_table say_with_time("Cloning table") { clone_new_table! } say_with_time("Creating #{partitions.tables.size} partition tables") { create_tables! } say_with_time("Updating trigger function") { update_trigger_function! } say_with_time("Creating trigger") { create_trigger! } say_with_time("Migrating data") { migrate_data!(from: inherited_table_name, to: new_table) } say_with_time("Swapping retired & new tables") { swap_tables! } end end |
#migrate_data!(from:, to:) ⇒ Object
85 86 87 88 89 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 85 def migrate_data!(from:, to:) connection.execute <<-SQL INSERT INTO #{to} ( SELECT * FROM #{from} ) SQL end |
#rotate! ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 28 def rotate! connection.transaction do create_tables! expire_tables! update_trigger_function! end end |
#setup! ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 20 def setup! connection.transaction do say_with_time("Creating tables") { create_tables! } say_with_time("Updating trigger function") { update_trigger_function! } say_with_time("Creating trigger") { create_trigger! } end end |
#swap_tables! ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/sliding_partition/partition_ddl_changer.rb', line 76 def swap_tables! connection.execute <<-SQL ALTER TABLE #{inherited_table_name} RENAME TO #{retired_table}; ALTER TABLE #{new_table} RENAME TO #{inherited_table_name}; SQL # update_trigger_function! # create_trigger! end |