Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove foreign key constraints #4206

Merged
merged 10 commits into from
Nov 4, 2022
Merged

Remove foreign key constraints #4206

merged 10 commits into from
Nov 4, 2022

Conversation

theoilie
Copy link
Contributor

@theoilie theoilie commented Oct 28, 2022

Description

Removes foreign key constraints on AudiusUsers and Tracks tables by removing their creation from a previous migration and adding a new migration to alter the tables.

Tests

  • Ran A down && A up and verified that the database constraints were not present
  • CI still passes
  • Performed benchmarks with a clone of a large database (CN2)
  • Verified on staging that users who previously failed syncs due to ForeignKeyConstraintError are now successfully completing syncs

Monitoring - How will this change be monitored? Are there sufficient logs / alerts?

We shouldn't see the following error anymore: error: insert or update on table "Tracks" violates foreign key constraint "Tracks_metadataFileUUID_fkey.

@theoilie theoilie added the content-node Content Node (previously known as Creator Node) label Oct 28, 2022
Comment on lines -30 to -38
await queryInterface.sequelize.query(`
ALTER TABLE "AudiusUsers" ADD CONSTRAINT "AudiusUsers_coverArtFileUUID_fkey" FOREIGN KEY ("coverArtFileUUID") REFERENCES "Files" ("fileUUID") ON DELETE RESTRICT;
ALTER TABLE "AudiusUsers" ADD CONSTRAINT "AudiusUsers_metadataFileUUID_fkey" FOREIGN KEY ("metadataFileUUID") REFERENCES "Files" ("fileUUID") ON DELETE RESTRICT;
ALTER TABLE "AudiusUsers" ADD CONSTRAINT "AudiusUsers_profilePicFileUUID_fkey" FOREIGN KEY ("profilePicFileUUID") REFERENCES "Files" ("fileUUID") ON DELETE RESTRICT;
await queryInterface.sequelize.query(
`
DELETE FROM "Tracks" WHERE "metadataFileUUID" IN (SELECT "metadataFileUUID" FROM "Tracks" t LEFT OUTER JOIN "Files" f ON t."metadataFileUUID" = f."fileUUID" WHERE f."fileUUID" IS NULL AND t."metadataFileUUID" IS NOT NULL);
DELETE FROM "Tracks" WHERE "coverArtFileUUID" IN (SELECT "coverArtFileUUID" FROM "Tracks" t LEFT OUTER JOIN "Files" f ON t."coverArtFileUUID" = f."fileUUID" WHERE f."fileUUID" IS NULL AND t."coverArtFileUUID" IS NOT NULL);
ALTER TABLE "Tracks" ADD CONSTRAINT "Tracks_coverArtFileUUID_fkey " FOREIGN KEY ("coverArtFileUUID") REFERENCES "Files" ("fileUUID") ON DELETE RESTRICT;
ALTER TABLE "Tracks" ADD CONSTRAINT "Tracks_metadataFileUUID_fkey" FOREIGN KEY ("metadataFileUUID") REFERENCES "Files" ("fileUUID") ON DELETE RESTRICT;
`, { transaction })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these lines are the only real changes in this file - everything else is formatting

Copy link
Contributor

Choose a reason for hiding this comment

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

instead of deleting could we comment these out with a msg like this was previously run but taken out on X date?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah good idea - it's a bit tricky to comment out line by line since it's in a giant string, but I just pushed a change to comment out the old query and make the new one not include the constraints

@theoilie theoilie changed the title Theo foreign keys fail safes Remove foreign key constraints and fail safes Oct 28, 2022
@dmanjunath
Copy link
Contributor

@theoilie can we break this into two separate PRs? one for the primarySyncFromSecondary fix and the other for the migration changes? i want to test the migration stuff super deeply and benchmark how long it would take to run and soak it on our nodes for a while

@theoilie theoilie changed the title Remove foreign key constraints and fail safes Remove foreign key constraints Oct 28, 2022
@theoilie
Copy link
Contributor Author

theoilie commented Nov 3, 2022

@dmanjunath There was an important typo that I fixed while benchmarking. It looks like, from stage CN8, removing the constraints is very fast (adding back if needed takes a bit longer). I turned timings on (\timing) since we can't EXPLAIN ANALYZE an ALTER TABLE query - here are the results:

Removing constraint from "Tracks" table (~3ms):
Screen Shot 2022-11-03 at 12 26 02 PM

Adding back constraint to "Tracks" table (~9s but we only need to do this to revert in a failure scenario):
Screen Shot 2022-11-03 at 12 27 26 PM

Removing constraint from "AudiusUsers" table (~3ms):
Screen Shot 2022-11-03 at 12 28 35 PM

Adding back constraint to "AudiusUsers" table (~29ms and we only need to do this to revert in a failure scenario):
Screen Shot 2022-11-03 at 12 29 17 PM

@dmanjunath
Copy link
Contributor

@theoilie quotes are essentail good catch. as far as benchmarking i think stage is a good start but mind making a clone of something massive like prod cn2 and testing the times on there?

@theoilie
Copy link
Contributor Author

theoilie commented Nov 3, 2022

Here it is for prod db2 clone - super quick!
Screen Shot 2022-11-03 at 2 04 47 PM

@AudiusProject AudiusProject deleted a comment from gitguardian bot Nov 3, 2022
@AudiusProject AudiusProject deleted a comment from gitguardian bot Nov 3, 2022
Comment on lines -30 to -38
await queryInterface.sequelize.query(`
ALTER TABLE "AudiusUsers" ADD CONSTRAINT "AudiusUsers_coverArtFileUUID_fkey" FOREIGN KEY ("coverArtFileUUID") REFERENCES "Files" ("fileUUID") ON DELETE RESTRICT;
ALTER TABLE "AudiusUsers" ADD CONSTRAINT "AudiusUsers_metadataFileUUID_fkey" FOREIGN KEY ("metadataFileUUID") REFERENCES "Files" ("fileUUID") ON DELETE RESTRICT;
ALTER TABLE "AudiusUsers" ADD CONSTRAINT "AudiusUsers_profilePicFileUUID_fkey" FOREIGN KEY ("profilePicFileUUID") REFERENCES "Files" ("fileUUID") ON DELETE RESTRICT;
await queryInterface.sequelize.query(
`
DELETE FROM "Tracks" WHERE "metadataFileUUID" IN (SELECT "metadataFileUUID" FROM "Tracks" t LEFT OUTER JOIN "Files" f ON t."metadataFileUUID" = f."fileUUID" WHERE f."fileUUID" IS NULL AND t."metadataFileUUID" IS NOT NULL);
DELETE FROM "Tracks" WHERE "coverArtFileUUID" IN (SELECT "coverArtFileUUID" FROM "Tracks" t LEFT OUTER JOIN "Files" f ON t."coverArtFileUUID" = f."fileUUID" WHERE f."fileUUID" IS NULL AND t."coverArtFileUUID" IS NOT NULL);
ALTER TABLE "Tracks" ADD CONSTRAINT "Tracks_coverArtFileUUID_fkey " FOREIGN KEY ("coverArtFileUUID") REFERENCES "Files" ("fileUUID") ON DELETE RESTRICT;
ALTER TABLE "Tracks" ADD CONSTRAINT "Tracks_metadataFileUUID_fkey" FOREIGN KEY ("metadataFileUUID") REFERENCES "Files" ("fileUUID") ON DELETE RESTRICT;
`, { transaction })
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of deleting could we comment these out with a msg like this was previously run but taken out on X date?

@AudiusProject AudiusProject deleted a comment from gitguardian bot Nov 4, 2022
@AudiusProject AudiusProject deleted a comment from gitguardian bot Nov 4, 2022
Copy link
Contributor

@SidSethi SidSethi left a comment

Choose a reason for hiding this comment

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

lgtm - i def share @dmanjunath 's paranoia with this change lol
also ci failing

Copy link
Contributor

@dmanjunath dmanjunath left a comment

Choose a reason for hiding this comment

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

looks good with sequelize model changes. thanks for making that change as well

@theoilie theoilie merged commit 0976501 into main Nov 4, 2022
@theoilie theoilie deleted the theo-foreign-keys-fail-safes branch November 4, 2022 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content-node Content Node (previously known as Creator Node) size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants