Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export default defineConfig({
{ text: "Introduction", link: "/database/" },
{ text: "Key Entities", link: "/database/key-entities" },
{ text: "ER Diagram", link: "/database/er-diagram" },
{ text: "Constraints", link: "/database/constraints" },
{ text: "Migration Scripts", link: "/database/migrations" },
{ text: "Seed Data", link: "/database/seed-data" },
],
},
{
Expand Down Expand Up @@ -88,9 +91,16 @@ export default defineConfig({
items: [
{ text: "Introduction", link: "/frontend/" },
{ text: "Architecture", link: "/frontend/architecture" },
{ text: "Routing", link: "/frontend/routing" },
{ text: "Pages and Components", link: "/frontend/pages-and-components" },
{ text: "State Management", link: "/frontend/state-management" },
{ text: "Forms and Validation", link: "/frontend/forms-and-validation" },
{ text: "API Integration", link: "/frontend/api-integration" },
{ text: "Authentication", link: "/frontend/authentication" },
{ text: "Maps and Real-time", link: "/frontend/maps-and-real-time" },
{ text: "Build and Tooling", link: "/frontend/build-and-tooling" },
{ text: "Testing", link: "/frontend/testing" },
{ text: "Deployment", link: "/frontend/deployment" },
],
},
{
Expand All @@ -108,6 +118,10 @@ export default defineConfig({
text: "Setup",
collapsed: true,
items: [
{ text: "Deployment and Contribution", link: "/deployment/" },
{ text: "Deployment", link: "/deployment/deployment" },
{ text: "Branching and Pull Requests", link: "/deployment/branching-and-prs" },
{ text: "Code Contribution", link: "/deployment/code-contribution" },
{ text: "Installation", link: "/setup/installation" },
{ text: "Run Locally", link: "/setup/run-locally" },
{ text: "Database Setup", link: "/setup/database-setup" },
Expand Down Expand Up @@ -137,4 +151,4 @@ export default defineConfig({
},
],
},
})
})
Binary file added docs/assets/dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/fishers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/incidents.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/register.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/reports.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/responders.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/signin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/wristbands.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions docs/database/constraints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Constraints

This page lists the rules the database enforces on its data. Items marked **TO CONFIRM** must be checked against the real schema before this page is treated as final.

To inspect the real constraints of a table, connect with `psql` and run:

```sql
\d fishers
```

Use `\d+` with no table name to list every table. Update this page whenever a migration adds, changes or removes a constraint.

## Primary keys

Every table has a single primary key. It is the bold column in the [entity relationship diagram](./er-diagram.md).

## Foreign keys

| Table | Column | References | Meaning |
| --- | --- | --- | --- |
| `fishers` | `operator_id` | `station_operators` | The operator who registered the fisher. |
| `wristbands` | `fisher_id` | `fishers` | The fisher the wristband is assigned to. |
| `trips` | `fisher_id` | `fishers` | The fisher taking the trip. |
| `locations` | `trip_id` | `trips` | The trip the GPS point belongs to. |
| `locations` | `fisher_id` | `fishers` | The fisher the GPS point belongs to. |
| `distressed_fisher_alerts` | `fisher_id` | `fishers` | The fisher in distress. |
| `distressed_fisher_alerts` | `location_id` | `locations` | Where the fisher was when the alert was raised. |
| `distressed_fisher_alerts` | `acknowledged_by_operator_id` | `station_operators` | The operator who acknowledged the alert. |
| `alert_responders` | `alert_id` | `distressed_fisher_alerts` | The alert being responded to. |
| `alert_responders` | `responder_fisher_id` | `fishers` | The nearby fisher asked to help. |

`islands` and `alembic_version` have no foreign keys.

## Unique constraints

| Table | Column(s) | Purpose | Status |
| --- | --- | --- | --- |
| `fishers` | `phone_number` | No two fishers share a phone number. Adding this constraint to a database that already holds duplicates fails, so clean the data first. | **TO CONFIRM** |
| `wristbands` | `rfid_tag` | One physical tag maps to one wristband record. | **TO CONFIRM** |
| `station_operators` | `email` | One account per email address, used to log in. | **TO CONFIRM** |
| `alert_responders` | `alert_id`, `responder_fisher_id` | A fisher is asked to respond to a given alert only once. | **TO CONFIRM** |

## Required and optional columns

| Column | Rule |
| --- | --- |
| `station_operators.password_hash` | Required. Passwords are stored only as hashes, never in plain text. |
| `distressed_fisher_alerts.resolved_at` | Empty while the alert is open. Setting it closes the alert. |
| `distressed_fisher_alerts.acknowledged_by_operator_id`, `acknowledged_at` | Empty until an operator acknowledges the alert. **TO CONFIRM** |
| `alert_responders.arrived_at` | Empty until the responder reaches the fisher in distress. **TO CONFIRM** |
| `trips.checkout_at` | Empty until the trip ends. **TO CONFIRM** |
| All other columns | **TO CONFIRM** which are required. |

## Allowed values

| Column | Allowed values | Enforced by |
| --- | --- | --- |
| `wristbands.status` | **TO CONFIRM** | **TO CONFIRM** (database enum or check, or application only) |
| `trips.status` | **TO CONFIRM** | **TO CONFIRM** |

## Deletion behaviour

**TO CONFIRM:** what happens to dependent rows when a fisher, trip or operator is deleted (cascade, restrict or set null). Alerts, locations and responder records are emergency history, so deleting a fisher should not silently remove them. Prefer restricting deletes on rows that alerts depend on.

## Indexes

**TO CONFIRM** which indexes exist beyond the primary keys. Lookups worth indexing are the latest location per fisher (`locations` by `fisher_id` and `recorded_at`) and open alerts (`distressed_fisher_alerts` where `resolved_at` is empty).

## Rules the application enforces

Some rules live in the backend rather than the database:

- An alert is open until `resolved_at` is set.
- Nearest responders and response status are calculated from the timestamps, not stored.
- Passwords are hashed before they reach the database.
3 changes: 3 additions & 0 deletions docs/database/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ MvuviGo stores everything in a **PostgreSQL** database named `MvuviGo`. The back

- [Entity relationship diagram](./er-diagram.md): how the tables connect.
- [Key entities](./key-entities.md): what each table stores and how an alert moves through them.
- [Constraints](./constraints.md): keys, unique rules, required columns and allowed values.
- [Migrations](./migrations.md): how the schema is versioned and changed with Alembic.
- [Seed data](./seed-data.md): the data a fresh database needs and how it is loaded.

## Working with the database locally

Expand Down
16 changes: 8 additions & 8 deletions docs/database/key-entities.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# Key Entities

## fishers
## Fishers

The people the system protects. Stores `full_name`, `phone_number`, the registering `operator_id`, and `created_at` / `updated_at`. A fisher can be in distress, or be asked to respond to someone else's alert.

## wristbands
## Wristbands

The wearable that carries the RFID tag. Stores `rfid_tag`, `status`, the `fisher_id` it is assigned to, and `assigned_at`. Assigning a wristband to a fisher is what ties a physical signal to a person.

## station_operators
## Station_operators

Staff who run a landing station and use the dashboard. Stores `first_name`, `last_name`, `email` and `password_hash`. Operators register fishers and acknowledge alerts. Passwords are never stored in plain text.

## trips
## Trips

A fishing trip. Stores `fisher_id`, `total_fishers`, `checkin_at`, `checkout_at` and `status`. A trip is the context that gives location records meaning.

## locations
## Locations

GPS points recorded during a trip. Stores `fisher_id`, `trip_id`, `latitude`, `longitude` and `recorded_at`. The latest location is what an alert points at.

## islands
## Islands

Reference data for the fishing areas. Stores `island_name`, `island_latitude` and `island_longitude`.

## distressed_fisher_alerts
## Distressed_fisher_alerts

One row per emergency.

Expand All @@ -37,7 +37,7 @@ One row per emergency.
| `acknowledged_at` | When the operator took it on. |
| `resolved_at` | When the emergency ended. Empty while the alert is open. |

## alert_responders
## Alert_responders

One row per fisher asked to help with an alert.

Expand Down
53 changes: 53 additions & 0 deletions docs/database/migrations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Migrations

The schema is versioned with **Alembic** in `pixels-backend`. The SQLAlchemy models describe what the database should look like, and migrations are the steps that get a database there. The current version is recorded in the `alembic_version` table.

## Rules

- Every change to a SQLAlchemy model needs a new migration.
- Never edit the database by hand and leave the models behind.
- Keep one concern per migration, and keep it in the same PR as the model change.
- Read every generated migration before committing it. Autogenerate can miss renames and constraints.
- Write a working `downgrade` for every migration so it can be undone.

## Commands

Run these from the backend repository.

| Task | Command |
| --- | --- |
| Apply all migrations | `alembic upgrade head` |
| Show the current version | `alembic current` |
| List the history | `alembic history --verbose` |
| Create a migration from model changes | `alembic revision --autogenerate -m "describe the change"` |
| Undo the last migration | `alembic downgrade -1` |
| Mark an existing schema as up to date | `alembic stamp head` |

Migration files live in the Alembic `versions` folder (**TO CONFIRM** the path in `pixels-backend`).

## Adding a migration

1. Change the model in `pixels-backend`.
2. Run `alembic revision --autogenerate -m "describe the change"`.
3. Open the new file and check the `upgrade` and `downgrade` steps.
4. Run `alembic upgrade head` on your local database.
5. Run `alembic downgrade -1` and then `alembic upgrade head` again to prove both directions work.
6. Run the backend tests (`pytest`).
7. Commit the model change and the migration together.
8. Update [Key entities](./key-entities.md), [Constraints](./constraints.md) and the [entity relationship diagram](./er-diagram.md) if the schema changed.

## Common problems

| Symptom | Cause | Fix |
| --- | --- | --- |
| `relation "islands" already exists` | The tables were created before Alembic was set up on your machine. | Drop and recreate the local database, or run `alembic stamp head` and then `alembic upgrade head`. |
| Adding a unique constraint fails | The table already contains duplicate values. | Clean the duplicates first, for example in the migration itself, then upgrade again. |
| The app and the migrations disagree about the data | Alembic and the application are connected to different databases. | Make sure the connection setting in `.env` and the default in the application's database code name the same database. |

PostgreSQL treats names that differ only in capitals, such as `MvuviGo` and `mvuvigo`, as different databases when the name was created with quotes. The documented name is `MvuviGo`. **TO CONFIRM** the exact spelling every environment must use, and use it everywhere.

## Production

- Take a database backup before every production migration.
- Run `alembic upgrade head` on every deploy, after the backup and before the new backend starts.
- Follow [Rollback and recovery](../deployment/deployment.md#rollback-and-recovery) if a migration has to be undone.
33 changes: 33 additions & 0 deletions docs/database/seed-data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Seed Data

Seed data is the information a fresh database needs before the system is useful. Everything else is created by people using the dashboard and by the wristbands and stations.

## What needs seeding

| Table | Why | Needed in production |
| --- | --- | --- |
| `islands` | Reference data for the fishing areas: `island_name`, `island_latitude` and `island_longitude`. | Yes |
| `station_operators` | At least one operator must exist to log in and register fishers. The password is stored as a hash, never in plain text. | Yes, the first operator only |
| `fishers`, `wristbands`, `trips`, `locations` | Sample records for local development and demos. | No |
| `distressed_fisher_alerts`, `alert_responders` | Sample alerts for local development and demos. | No |

The list of islands to load: **TO CONFIRM**.

## How seed data is loaded

**TO CONFIRM:** whether seed data is loaded by a script, by a migration or by hand, and the exact command. Document it here once decided, for example:

```bash
# TO CONFIRM: replace with the real command
python <seed-script>
```

If there is no seed mechanism yet, state "none" here and list how the first operator and the islands are created.

## Rules

- A seed script must be safe to run twice. Check for existing rows before inserting.
- Never load development sample data into production.
- Sample data uses invented names and phone numbers, never real people.
- Do not commit real passwords or real operator accounts. Create the first production operator through a secured step and store its credentials in the host's secret store.
- Backend tests create their own data and roll it back, so they do not depend on seed data.
1 change: 0 additions & 1 deletion docs/deployment/branching-and-prs.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
git checkout develop
git pull
git checkout -b feature/<name>
# make your changes, then
git add .
git commit -m "Describe what changed"
git push -u origin feature/<name>
Expand Down
Loading
Loading