mirror of
https://github.com/Luchexx/wacrm.git
synced 2026-11-02 09:19:51 +00:00
Turns every wacrm install multi-tenant on the database side. A single user's signup now creates an `accounts` row, links the profile to it as `owner`, and every domain row is scoped via a new `account_id` column instead of `user_id`. Why now: the next handful of PRs add the invite/members user surface (gated by the `account_sharing` beta flag); that surface needs RLS to already speak "membership" rather than "ownership". Doing the schema flip in isolation keeps the diff reviewable and lets the follow-up PRs stay focused on TS/UI. What the migration does: - Introduces `account_role_enum` + `accounts` + `account_invitations`. - Adds `account_id` to every parent tenant table (16 tables incl. profiles) and `account_role` to profiles. - Backfills one account per existing user, then sets NOT NULL. - Replaces every `auth.uid() = user_id` RLS policy with a membership check via a SECURITY DEFINER `is_account_member(account_id, min)` helper. Three tiers: viewers read, agents write operational data, admins write settings-class tables (whatsapp_config, templates, pipelines, tags, custom_fields). - Swaps `whatsapp_config.UNIQUE(user_id)` → `UNIQUE(account_id)` and `flow_runs` active-run idempotency key to `(account_id, contact_id)`. - Replaces `handle_new_user` so new signups bootstrap their own account atomically. Backwards compatibility: every existing user is the sole `owner` of their freshly-created account, every existing row is linked to it, and the legacy `user_id` columns stay (still used for assignment / audit, no longer for isolation). Net behavioural change on its own: zero. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> |
||
|---|---|---|
| .. | ||
| 001_initial_schema.sql | ||
| 002_pipelines_enhancements.sql | ||
| 003_broadcast_recipient_wamid.sql | ||
| 004_contact_delete_set_null.sql | ||
| 005_broadcast_counts_incremental.sql | ||
| 006_automations.sql | ||
| 007_automations_increment_counter.sql | ||
| 008_profile_avatars_storage.sql | ||
| 009_message_actions.sql | ||
| 010_flows.sql | ||
| 011_profile_beta_features.sql | ||
| 012_flows_increment_counter.sql | ||
| 013_whatsapp_config_phone_number_id_unique.sql | ||
| 014_message_templates_meta_integration.sql | ||
| 015_whatsapp_config_registration.sql | ||
| 016_flow_media.sql | ||
| 017_account_sharing.sql | ||