> ## Documentation Index
> Fetch the complete documentation index at: https://docs.encoreos.io/llms.txt
> Use this file to discover all available pages before exploring further.

# SLA Management — Migration Notes

> Feature: SLA Management Platform Layer Last Updated: 2026-03-17

**Feature:** SLA Management Platform Layer\
**Spec:** PF-83\
**Last Updated:** 2026-03-17

***

## Overview

PF-83 introduced two new tables and extended `pf_module_settings` with SLA-related configuration columns. The migration was applied via the Supabase MCP `execute_sql` tool — no `.sql` file exists on disk in `supabase/migrations/`.

## Tables Created

### `pf_sla_definitions` (20 columns)

| Column                  | Type        | Notes                                                     |
| ----------------------- | ----------- | --------------------------------------------------------- |
| `id`                    | UUID        | PK, `gen_random_uuid()`                                   |
| `organization_id`       | UUID        | FK → `pf_organizations`, tenant isolation                 |
| `name`                  | TEXT        | NOT NULL                                                  |
| `description`           | TEXT        | Nullable                                                  |
| `sla_type`              | TEXT        | `response_time`, `resolution_time`, `follow_up`, `custom` |
| `owning_core`           | TEXT        | Which module owns this SLA (e.g., `ce`, `hr`)             |
| `trigger_event_type`    | TEXT        | Event that starts the SLA clock                           |
| `completion_event_type` | TEXT        | Event that stops the SLA clock                            |
| `target_duration_hours` | NUMERIC     | Deadline in hours                                         |
| `warning_threshold_pct` | INTEGER     | Default 80                                                |
| `escalation_config`     | JSONB       | Escalation levels and notification targets                |
| `entity_match_config`   | JSONB       | Rules for matching events to entities                     |
| `priority_overrides`    | JSONB       | Per-priority deadline adjustments                         |
| `priority_field`        | TEXT        | Which field to read priority from                         |
| `is_active`             | BOOLEAN     | Default `true`                                            |
| `custom_fields`         | JSONB       | Default `'{}'`                                            |
| `created_at`            | TIMESTAMPTZ | Default `now()`                                           |
| `updated_at`            | TIMESTAMPTZ | Default `now()`                                           |
| `created_by`            | UUID        | FK → `pf_profiles`                                        |
| `updated_by`            | UUID        | FK → `pf_profiles`                                        |

### `pf_sla_instances` (21 columns)

| Column                | Type        | Notes                                                  |
| --------------------- | ----------- | ------------------------------------------------------ |
| `id`                  | UUID        | PK, `gen_random_uuid()`                                |
| `organization_id`     | UUID        | FK → `pf_organizations`, tenant isolation              |
| `definition_id`       | UUID        | FK → `pf_sla_definitions`                              |
| `entity_type`         | TEXT        | Type of entity this instance tracks                    |
| `entity_id`           | UUID        | ID of the tracked entity                               |
| `status`              | TEXT        | `active`, `at_risk`, `breached`, `completed`, `paused` |
| `started_at`          | TIMESTAMPTZ | When the SLA clock started                             |
| `deadline_at`         | TIMESTAMPTZ | Computed deadline                                      |
| `completed_at`        | TIMESTAMPTZ | When completed (nullable)                              |
| `breached_at`         | TIMESTAMPTZ | When breached (nullable)                               |
| `paused_at`           | TIMESTAMPTZ | When paused (nullable)                                 |
| `pause_reason`        | TEXT        | Reason for pause (nullable)                            |
| `elapsed_seconds`     | NUMERIC     | Total elapsed time                                     |
| `remaining_seconds`   | NUMERIC     | Time remaining until deadline                          |
| `percentage_elapsed`  | NUMERIC     | Progress percentage                                    |
| `trigger_event_id`    | UUID        | FK → `fw_domain_events` that started this instance     |
| `completion_event_id` | UUID        | FK → `fw_domain_events` that completed this instance   |
| `custom_fields`       | JSONB       | Default `'{}'`                                         |
| `created_at`          | TIMESTAMPTZ | Default `now()`                                        |
| `updated_at`          | TIMESTAMPTZ | Default `now()`                                        |
| `deleted_at`          | TIMESTAMPTZ | Soft delete (nullable)                                 |

## RLS Policies

Both tables have RLS enabled with policies using the `pf_has_org_access(organization_id)` SECURITY DEFINER function:

* **SELECT:** Authenticated users with org access
* **INSERT:** Authenticated users with org access
* **UPDATE:** Authenticated users with org access, `WITH CHECK` clause
* **DELETE:** Authenticated users with org access

## Settings Columns Added to `pf_module_settings`

SLA-related configuration is stored in the existing `pf_module_settings` table via its `custom_fields` JSONB column. No new columns were added to the table schema.

## Rollback Procedure

To remove PF-83 tables (destructive — all SLA data will be lost):

```sql theme={null}
-- Drop instances first (FK dependency)
DROP TABLE IF EXISTS pf_sla_instances CASCADE;
DROP TABLE IF EXISTS pf_sla_definitions CASCADE;
```

> **Warning:** This is irreversible. Back up data before executing.

## Notes

* **No `.sql` migration file on disk** — The migration was applied directly via Supabase MCP `execute_sql`. This is a known limitation of the Lovable workflow. The schema is in the live database and reflected in `types.ts`.
* **PF-84 dependency** — Business calendar-aware deadline calculation is deferred until PF-84 is implemented. Current deadlines use simple interval math.
* **Edge function `pf-sla-checker`** — Deferred to Phase 2. Will provide server-side periodic breach detection.

## References

* [PF-83 Spec](../../specs/pf/specs/PF-83-sla-management-platform-layer.md)
* [Admin Guide](sla-management-admin-guide.md)
* [Runbook](sla-management-runbook.md)
