ALTER TABLE `restaurants` ADD `plan` text DEFAULT 'active' NOT NULL;
--> statement-breakpoint
ALTER TABLE `restaurants` ADD `plan_activated_at` text;
--> statement-breakpoint
ALTER TABLE `restaurants` ADD `plan_note` text;
--> statement-breakpoint
ALTER TABLE `restaurants` ADD `owner_email` text;
--> statement-breakpoint
CREATE INDEX `idx_restaurants_plan` ON `restaurants` (`plan`);
--> statement-breakpoint
-- Restaurants that already exist were set up by hand before self-registration,
-- so they stay unlocked. Only newly registered ones start on the trial plan.
UPDATE `restaurants` SET `plan` = 'active', `plan_activated_at` = CURRENT_TIMESTAMP;
--> statement-breakpoint
UPDATE `restaurants`
SET `owner_email` = (
	SELECT m.`email` FROM `memberships` m
	WHERE m.`restaurant_id` = `restaurants`.`id` AND m.`role` = 'owner'
	ORDER BY m.`created_at` LIMIT 1
)
WHERE `owner_email` IS NULL;
--> statement-breakpoint
PRAGMA optimize;
