Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Protect foreign key constraint between imagelist_images and imagelist #356

Merged
merged 2 commits into from
Nov 17, 2021

Conversation

dhruvkb
Copy link
Member

@dhruvkb dhruvkb commented Nov 16, 2021

Description

Since migration 0002 changes the primary key on the ImageList model, it breaks the foreign key constraint that refers to that column. This PR merges 0002 into 0001 so that the constraint is set after the changes to the primary key.

Credits to @AetherUnbound for pointing this bug out in prod.

Testing Instructions

  1. Migrate the database
    docker-compose exec -T web python manage.py migrate
  2. Describe the imagelist_images table.
    docker-compose exec -T db psql -U deploy -d openledger <<-EOF
    \d+ imagelist_images;
    EOF
  3. Ensure 2 foreign key constraints:
    • FOREIGN KEY (image_id) REFERENCES image(id)
    • FOREIGN KEY (imagelist_id) REFERENCES imagelist(id)

⚠️ Production notice

Since 0002 is already a performed migration, removing the corresponding file might interfere with rollbacks etc. To prevent potential future problems, remove the entry from the django_migrations table.

DELETE FROM 
  django_migrations
WHERE
  app = 'api' 
  AND name = '0002_auto_20180723_1737';

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@dhruvkb dhruvkb added 🛠 goal: fix Bug fix 💻 aspect: code Concerns the software code in the repository labels Nov 16, 2021
@dhruvkb dhruvkb requested a review from a team as a code owner November 16, 2021 13:53
Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to fix this that doesn't involve changing the migration history? If not this LGTM but I think leaving the migration history as immutable is preferrable to modifying it.

Also if we go with this then let's go ahead and use the full name of the migration rather than an abbreviation when we run that script in prod.

ingestion_server/test/unit_test.py Outdated Show resolved Hide resolved
Copy link
Member

@zackkrida zackkrida left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @dhruvkb and @AetherUnbound, this is great.

Copy link
Member

@krysal krysal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Django has a command for squashing migrations, didn't it work here? Altering the old ones manually is not a good practice. Otherwise, it seems to work well.

ingestion_server/test/unit_test.py Show resolved Hide resolved
@dhruvkb
Copy link
Member Author

dhruvkb commented Nov 16, 2021

@krysal the reason I did not squash the migrations and rather edit them by hand was that this approach removes the need to actually run any migrations in production.

Editing migrations is bad but, in our case, the prod environment and dev are quite out of sync so the usual guidelines need to be bent a little to bring all environments to the same level again.

@dhruvkb dhruvkb merged commit e0949d2 into main Nov 17, 2021
@dhruvkb dhruvkb deleted the fk_constraint branch November 17, 2021 09:57
@AetherUnbound
Copy link
Contributor

This migration ran successfully on a snapshot of prod ✔️

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants