# Phase 2 — Database and Migrations

Phase 2 establishes the MySQL/MariaDB schema foundation for the CRM. It does not implement application workflows or UI for these modules.

## Apply migrations

Run the SQL files in numeric order:

1. `migrations/001_core.sql`
2. `migrations/002_clinical.sql`
3. `migrations/003_billing.sql`
4. `migrations/004_system.sql`
5. `seeders/001_roles_permissions.sql`

The files can be imported through phpMyAdmin or executed through PDO/MySQL tooling.

## Design notes

- InnoDB and utf8mb4 are used throughout.
- Foreign keys preserve relational integrity.
- Indexes target the fields identified by the master prompt as common search/filter fields.
- Statuses are VARCHAR values rather than rigid ENUMs so later workflow changes do not require unnecessary schema changes.
- Users can have multiple roles, allowing one owner account to have both administrative and therapist capabilities.
- Approved reports use immutable version records rather than silently overwriting approved content.
- Invoice totals are stored on invoices; payments are separate records so partial payments do not alter invoice totals.
- Business settings and numbering sequences are configurable data rather than hard-coded application values.
- Password reset tokens are stored as hashes only.
- Audit logs are append-oriented records and are not given application-level update/delete semantics.


## Phase 10
Run `migrations/007_phase10_session_fields.sql` once, then `seeders/007_phase10_session_permissions.sql`. This adds session type/status fields and session permissions.

## Phase 16 — Payments and Receipts
Run `migrations/008_phase16_payment_fields.sql` once, then `seeders_013_phase16_payment_permissions.sql`. This adds payment status/void tracking and payment/receipt permissions. Payment records generate a receipt record automatically.
