Skip to content

Repair the database rules and validation - #171

Merged
otigges merged 8 commits into
mainfrom
database-fixes
Oct 7, 2026
Merged

otigges merged 8 commits into
mainfrom
database-fixes

Conversation

@otigges

@otigges otigges commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

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).

  1. Backlog: the PostgreSQL upgrade of production (15.18 in production, 18 in the repository).
  2. Delete rules for the system and asset references. A system with assets or threats, and an asset with threats, could not be deleted. Deleting a system now deletes its assets and threats (the rule from 2023 that the migration consolidation lost); deleting an asset keeps its threats and clears the reference. Decision in docs/adr/0001-delete-rules-for-system-and-asset-references.md (status proposed).
  3. Quota usage table. user_id referenced organisations instead of users; 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 column organisations.quota_period is dropped.
  4. Invites. The address is citext like users.email, so "Bob@x.com" matches "bob@x.com", and one address can be invited only once per organisation.
  5. Indexes and defaults. Indexes on threats.organisation_id, threats.asset_id, memberships.organisation_id. systems.attributes is NOT NULL with {} as default.
  6. Validation. Descriptions and text fields limited to the column sizes, number ranges for risks and asset criticality, attribute values must be short texts, unique system name as a changeset error.
  7. Changeset errors instead of exceptions. The create and update functions used Repo.insert! and Repo.update! inside a transaction, so invalid input crashed the page. Repo.insert_or_rollback/1 and update_or_rollback/1 return {:error, changeset} and the forms show the errors.
  8. Registration stores first and last name, which the form asks for.

Before the deployment

  • Take a backup of the Cloud SQL instance. Migration 2 changes delete rules, and migration 3 changes data (quota_usages.user_id values that are no user id are set to NULL).
  • Migrations run on container start (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

otigges and others added 8 commits October 7, 2026 07:27
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>
@otigges
otigges merged commit a3aa9cf into main Oct 7, 2026
2 checks passed
@otigges
otigges deleted the database-fixes branch October 7, 2026 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant