DB structure for loose foreign keys
What does this MR do and why?
This MR is a split-down from !69165 (merged) which adds the database table, DB function, model and migration helpers for the loose foreign key feature.
- Initial plan: #338535 (comment 660060073)
- More context, examples: !69165 (merged)
Database migration
Up:
== 20210826122748 CreateLooseForeignKeysDeletedRecords: migrating =============
-- execute("CREATE TABLE loose_foreign_keys_deleted_records (\n created_at timestamp with time zone NOT NULL DEFAULT NOW(),\n deleted_table_name text NOT NULL,\n deleted_table_primary_key_value bigint NOT NULL,\n PRIMARY KEY (created_at, deleted_table_name, deleted_table_primary_key_value),\n CONSTRAINT check_7229f9527e CHECK ((char_length(deleted_table_name) <= 63))\n) PARTITION BY RANGE (created_at);\n")
-> 0.0063s
-- table_exists?("gitlab_partitions_dynamic.loose_foreign_keys_deleted_records_000000")
-> 0.0005s
-- execute("CREATE TABLE gitlab_partitions_dynamic.loose_foreign_keys_deleted_records_000000 PARTITION OF loose_foreign_keys_deleted_records\nFOR VALUES FROM (MINVALUE) TO ('2021-08-01')\n")
-> 0.0155s
-- table_exists?("gitlab_partitions_dynamic.loose_foreign_keys_deleted_records_202108")
-> 0.0003s
-- execute("CREATE TABLE gitlab_partitions_dynamic.loose_foreign_keys_deleted_records_202108 PARTITION OF loose_foreign_keys_deleted_records\nFOR VALUES FROM ('2021-08-01') TO ('2021-09-01')\n")
-> 0.0061s
-- table_exists?("gitlab_partitions_dynamic.loose_foreign_keys_deleted_records_202109")
-> 0.0004s
-- execute("CREATE TABLE gitlab_partitions_dynamic.loose_foreign_keys_deleted_records_202109 PARTITION OF loose_foreign_keys_deleted_records\nFOR VALUES FROM ('2021-09-01') TO ('2021-10-01')\n")
-> 0.0045s
-- table_exists?("gitlab_partitions_dynamic.loose_foreign_keys_deleted_records_202110")
-> 0.0005s
-- execute("CREATE TABLE gitlab_partitions_dynamic.loose_foreign_keys_deleted_records_202110 PARTITION OF loose_foreign_keys_deleted_records\nFOR VALUES FROM ('2021-10-01') TO ('2021-11-01')\n")
-> 0.0044s
-- table_exists?("gitlab_partitions_dynamic.loose_foreign_keys_deleted_records_202111")
-> 0.0004s
-- execute("CREATE TABLE gitlab_partitions_dynamic.loose_foreign_keys_deleted_records_202111 PARTITION OF loose_foreign_keys_deleted_records\nFOR VALUES FROM ('2021-11-01') TO ('2021-12-01')\n")
-> 0.0049s
-- table_exists?("gitlab_partitions_dynamic.loose_foreign_keys_deleted_records_202112")
-> 0.0008s
-- execute("CREATE TABLE gitlab_partitions_dynamic.loose_foreign_keys_deleted_records_202112 PARTITION OF loose_foreign_keys_deleted_records\nFOR VALUES FROM ('2021-12-01') TO ('2022-01-01')\n")
-> 0.0047s
== 20210826122748 CreateLooseForeignKeysDeletedRecords: migrated (0.0502s) ====
== 20210826145509 AddFunctionForInsertingDeletedRecords: migrating ============
-- execute("CREATE FUNCTION insert_into_loose_foreign_keys_deleted_records()\nRETURNS TRIGGER AS\n$$\nBEGIN\n INSERT INTO loose_foreign_keys_deleted_records\n (deleted_table_name, deleted_table_primary_key_value)\n VALUES (TG_TABLE_NAME, OLD.id)\n ON CONFLICT DO NOTHING;\n\n RETURN NULL;\nEND\n$$ LANGUAGE PLPGSQL\n")
-> 0.0023s
== 20210826145509 AddFunctionForInsertingDeletedRecords: migrated (0.0024s) ===
Down:
== 20210826145509 AddFunctionForInsertingDeletedRecords: reverting ============
-- execute("DROP FUNCTION IF EXISTS insert_into_loose_foreign_keys_deleted_records()")
-> 0.0012s
== 20210826145509 AddFunctionForInsertingDeletedRecords: reverted (0.0012s) ===
== 20210826122748 CreateLooseForeignKeysDeletedRecords: reverting =============
-- drop_table(:loose_foreign_keys_deleted_records)
-> 0.0068s
== 20210826122748 CreateLooseForeignKeysDeletedRecords: reverted (0.0069s) ====
MR acceptance checklist
These checklists encourage us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
Quality
-
Quality checklist confirmed
- I have self-reviewed this MR per code review guidelines.
- For the code that that this change impacts, I believe that the automated tests (Testing Guide) validate functionality that is highly important to users (including consideration of all test levels). If the existing automated tests do not cover this functionality, I have added the necessary additional tests or I have added an issue to describe the automation testing gap and linked it to this MR.
- I have considered the technical aspects of the impact of this change on both gitlab.com hosted customers and self-hosted customers.
- I have considered the impact of this change on the front-end, back-end, and database portions of the system where appropriate and applied frontend, backend and database labels accordingly.
- I have tested this MR in all supported browsers, or determiend that this testing is not needed.
- I have confirmed that this change is backwards compatible across updates, or I have decided that this does not apply.
- I have properly separated EE content from FOSS, or this MR is FOSS only. (Where should EE code go?)
- If I am introducing a new expectation for existing data, I have confirmed that existing data meets this expectation or I have made this expectation optional rather than required.
Performance, reliability, and availability
-
Performance, reliability, and availability checklist confirmed
- I am confident that this MR does not harm performance, or I have asked a reviewer to help assess the performance impact. (Merge request performance guidelines)
- I have added information for database reviewers in the MR description, or I have decided that it is unnecessary. (Does this MR have database-related changes?)
- I have considered the availability and reliability risks of this change. I have also considered the scalability risk based on future predicted growth
- I have considered the performance, reliability and availability impacts of this change on large customers who may have significantly more data than the average customer.
Documentation
-
Documentation checklist confirmed
- I have included changelog trailers, or I have decided that they are not needed. (Does this MR need a changelog?)
- I have added/updated documentation, or I have decided that documentation changes are not needed for this MR. (Is documentation required?)
Security
-
Security checklist confirmed
- I have confirmed that if this MR contains changes to processing or storing of credentials or tokens, authorization, and authentication methods, or other items described in the security review guidelines, I have added the label security and I have
@
-mentioned@gitlab-com/gl-security/appsec
.
Deployment
-
Deployment checklist confirmed
- I have considered using a feature flag for this change because the change may be high risk. If I decided to use a feature flag, I plan to test the change in staging before I test it in production, and I have considered rolling it out to a subset of production customers before doing rolling it out to all customers. When to use a feature flag
- I have informed the Infrastructure department of a default setting or new setting change per definition of done, or decided that this is not needed.
Related to #338535 (closed)
Edited by Adam Hegyi