002_consultant_roles.sql 1.2 KB

123456789101112131415161718192021222324
  1. -- ============================================================
  2. -- Migration 002: Consultant roles + client assignment
  3. -- Run once against the cropmonitor database
  4. -- ============================================================
  5. -- 1. Add user_type column to users table
  6. -- Values: 'client' (default), 'consultant', 'admin'
  7. ALTER TABLE `users`
  8. ADD COLUMN `user_type` ENUM('client','consultant','admin')
  9. NOT NULL DEFAULT 'client'
  10. AFTER `role`;
  11. -- 2. Junction table: maps consultants → the client_records they manage
  12. CREATE TABLE IF NOT EXISTS `consultant_clients` (
  13. `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  14. `consultant_id` INT UNSIGNED NOT NULL, -- users.id (user_type = 'consultant')
  15. `client_id` INT NOT NULL, -- client_records.id
  16. `assigned_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  17. `assigned_by` INT UNSIGNED DEFAULT NULL, -- users.id of admin who made the assignment
  18. PRIMARY KEY (`id`),
  19. UNIQUE KEY `uq_consultant_client` (`consultant_id`, `client_id`),
  20. KEY `idx_cc_consultant` (`consultant_id`),
  21. KEY `idx_cc_client` (`client_id`)
  22. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;