# Multi-Tenant Migration

This release introduces a tenant/branch hierarchy so different counsels can share a single installation.

## New Tables
- `tenants` – top level organisations storing `id`, `name`, `settings`, `isDeleted` and `updatedAt`.
- `branches` – subdivisions linked to a tenant. Columns include `id`, `tenant_id`, `name`, `settings`, `isDeleted` and `updatedAt`.
- `tenant_modules` – enables or disables modules per tenant or branch.
- `tenant_module_settings` – custom settings for each enabled module.

## Field Changes
Most existing tables now include `tenant_id` and `branch_id`. Queries automatically filter on these columns via `QueryBuilder`. See `TenantBranchMigration.md` for mapping the old `company_id` column.

## Upgrade Steps
1. Apply the schema changes from `TAFDB.sql` to create the new tables and add `tenant_id`/`branch_id` columns.
2. Migrate `companies` into `branches` and create tenant records. Update foreign keys to reference `branches.id` with the appropriate `tenant_id`.
3. Ensure JWT payloads or sessions contain `tenant_id` and `branch_id`. The login endpoint already returns these values in the token.
4. Review `tenant_modules` and `tenant_module_settings` to enable modules for each tenant after migration.

## Running Tests

Use the helper script to set up the database, then run the tests yourself:

```bash
DB_DRIVER=mongo ../test_setup.sh
phpunit -c api/phpunit.xml
jest
```

Only run the migration tests when touching this area of the code unless a full suite is required.

