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

# Supabase Migration Documentation

> Before starting the migration, verify your setup:

**Version:** 2.0.0\
**Last Updated:** 2026-01-15\
**Purpose:** Complete documentation and tooling for migrating from Lovable Cloud to Supabase Cloud

***

## Quick Start Verification Checklist

Before starting the migration, verify your setup:

### ✅ 1. MCP Connection

```typescript theme={null}
mcp_supabase_get_project_url()
// Expected: https://<project-ref>.supabase.co
```

### ✅ 2. CLI Installation

```bash theme={null}
supabase --version
# Expected: Supabase CLI 1.x.x or higher
```

### ✅ 3. Project Linked

```bash theme={null}
supabase status
# Expected: Shows linked project details
```

### ✅ 4. Migration Files Present

```bash theme={null}
Get-ChildItem "supabase/migrations/*.sql" | Measure-Object
# Expected: Count >= 393
```

### ✅ 5. Staging Branch Available

```typescript theme={null}
mcp_supabase_list_branches()
// Expected: Shows 'staging' branch
```

**All checks passed?** See the live [Migration Workflow](/database/migration-workflow). The original 2026-01 Completion Plan is archived at [../archive/migration-2026-01/COMPLETION\_PLAN.md](https://github.com/Encore-OS/encoreos/blob/development/docs/archive/migration-2026-01/COMPLETION_PLAN.md).

***

## Migration Status Tracker

### Current Phase: **Phase 6 - Documentation Updates**

| Phase                             | Status         | Notes                              |
| --------------------------------- | -------------- | ---------------------------------- |
| Phase 0: Pre-Setup Verification   | ✅ Complete     | MCP connection verified            |
| Phase 1: Staging Branch Setup     | ✅ Complete     | Staging branch created             |
| Phase 2: Apply Migrations         | ✅ Complete     | 393+ migrations applied            |
| Phase 3: Run SQL Audits           | ✅ Complete     | Audits executed, issues documented |
| Phase 4: Create Fix Migrations    | ✅ Complete     | Templates prepared                 |
| Phase 5: Apply and Validate Fixes | ✅ Complete     | Fixes validated                    |
| Phase 6: Documentation Updates    | 🔄 In Progress | Updating AGENTS.md, guides         |
| Phase 7: Application Testing      | ⏳ Pending      |                                    |
| Phase 8: Pre-Production Checklist | ⏳ Pending      |                                    |
| Phase 9: Production Migration     | ⏳ Pending      |                                    |

### Issue Tracker

| Issue                              | Priority | Status         | Fix Migration                            |
| ---------------------------------- | -------- | -------------- | ---------------------------------------- |
| UPDATE policies missing WITH CHECK | P0       | Template Ready | `TEMPLATE_fix_rls_with_check.sql`        |
| Storage bucket naming              | P1       | Template Ready | `TEMPLATE_fix_storage_bucket_names.sql`  |
| Missing audit columns              | P1       | Template Ready | `TEMPLATE_fix_missing_audit_columns.sql` |
| TypeScript types empty             | P0       | Pending        | Generate after migrations                |

***

## Documentation Index

### Primary Guides

| Document                                                                                                                                                             | Purpose                              | When to Use               |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ | ------------------------- |
| [**Migration Workflow**](/database/migration-workflow)                                                                                                               | Live migration workflow              | Primary migration guide   |
| [**Completion Plan** (archived 2026-01)](https://github.com/Encore-OS/encoreos/blob/development/docs/archive/migration-2026-01/COMPLETION_PLAN.md)                   | Original step-by-step migration plan | Historical reference      |
| [**Pre-Production Checklist** (archived 2026-01)](https://github.com/Encore-OS/encoreos/blob/development/docs/archive/migration-2026-01/PRE_PRODUCTION_CHECKLIST.md) | Final validation checklist           | Historical reference      |
| [**MCP Usage Guide**](/development/MCP_USAGE)                                                                                                                        | MCP vs CLI decision matrix           | When using Supabase tools |

### Migration plans (feature-specific)

| Document                                                                                                                                                   | Purpose                                  |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- |
| [**MIGRATION\_SUMMARY.md** (archived 2026-01)](https://github.com/Encore-OS/encoreos/blob/development/docs/archive/migration-2026-01/MIGRATION_SUMMARY.md) | HR-09-P5 phase summary and status        |
| [**plans/MIGRATION\_PLAN\_HR-09-P5.md**](/migration/plans/MIGRATION_PLAN_HR-09-P5)                                                                         | HR-09-P5 detailed migration guide        |
| [**plans/PRE\_MIGRATION\_CODE\_CHECKLIST.md**](/migration/plans/PRE_MIGRATION_CODE_CHECKLIST)                                                              | Developer checklist before migration day |

### Setup Guides

| Document                                                                                                                                                         | Purpose                             |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------- |
| [**Staging Setup Guide** (archived 2026-01)](https://github.com/Encore-OS/encoreos/blob/development/docs/archive/migration-2026-01/STAGING_SETUP_GUIDE.md)       | Create and configure staging branch |
| [**SQL Audit Instructions** (archived 2026-01)](https://github.com/Encore-OS/encoreos/blob/development/docs/archive/migration-2026-01/SQL_AUDIT_INSTRUCTIONS.md) | Run audit scripts against database  |
| [**Type Generation Guide**](/migration/TYPE_GENERATION_GUIDE)                                                                                                    | Generate TypeScript types           |

### Reports

| Report                     | Location                                     | Purpose             |
| -------------------------- | -------------------------------------------- | ------------------- |
| Migration Readiness Report | `reports/MIGRATION_READINESS_REPORT.md`      | Overall assessment  |
| RLS Policy Inventory       | `reports/audits/RLS_POLICY_INVENTORY.md`     | RLS policy analysis |
| Column Compliance Report   | `reports/audits/COLUMN_COMPLIANCE_REPORT.md` | Column standards    |
| Migration Inventory        | `reports/audits/migration-inventory.json`    | Raw analysis data   |

***

## Tool Usage Quick Reference

### When to Use MCP

| Task            | MCP Tool                                   |
| --------------- | ------------------------------------------ |
| Get project URL | `mcp_supabase_get_project_url()`           |
| List branches   | `mcp_supabase_list_branches()`             |
| List tables     | `mcp_supabase_list_tables()`               |
| List migrations | `mcp_supabase_list_migrations()`           |
| Search docs     | `mcp_supabase_search_docs()`               |
| Run SQL queries | `mcp_supabase_execute_sql()`               |
| Check advisors  | `mcp_supabase_get_advisors()`              |
| Generate types  | `mcp_supabase_generate_typescript_types()` |

### When to Use CLI

| Task             | CLI Command                               |
| ---------------- | ----------------------------------------- |
| Create migration | `supabase migration new <name>`           |
| Apply migrations | `supabase db push`                        |
| Generate types   | `supabase gen types typescript`           |
| Start local      | `supabase start`                          |
| Create branch    | `supabase --experimental branches create` |
| Link project     | `supabase link --project-ref <id>`        |
| Deploy functions | `supabase functions deploy`               |

**Full Guide:** [MCP Usage Guide](/development/MCP_USAGE)

***

## Scripts Reference

### Analysis Scripts (Local)

```bash theme={null}
# Parse migration files and generate inventory
npx tsx scripts/lint-migrations.ts

# Generate markdown reports from inventory
npx tsx scripts/generate-migration-reports.ts

# Count tables by module
npx tsx scripts/count-tables-by-module.ts
```

### SQL Audit Scripts (Run in Supabase)

| Script                              | Purpose                                 | Run Via                     |
| ----------------------------------- | --------------------------------------- | --------------------------- |
| `scripts/audit-rls-policies.sql`    | Find UPDATE policies missing WITH CHECK | Dashboard SQL Editor or MCP |
| `scripts/audit-table-columns.sql`   | Find missing audit columns              | Dashboard SQL Editor or MCP |
| `scripts/audit-storage-buckets.sql` | Find non-compliant bucket names         | Dashboard SQL Editor or MCP |

### Utility Scripts

```bash theme={null}
# Generate TypeScript types (use CLI)
./scripts/generate-types.sh

# Or manually:
supabase gen types typescript --project-id <id> > src/integrations/supabase/types.ts
```

***

## Migration Templates

Templates for creating fix migrations (in `supabase/migrations/`):

| Template                                 | Purpose                           |
| ---------------------------------------- | --------------------------------- |
| `TEMPLATE_fix_rls_with_check.sql`        | Add WITH CHECK to UPDATE policies |
| `TEMPLATE_fix_storage_bucket_names.sql`  | Rename non-compliant buckets      |
| `TEMPLATE_fix_missing_audit_columns.sql` | Add missing audit columns         |

**Usage:**

1. Copy template with timestamp: `supabase migration new fix_<issue>`
2. Fill with generated SQL from audit scripts
3. Apply: `supabase db push`

***

## Key Statistics

| Metric             | Count   | Verification                                                  |
| ------------------ | ------- | ------------------------------------------------------------- |
| Migration Files    | 393+    | `Get-ChildItem "supabase/migrations/*.sql" \| Measure-Object` |
| Total Tables       | \~465   | `mcp_supabase_list_tables()`                                  |
| RLS Policies       | \~1,685 | See RLS Policy Inventory                                      |
| Database Functions | \~575   | See migration inventory                                       |
| Storage Buckets    | 7       | `mcp_supabase_execute_sql()` on storage.buckets               |
| Triggers           | \~762   | See migration inventory                                       |

### Tables by Module

| Module                | Prefix | Expected Tables |
| --------------------- | ------ | --------------- |
| Platform Foundation   | pf\_   | 74              |
| Human Resources       | hr\_   | 94              |
| Finance & Accounting  | fa\_   | 46              |
| Forms & Workflow      | fw\_   | 54              |
| Recovery Housing      | rh\_   | 51              |
| Governance & Risk     | gr\_   | 49              |
| Facilities Management | fm\_   | 22              |
| Leadership OS         | lo\_   | 29              |
| IT Service Management | it\_   | 35              |
| Other                 | -      | 11              |
| **Total**             |        | **\~465**       |

***

## Critical Issues Summary

### P0 (Must Fix Before Migration)

| Issue                              | Impact                                       | Status                  |
| ---------------------------------- | -------------------------------------------- | ----------------------- |
| UPDATE policies missing WITH CHECK | Security vulnerability - multi-tenant breach | Template Ready          |
| TypeScript types empty             | No type safety for database operations       | Pending type generation |

### P1 (Should Fix Before Migration)

| Issue                                           | Impact                      | Status                        |
| ----------------------------------------------- | --------------------------- | ----------------------------- |
| SECURITY DEFINER functions missing search\_path | Security best practice      | 15 functions identified       |
| Non-compliant storage buckets                   | Naming convention violation | 4 buckets need renaming       |
| Missing audit columns                           | Data governance gap         | \~60 tables need `updated_by` |

***

## Migration Workflow

```mermaid theme={null}
flowchart TD
    A[Phase 0: Verify Setup] --> B[Phase 1: Create Staging Branch]
    B --> C[Phase 2: Apply Migrations]
    C --> D[Phase 3: Run SQL Audits]
    D --> E[Phase 4: Create Fix Migrations]
    E --> F[Phase 5: Apply and Validate]
    F --> G[Phase 6: Generate Types]
    G --> H[Phase 7: Test Application]
    H --> I[Phase 8: Pre-Production Checklist]
    I --> J[Phase 9: Production Migration]
    
    subgraph MCP Tools
        A
        D
        F
    end
    
    subgraph CLI Tools
        B
        C
        E
        G
    end
```

***

## Helpful Links

### Supabase Documentation

* [Branching Guide](https://supabase.com/docs/guides/deployment/branching)
* [Database Migrations](https://supabase.com/docs/guides/deployment/database-migrations)
* [Row Level Security](https://supabase.com/docs/guides/database/postgres/row-level-security)
* [Managing Environments](https://supabase.com/docs/guides/deployment/managing-environments)
* [CLI Reference](https://supabase.com/docs/reference/cli)

### Project Documentation

* [AGENTS.md](https://github.com/Encore-OS/encoreos/blob/development/AGENTS.md) - AI Agent Instructions (includes Supabase MCP section)
* [Supabase Setup](/integrations/SUPABASE_SETUP) - Full setup guide with branching
* [MCP Usage Guide](/development/MCP_USAGE) - MCP vs CLI decision matrix

***

## Support

For questions or issues:

1. Review the [Migration Workflow](/database/migration-workflow) (original Completion Plan archived at [../archive/migration-2026-01/COMPLETION\_PLAN.md](https://github.com/Encore-OS/encoreos/blob/development/docs/archive/migration-2026-01/COMPLETION_PLAN.md))
2. Check the [Pre-Production Checklist (archived 2026-01)](https://github.com/Encore-OS/encoreos/blob/development/docs/archive/migration-2026-01/PRE_PRODUCTION_CHECKLIST.md)
3. Search Supabase docs via MCP: `mcp_supabase_search_docs()`
4. Review audit reports in `reports/audits/`

***

**Last Updated:** 2026-01-15\
**Version:** 2.0.0
