CREATE TABLE `activation_code` ( `id` bigint AUTO_INCREMENT NOT NULL, `child_user_id` bigint NOT NULL, `code_hash` varchar(64) NOT NULL, `created_by` bigint NOT NULL, `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `expires_at` datetime NOT NULL, `used_at` datetime, CONSTRAINT `activation_code_id` PRIMARY KEY(`id`) ); --> statement-breakpoint CREATE TABLE `family` ( `id` bigint AUTO_INCREMENT NOT NULL, `name` varchar(50) NOT NULL, `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT `family_id` PRIMARY KEY(`id`) ); --> statement-breakpoint CREATE TABLE `photo` ( `id` bigint AUTO_INCREMENT NOT NULL, `task_instance_id` bigint NOT NULL, `oss_key` varchar(300) NOT NULL, `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT `photo_id` PRIMARY KEY(`id`) ); --> statement-breakpoint CREATE TABLE `session` ( `id` bigint AUTO_INCREMENT NOT NULL, `user_id` bigint NOT NULL, `token_hash` varchar(64) NOT NULL, `device_label` varchar(100), `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `expires_at` datetime NOT NULL, CONSTRAINT `session_id` PRIMARY KEY(`id`), CONSTRAINT `uq_session_token` UNIQUE(`token_hash`) ); --> statement-breakpoint CREATE TABLE `task_instance` ( `id` bigint AUTO_INCREMENT NOT NULL, `child_user_id` bigint NOT NULL, `date` date NOT NULL, `title` varchar(100) NOT NULL, `requires_photo` boolean NOT NULL DEFAULT false, `status` enum('pending','done') NOT NULL DEFAULT 'pending', `is_makeup` boolean NOT NULL DEFAULT false, `done_at` datetime, `sort_order` int NOT NULL DEFAULT 0, `source_template_id` bigint, `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT `task_instance_id` PRIMARY KEY(`id`) ); --> statement-breakpoint CREATE TABLE `task_template` ( `id` bigint AUTO_INCREMENT NOT NULL, `child_user_id` bigint NOT NULL, `dow` int NOT NULL, `title` varchar(100) NOT NULL, `requires_photo` boolean NOT NULL DEFAULT false, `sort_order` int NOT NULL DEFAULT 0, `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT `task_template_id` PRIMARY KEY(`id`) ); --> statement-breakpoint CREATE TABLE `user` ( `id` bigint AUTO_INCREMENT NOT NULL, `family_id` bigint NOT NULL, `role` enum('parent','child') NOT NULL, `display_name` varchar(50) NOT NULL, `phone` varchar(20), `password_hash` varchar(100), `sort_order` int NOT NULL DEFAULT 0, `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT `user_id` PRIMARY KEY(`id`), CONSTRAINT `uq_user_phone` UNIQUE(`phone`) ); --> statement-breakpoint ALTER TABLE `activation_code` ADD CONSTRAINT `activation_code_child_user_id_user_id_fk` FOREIGN KEY (`child_user_id`) REFERENCES `user`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE `activation_code` ADD CONSTRAINT `activation_code_created_by_user_id_fk` FOREIGN KEY (`created_by`) REFERENCES `user`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE `photo` ADD CONSTRAINT `photo_task_instance_id_task_instance_id_fk` FOREIGN KEY (`task_instance_id`) REFERENCES `task_instance`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE `session` ADD CONSTRAINT `session_user_id_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE `task_instance` ADD CONSTRAINT `task_instance_child_user_id_user_id_fk` FOREIGN KEY (`child_user_id`) REFERENCES `user`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE `task_template` ADD CONSTRAINT `task_template_child_user_id_user_id_fk` FOREIGN KEY (`child_user_id`) REFERENCES `user`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE `user` ADD CONSTRAINT `user_family_id_family_id_fk` FOREIGN KEY (`family_id`) REFERENCES `family`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint CREATE INDEX `idx_photo_instance` ON `photo` (`task_instance_id`);--> statement-breakpoint CREATE INDEX `idx_session_user` ON `session` (`user_id`);--> statement-breakpoint CREATE INDEX `idx_instance_child_date` ON `task_instance` (`child_user_id`,`date`);--> statement-breakpoint CREATE INDEX `idx_template_child_dow` ON `task_template` (`child_user_id`,`dow`);