-- PantryItem: make user-scoped
ALTER TABLE `PantryItem` ADD COLUMN `userId` INTEGER NULL;

UPDATE `PantryItem`
SET `userId` = (SELECT `id` FROM `User` ORDER BY `id` ASC LIMIT 1)
WHERE `userId` IS NULL;

ALTER TABLE `PantryItem` ADD INDEX `PantryItem_productId_idx`(`productId`);
ALTER TABLE `PantryItem` DROP INDEX `PantryItem_productId_key`;
ALTER TABLE `PantryItem` MODIFY `userId` INTEGER NOT NULL;
ALTER TABLE `PantryItem` ADD INDEX `PantryItem_userId_idx`(`userId`);
ALTER TABLE `PantryItem` ADD UNIQUE INDEX `PantryItem_userId_productId_key`(`userId`, `productId`);
ALTER TABLE `PantryItem` ADD CONSTRAINT `PantryItem_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- MealPlanEntry: make user-scoped
ALTER TABLE `MealPlanEntry` ADD COLUMN `userId` INTEGER NULL;

UPDATE `MealPlanEntry`
SET `userId` = (SELECT `id` FROM `User` ORDER BY `id` ASC LIMIT 1)
WHERE `userId` IS NULL;

ALTER TABLE `MealPlanEntry` DROP INDEX `MealPlanEntry_date_key`;
ALTER TABLE `MealPlanEntry` MODIFY `userId` INTEGER NOT NULL;
ALTER TABLE `MealPlanEntry` ADD INDEX `MealPlanEntry_userId_idx`(`userId`);
ALTER TABLE `MealPlanEntry` ADD UNIQUE INDEX `MealPlanEntry_userId_date_key`(`userId`, `date`);
ALTER TABLE `MealPlanEntry` ADD CONSTRAINT `MealPlanEntry_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
