Module: Gitlab::Database::MigrationHelpers::LooseForeignKeyHelpers

Includes:
SchemaHelpers
Included in:
Partitioning::PartitionManager, PartitioningMigrationHelpers::TableManagementHelpers
Defined in:
lib/gitlab/database/migration_helpers/loose_foreign_key_helpers.rb

Constant Summary collapse

INSERT_FUNCTION_NAME =
'insert_into_loose_foreign_keys_deleted_records'
INSERT_FUNCTION_NAME_OVERRIDE_TABLE =
'insert_into_loose_foreign_keys_deleted_records_override_table'

Instance Method Summary collapse

Methods included from SchemaHelpers

#assert_not_in_transaction_block, #create_comment, #create_trigger, #create_trigger_function, #drop_function, #drop_trigger, #find_all_id_columns_sql, #function_exists?, #object_name, #reset_all_trigger_functions, #reset_trigger_function, #tmp_table_name, #trigger_exists?

Instance Method Details

#has_loose_foreign_key?(table) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/gitlab/database/migration_helpers/loose_foreign_key_helpers.rb', line 48

def has_loose_foreign_key?(table)
  trigger_exists?(table, record_deletion_trigger_name(table))
end

#track_record_deletions(table_name) ⇒ Object

This adds a LFK standard trigger to tables, where the loose_foreign_keys_deleted_records record is referencing the table. This should be used for non-partitioned tables.



14
15
16
17
18
19
20
21
22
23
# File 'lib/gitlab/database/migration_helpers/loose_foreign_key_helpers.rb', line 14

def track_record_deletions(table_name)
  trigger_name = record_deletion_trigger_name(table_name)

  execute("    CREATE TRIGGER \#{trigger_name}\n    AFTER DELETE ON \#{table_name} REFERENCING OLD TABLE AS old_table\n    FOR EACH STATEMENT\n    EXECUTE FUNCTION \#{INSERT_FUNCTION_NAME}();\n  SQL\nend\n".squish)

#track_record_deletions_override_table_name(table_identifier, parent_table = nil) ⇒ Object

This is used to track deletions on partitioned tables and their partitions. parent_table is the table name that is insert into loose_foreign_keys_deleted_records table it defaults to the table_name, and that’s for when we track deletions on partitioned (parent) tables.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gitlab/database/migration_helpers/loose_foreign_key_helpers.rb', line 28

def track_record_deletions_override_table_name(table_identifier, parent_table = nil)
  table_name = table_identifier.to_s.split('.').last
  parent_table ||= table_name

  execute("    CREATE TRIGGER \#{record_deletion_trigger_name(table_name)}\n    AFTER DELETE ON \#{table_identifier} REFERENCING OLD TABLE AS old_table\n    FOR EACH STATEMENT\n    EXECUTE FUNCTION\n    \#{INSERT_FUNCTION_NAME_OVERRIDE_TABLE}(\#{connection.quote(parent_table)});\n  SQL\nend\n".squish)

#untrack_record_deletions(table) ⇒ Object

This method also works on tables that are not in the default schema, but the full table identifier has to be passed in this case.



43
44
45
46
# File 'lib/gitlab/database/migration_helpers/loose_foreign_key_helpers.rb', line 43

def untrack_record_deletions(table)
  trigger_name = record_deletion_trigger_name(table)
  drop_trigger(table, trigger_name)
end