Fix/cascade#583
Conversation
having a wallet assigned to a preparation currently blocks its deletion with an unhelpful error message this alters the FK constraints to remove the join table entries if either side is removed
we have a multipath/circular dependency which prevents preparations from being deleted, this removes the FK cascade files -> attachments and should resolve the situation deletions still will happen via the directory transitive FK
|
ok uhhh I have no idea what is going on with the workflows here |
0901be4 to
b86b02d
Compare
|
this is waiting on validation (repro with test cherrypicked into parent branch + clear with my changes 🤞 ), if that works as intended it will be ready for review |
| return errors.WithStack(err) | ||
| } | ||
| fmt.Printf("Current migration: " + last + "\n") | ||
| fmt.Printf("Current migration: %s\n", last) |
There was a problem hiding this comment.
test runner was complaining about this, I'm actually not 100% sure why but it seems happy now
There was a problem hiding this comment.
ok this seems to be a legit vet error (non-constant format string potential) 👍
|
ok, test harness working locally in devcontainer, I've identified an issue with the FK pattern, will fix and push |
this addresses the postgres-specific foreign key constraint violations when deleting preparations by implementing a database-level solution that: 1. makes files.attachment_id nullable 2. changes the fk constraint to ON DELETE SET NULL instead of restrict/no action 3. adds a postgres trigger that immediately deletes files when attachment_id becomes null this approach keeps the application model unchanged (no pointer types) while solving the constraint violation. the trigger ensures the app never sees null attachment_id values so behavior stays consistent. the solution is postgres-specific because: - postgres has stricter fk enforcement that causes the deadlock/violation issues - mysql tests were already passing with the existing restrict behavior - we removed cascade deletes in 640154e to avoid deadlocks, but that created the constraint violation when deleting preparations longer term consideration: files conceptually belong to storages not preparations. attachments are just prep<->storage associations. we might want to remove this fk entirely since files should be storage-scoped and reusable across preparations (expensive to re-scan). this would eliminate the cascade issues entirely while enabling file reuse when recreating preparations with the same storage.
|
ok, I was able to work around the problem, relevant test is passing with both mysql and sqlite backends: as you can see our ontology is pretty messy, and as mentioned above actually running the full test suite with DB backends fails miserably, so I'm not going to merge the test harness fixes in this branch, however this specific fix is ready for review please see details in d2b7aa7 -- there may be a cleaner fix (removing FK entirely from files to attachments) but that is more of a product-level decision |
| } | ||
| case "postgres": | ||
| connStr = "postgres://singularity:singularity@localhost:5432/singularity?sslmode=disable" | ||
| connStr = "postgres://postgres@localhost:5432/postgres?sslmode=disable" |
There was a problem hiding this comment.
this is so that default settings devcontainer postgres feature can be used; it has no overrides like bitnami (running test daemons in devcontainer was the least complex approach I've come up with; it can also be used in github workflows with devcontainers/ci, which I will PR separately)
| return errors.WithStack(err) | ||
| } | ||
| fmt.Printf("Current migration: " + last + "\n") | ||
| fmt.Printf("Current migration: %s\n", last) |
resolves several foreign key constraint/cascade issues, primarily unblocking deletion of preparations: - fix join table blocking deletion of preps with attached wallets - fix mysql refusing to delete preprations due to fk cascade parallelogram (#465) - fix postgres fk cascade deadlocks (same issue as above) with set null + trigger approach this addresses the postgres-specific foreign key constraint violations when deleting preparations by implementing a database-level solution that: 1. makes files.attachment_id nullable 2. changes the fk constraint to ON DELETE SET NULL instead of restrict/no action 3. adds a postgres trigger that immediately deletes files when attachment_id becomes null this approach keeps the application model unchanged (no pointer types) while solving the constraint violation. the trigger ensures the app never sees null attachment_id values so behavior stays consistent. the solution is postgres-specific because: - postgres has stricter fk enforcement that causes the deadlock/violation issues - mysql tests were already passing with the existing restrict behavior - we removed cascade deletes in 640154e to avoid deadlocks, but that created the constraint violation when deleting preparations longer term consideration: files conceptually belong to storages not preparations. attachments are just prep<->storage associations. we might want to remove this fk entirely since files should be storage-scoped and reusable across preparations (expensive to re-scan). this would eliminate the cascade issues entirely while enabling file reuse when recreating preparations with the same storage. this resolves two issues that prevent deletion of preparations. fixes #465

fix postgres fk cascade deadlocks with set null + trigger approach
this addresses the postgres-specific foreign key constraint violations when deleting
preparations by implementing a database-level solution that:
this approach keeps the application model unchanged (no pointer types) while solving
the constraint violation. the trigger ensures the app never sees null attachment_id
values so behavior stays consistent.
the solution is postgres-specific because:
the constraint violation when deleting preparations
longer term consideration: files conceptually belong to storages not preparations.
attachments are just prep<->storage associations. we might want to remove this fk
entirely since files should be storage-scoped and reusable across preparations
(expensive to re-scan). this would eliminate the cascade issues entirely while
enabling file reuse when recreating preparations with the same storage.
this resolves two issues that prevent deletion of preparations. fixes #465