feat: update main migration script and add sync migration for existing deployments

Main migration now canonical for fresh deployments; added sync migration for legacy DBs.

Co-authored-by: Moheeb Zara <[email protected]>
This commit is contained in:
v0
2026-03-11 06:36:43 +00:00
co-authored by Moheeb Zara
parent 1d08719a04
commit e36dee2601
2 changed files with 212 additions and 12 deletions
+19 -12
View File
@@ -4,29 +4,36 @@
### Fresh deployment
Run **one file**:
Run **one file** in Supabase SQL Editor:
```
scripts/schema.sql
```
Paste it in Supabase → **SQL Editor → New query** and hit Run.
That is the complete, idempotent schema — all tables, enums, indexes, RLS policies, functions, and triggers.
This creates the entire schema from scratch — all tables, enums, indexes, RLS policies, functions, and triggers. Idempotent, can be run multiple times safely.
### Existing deployment
### Existing deployment migration
The numbered scripts (`001_*` → `011_*`) are the **migration history** of the live database.
Do not re-run them. They are kept for reference only.
If you have a legacy Hackerspace database and want to sync it to the canonical schema:
```
scripts/012_canonical_sync.sql
```
This migration adds all missing columns, renames `full_name` → `display_name`, creates missing constraints, fixes the auth trigger, and updates all other tables to match the canonical schema. Also idempotent — safe to run multiple times.
### Migration history (reference only)
Scripts `001_*` through `011_*` represent the evolution of the live database. **Do not re-run them.** They exist for historical reference.
### What `schema.sql` contains
| Section | Contents |
|---------|----------|
| Extensions | `uuid-ossp` |
| Enums | 14 custom types (`member_role`, `task_status`, etc.) |
| Enums | 14 custom types (`member_role`, `task_status`, `payment_platform`, etc.) |
| Helper functions | `get_user_space_ids`, `user_has_role_in_space` |
| Tables | `spaces`, `space_members`, `tasks`, `projects`, `payments`, `contacts`, `knowledge_base`, `secrets`, `area_leads`, `integrations`, `comms_channels`, `comms_messages`, `activity_log` |
| Indexes | 22 performance indexes |
| RLS policies | Full row-level security on every table |
| Triggers | `on_space_created` (auto-creates 4 default channels) |
| Auth hook | `on_auth_user_created` (handles create-space and join-space signup flows) |
| Tables | 13 tables: `spaces`, `space_members`, `tasks`, `projects`, `payments`, `contacts`, `knowledge_base`, `secrets`, `area_leads`, `integrations`, `comms_channels`, `comms_messages`, `activity_log` |
| Indexes | 22 performance indexes on foreign keys and common filters |
| RLS policies | Full row-level security on all tables (access gated by space membership) |
| Triggers | `on_space_created` (auto-creates 4 default channels), `on_auth_user_created` (auth signup hook) |