-- Migration: all-region stores. -- store_info.region_id becomes nullable (NULL = serves all regions) and gains -- available_in_all_regions, mirroring product_info. The table is rebuilt -- (create-copy-drop-rename) because SQLite cannot drop the NOT NULL from a -- column, and product_market_stats.store_id references store_info, so the -- stats table is rebuilt alongside it (leaves-first: market_stats has no -- inbound FKs, so it drops first). IDs are preserved, so relatedStores JSON -- and all other references survive unchanged. -- 1) replacement store table: nullable region + all-regions flag CREATE TABLE `__new_store_info` ( `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, `name` text NOT NULL, `description` text, `image_url` text, `created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL, `owner` integer NOT NULL, `region_id` integer, `available_in_all_regions` integer NOT NULL DEFAULT 0, FOREIGN KEY (`owner`) REFERENCES `staff_users`(`id`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action ); --> statement-breakpoint INSERT INTO `__new_store_info` ( `id`, `name`, `description`, `image_url`, `created_at`, `owner`, `region_id`, `available_in_all_regions` ) SELECT `id`, `name`, `description`, `image_url`, `created_at`, `owner`, `region_id`, 0 FROM `store_info`; --> statement-breakpoint -- 2) rebuild market stats retargeted at the replacement store table CREATE TABLE `__new_product_market_stats` ( `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, `sku_id` integer NOT NULL, `region_id` integer NOT NULL, `store_id` integer, `market_price` numeric, `our_price` numeric NOT NULL, `is_flash_available` integer DEFAULT false NOT NULL, `flash_price` numeric, `is_out_of_stock` integer DEFAULT false NOT NULL, `is_suspended` integer DEFAULT false NOT NULL, `is_offer` integer DEFAULT false NOT NULL, FOREIGN KEY (`sku_id`) REFERENCES `product_skus`(`id`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (`region_id`) REFERENCES `regions`(`id`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (`store_id`) REFERENCES `__new_store_info`(`id`) ON UPDATE no action ON DELETE no action ); --> statement-breakpoint INSERT INTO `__new_product_market_stats` ( `id`, `sku_id`, `region_id`, `store_id`, `market_price`, `our_price`, `is_flash_available`, `flash_price`, `is_out_of_stock`, `is_suspended`, `is_offer` ) SELECT `id`, `sku_id`, `region_id`, `store_id`, `market_price`, `our_price`, `is_flash_available`, `flash_price`, `is_out_of_stock`, `is_suspended`, `is_offer` FROM `product_market_stats`; --> statement-breakpoint -- 3) drop originals, leaves first DROP TABLE `product_market_stats`; --> statement-breakpoint DROP TABLE `store_info`; --> statement-breakpoint -- 4) promote the replacements (child FK clauses auto-rewrite to final names) ALTER TABLE `__new_store_info` RENAME TO `store_info`; --> statement-breakpoint ALTER TABLE `__new_product_market_stats` RENAME TO `product_market_stats`; --> statement-breakpoint -- 5) recreate the index (name freed by the drops above) CREATE UNIQUE INDEX `unique_sku_region` ON `product_market_stats` (`sku_id`, `region_id`);