001_create_users.sql 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. -- ============================================================
  2. -- Migration 001: User authentication tables
  3. -- Run once against the cropmonitor database
  4. -- ============================================================
  5. -- Users table (replaces modX user management)
  6. CREATE TABLE IF NOT EXISTS `users` (
  7. `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  8. `fullname` VARCHAR(255) NOT NULL,
  9. `email` VARCHAR(255) NOT NULL,
  10. `password` VARCHAR(255) NOT NULL, -- bcrypt hash
  11. `company` VARCHAR(255) DEFAULT '',
  12. `mobilephone` VARCHAR(50) DEFAULT '',
  13. `industry` VARCHAR(100) DEFAULT '',
  14. `role` VARCHAR(100) DEFAULT '',
  15. `city` VARCHAR(100) DEFAULT '',
  16. `state` VARCHAR(100) DEFAULT '',
  17. `postcode` VARCHAR(20) DEFAULT '',
  18. `country` VARCHAR(100) DEFAULT 'Australia',
  19. `active` TINYINT(1) NOT NULL DEFAULT 1,
  20. `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  21. `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  22. PRIMARY KEY (`id`),
  23. UNIQUE KEY `uq_users_email` (`email`)
  24. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  25. -- Password reset tokens
  26. CREATE TABLE IF NOT EXISTS `password_resets` (
  27. `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  28. `email` VARCHAR(255) NOT NULL,
  29. `token` VARCHAR(64) NOT NULL, -- bin2hex(random_bytes(32))
  30. `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  31. `expires_at` DATETIME NOT NULL, -- created_at + 1 hour
  32. PRIMARY KEY (`id`),
  33. KEY `idx_resets_email` (`email`),
  34. KEY `idx_resets_token` (`token`)
  35. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  36. -- Link existing client_records rows to the new users table.
  37. -- The old modx_user_id (always 1 in legacy data) is replaced by users.id.
  38. -- No structural change needed: client_records.modx_user_id stays as the FK column;
  39. -- just populate users first, then update client_records rows as users are created.