Class: Gitlab::Database::Partitioning::List::ConvertTable
- Inherits:
-
Object
- Object
- Gitlab::Database::Partitioning::List::ConvertTable
- Defined in:
- lib/gitlab/database/partitioning/list/convert_table.rb
Constant Summary collapse
- UnableToPartition =
Class.new(StandardError)
- SQL_STATEMENT_SEPARATOR =
";\n\n"
- PARTITIONING_CONSTRAINT_NAME =
'partitioning_constraint'
Instance Attribute Summary collapse
-
#parent_table_name ⇒ Object
readonly
Returns the value of attribute parent_table_name.
-
#partitioning_column ⇒ Object
readonly
Returns the value of attribute partitioning_column.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
-
#zero_partition_value ⇒ Object
readonly
Returns the value of attribute zero_partition_value.
Instance Method Summary collapse
-
#initialize(migration_context:, table_name:, parent_table_name:, partitioning_column:, zero_partition_value:) ⇒ ConvertTable
constructor
A new instance of ConvertTable.
- #partition ⇒ Object
- #prepare_for_partitioning(async: false) ⇒ Object
- #revert_partitioning ⇒ Object
- #revert_preparation_for_partitioning ⇒ Object
Constructor Details
#initialize(migration_context:, table_name:, parent_table_name:, partitioning_column:, zero_partition_value:) ⇒ ConvertTable
Returns a new instance of ConvertTable.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gitlab/database/partitioning/list/convert_table.rb', line 16 def initialize( migration_context:, table_name:, parent_table_name:, partitioning_column:, zero_partition_value:) @migration_context = migration_context @connection = migration_context.connection @table_name = table_name @parent_table_name = parent_table_name @partitioning_column = partitioning_column @zero_partition_value = Array.wrap(zero_partition_value) end |
Instance Attribute Details
#parent_table_name ⇒ Object (readonly)
Returns the value of attribute parent_table_name.
14 15 16 |
# File 'lib/gitlab/database/partitioning/list/convert_table.rb', line 14 def parent_table_name @parent_table_name end |
#partitioning_column ⇒ Object (readonly)
Returns the value of attribute partitioning_column.
14 15 16 |
# File 'lib/gitlab/database/partitioning/list/convert_table.rb', line 14 def partitioning_column @partitioning_column end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
14 15 16 |
# File 'lib/gitlab/database/partitioning/list/convert_table.rb', line 14 def table_name @table_name end |
#zero_partition_value ⇒ Object (readonly)
Returns the value of attribute zero_partition_value.
14 15 16 |
# File 'lib/gitlab/database/partitioning/list/convert_table.rb', line 14 def zero_partition_value @zero_partition_value end |
Instance Method Details
#partition ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gitlab/database/partitioning/list/convert_table.rb', line 46 def partition # If already partitioned, the table is no longer partitionable. Thus we skip checks leading up # to partitioning if the partitioning transaction has already succeeded. unless already_partitioned? assert_existing_constraints_partitionable assert_partitioning_constraint_present create_parent_table migration_context.with_lock_retries do redefine_loose_foreign_key_triggers do migration_context.execute(sql_to_convert_table) end end end # Attaching foreign keys handles cases where one or more foreign keys already exists, so it doesn't # need a check similar to the rest of this method attach_foreign_keys_to_parent analyze_parent_table end |
#prepare_for_partitioning(async: false) ⇒ Object
28 29 30 31 32 |
# File 'lib/gitlab/database/partitioning/list/convert_table.rb', line 28 def prepare_for_partitioning(async: false) assert_existing_constraints_partitionable add_partitioning_check_constraint(async: async) end |
#revert_partitioning ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/gitlab/database/partitioning/list/convert_table.rb', line 68 def revert_partitioning migration_context.with_lock_retries(raise_on_exhaustion: true) do migration_context.execute(<<~SQL) ALTER TABLE #{connection.quote_table_name(parent_table_name)} DETACH PARTITION #{connection.quote_table_name(table_name)}; SQL alter_sequences_sql = alter_sequence_statements(old_table: parent_table_name, new_table: table_name) .join(SQL_STATEMENT_SEPARATOR) migration_context.execute(alter_sequences_sql) # This takes locks for all the foreign keys that the parent table had. # However, those same locks were taken while detaching the partition, and we can't avoid that. # If we dropped the foreign key before detaching the partition to avoid this locking, # the drop would cascade to the child partitions and drop their foreign keys as well migration_context.drop_table(parent_table_name) end add_partitioning_check_constraint end |
#revert_preparation_for_partitioning ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/gitlab/database/partitioning/list/convert_table.rb', line 34 def revert_preparation_for_partitioning unless partitioning_constraint.present? return Gitlab::AppLogger.warn <<~MSG The partitioning constraint does not exist for: table: `#{table_name}`, partitioning_column: #{partitioning_column}, parent_table_name: #{parent_table_name}, initial_partitioning_value: #{zero_partition_value} MSG end migration_context.remove_check_constraint(table_name, partitioning_constraint.name) end |