Repository navigation
Repair the database rules and validation - #171
Merged
Merged
Conversation
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
A system with assets or threats, and an asset with threats, could not be deleted: the foreign keys had no delete rule, and the delete failed with a database error. The migrations of September 2023 had cascade rules for the system references; the consolidation of February 2024 lost them. Deleting a system now deletes its assets and threats. Deleting an asset keeps its threats and clears the reference. The decision is recorded in docs/adr/0001. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
- quota_usages.user_id referenced organisations instead of users. The migration points it to users (set to NULL on user delete) and clears values that are no user id. - quota_usages.organisation_id is deleted with the organisation. Before, an organisation with AI usage could not be deleted. - The usage timestamp is set when the row is inserted. The schema default was evaluated at compile time, so every row had the build time and the monthly quota counted nothing after one month. In production, no usage was recorded since June 2024. - The usage is recorded inside the AI task, right after the quota check, instead of in a detached task whose failure was invisible. - Indexes on both foreign keys. The unused column organisations.quota_period is dropped where it exists. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
invites.email was a plain string, while users.email is citext. An invite to Bob@x.com did not match the user bob@x.com. The column is now citext, the address is compared by the database when an invite is accepted, and one address can be invited only once per organisation. The form shows this as a validation error. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Indexes for threats.organisation_id, threats.asset_id, and memberships.organisation_id, which are used in joins and filters. systems.attributes could be NULL, and System.describe/1 failed on it. The column is now NOT NULL with an empty map as default, and the schema has the same default. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…names - Descriptions are limited to 4000 characters, the organisation name and location and the mitigation text fields to 255, as the columns. - Risk severity and asset criticality are between 0 and 5, probability and cost are not negative, and the cost fits the integer column. - Attribute values of organisations and systems must be texts of at most 500 characters. A nested value crashed the organisation page. - The unique system name per organisation is a changeset error. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The create and update functions of systems, assets, threats, risks,
organisations, and mitigations used Repo.insert! and Repo.update!
inside a transaction. Invalid input raised an exception and crashed
the page, and the error branches of the forms were unreachable.
Repo.insert_or_rollback/1 and Repo.update_or_rollback/1 roll the
transaction back with the changeset, so the functions return
{:error, changeset} and the forms show the errors. Creating an
organisation also checks the insert of the owner membership now.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The registration form asks for both names as required fields, but the changeset did not cast them, so they were discarded. They are stored now and limited to the 40 characters of the columns. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Database-related fixes from the audit, one commit each. The migrations ran against an empty database in the tests, and against a copy of the production schema (structure only, no data).
docs/adr/0001-delete-rules-for-system-and-asset-references.md(status proposed).user_idreferencedorganisationsinstead ofusers; an organisation with usage rows could not be deleted; the usage timestamp was the build time, so the monthly quota counted nothing after a month. In production no usage was recorded since June 2024. The usage is now written inside the AI task, right after the quota check. The unused columnorganisations.quota_periodis dropped.citextlikeusers.email, so "Bob@x.com" matches "bob@x.com", and one address can be invited only once per organisation.threats.organisation_id,threats.asset_id,memberships.organisation_id.systems.attributesis NOT NULL with{}as default.Repo.insert!andRepo.update!inside a transaction, so invalid input crashed the page.Repo.insert_or_rollback/1andupdate_or_rollback/1return{:error, changeset}and the forms show the errors.Before the deployment
quota_usages.user_idvalues that are no user id are set to NULL).rel/overlays/bin/start), so the deployment applies them.Tests
mix test: 199 tests, 0 failures (181 before). The four migrations were also rolled back and applied again.🤖 Generated with Claude Code