-
Notifications
You must be signed in to change notification settings - Fork 106
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
Conversation
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 }) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 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 |
@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 ( Removing constraint from "Tracks" table (~3ms): Adding back constraint to "Tracks" table (~9s but we only need to do this to revert in a failure scenario): Removing constraint from "AudiusUsers" table (~3ms): Adding back constraint to "AudiusUsers" table (~29ms and we only need to do this to revert in a failure scenario): |
@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? |
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 }) |
There was a problem hiding this comment.
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?
There was a problem hiding this 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
There was a problem hiding this 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
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
A down && A up
and verified that the database constraints were not presentForeignKeyConstraintError
are now successfully completing syncsMonitoring - 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
.