closeio / sync-engine

GNU Affero General Public License v3.0
25 stars 9 forks source link

Investigate missing foreign keys on `messagecategory` table. #302

Open squeaky-pl opened 2 years ago

squeaky-pl commented 2 years ago

Currently the table looks like this:

CREATE TABLE `messagecategory` (
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `message_id` bigint(20) NOT NULL,
  `category_id` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `ix_messagecategory_category_id` (`category_id`),
  KEY `ix_messagecategory_created_at` (`created_at`),
  KEY `message_category_ids` (`message_id`,`category_id`)
);

Notice that there are no foreign key constraints on message_id and category_id columns. This can lead to to data corruption and exceptions on Python side. We should investigate if they were omitted on purpose. If not we should add them.

squeaky-pl commented 2 years ago

It looks like they were dropped on purpose https://github.com/closeio/sync-engine/blob/9022236c98ca7abdef67062a38a7a295c498b6e0/migrations/versions/225_drop_messagecategory_foreign_keys.py#L20

squeaky-pl commented 2 years ago

The commit that deletes them is this one: https://github.com/closeio/sync-engine/commit/ff8b0bd749ac71f2c8c52430ccb8d3707485a499