Module: Gitlab::Database::PartitioningMigrationHelpers::ForeignKeyHelpers
- Includes:
- SchemaHelpers
- Included in:
- Gitlab::Database::PartitioningMigrationHelpers
- Defined in:
- lib/gitlab/database/partitioning_migration_helpers/foreign_key_helpers.rb
Instance Method Summary collapse
-
#add_partitioned_foreign_key(from_table, to_table, column: nil, primary_key: :id, on_delete: :cascade) ⇒ Object
Creates a “foreign key” that references a partitioned table.
-
#remove_partitioned_foreign_key(from_table, to_table, column: nil, primary_key: :id) ⇒ Object
Drops a “foreign key” that references a partitioned table.
Methods included from SchemaHelpers
#assert_not_in_transaction_block, #create_comment, #create_trigger, #create_trigger_function, #drop_function, #drop_trigger, #function_exists?, #object_name, #tmp_table_name, #trigger_exists?, #with_lock_retries
Instance Method Details
#add_partitioned_foreign_key(from_table, to_table, column: nil, primary_key: :id, on_delete: :cascade) ⇒ Object
Creates a “foreign key” that references a partitioned table. Because foreign keys referencing partitioned tables are not supported in PG11, this does not create a true database foreign key, but instead implements the same functionality at the database level by using triggers.
Example:
add_partitioned_foreign_key :issues, :projects
Available options:
:column - name of the referencing column (otherwise inferred from the referenced table name)
:primary_key - name of the primary key in the referenced table (defaults to id)
:on_delete - supports either :cascade for ON DELETE CASCADE or :nullify for ON DELETE SET NULL
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/gitlab/database/partitioning_migration_helpers/foreign_key_helpers.rb', line 23 def add_partitioned_foreign_key(from_table, to_table, column: nil, primary_key: :id, on_delete: :cascade) cascade_delete = extract_cascade_option(on_delete) update_foreign_keys(from_table, to_table, column, primary_key, cascade_delete) do |current_keys, existing_key, specified_key| if existing_key.nil? unless specified_key.save raise "failed to create foreign key: #{specified_key.errors..to_sentence}" end current_keys << specified_key else Gitlab::AppLogger.warn "foreign key not added because it already exists: #{specified_key}" current_keys end end end |
#remove_partitioned_foreign_key(from_table, to_table, column: nil, primary_key: :id) ⇒ Object
Drops a “foreign key” that references a partitioned table. This method ONLY applies to foreign keys previously created through the `add_partitioned_foreign_key` method. Standard database foreign keys should be managed through the familiar Rails helpers.
Example:
remove_partitioned_foreign_key :issues, :projects
Available options:
:column - name of the referencing column (otherwise inferred from the referenced table name)
:primary_key - name of the primary key in the referenced table (defaults to id)
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gitlab/database/partitioning_migration_helpers/foreign_key_helpers.rb', line 53 def remove_partitioned_foreign_key(from_table, to_table, column: nil, primary_key: :id) update_foreign_keys(from_table, to_table, column, primary_key) do |current_keys, existing_key, specified_key| if existing_key existing_key.delete current_keys.delete(existing_key) else Gitlab::AppLogger.warn "foreign key not removed because it doesn't exist: #{specified_key}" end current_keys end end |