-- Gold Jewellery Software - Username-based login
-- Migration 010: login now uses username instead of email (see
-- App\Service\AuthService and the login form). This tightens the column
-- to NOT NULL — every existing user already has one (backfilled from
-- their email's local part back in migration 004), this just closes off
-- the possibility of a future blank one slipping through.
USE gold_jewellery;

-- Safety-net backfill in case any row somehow still has a blank username
-- (e.g. a user created outside the normal UserService flow).
UPDATE users SET username = SUBSTRING_INDEX(email, '@', 1) WHERE username IS NULL OR username = '';

ALTER TABLE users MODIFY COLUMN username VARCHAR(100) NOT NULL;
