ALTER TABLE "users" ADD COLUMN "username" text;--> statement-breakpoint
-- Backfill existing rows before enforcing NOT NULL — a plain
-- `ADD COLUMN ... NOT NULL` (drizzle-kit's default generation) fails
-- outright against any table with existing data and no default. Suffixing
-- with part of the id keeps this collision-safe without a second pass.
UPDATE "users" SET "username" = lower(regexp_replace(split_part("email", '@', 1), '[^a-zA-Z0-9_]', '', 'g')) || substr("id", 1, 6) WHERE "username" IS NULL;--> statement-breakpoint
ALTER TABLE "users" ALTER COLUMN "username" SET NOT NULL;--> statement-breakpoint
CREATE UNIQUE INDEX IF NOT EXISTS "users_username_idx" ON "users" USING btree ("username");
