148 lines
No EOL
5.6 KiB
SQL
148 lines
No EOL
5.6 KiB
SQL
-- Migration: region scoping.
|
|
-- Adds region ownership across the platform and moves a product's store to the
|
|
-- per-region availability table. Existing rows are backfilled to region id 1.
|
|
--
|
|
-- product_info is rebuilt (create-copy-drop-rename) instead of using
|
|
-- ALTER TABLE DROP COLUMN, because store_id is part of a FOREIGN KEY definition
|
|
-- and SQLite refuses to drop columns used in FK constraints. The rebuild follows
|
|
-- the same pattern as 0002_sku_split.sql.
|
|
PRAGMA defer_foreign_keys = on;
|
|
--> statement-breakpoint
|
|
|
|
-- 1) region_id columns (backfill to region 1)
|
|
ALTER TABLE `staff_users` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
|
|
--> statement-breakpoint
|
|
ALTER TABLE `addresses` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
|
|
--> statement-breakpoint
|
|
ALTER TABLE `vendor_snippets` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
|
|
--> statement-breakpoint
|
|
ALTER TABLE `store_info` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
|
|
--> statement-breakpoint
|
|
ALTER TABLE `product_tag_info` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
|
|
--> statement-breakpoint
|
|
ALTER TABLE `product_group_info` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
|
|
--> statement-breakpoint
|
|
ALTER TABLE `delivery_slot_info` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
|
|
--> statement-breakpoint
|
|
ALTER TABLE `home_banners` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
|
|
--> statement-breakpoint
|
|
|
|
-- 2) move storeId from product_info to product_market_stats
|
|
ALTER TABLE `product_market_stats` ADD COLUMN `region_id` integer NOT NULL DEFAULT 1;
|
|
--> statement-breakpoint
|
|
ALTER TABLE `product_market_stats` ADD COLUMN `store_id` integer;
|
|
--> statement-breakpoint
|
|
|
|
-- 3) backfill market-stats store_id from the old product_info.store_id (before the rebuild drops it)
|
|
UPDATE `product_market_stats`
|
|
SET `store_id` = (
|
|
SELECT `product_info`.`store_id`
|
|
FROM `product_skus`
|
|
JOIN `product_info` ON `product_info`.`id` = `product_skus`.`product_id`
|
|
WHERE `product_skus`.`id` = `product_market_stats`.`sku_id`
|
|
);
|
|
--> statement-breakpoint
|
|
|
|
-- 3b) move is_offer from product_skus to product_market_stats (per-region flag)
|
|
ALTER TABLE `product_market_stats` ADD COLUMN `is_offer` integer NOT NULL DEFAULT 0;
|
|
--> statement-breakpoint
|
|
UPDATE `product_market_stats`
|
|
SET `is_offer` = (
|
|
SELECT `product_skus`.`is_offer`
|
|
FROM `product_skus`
|
|
WHERE `product_skus`.`id` = `product_market_stats`.`sku_id`
|
|
);
|
|
--> statement-breakpoint
|
|
ALTER TABLE `product_skus` DROP COLUMN `is_offer`;
|
|
--> statement-breakpoint
|
|
|
|
-- 4) rebuild product_info: drops store_id (with its FK), adds region_id + available_in_all_regions
|
|
CREATE TABLE `__new_product_info` (
|
|
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
`name` text NOT NULL,
|
|
`short_description` text,
|
|
`long_description` text,
|
|
-- NULL when the product is available in all regions. Existing rows belong to region 1.
|
|
`region_id` integer,
|
|
`available_in_all_regions` integer NOT NULL DEFAULT 0,
|
|
`increment_step` real DEFAULT 1 NOT NULL,
|
|
`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
|
`product_type` text DEFAULT 'item' NOT NULL
|
|
);
|
|
--> statement-breakpoint
|
|
INSERT INTO `__new_product_info` (
|
|
`id`, `name`, `short_description`, `long_description`, `region_id`, `available_in_all_regions`, `increment_step`, `created_at`, `product_type`
|
|
)
|
|
SELECT
|
|
`id`, `name`, `short_description`, `long_description`, 1, 0, `increment_step`, `created_at`, `product_type`
|
|
FROM `product_info`;
|
|
--> statement-breakpoint
|
|
DROP TABLE `product_info`;
|
|
--> statement-breakpoint
|
|
ALTER TABLE `__new_product_info` RENAME TO `product_info`;
|
|
--> statement-breakpoint
|
|
|
|
-- 5) composite unique replaces the single-column unique on sku_id
|
|
DROP INDEX IF EXISTS `product_market_stats_sku_id_unique`;
|
|
--> statement-breakpoint
|
|
CREATE UNIQUE INDEX IF NOT EXISTS `product_market_stats_sku_region_unique`
|
|
ON `product_market_stats` (`sku_id`, `region_id`);
|
|
--> statement-breakpoint
|
|
|
|
-- 6) region- and bound-scoped app constants.
|
|
-- New region_consts table holds the admin customize-app visible fields per
|
|
-- region (bound_id NULL = region-level fallback) and per bound. Existing
|
|
-- values are backfilled from key_val_store for every region, then removed
|
|
-- from key_val_store so the global table keeps global-only keys.
|
|
CREATE TABLE `region_consts` (
|
|
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
`region_id` integer NOT NULL REFERENCES `regions`(`id`),
|
|
`bound_id` integer REFERENCES `region_bounds`(`id`),
|
|
`key` text NOT NULL,
|
|
`value` text
|
|
);
|
|
--> statement-breakpoint
|
|
CREATE UNIQUE INDEX `region_consts_region_key_bound_unique`
|
|
ON `region_consts` (`region_id`, `key`, `bound_id`);
|
|
--> statement-breakpoint
|
|
|
|
INSERT INTO `region_consts` (`region_id`, `bound_id`, `key`, `value`)
|
|
SELECT `regions`.`id`, NULL, `key_val_store`.`key`, `key_val_store`.`value`
|
|
FROM `regions`
|
|
CROSS JOIN `key_val_store`
|
|
WHERE `key_val_store`.`key` IN (
|
|
'minRegularOrderValue',
|
|
'freeDeliveryThreshold',
|
|
'deliveryCharge',
|
|
'flashFreeDeliveryThreshold',
|
|
'flashDeliveryCharge',
|
|
'allItemsOrder',
|
|
'isFlashDeliveryEnabled',
|
|
'supportMobile',
|
|
'supportEmail',
|
|
'welcomeMessage'
|
|
);
|
|
--> statement-breakpoint
|
|
|
|
DELETE FROM `key_val_store`
|
|
WHERE `key` IN (
|
|
'minRegularOrderValue',
|
|
'freeDeliveryThreshold',
|
|
'deliveryCharge',
|
|
'flashFreeDeliveryThreshold',
|
|
'flashDeliveryCharge',
|
|
'allItemsOrder',
|
|
'isFlashDeliveryEnabled',
|
|
'supportMobile',
|
|
'supportEmail',
|
|
'welcomeMessage'
|
|
);
|
|
--> statement-breakpoint
|
|
|
|
-- 7) tag names are unique per region, not globally.
|
|
DROP INDEX IF EXISTS `product_tag_info_tag_name_unique`;
|
|
--> statement-breakpoint
|
|
CREATE UNIQUE INDEX IF NOT EXISTS `unique_tag_name_region`
|
|
ON `product_tag_info` (`tag_name`, `region_id`);
|
|
--> statement-breakpoint
|
|
PRAGMA defer_foreign_keys = off; |